tensorflow - 在 Keras 中使用 Masking-layer 和 ConvLSTM2D-layer

标签 tensorflow keras keras-2

我正在尝试使用 Keras (2.0.6) 和 TensorFlow 后端 (1.2.1) 来掩盖卷积 LSTM 层中的缺失数据:

import keras
from keras.models import Sequential
from keras.layers import Masking, ConvLSTM2D

n_timesteps = 10
n_width = 64
n_height = 64
n_channels = 1

model = Sequential()
model.add(Masking(mask_value = 0., input_shape = (n_timesteps, n_width, n_height, n_channels)))
model.add(ConvLSTM2D(filters = 64, kernel_size = (3, 3)))

但是我收到以下 ValueError:

ValueError: Shape must be rank 4 but is rank 2 for 'conv_lst_m2d_1/while/Tile' (op: 'Tile') with input shapes: [?,64,64,1], [2].

如何将 Masking 与 ConvLSTM2D 层结合使用?

最佳答案

Keras 在将 Masking 层应用于图像等任意张量时似乎存在问题。我可以找到一种(远非理想的)解决方法,其中涉及在应用 mask 层之前展平输入。因此考虑原始模型:

model = Sequential()
model.add(Masking(mask_value=0., input_shape=(n_timesteps, n_width, n_height, n_channels)))
model.add(ConvLSTM2D(filters=64, kernel_size=(3, 3)))

我们可以修改如下:

input_shape = (n_timesteps, n_width, n_height, n_channels)
model = Sequential()
model.add(TimeDistributed(Flatten(), input_shape=input_shape))
model.add(TimeDistributed(Masking(mask_value=0.)))
model.add(TimeDistributed(Reshape(input_shape[1:])))
model.add(ConvLSTM2D(filters=64, kernel_size=(3, 3)))

此解决方案在图表中添加了额外的计算,但没有额外的参数。

关于tensorflow - 在 Keras 中使用 Masking-layer 和 ConvLSTM2D-layer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45562920/

相关文章:

macos - Tensorflow 错误 tensorflow/core/platform/cloud/http_request.cc :334

apache-spark - "Check whether your GraphDef-interpreting binary is up to date with your GraphDef-generating binary."

python-3.x - 没有名为 'pandas' 的模块 - Jupyter、Python3 内核、TensorFlow 通过 Docker

python - TensorFlow、Keras、Flask - 无法通过 Flask 将我的 Keras 模型作为 Web 应用程序运行

python - 将 tensorflow 数据集输入模型

python - Keras 2 fit_generator 用户警告 : `steps_per_epoch` is not the same as the Keras 1 argument `samples_per_epoch`

TensorFlow:当 is_training = False 时,Batch Norm 会破坏网络

python - Tensorflow 模型返回疯狂的损失值

Keras 外部的 Keras 初始值设定项

python - 喀拉斯 2 : Using lambda function in "Merge" layers