简体   繁体   English

如何在 keras 中将 Conv1D 的输出与 Conv2D 的输出结合起来

[英]How to combine output of Conv1D with output of Conv2D in keras

I want to add a scalar value to the output of a Conv2D operation in the following way:我想通过以下方式向 Conv2D 操作的输出添加一个标量值:

num_filters = 16
num_targets = 10
input_conv = layers.Conv2D(num_filters, (3, 3), activation='relu', padding='same')(input_img)
target_conv = layers.Conv1D(num_filters, num_targets, use_bias=False, activation='linear')(label)
# TODO: add output target_conv to input_conv along each of the filter dimensions
# this is like adding a scalar value for each of the filter dimensions

The input_conv output is of shape (None, 28, 28, 16) and the target_conv is of shape (None, 1, 16) , where 28 x 28 corresponds to the image dimensions and 16 is the number of filters. input_conv输出的形状为(None, 28, 28, 16)target_conv的形状为(None, 1, 16) ,其中 28 x 28 对应于图像尺寸,16 是过滤器的数量。 For each filter (each of the 16 dimensions), I want to add the corresponding target_conv value as a scalar.对于每个过滤器(16 个维度中的每一个),我想添加相应的 target_conv 值作为标量。 So imagine we are just looking at the output of the first filter for input_conv and target_conv , that gives us shapes (None, 28, 28, 1) and (None, 1, 1).所以想象一下,我们只是在查看input_convtarget_conv的第一个过滤器的输出,它为我们提供了形状 (None, 28, 28, 1) 和 (None, 1, 1)。 I want to add the output of target_conv to the output of input_conv which would still output (None, 28, 28, 1) for the first filter, and this would then happen for each filter dimension to output (None, 28, 28, 16).我想将target_conv的输出添加到input_conv的输出中,该输出仍然会为第一个过滤器输出 (None, 28, 28, 1),然后每个过滤器维度都会发生这种情况以输出 (None, 28, 28, 16 )。

However, I am not sure how to implement this?但是,我不确定如何实现这一点?

All you need to do is simply add one dimensionality to the target_conv output in order to have it in 4D.您需要做的就是简单地向 target_conv 输出添加一维,以便将其呈现为 4D。 This enables you to make a simple sum between the two outputs这使您可以在两个输出之间进行简单的求和

num_filters = 16

input_img = Input((28,28,1))
label = Input((10,1))
input_conv = Conv2D(num_filters, (3, 3), 
                    activation='relu', padding='same')(input_img) # (None,28,28,num_filters)
target_conv = Conv1D(num_filters, 10, 
                     use_bias=False, activation='linear')(label) # (None,1,num_filters)

target_conv = Reshape((1,1,num_filters))(target_conv) # (None,1,1,num_filters)
sum_filter = Add()([input_conv,target_conv]) # (None,28,28,num_filters)

m = Model([input_img,label], sum_filter)
m.summary()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM