这是我的代码
model = Sequential()
model.add(LSTM(512, return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(512, return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(1, return_sequences=True))
我收到此错误
ValueError: Error when checking target: expected lstm_3 to have 3 dimensions, but got array with shape (62796, 1)
如果我设置了return_sequences=True
,则输出形状为3D阵列
那么,为什么会发生此错误?
答案 0 :(得分:1)
keras LSTM层的输入和输出应为3维,默认情况下为
(批量大小,时间步长,功能)。
似乎您从错误消息中仅使用了两个维度(62796,1)。
以下是一个使用合成数据的最小工作示例,它说明了LSTM网络所需的输入和输出形状。
from keras.models import Sequential
from keras.layers import LSTM, Dropout
import numpy as np
numb_outputs = 1
batch_size = 10
timesteps = 5
features = 2
x_single_batch = np.random.rand(batch_size, timesteps, features)
y_single_batch = np.random.rand(batch_size, timesteps, numb_outputs)
model = Sequential()
model.add(LSTM(512, return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(512, return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(numb_outputs, return_sequences=True))
model.compile(optimizer='adam',loss='mse')
model.fit(x= x_single_batch, y=y_single_batch)