我正在构建一个LSTM网络。我的数据如下所示:
X_train.shape = (134, 300000, 4)
X_train包含134个序列,每个序列有300000个时间步和4个特征。
Y_train.shape = (134, 2)
Y_train包含134个标签,[1, 0]表示True,[0, 1]表示False。
以下是我的Keras模型。
model = Sequential()model.add(LSTM(4, input_shape=(300000, 4), return_sequences=True))model.compile(loss='categorical_crossentropy', optimizer='adam')
每当我运行模型时,我都会收到以下错误:
Error when checking target: expected lstm_52 to have 3 dimensions, but got array with shape (113, 2)
这个问题似乎与我的Y_train数据有关——它的形状是(113, 2)。
谢谢!
回答:
你的LSTM层的输出形状是(batch_size, 300000, 4)
(因为return_sequences=True
)。因此,你的模型期望目标y_train
有3个维度,但你传递了一个只有2个维度的数组(batch_size, 2)
。
你可能需要使用return_sequences=False
。在这种情况下,LSTM层的输出形状将是(batch_size, 4)
。此外,你应该在模型中添加一个最终的softmax层,以获得期望的输出形状(batch_size, 2)
:
model = Sequential()model.add(LSTM(4, input_shape=(300000, 4), return_sequences=False))model.add(Dense(2, activation='softmax')) # 因为你有2个类别,所以使用2个神经元model.compile(loss='categorical_crossentropy', optimizer='adam')