我在tensorflow中找到了两种RNN实现.
第一个实现是this(从第124行到第129行).它使用循环来定义RNN中的每个输入步骤.
with tf.variable_scope("RNN"): for time_step in range(num_steps): if time_step > 0: tf.get_variable_scope().reuse_variables() (cell_output,state) = cell(inputs[:,time_step,:],state) outputs.append(cell_output) states.append(state)
第二个实现是this(从第51行到第70行).它不使用任何循环来定义RNN中的每个输入步骤.
def RNN(_X,_istate,_weights,_biases): # input shape: (batch_size,n_steps,n_input) _X = tf.transpose(_X,[1,2]) # permute n_steps and batch_size # Reshape to prepare input to hIDden activation _X = tf.reshape(_X,[-1,n_input]) # (n_steps*batch_size,n_input) # linear activation _X = tf.matmul(_X,_weights['hIDden']) + _biases['hIDden'] # define a lstm cell with tensorflow lstm_cell = rnn_cell.BasicLSTMCell(n_hIDden,forget_bias=1.0) # Split data because rnn cell needs a List of inputs for the RNN inner loop _X = tf.split(0,_X) # n_steps * (batch_size,n_hIDden) # Get lstm cell output outputs,states = rnn.rnn(lstm_cell,_X,initial_state=_istate) # linear activation # Get inner loop last output return tf.matmul(outputs[-1],_weights['out']) + _biases['out']
在第一个实现中,我发现输入单元到隐藏单元之间没有权重矩阵,只定义隐藏单元到输出单元之间的权重矩阵(从132到133行).
output = tf.reshape(tf.concat(1,outputs),size]) softmax_w = tf.get_variable("softmax_w",[size,vocab_size]) softmax_b = tf.get_variable("softmax_b",[vocab_size]) logits = tf.matmul(output,softmax_w) + softmax_b
但是在第二种实现中,定义了两个权重矩阵(从第42行到第47行).
weights = { 'hIDden': tf.Variable(tf.random_normal([n_input,n_hIDden])),# HIDden layer weights 'out': tf.Variable(tf.random_normal([n_hIDden,n_classes]))}biases = { 'hIDden': tf.Variable(tf.random_normal([n_hIDden])),'out': tf.Variable(tf.random_normal([n_classes]))}
我想知道为什么?最佳答案我注意到的差异是second implementation中的代码使用tf.nn.rnn,它获取每个时间步的输入列表并生成每个时间步的输出列表.
(输入:输入的长度T列表,每个都是一个形状的张量
[batch_size,input_size].)
因此,如果您检查第62行第二个实现中的代码,输入数据将被整形为n_steps *(batch_size,n_hIDden)
# Split data because rnn cell needs a List of inputs for the RNN inner loop_X = tf.split(0,n_hIDden)
在1st implementation中,它们循环遍历n_time_steps并提供输入并获得相应的输出并存储在输出列表中.
第113到117行的代码段
outputs = [] state = self._initial_state with tf.variable_scope("RNN"): for time_step in range(num_steps): if time_step > 0: tf.get_variable_scope().reuse_variables() (cell_output,state) outputs.append(cell_output)
来到你的第二个问题:
如果您仔细注意在两种实现中输入都被馈送到RNN的方式.
在第一个实现中,输入的形状已经是batch_size x num_steps(这里num_steps是隐藏的大小):
self._input_data = tf.placeholder(tf.int32,[batch_size,num_steps])
而在第二种实现中,初始输入具有形状(batch_size x n_steps x n_input).因此需要权重矩阵转换为形状(n_steps x batch_size x hIDden_size):
# input shape: (batch_size,2]) # Permute n_steps and batch_size # Reshape to prepare input to hIDden activation _X = tf.reshape(_X,_weights['hIDden']) + _biases['hIDden'] # Split data because rnn cell needs a List of inputs for the RNN inner loop _X = tf.split(0,n_hIDden)
我希望这是有帮助的… 总结
以上是内存溢出为你收集整理的python – 张量流中两个RNN实现的区别是什么?全部内容,希望文章能够帮你解决python – 张量流中两个RNN实现的区别是什么?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)