在构建多层LSTM结构时容易出现以下错误
ValueError: Dimensions must be equal, but are 200 and 104 for 'stack_bidirectional_rnn/cell_0/bidirectional_rnn/fw/fw/while/fw/multi_rnn_cell/cell_0/basic_lstm_cell/MatMul_1' (op: 'MatMul') with input shapes: [?,200], [104,400]
ValueError: Dimensions must be equal, but are 200 and 104 for 'stack_bidirectional_rnn/cell_0/bidirectional_rnn/fw/fw/while/fw/multi_rnn_cell/cell_0/basic_lstm_cell/MatMul_1' (op: 'MatMul') with input shapes: [?,200], [104,400]
设置不同的隐含层节点上面的数字会不同,主要由下面这一段代码引起:
lstm_fw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
lstm_bw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
stacked_fw_rnn = []
stacked_bw_rnn = []
for i in range(num_layer):
stacked_fw_rnn.append(lstm_fw_cell)
stacked_bw_rnn.append(lstm_bw_cell)
mcell_fw = tf.contrib.rnn.MultiRNNCell(stacked_fw_rnn,state_is_tuple=True)
mcell_bw = tf.contrib.rnn.MultiRNNCell(stacked_bw_rnn,state_is_tuple=True)
上面这段代码主要是先定义好正向和反向的cell, 再根据设置的隐含层数目用append将其构建成列表,
再看下面的代码与其区别:
stacked_fw_rnn = []
stacked_bw_rnn = []
for i in range(num_layer):
stacked_fw_rnn.append(tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0))
stacked_bw_rnn.append(tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0))
# 实现多层搭建
mcell_fw = tf.contrib.rnn.MultiRNNCell(stacked_fw_rnn,state_is_tuple=True)
mcell_bw = tf.contrib.rnn.MultiRNNCell(stacked_bw_rnn,state_is_tuple=True)
主要区别就在于每构建一层都会调用一次tf.contrib.rnn.BasicLSTMCell,问题可以解决