如何解决预测函数上的LSTM错误?



这是下面附加的代码。我试图运行它,并得到下面这个错误。需要帮助解决。图定义为变量名。

graph = Sequential()
graph.add(LSTM(50, return_sequences=True, input_shape = (60, 1)))
graph.add(LSTM(50, return_sequences=False))
graph.add(Dense(25))
graph.add(Dense(1))
graph.compile(optimizer='adam', loss='mean_squared_error')
graph.fit(x_train, y_train, batch_size=1, epochs=1)
test_data = train_data[training_data_length - 60: , : ]
x_test = []
y_test =  dataset[training_data_length : , : ]
for i in range(60,len(test_data)):
x_test.append(test_data[i-60:i,0])
x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0],x_test.shape[0],1))
predictions = graph.predict(x_test)
predictions = transfer.inverse_transform(predictions)

得到这个错误

ValueError                                Traceback (most recent call last)
<ipython-input-40-4624ea9a4af1> in <module>()
6 x_test = np.array(x_test)
7 x_test = np.reshape(x_test, (x_test.shape[0],x_test.shape[0],1))
----> 8 predictions = graph.predict(x_test)
9 predictions = transfer.inverse_transform(predictions)
1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1995             callbacks.on_predict_batch_end(end_step, {'outputs': batch_outputs})
1996       if batch_outputs is None:
-> 1997         raise ValueError('Unexpected result of `predict_function` '
1998                          '(Empty batch_outputs). Please use '
1999                          '`Model.compile(..., run_eagerly=True)`, or '
ValueError: Unexpected result of `predict_function` (Empty batch_outputs). Please use `Model.compile(..., run_eagerly=True)`, or `tf.config.run_functions_eagerly(True)` for more information of where went wrong, or file a issue/bug to `tf.keras`.

根据您的结构,您的test_data的长度始终为60

test_data = train_data[training_data_length - 60: , : ]

所以,你的for循环永远不会执行,因为它总是等于for i in range(60,60)。因此,x_test仍然是[]并导致错误。

我猜你想构造一组长度为60的序列,每个序列在下一个时间步偏移1。在这种情况下,测试数据应该被构造为

for i in range(60,len(train_data)):
x_test.append(train_data[i-60:i,0])

还有,你的np.reshape用法似乎是错误的。重塑后的元素数量将是x_test.shape[0] * x_test.shape[0],但原来的x_testx_test.shape[0] * x_test.shape[1],所以除非x_test.shape[0] == x_test.shape[1],否则你总是会得到一个错误。你不需要重塑,因为你的x_test应该已经在正确的形状。

相关内容

  • 没有找到相关文章

最新更新