【发布时间】:2017-04-10 04:29:25
【问题描述】:
我想制作一个自定义层,它应该将密集层的输出与 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 层数据的方式对我来说也是可以接受的......它不一定是论文中提到的方式,但它至少需要返回一个可接受的输出。 ..
【问题讨论】:
标签: python neural-network theano keras keras-layer