gpt4 book ai didi

python - NoneType' 对象没有属性 '_inbound_nodes'

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:27 26 4
gpt4 key购买 nike

您好,我正在尝试构建专家混合神经网络。我在这里找到了一个代码:http://blog.sina.com.cn/s/blog_dc3c53e90102x9xu.html .我的目标是门和专家来自不同的数据,但具有相同的维度。

def sliced(x,expert_num):
return x[:,:,:expert_num]

def reduce(x, axis):
return K.sum(x, axis=axis, keepdims=True)

def gatExpertLayer(inputGate, inputExpert, expert_num, nb_class):
#expert_num=30
#nb_class=10
input_vector1 = Input(shape=(inputGate.shape[1:]))
input_vector2 = Input(shape=(inputExpert.shape[1:]))

#The gate
gate = Dense(expert_num*nb_class, activation='softmax')(input_vector1)
gate = Reshape((1,nb_class, expert_num))(gate)
gate = Lambda(sliced, output_shape=(nb_class, expert_num), arguments={'expert_num':expert_num})(gate)

#The expert
expert = Dense(nb_class*expert_num, activation='sigmoid')(input_vector2)
expert = Reshape((nb_class, expert_num))(expert)

#The output
output = tf.multiply(gate, expert)
#output = keras.layers.merge([gate, expert], mode='mul')
output = Lambda(reduce, output_shape=(nb_class,), arguments={'axis': 2})(output)

model = Model(input=[input_vector1, input_vector2], output=output)

model.compile(loss='mean_squared_error', metrics=['mse'], optimizer='adam')

return model

但是,我得到“‘NoneType’对象没有属性‘_inbound_nodes’”。我在这里检查了其他类似的问题:AttributeError: 'NoneType' object has no attribute '_inbound_nodes' while trying to add multiple keras Dense layers但是问题是通过keras的Lambda函数转换成层来解决的。

最佳答案

好吧,您需要将 tf.multiply() 放在 Lambda 层中以获得 Keras 张量作为输出(而不是张量):

output = Lambda(lambda x: tf.multiply(x[0], x[1]))([gate, expert])

关于python - NoneType' 对象没有属性 '_inbound_nodes',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52636328/

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