我在Keras中具有以下模型:
main_input = Input(shape=(None, 2, 100, 100), dtype='float32', name='input')
hidden = ConvLSTM2D(filters=16,
kernel_size=(5, 5),
padding='same',
return_sequences=False,
data_format='channels_first')(main_input)
output = Conv2D(filters=1,
kernel_size=(1, 1),
padding='same',
activation='sigmoid',
kernel_initializer='glorot_uniform',
data_format='channels_first',
name='output')(hidden)
sgd = SGD(lr=0.002, momentum=0.0, decay=0.0, nesterov=False)
我想将2d数组的输出乘以一个掩码(每个示例都有一个单独的掩码)。我如何在Keras中做到这一点?
答案 0 :(得分:2)
使用tensorflow 2.0和tf.keras进行这项工作。
import tensorflow as tf
from tensorflow.keras.layers import Multiply, Conv2D, ConvLSTM2D, Input
main_input = Input(shape=(None, 2, 100, 100), dtype='float32', name='input')
mask=Input(shape=(1, 100, 100), dtype='float32', name='mask')
hidden = ConvLSTM2D(filters=16,
kernel_size=(5, 5),
padding='same',
return_sequences=False,
data_format='channels_first')(main_input)
output = Conv2D(filters=1,
kernel_size=(1, 1),
padding='same',
activation='sigmoid',
kernel_initializer='glorot_uniform',
data_format='channels_first',
name='output')(hidden)
output_with_mask=Multiply()([output, mask])
答案 1 :(得分:1)
我认为您应该同时将每个样本的蒙版输入模型。
这是建议的代码:
from keras.layers import Multiply
main_input = Input(shape=(None, 2, 100, 100), dtype='float32', name='input')
mask=Input(shape=(1, 100, 100), dtype='float32', name='mask')
hidden = ConvLSTM2D(filters=16,
kernel_size=(5, 5),
padding='same',
return_sequences=False,
data_format='channels_first')(main_input)
output = Conv2D(filters=1,
kernel_size=(1, 1),
padding='same',
activation='sigmoid',
kernel_initializer='glorot_uniform',
data_format='channels_first',
name='output')(hidden)
output_with_mask=Multiply()([output, mask])
model=Model([main_input, mask], output_with_mask)
摘要如下:
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input (InputLayer) (None, None, 2, 100, 0
__________________________________________________________________________________________________
conv_lst_m2d_7 (ConvLSTM2D) (None, 16, 100, 100) 28864 input[0][0]
__________________________________________________________________________________________________
output (Conv2D) (None, 1, 100, 100) 17 conv_lst_m2d_7[0][0]
__________________________________________________________________________________________________
mask (InputLayer) (None, 1, 100, 100) 0
__________________________________________________________________________________________________
multiply_7 (Multiply) (None, 1, 100, 100) 0 output[0][0]
mask[0][0]
==================================================================================================
Total params: 28,881
Trainable params: 28,881
Non-trainable params: 0
__________________________________________________________________________________________________
答案 2 :(得分:0)
创建一个新输出,并将您的旧输出用作第二个隐藏层。
您想在“旧输出”上进行第二次卷积(带有特殊掩码)以获取新输出
希望对您有帮助