2016-11-25 96 views
4

我想製作一個自定義層,它應該將密集層的輸出與Convolution2D層融合。Keras - 密集層與Convolution2D層的融合

的創意來自this paper來了,這裏的網絡:

The Network

融合層試圖融合Convolution2D張量(256x28x28)與密集張量(256)。這裏是它的公式:

The Fusion Formula

y_global => Dense layer output with shape 256 y_mid => Convolution2D layer output with shape 256x28x28

以下是關於融合的過程中,紙張的描述:

capture3

我最終作出新的自定義層如下所示:

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 dimesnions)與y-mid(256x28x28的尺寸),使得輸出將是相同提到的公式以上。

如何在call方法中實現此方程?

感謝這麼多...

UPDATE:任何其它方式成功整合這些2層的數據對我來說也是可以接受的......這不正是必須的方式在文件中提到但它至少需要返回一個可以接受的輸出...

回答

2

我不得不問這個問題的Keras Github的頁面上,有人幫我如何正確地執行它......這裏的github上issue ...

1

在我看來,實施一種新的圖層是一種複雜的任務。我強烈建議您使用以下層:

,以獲得預期的行爲。