[英]Keras LSTM ValueError: Error when checking target: expected dense_23 to have shape (1,) but got array with shape (70,)
I am trying to train a basic LSTM network, but I am running into an error with model.fit.我正在尝试训练一个基本的 LSTM 网络,但我遇到了 model.fit 的错误。 I have two datasets, each with 3145 sequences of length 7. I want to package these two datasets into the same time step.
我有两个数据集,每个数据集都有 3145 个长度为 7 的序列。我想将这两个数据集 package 放到同一个时间步中。 As such, I have reshaped my x_train and y_train into the following shapes:
因此,我将 x_train 和 y_train 重新塑造成以下形状:
x_train.shape = (3145, 70, 2)
y_train.shape = (3145, 70)
As you can see, I should have 3145 samples, each with 70 timesteps, and each with 2 features and a target.如您所见,我应该有 3145 个样本,每个样本有 70 个时间步长,每个样本有 2 个特征和一个目标。 I then define the following model:
然后我定义以下 model:
model = Sequential()
model.add(LSTM(4, input_shape=(x_train.shape[1], x_train.shape[2])))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
To train, I run the line为了训练,我跑线
model.fit(x_train, y_train, epochs=100, batch_size=1, verbose=2)
But this gives me the error但这给了我错误
ValueError: Error when checking target: expected dense_23 to have shape (1,) but got array with shape (70,)
I'm confused as to why this error is occurring.我很困惑为什么会发生这个错误。 With 70 time steps, I should have 70 targets right?
有 70 个时间步,我应该有 70 个目标,对吗?
I would greatly appreciate any help explaining this error!我将不胜感激任何帮助解释这个错误!
You need the same units in input layer dense than the output dim of the LSTM layer.您需要与 LSTM 层的 output dim 相同的输入层密集单元。 In your case the output of the LSTM layer is 70 like you can see in the shape.
在您的情况下,LSTM 层的 output 是 70,就像您在形状中看到的那样。
try change by尝试改变
model.add(Dense(70))
or let the model infer the amount of units of the layer like this.或者让 model 像这样推断层的单位数量。
model.add(Dense())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.