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(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
解决方案
从RepeatVector 的文档中,它只接受 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
这里使用 a ,因为来自repeat_elements 的文档,它不会创建新轴。
推荐阅读
- c# - 将文件添加到 Flurl 多部分 POST 请求时,IFormFileCollection 为空
- python-3.x - Pywin32服务启动失败,无法读取json文件
- javascript - 如何使用 Foundation 修复响应式网站中的手风琴错误
- python - Matplotlib 子图轴共享:适用于其他所有图?
- matlab - Collapsing and averaging redundant entries in MATLAB table
- windows - Windows 的 Docker Engine 版本 18.XX.X 是否存在,如果存在,我该如何下载?
- powershell - 如何在 PowerShell 中将嵌套哈希表转换为 PSObjects
- c# - 简单的异步任务无法在 Wpf 应用程序中继续,但在控制台应用程序中有效
- reactjs - 如何在自定义 3d 对象(.obj)上添加自定义 3d 对象材质(.mtl)?
- azure-devops - 如何获取 Azure Devops API 的 PackageID?