【问题标题】:Issue in LSTM Input Dimensions in KerasKeras 中的 LSTM 输入维度问题
【发布时间】:2018-01-03 09:54:41
【问题描述】:

我正在尝试使用 keras 实现多输入 LSTM 模型。代码如下:

data_1 -> shape (1150,50) 
data_2 -> shape (1150,50)
y_train -> shape (1150,50)

input_1 = Input(shape=data_1.shape)
LSTM_1 = LSTM(100)(input_1)

input_2 = Input(shape=data_2.shape)
LSTM_2 = LSTM(100)(input_2)

concat = Concatenate(axis=-1)
x = concat([LSTM_1, LSTM_2])
dense_layer = Dense(1, activation='sigmoid')(x)
model = keras.models.Model(inputs=[input_1, input_2], outputs=[dense_layer])

model.compile(loss='binary_crossentropy',
                      optimizer='adam',
                      metrics=['acc'])

model.fit([data_1, data_2], y_train, epochs=10)

当我运行这段代码时,我得到一个 ValueError:

ValueError: 检查模型输入时出错:预期 input_1 有 3 个维度,但得到的数组形状为 (1150, 50)

有没有人可以解决这个问题?

【问题讨论】:

    标签: machine-learning neural-network keras lstm keras-layer


    【解决方案1】:

    在定义模型之前使用data1 = np.expand_dims(data1, axis=2)LSTM 需要维度为 (batch_size, timesteps, features) 的输入,因此,在您的情况下,我猜您有 1 个特征、50 个时间步长和 1150 个样本,您需要在向量的末尾添加一个维度。

    这需要在定义模型之前完成,否则当您设置 input_1 = Input(shape=data_1.shape) 时,您告诉 keras 您的输入有 1150 个时间步长和 50 个特征,因此它将期望输入形状为 (None, 1150, 50)(非代表“任何维度都可以接受”)。

    input_2 也是如此

    希望对你有帮助

    【讨论】:

    • 感谢您的回复。但这给出了另一个错误:Error when checking model input: expected input_2 to have shape (None, 1150, 50) but got array with shape (1150, 50, 1)
    • 正如我在答案中所说,您需要在初始化输入之前执行data2 = np.expand_dims(data2, axis=2),否则您会收到这种错误
    • 好的。问题出在轴上。 data_1 = np.expand_dims(data_1, axis=0) 为我工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2018-06-16
    • 2017-07-25
    • 1970-01-01
    • 2019-01-21
    相关资源
    最近更新 更多