有状态LSTM输入形状错误

时间:2018-08-04 08:56:57

标签: python tensorflow keras deep-learning lstm

我正在做一个处理长序列多类分类的项目。
这是我的数据的组织 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)

我想知道为什么会这样以及如何解决,输入形状有问题还是我的概念有误?
附言在这种情况下,我只使用一个序列来测试我的模型(如果模型可以正确运行),下一步,我将使用循环来训练所有数据!现在,我陷入了这个问题。如果有人可以指导我,我将不胜感激!

1 个答案:

答案 0 :(得分: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]的3维张量

现在,如果要为此类时间序列数据使用密集层,则需要按以下方式在Keras中使用TimeDistributed

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

相关问题