关于bidirectional_dynamic_rnn出现 Dimensions of inputs should match问题

在搭建双向BIRNN模型的时候,调用tensorflow自动展开函数bidirectional_dynamic_rnn(cell_fw, cell_bw, data, dtype=tf.float32)时候出现异常:InvalidArgumentError (see above for traceback): ConcatOp : Dimensions of inputs should match: shape[0] = [5,60] vs. shape[1] = [100,10]
     [[node bidirectional_rnn/bw/bw/while/lstm_cell/concat (defined at F:/Python/NLP/BiRNN.py:28)  = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](bidirectional_rnn/bw/bw/while/TensorArrayReadV3, bidirectional_rnn/bw/bw/while/Switch_4:1, bidirectional_rnn/bw/bw/while/lstm_cell/split/split_dim)]]
 

 

原因是,当我们设置

init_fw = gru_fw_cell.zero_state(batch_sizes, dtype=tf.float32)
init_bw = gru_bw_cell.zero_state(batch_sizes, dtype=tf.float32)

初始化state时候,第一个维度必须与batch_size保持一致,某则就会出现上面的问题,我出现这个问题还有一个原因,明明我设置的是这个batch_size,在训练阶段没有问题,但是测试阶段报错,后来发现,验证时候,数据集不进行重复batch,最后一个batch不是batch_size的大小,也会出现这种问题。

你可能感兴趣的:(tensorflow报错问题)