在Tensorflow重量和偏见的认识初始化

时间:2019-02-02 08:18:07

标签: python python-3.x tensorflow lstm

目前,我最后正在使用LSTM和FC的堆叠层。

tf.contrib.rnn.BasicLSTMCell
tf.contrib.rnn.MultiRNNCell
tf.nn.dynamic_rnn
tf.contrib.layers.fully_connected

根据我的理解,如果我尝试使用

下定义的任何体系结构
tf.nn

类,则重量初始化等

W2 = tf.Variable(np.random.rand(state_size, num_classes),dtype=tf.float32)
b2 = tf.Variable(np.zeros((1,num_classes)), dtype=tf.float32) 

需要完成,并且必须使用 tf.matmul

但是,对于下类

tf.contrib

权重初始化会自动发生,并且可能不需要 tf.matmul

我的理解正确吗?请让我知道

1 个答案:

答案 0 :(得分:0)

在您的情况下,权重和偏差变量的创建发生在单元内部,然后在完全连接的层内部。因此,无需显式定义它们。此外,在构建图形时,Tensorflow不会初始化任何变量。之后,在开始执行图中的节点之前,您需要预先初始化图中的变量。看看:

# Some input
inputs = np.ones((5,4,3), dtype=np.float32)
# Cleaning the default graph before building it
tf.reset_default_graph()
# Creating a MultiRNNCell (The cell itself creates the weight and bias matrices)
cells = tf.contrib.rnn.MultiRNNCell([tf.contrib.rnn.BasicLSTMCell(10) for _ in range(2)])
# A wrapper around the cells to ensure the recurrent relation
hidden_states, cell_state = tf.nn.dynamic_rnn(cell=cells, inputs=inputs, dtype=tf.float32)
# Fully connected layer (What it does is create a W and b matrices and performs tf.matmul and then tf.add)
logits = tf.contrib.layers.fully_connected(hidden_states[-1], num_outputs=2, activation_fn=None)
# A new session
sess = tf.Session()
# All variables that were created above (implicitly, by creating a cell and a fully connected layer) will be initialized
sess.run(tf.global_variables_initializer())
# Executing the last node in the graph
sess.run(logits)
相关问题