有状态LSTM输入形状错误

问题描述 投票:1回答:1

我正在做一个处理长序列多类分类的项目。 这是我的数据google colab snapshot的组织 我将整个序列分成60个子序列,每个子序列包含250个样本。 y_train如下。

y_train = []
for i in range(len(resampled_data)):
  y_train.append(1)
y_train_array = np.array(y_train)
one_hot_y_train = keras.utils.to_categorical(1,num_classes=4)
print("y_train")
print(len(one_hot_y_train))
y_train = np.array([one_hot_y_train]*60)
print(np.shape(y_train))
print(y_train)

我有4个分类类,这个序列被标记为'1'(1级)

并且模型定义如下。

#start constructing  stateful LSTM  model
model = Sequential()       
model.add(keras.layers.LSTM(250,batch_input_shape=(60,250,1), 
activation='tanh',return_sequences=True,stateful=True,recurrent_dropout=0.2))
model.add(keras.layers.core.Dropout(0.2))
model.add(keras.layers.Dense(4,activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',
          optimizer = 'adam',
          metrics=['accuracy'])
print(model.summary())
model.fit(x_train, y_train, epochs=10, batch_size=250,shuffle=False)

但是在训练模型时,有一个我无法完全理解的错误。

ValueError: Error when checking target: expected dense_23 to have 3 dimensions, but got array with shape (60, 4)

我想知道为什么会发生这种情况以及如何修复它,我的输入形状有问题或者我的概念是错误的吗? PS。在这种情况下,我只是使用一个序列来测试我的模型,如果它可以无误地运行,并且在下一步,我将使用循环来训练所有数据!现在我坚持这个问题。如果有人可以指导我,我将非常感谢!

python tensorflow keras deep-learning lstm
1个回答
0
投票

因为你有return_sequences=True

model.add(keras.layers.LSTM(250,batch_input_shape=(60,250,1), activation='tanh',return_sequences=True,stateful=True,recurrent_dropout=0.2))

它返回形状[batch_size, time_step, hidden_dim]的三维张量

现在,如果你想对这样的时间序列数据使用密集层,你需要在Keras中使用TimeDistributed,方法如下:

model.add(TimeDistributed(Dense(4,activation='softmax')))

© www.soinside.com 2019 - 2024. All rights reserved.