gpt4 book ai didi

python - 如何在 Keras 中实现具有多个输入的自定义层

转载 作者:太空狗 更新时间:2023-10-29 23:57:07 25 4
gpt4 key购买 nike

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

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<​​/p>

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

这是我的模型摘要:

__________________________________________________________________________________________________
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) 行。

关于python - 如何在 Keras 中实现具有多个输入的自定义层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46771516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com