【发布时间】:2019-12-28 01:01:55
【问题描述】:
我正在尝试使用 LSTM 对时间序列数据进行多类分类。
训练集的维度为 (390, 179),即 390 个对象,每个对象有 179 个时间步长。
有 37 个可能的类。
我想使用只有 LSTM 和激活层的 Keras 模型对输入数据进行分类。
我还需要所有通过模型的训练数据和测试数据的隐藏状态,在 LSTM 的每一步(不仅仅是最终状态)。
我知道return_sequences=True 是必需的,但我无法让尺寸匹配。
以下是我尝试过的一些代码,但我已经尝试过大量其他来自 stackexchange 和 git 问题的调用组合。在所有这些中,我都会遇到一些尺寸不匹配或其他问题。
我不知道如何从模型中提取隐藏状态表示。
我们有X_train.shape = (390, 1, 179)、Y_train.shape = (390, 37)(一次性二元向量)/。
n_units = 8
n_sequence = 179
n_class = 37
x = Input(shape=(1, n_sequence))
y = LSTM(n_units, return_sequences=True)(x)
z = Dense(n_class, activation='softmax')(y)
model = Model(inputs=[x], outputs=[y])
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.fit(X_train, Y_train, epochs=100, batch_size=128)
Y_test_predict = model.predict(X_test, batch_size=128)
这就是上面给我的:
ValueError: A target array with shape (390, 37) was passed for an output of shape (None, 1, 37) while using as loss 'categorical_crossentropy'. This loss expects targets to have the same shape as the output.
【问题讨论】:
标签: python keras time-series lstm multiclass-classification