Keras 如何使用重复向量预测输出序列?

Keras 如何使用重复向量预测输出序列?,keras,Keras,我试图定义一个适用于图像序列的模型,并尝试依次预测序列。我的问题是重复向量,特别是它的正确使用,其次是如何解决我得到的异常 input_frames=Input(shape=(None, 128, 64, 1)) x=ConvLSTM2D(filters=64, kernel_size=(5, 5), padding='same', return_sequences=True)(input_frames) x=BatchNormalization()(x) x=ConvLSTM2D(filter

我试图定义一个适用于图像序列的模型,并尝试依次预测序列。我的问题是重复向量,特别是它的正确使用,其次是如何解决我得到的异常

input_frames=Input(shape=(None, 128, 64, 1))
x=ConvLSTM2D(filters=64, kernel_size=(5, 5), padding='same', return_sequences=True)(input_frames)
x=BatchNormalization()(x)
x=ConvLSTM2D(filters=64, kernel_size=(5, 5), padding='same', return_sequences=False)(x)
x=BatchNormalization()(x)
x=Conv2D(filters=1, kernel_size=(5, 5), activation='sigmoid',padding='same')(x)
x=RepeatVector(10)(x)
model=Model(inputs=input_frames,outputs=x)
具体来说,我试图预测未来的10帧。上述代码引发以下异常:

assert_input_compatibility
str(K.ndim(x)))
ValueError: Input 0 is incompatible with layer repeat_vector_5: expected ndim=2, found ndim=4
从中,它只接受2D输入,这就是错误消息告诉您的

以下是使用
Lambda
层的解决方法:

from keras.layers import Input, ConvLSTM2D, BatchNormalization, RepeatVector, Conv2D
from keras.models import Model
from keras.backend import expand_dims, repeat_elements
from keras.layers import Lambda

input_frames=Input(shape=(None, 128, 64, 1))
x=ConvLSTM2D(filters=64, kernel_size=(5, 5), padding='same', return_sequences=True)(input_frames)
x=BatchNormalization()(x)
x=ConvLSTM2D(filters=64, kernel_size=(5, 5), padding='same', return_sequences=False)(x)
x=BatchNormalization()(x)
x=Conv2D(filters=1, kernel_size=(5, 5), activation='sigmoid',padding='same')(x)
#x=RepeatVector(10)(x)
x=Lambda(lambda x: repeat_elements(expand_dims(x, axis=1), 10, 1))(x)
model=Model(inputs=input_frames,outputs=x)
model.summary()

"""
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_15 (InputLayer)        (None, None, 128, 64, 1)  0         
_________________________________________________________________
conv_lst_m2d_5 (ConvLSTM2D)  (None, None, 128, 64, 64) 416256    
_________________________________________________________________
batch_normalization_5 (Batch (None, None, 128, 64, 64) 256       
_________________________________________________________________
conv_lst_m2d_6 (ConvLSTM2D)  (None, 128, 64, 64)       819456    
_________________________________________________________________
batch_normalization_6 (Batch (None, 128, 64, 64)       256       
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 128, 64, 1)        1601      
_________________________________________________________________
lambda_1 (Lambda)            (None, 10, 128, 64, 1)    0         
=================================================================
Total params: 1,237,825
Trainable params: 1,237,569
Non-trainable params: 256
_________________________________________________________________
"""
请注意,我在这里使用了一个
expand_dims
,因为从开始,它不会创建新的轴