我想构建一个包含3个图层的LSTM网络。这是代码:
num_layers=3
time_steps=10
num_units=128
n_input=1
learning_rate=0.001
n_classes=1
...
x=tf.placeholder("float",[None,time_steps,n_input],name="x")
y=tf.placeholder("float",[None,n_classes],name="y")
input=tf.unstack(x,time_steps,1)
lstm_layer=rnn_cell.BasicLSTMCell(num_units,state_is_tuple=True)
network=rnn_cell.MultiRNNCell([lstm_layer for _ in range(num_layers)],state_is_tuple=True)
outputs,_=rnn.static_rnn(network,inputs=input,dtype="float")
使用num_layers=1
它可以正常工作,但如果有多个图层,我会在此行收到错误:
outputs,_=rnn.static_rnn(network,inputs=input,dtype="float")
ValueError:尺寸必须相等,但为256和129 ' RNN / RNN / multi_rnn_cell / cell_0 / cell_0 / basic_lstm_cell / MatMul_1' (OP: 输入形状为:[?,256],[129,512]。
任何人都可以解释129和512的来源吗?
答案 0 :(得分:3)
您不应该为第一层和更深层重复使用相同的单元格,因为它们的输入是不同的,因此内核矩阵是不同的。试试这个:
# Extra function is for readability. No problem to inline it.
def make_cell(lstm_size):
return tf.nn.rnn_cell.BasicLSTMCell(lstm_size, state_is_tuple=True)
network = rnn_cell.MultiRNNCell([make_cell(num_units) for _ in range(num_layers)],
state_is_tuple=True)