AttributeError:'Tensor'对象没有属性'compile'

时间:2018-12-15 20:14:10

标签: python machine-learning keras lstm

当我尝试运行此命令时:

p0 = Sequential()
p0.add(Embedding(vocabulary_size1, 50, weights=[embedding_matrix_passage], 
input_length=50, trainable=False))
p0.add(LSTM(64))
p0.add(Dense(256,name='FC1'))
p0.add(Activation('relu'))
p0.add(Dropout(0.5))
p0.add(Dense(50,name='out_layer'))
p0.add(Activation('sigmoid'))

q0 = Sequential()
q0.add(Embedding(vocabulary_size2,50,weights=embedding_matrix_query], 
input_length=50, trainable=False))
q0.add(LSTM(64))
q0.add(Dense(256,name='FC1'))
q0.add(Activation('relu'))
q0.add(Dropout(0.5))
q0.add(Dense(50,name='out_layer'))
q0.add(Activation('sigmoid'))

model = concatenate([p0.output, q0.output])
model = Dense(10)(model)
model = Activation('softmax')(model)
model.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics= 
['accuracy'])

这给了我这个错误:

AttributeError             
--->  model.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics=['accuracy'])

1 个答案:

答案 0 :(得分:1)

如评论中所述,您需要使用Keras Functional API创建具有分支,多个输入/输出的模型。但是,无需为所有代码执行此操作,仅针对最后一部分:

concat = concatenate([p0.output, q0.output])
x = Dense(10)(concat)
out = Activation('softmax')(x)

model = Model([p0.input, q0.input], out)

model.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics=['accuracy'])