代码之家  ›  专栏  ›  技术社区  ›  Arpitha

Keras中LSTM输入维度的问题

  •  2
  • Arpitha  · 技术社区  · 8 年前

    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:检查模型输入时出错:预期输入_1有3个维度,但得到了具有形状的数组(1150,50)

    有人能解决这个问题吗?

    1 回复  |  直到 8 年前
        1
  •  1
  •   gionni    8 年前

    使用 data1 = np.expand_dims(data1, axis=2) ,然后再定义模型。 LSTM (batch_size, timesteps, features)

    这需要在定义模型之前完成,否则在设置 input_1 = Input(shape=data_1.shape) 你告诉keras,你的输入有1150个时间步长和50个特性,因此它需要形状输入 (None, 1150, 50)

    同样适用于 input_2

    希望这有帮助