我想创建一个自定义层,它应该将密集层的输出与Convolution2D层融合 .
想法来自this paper,这是网络:
融合层试图将Convolution2D张量( 256x28x28
)与密集张量( 256
)融合 . 这是它的等式:
y_global => Dense layer output with shape 256
y_mid => Convolution2D layer output with shape 256x28x28
以下是有关Fusion流程的文章说明:
我最终制作了一个新的自定义图层,如下所示:
class FusionLayer(Layer):
def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
super(FusionLayer, self).__init__(**kwargs)
def build(self, input_shape):
input_dim = input_shape[1][1]
initial_weight_value = np.random.random((input_dim, self.output_dim))
self.W = K.variable(initial_weight_value)
self.b = K.zeros((input_dim,))
self.trainable_weights = [self.W, self.b]
def call(self, inputs, mask=None):
y_global = inputs[0]
y_mid = inputs[1]
# the code below should be modified
output = K.dot(K.concatenate([y_global, y_mid]), self.W)
output += self.b
return self.activation(output)
def get_output_shape_for(self, input_shape):
assert input_shape and len(input_shape) == 2
return (input_shape[0], self.output_dim)
我认为我得到了 __init__
和 build
方法,但我不知道如何在 call
层中连接 y_global
(256个像素)与 y-mid
(256x28x28维度),以便输出与上面提到的等式相同 .
如何在 call
方法中实现此等式?
非常感谢...
更新:成功整合这两层数据的任何其他方式对我来说也是可以接受的......它不一定是文中提到的方式,但它至少需要返回一个可接受的输出......
2 回答
我不得不在Keras Github页面上问这个问题,有人帮我解决了如何正确实现它...这里是issue在github上...
在我看来,实现一种新的层是一种复杂的任务 . 我强烈建议您使用以下图层:
Flatten,
Merge,
Dense,
为了获得预期的行为 .