我想制作一个自定义层,它应该将密集层的输出与 Convolution2D 层融合。
这个想法来自这篇论文,这里是网络:
融合层尝试将 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__
andbuild
方法是正确的,但我不知道如何在层中将y_global
(256 个维度)与y-mid
(256x28x28 维度)连接起来,call
以便输出与上述等式相同。
我怎样才能在方法中实现这个方程call
?
非常感谢...
更新:成功整合这两层数据的任何其他方式对我来说也是可以接受的......它不一定是论文中提到的方式,但它至少需要返回一个可接受的输出......