我有一个简单的seq2seq模型来预测股价。我创建了一个lstm单元的编码器和一个解码器,它们可以预测接下来的5个时间步长值,但是会引发错误:
ValueError: Dimensions must be equal, but are 517 and 562 for 'rnn/while/rnn/multi_rnn_cell/cell_0/lstm_cell/MatMul_1' (op: 'MatMul') with input shapes: [10,517], [562,2048].
数据样本
t1 t2 t3 t4 t5 ...
19/10/2018 0.005 0.100 -0.021 0.030 -0.025
20/10/2018 0.023 0.020 0.020 0.130 0.125
21/10/2018 -0.205 0.140 -0.011 0.020 -0.305
代码
import tensorflow as tf
import numpy as np
seq_len = 1
n_inputs = 50
n_outputs = 5
n_layers = 3
n_neurons = 512
batch_size = 10
g = tf.Graph()
with g.as_default():
X = tf.placeholder(tf.float32,shape=(None,seq_len,n_inputs),name="X")
y = tf.placeholder(tf.float32,shape=(None,seq_len,n_outputs),name="y")
cells = tf.nn.rnn_cell.MultiRNNCell([ tf.nn.rnn_cell.LSTMCell(n_neurons) for _ in range(n_layers) ])
init_state = cells.zero_state(batch_size, tf.float32)
enc_outputs, enc_states = tf.nn.dynamic_rnn(cells, X,initial_state=init_state)
dec_outputs,dec_states = tf.nn.dynamic_rnn(cells, y, initial_state=enc_states)
loss = tf.reduce_mean(tf.square(dec_outputs - y))
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session(graph=g)
sess.run(init)
欢迎任何帮助。
答案 0 :(得分:1)
首先,我无法将您的问题标记为重复,因为它有很多悬赏。之所以会出现错误,是因为您必须不要在第一层以及更深的层中重复使用相同的单元格。这是因为提供给它们的输入不同,这使得内核矩阵不同。根据{{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)
this在此问题上提供了更多帮助。