AttributeError: ‘Tensor’ object has no attribute ‘compile’

当我尝试运行以下代码时:

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'])

回答:

正如评论中提到的,你需要使用Keras 函数式 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'])

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注