python - 如何创建具有本地连接层和密集父层的本地连接层?

标签 python tensorflow keras

我目前有一个带有两个输入层的网络( image of toy example )。 in1只是一个短而扁平的值向量,但是 in2是27 channel 图像。我希望我的网络构建在本地连接的层上,但我不知道如何添加 in1的数据为in2 。我目前正在展平in2几层后的分支,与 in1 合并,然后继续添加密集层。

如何密集介绍in1的数据,同时保持本地连接的架构?上面链接的图像用红色箭头显示了这个目标。

我想出的一个可能的解决方案是复制 in1的向量作为 in2 的 channel 这样in2的维度将是 width * height * (num_original_channels + len(in1) ) 。这看起来不优雅,因为它会复制 in1 很多次。一定有更好的方法。

我是 keras 新手,所以请原谅我不稳定的词汇。另外,这是一个玩具示例,只是为了说明我的想法,因此可能会有一些其他/不相关的架构批评。

预先感谢您的任何建议!

fwiw,这是我正在使用的代码:

input1 = Input( ... ) #small flat vec
input2 = Input( ... ) #deep picture

pre = Reshape( ... )( input2 )
l1 = LocallyConnected2D( ... )( pre )
l2 = LocallyConnected2D( ... )( l1 )
l3 = LocallyConnected2D( ... )( l2 )
flat = Flatten( ... )( l3 )
merge = tensorflow.keras.layers.concatenate( [flat, input1], ... )
l4 = Dense( ... )( merge )
l5 = Dense( ... )( l4 )
output = Dense( ... )( l5 )

最佳答案

在这里回答我自己的问题。看起来最好的解决方案是让 input1 和 input2 创建两个具有相同层且没有激活函数的独立张量,将它们加在一起,然后添加激活。

使用我之前的示例,它看起来像这样:

(我添加示例尺寸是为了澄清我的意思。它们是凭空组成的)

input1 = Input( ... ) #small flat vec, 1x200
input2 = Input( ... ) #deep picture,   50x50x10

l1 = LocallyConnected2D( activation=None, ... )( input2 ) # 40x40x5

num_elements = 40 * 40 * 5
d1 = Dense( units=num_elements, activation=None, ... )( input1 ) # 1x8000
d1_3D = Reshape( target_shape=(40, 40, 5,) )( d1 ) #40x40x5

merge = Add()([ l1, d1_3D ]) #40x40x5
l2 = LeakyReLU( ... )( merge ) #Or whatever activation function you want, 40x40x5

# ...

关于python - 如何创建具有本地连接层和密集父层的本地连接层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56975408/

相关文章:

python - (Selenium) send_keys(Keys.ENTER) 不工作但显示错误

tensorflow - 如何找到 LSTMCell 权重和偏差名称以将值加载到其中?

python - 无法将 NumPy 数组转换为张量(不支持的对象类型 int)

Python 计算错误的大 float

Python:如何从加拿大的 shapefile 创建等值线图?

tensorflow - TensorFlow中的Bazel是什么?什么时候需要重新构建?

python - 在 Keras 中 reshape 自定义损失函数中的张量

python - 是否可以根据批处理标签(y_true)分布更新每批处理的学习率?

python - 外部函数 : who deallocates memory?

tensorflow - Keras Multi GPU 示例给出 ResourceExhaustedError