keras中的点积导致AttributeError:“ NoneType”对象没有属性“ _inbound_nodes”错误

时间:2018-12-29 15:38:34

标签: keras matrix-multiplication

我正在尝试在keras图中做matmul,并在编译模型时得到AttributeError: 'NoneType' object has no attribute '_inbound_nodes' error

from keras import backend as K
from keras.layers import Input, Dense, Reshape

mainInput = Input(shape=(10*10,))
x = Dense(10*10, activation='relu')(x)
x1 =  Reshape((10, 10))(x)
x2 = Dense( 2 * 10, activation='relu')(x)
x2 =  Reshape((2, 10))(x2)
added = K.dot(x2, x1)
out = Dense(2 * 10, activation='linear')(added)
optimizer = optimizers.adam(lr=0.001)
model = Model(inputs=[mainInput], outputs=[out])
model.compile(loss='mse', metrics = ['mae'], optimizer=optimizer)

此错误的原因是什么?

1 个答案:

答案 0 :(得分:1)

所有操作必须在keras层内部:

added = Lambda(lambda x: K.dot(x[1],x[0]))([x1,x2])

我认为您的Reshape将会失败,因为您正试图将(None, 10, 20)重塑为(None, 2, 10)