python - TensorFlow stack_bidirectional_dynamic_rnn 的维度问题
问题描述
我正在使用 Tensorflow 构建一个用于机器翻译的玩具编码器-解码器模型。
我使用 Tensorflow 1.8.0 cpu 版本。嵌入层使用了 300 维的 FastText 预训练词向量。然后这批训练数据通过具有注意力机制的编码器和解码器。在训练阶段解码器使用 TrainHelper,在推理阶段使用 GreedyEmbeddingHelper。
我已经使用双向 LSTM 编码器成功运行了模型。但是,当我尝试通过使用多层 LSTM 进一步改进我的模型时,就会出现错误。构建训练阶段模型的代码如下:
def BuildTrainModel(train_iterator):
((source, source_lengths), (target, target_lengths)) = train_iterator.get_next()
encoder_inputs = tf.transpose(source, [1,0]) # to time major
decoder_inputs = tf.transpose(target, [1,0])
decoder_outputs = tf.pad(decoder_inputs[1:], tf.constant([[0,1],[0,0]]), constant_values=tar_eos_id)
embedding_encoder = tf.Variable(embedding_matrix_src, name='embedding_encoder')
embedding_decoder = tf.Variable(embedding_matrix_tar, name='embedding_decoder')
# Embedding layer
encoder_emb_inp = tf.nn.embedding_lookup(embedding_encoder, encoder_inputs)
decoder_emb_inp = tf.nn.embedding_lookup(embedding_decoder, decoder_inputs)
# Encoder
# Construct forward and backward cells
forward_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units)
backward_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units)
encoder_outputs, encoder_states_fw, encoder_states_bw = tf.contrib.rnn.stack_bidirectional_dynamic_rnn(
[forward_cell] * num_layers, [backward_cell] * num_layers, encoder_emb_inp, dtype=tf.float64,
sequence_length=source_lengths, time_major=True)
这里我只展示编码器部分。完整代码和超参数请看我的github:https ://github.com/nkjsy/Neural-Machine-Translation/blob/master/nmt3.ipynb
错误信息是:
InvalidArgumentError: Dimensions must be equal, but are 96 and 332 for 'stack_bidirectional_rnn/cell_0/bidirectional_rnn/fw/fw/while/basic_lstm_cell/MatMul_1' (op: 'MatMul') with input shapes: [?,96], [332,128].
我尝试将输入设置为 [forward_cell] 和 [backward_cell] 并没有问题,这意味着我之前所做的只有 1 层。一旦我添加更多层,就会出现问题。
解决方案
使用以下方法定义单元实例列表,
forward_cell = [tf.contrib.rnn.BasicLSTMCell(num_units),tf.contrib.rnn.BasicLSTMCell(num_units)]
当您打印两个列表时,您可以看到差异,
num_units =128
num_layers =2
#Method1
forward_cell = [tf.contrib.rnn.BasicLSTMCell( num_units),tf.contrib.rnn.BasicLSTMCell(num_units)]
print(forward_cell)
#Method2
forward_cell = [tf.contrib.rnn.BasicLSTMCell(num_units)]*num_layers
print(forward_cell)
上面的代码片段打印类似于以下内容,
[<tensorflow.python.ops.rnn_cell_impl.BasicLSTMCell object at 0x00000087798E6EF0>, <tensorflow.python.ops.rnn_cell_impl.BasicLSTMCell object at 0x0000008709AE72E8>]
[<tensorflow.python.ops.rnn_cell_impl.BasicLSTMCell object at 0x0000008709AFDC50>, <tensorflow.python.ops.rnn_cell_impl.BasicLSTMCell object at 0x0000008709AFDC50>]
如您所见#Method2
,输出相同单元格实例的列表,这不是预期的。
希望这可以帮助。
推荐阅读
- angular - 对数组进行角度过滤,但从过滤器中排除第一个元素
- office-scripts - 如何使用 Office 脚本删除表中的所有行
- python - 如何使用硒网络驱动程序python访问当前选项卡中新打开的小选项卡
- spring - 在将类型化的 Akka Actor 与 Spring 集成时,声明一个注入器并将其作为状态参数传递是个好主意吗?
- bash - shell脚本中的while循环给出错误:[:参数太多。如何解决这个问题?
- windows - Windows 终端,拆分窗格,但新窗格与前一个窗格位于同一目录中?
- git - Git推送旧版本的“--skip-worktree”文件
- sql - 在 QGIS 中查找函数 TO_DATE oracle
- angular - 数据绑定似乎很慢 - Audio Meter - Angular 9
- bash - 在退出时终止后台作业及其子项