NoneType' object has no attribute '_inboun

2019-07-22 07:45发布

Hi I am trying to build a Mixture-of-experts neural network. I found a code here: http://blog.sina.com.cn/s/blog_dc3c53e90102x9xu.html. My goal is that the gate and expert come from different data, but with same dimensions.

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

However, I got "'NoneType' object has no attribute '_inbound_nodes'". I checked other similar questions here: AttributeError: 'NoneType' object has no attribute '_inbound_nodes' while trying to add multiple keras Dense layers but the problem is fixed with the Lambda function of keras to convert into a layer.

1条回答
仙女界的扛把子
2楼-- · 2019-07-22 08:14

Well, you need to put tf.multiply() inside a Lambda layer to get a Keras Tensor as output (and not a Tensor):

output = Lambda(lambda x: tf.multiply(x[0], x[1]))([gate, expert])
查看更多
登录 后发表回答