问题:如何在 Keras 中实现多输入的自定义层

我需要像这样实现一个自定义层:

class MaskedDenseLayer(Layer):
    def __init__(self, output_dim, activation, **kwargs):
        self.output_dim = output_dim
        super(MaskedDenseLayer, self).__init__(**kwargs)
        self._activation = activations.get(activation)
    def build(self, input_shape):

        # Create a trainable weight variable for this layer.
        self.kernel = self.add_weight(name='kernel', 
                                  shape=(input_shape[0][1], self.output_dim),
                                  initializer='glorot_uniform',
                                  trainable=True)
        super(MaskedDenseLayer, self).build(input_shape)  

    def call(self, l):
        self.x = l[0]
        self._mask = l[1][1]
        print('kernel:', self.kernel)
        masked = Multiply()([self.kernel, self._mask])
        self._output = K.dot(self.x, masked)
        return self._activation(self._output)


    def compute_output_shape(self, input_shape):
    return (input_shape[0][0], self.output_dim)

这就像Keras API引入实现自定义层的方式一样。我需要像这样给这一层提供两个输入:

def main():
    with np.load('datasets/simple_tree.npz') as dataset:
        inputsize = dataset['inputsize']
        train_length = dataset['train_length']
        train_data = dataset['train_data']
        valid_length = dataset['valid_length']
        valid_data = dataset['valid_data']
        test_length = dataset['test_length']
        test_data = dataset['test_data']
        params = dataset['params']

    num_of_all_masks = 20
    num_of_hlayer = 6
    hlayer_size = 5
    graph_size = 4

    all_masks = generate_all_masks(num_of_all_masks, num_of_hlayer, hlayer_size, graph_size)

    input_layer = Input(shape=(4,))

    mask_1 = Input( shape = (graph_size , hlayer_size) )
    mask_2 = Input( shape = (hlayer_size , hlayer_size) )
    mask_3 = Input( shape = (hlayer_size , hlayer_size) )
    mask_4 = Input( shape = (hlayer_size , hlayer_size) )
    mask_5 = Input( shape = (hlayer_size , hlayer_size) )
    mask_6 = Input( shape = (hlayer_size , hlayer_size) )
    mask_7 = Input( shape = (hlayer_size , graph_size) )


    hlayer1 = MaskedDenseLayer(hlayer_size, 'relu')( [input_layer, mask_1] )
    hlayer2 = MaskedDenseLayer(hlayer_size, 'relu')( [hlayer1, mask_2] )
    hlayer3 = MaskedDenseLayer(hlayer_size, 'relu')( [hlayer2, mask_3] )
    hlayer4 = MaskedDenseLayer(hlayer_size, 'relu')( [hlayer3, mask_4] )
    hlayer5 = MaskedDenseLayer(hlayer_size, 'relu')( [hlayer4, mask_5] )
    hlayer6 = MaskedDenseLayer(hlayer_size, 'relu')( [hlayer5, mask_6] )
    output_layer = MaskedDenseLayer(graph_size, 'sigmoid')( [hlayer6, mask_7] )

    autoencoder = Model(inputs=[input_layer, mask_1, mask_2, mask_3,
                    mask_4, mask_5, mask_6, mask_7], outputs=[output_layer])

    autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
    #reassign_mask = ReassignMask()

    for i in range(0, num_of_all_masks):
        state = np.random.randint(0,20)
        autoencoder.fit(x=[train_data, 
                      np.tile(all_masks[state][0], [300, 1, 1]),
                      np.tile(all_masks[state][1], [300, 1, 1]),
                      np.tile(all_masks[state][2], [300, 1, 1]),
                      np.tile(all_masks[state][3], [300, 1, 1]),
                      np.tile(all_masks[state][4], [300, 1, 1]),
                      np.tile(all_masks[state][5], [300, 1, 1]),
                      np.tile(all_masks[state][6], [300, 1, 1])],
                    y=[train_data],
                    epochs=1,
                    batch_size=20,
                    shuffle=True,
                    #validation_data=(valid_data, valid_data),
                    #callbacks=[reassign_mask],
                    verbose=1)

不幸的是,当我运行此代码时,出现以下错误:

TypeError: can only concatenate tuple (not "int") to tuple

我需要的是一种实现自定义层的方法,其中两个输入包含前一层和一个掩码矩阵。这里的 all_mask 变量是一个列表,其中包含一些为所有层预先生成的掩码。

任何人都可以帮忙吗?我的代码有什么问题。

更新

一些参数:

训练数据:(300, 4)

隐藏层数:6

隐藏层单元:5

掩码:(前一层的大小,当前层的大小)

这是我的模型摘要:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_361 (InputLayer)          (None, 4)            0                                            
__________________________________________________________________________________________________
input_362 (InputLayer)          (None, 4, 5)         0                                            
__________________________________________________________________________________________________
masked_dense_layer_281 (MaskedD (None, 5)            20          input_361[0][0]                  
                                                                 input_362[0][0]                  
__________________________________________________________________________________________________
input_363 (InputLayer)          (None, 5, 5)         0                                            
__________________________________________________________________________________________________
masked_dense_layer_282 (MaskedD (None, 5)            25          masked_dense_layer_281[0][0]     
                                                                 input_363[0][0]                  
__________________________________________________________________________________________________
input_364 (InputLayer)          (None, 5, 5)         0                                            
__________________________________________________________________________________________________
masked_dense_layer_283 (MaskedD (None, 5)            25          masked_dense_layer_282[0][0]     
                                                                 input_364[0][0]                  
__________________________________________________________________________________________________
input_365 (InputLayer)          (None, 5, 5)         0                                            
__________________________________________________________________________________________________
masked_dense_layer_284 (MaskedD (None, 5)            25          masked_dense_layer_283[0][0]     
                                                                 input_365[0][0]                  
__________________________________________________________________________________________________
input_366 (InputLayer)          (None, 5, 5)         0                                            
__________________________________________________________________________________________________
masked_dense_layer_285 (MaskedD (None, 5)            25          masked_dense_layer_284[0][0]     
                                                                 input_366[0][0]                  
__________________________________________________________________________________________________
input_367 (InputLayer)          (None, 5, 5)         0                                            
__________________________________________________________________________________________________
masked_dense_layer_286 (MaskedD (None, 5)            25          masked_dense_layer_285[0][0]     
                                                                 input_367[0][0]                  
__________________________________________________________________________________________________
input_368 (InputLayer)          (None, 5, 4)         0                                            
__________________________________________________________________________________________________
masked_dense_layer_287 (MaskedD (None, 4)            20          masked_dense_layer_286[0][0]     
                                                                 input_368[0][0]                  
==================================================================================================
Total params: 165
Trainable params: 165
Non-trainable params: 0

解答

您的input_shape是一个元组列表。

input_shape:  [(None, 4), (None, 4, 5)]

您不能简单地使用input_shape[0]input_shape[1]。如果要使用实际值,则必须选择哪个元组,然后选择哪个值。例子:

self.kernel = self.add_weight(name='kernel', 

                              #here: 
                              shape=(input_shape[0][1], self.output_dim), 


                              initializer='glorot_uniform',
                              trainable=True)

在方法compute_output_shape中同样需要(遵循您自己的形状规则),您似乎想要连接元组:

return input_shape[0] + (self.output_dim,)

不要忘记取消注释super(MaskedDenseLayer, self).build(input_shape)行。

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐