简体   繁体   English

Keras LSTM ValueError:检查目标时出错:预期dense_23的形状为(1,),但数组的形状为(70,)

[英]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.

相关问题 Keras:ValueError:检查目标时出错:预期密集具有形状(10,)但得到形状为(400,)的数组 - Keras: ValueError: Error when checking target: expected dense to have shape (10,) but got array with shape (400,) Keras ValueError:检查目标时出错:预期dense_16具有形状(2,),但数组具有形状(1,) - Keras ValueError: Error when checking target: expected dense_16 to have shape (2,) but got array with shape (1,) Keras ValueError:检查目标时出错:预期dense_5的形状为(1,)但得到的数组形状为(0,) - Keras ValueError: Error when checking target: expected dense_5 to have shape (1,) but got array with shape (0,) ValueError:检查目标时出错:预期dense_3具有形状(1,)但得到的数组具有形状(2,)keras - ValueError: Error when checking target: expected dense_3 to have shape (1,) but got array with shape (2,) keras Keras ValueError:检查目标时出错:预期density_15具有3维,但数组的形状为(301390,8) - Keras ValueError: Error when checking target: expected dense_15 to have 3 dimensions, but got array with shape (301390, 8) ValueError:检查目标时出错:预期density_2具有4维,但数组的形状为(64,50)(Keras) - ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (64, 50) (Keras) Keras序列-ValueError:检查目标时出错:预期density_3具有形状(None,45),但具有形状的数组(2868700,1) - Keras Sequential - ValueError: Error when checking target: expected dense_3 to have shape (None, 45) but got array with shape (2868700, 1) ValueError:检查目标时出错:预期 activation_6 具有形状(70,)但得到形状为(71,)的数组 - ValueError: Error when checking target: expected activation_6 to have shape (70,) but got array with shape (71,) ValueError:检查目标时出错:期望dense_3具有形状(1000,)但是得到了具有形状的数组(1,) - ValueError: Error when checking target: expected dense_3 to have shape (1000,) but got array with shape (1,) ValueError:检查目标时出错:预期density_32的形状为(1,),但数组的形状为(10000,) - ValueError: Error when checking target: expected dense_32 to have shape (1,) but got array with shape (10000,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM