ValueError: Error when checking input: expected input_9 to have 3 dimensions, but got array with shape (80, 2048)

ValueError: Error when checking input: expected input_9 to have 3 dimensions, but got array with shape (80, 2048)

我目前正在尝试在 Keras 中构建一个工作的 lstm 模型,该模型从 RNN 之前的 CNN 接收嵌入。 CNN 显示了预期的行为,但我不能 100% 确定我是否可以将嵌入从 CNN 传递到 RNN。

def model_builder(input_shape):
    base_input = Input(shape = input_shape)
    x = LSTM(units=1, name='LSTM1', return_sequences=True)(base_input)
    x = Flatten()(x)
    x = Dense(units = 2)(x)
    x = Activation('softmax')(x)
    classification_model = Model(base_input, x,name='classifier')
    classification_model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

    return classification_model

embedding_model = build_model((256, 256, 3))
classification_model = model_builder((2048,1,))

try:
    image_embedding = embedding_model.predict(X)

    outcome = classification_model.fit(x=image_embedding, y=Y, batch_size=10, epochs=20, verbose=1, 
              callbacks=None, validation_split=0.5, validation_data=None, shuffle=False, 
              class_weight=None,sample_weight=None, initial_epoch=0, steps_per_epoch=None, 
              validation_steps=None, validation_freq=1)
except KeyboardInterrupt:
    pass

当我 运行 像这样训练时,我得到这个错误信息:

像这样调整代码为我解决了这个问题。问题是我需要为我正在通过我的 lstm 的嵌入添加一个时间维度。所以基本上是从 (batch_size,input_dimension) 到 (batch_size,input_dimension,timesteps)。这些维度的顺序可能不正确。我仍然需要检查一下,但这个解决方案至少应该能让模型正常工作。

image_embedding = embedding_model.predict(X)
new_image_embedding = image_embedding[:,:,np.newaxis]

outcome = classification_model.fit(x=new_image_embedding, y=Y, batch_size=batch_size, 
          epochs=epochs, verbose=1, callbacks=None, validation_split=0.5,  
          validation_data=None, shuffle=False, class_weight=None, sample_weight=None, 
          initial_epoch=0, steps_per_epoch=None, validation_steps=None, 
          validation_freq=1)