【发布时间】:2021-05-26 10:36:06
【问题描述】:
正如this 问题中提到的,我们需要顺序模型才能使用.predict_classes 我正在使用这个模型,但仍然得到
AttributeError: 'function' object has no attribute 'predict_classes'
错误。我正在使用以下代码
def Build_Model_RNN_Text(word_index, embeddings_index, nclasses, MAX_SEQUENCE_LENGTH=500, EMBEDDING_DIM=50, dropout=0.5):
model = Sequential()
hidden_layer = 3
gru_node = 32
embedding_matrix = np.random.random((len(word_index) + 1, EMBEDDING_DIM))
for word, i in word_index.items():
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
# words not found in embedding index will be all-zeros.
if len(embedding_matrix[i]) != len(embedding_vector):
print("could not broadcast input array from shape", str(len(embedding_matrix[i])),
"into shape", str(len(embedding_vector)), " Please make sure your"
" EMBEDDING_DIM is equal to embedding_vector file ,GloVe,")
exit(1)
embedding_matrix[i] = embedding_vector
model.add(Embedding(len(word_index) + 1,
EMBEDDING_DIM,
weights=[embedding_matrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=True))
print(gru_node)
for i in range(0,hidden_layer):
model.add(GRU(gru_node,return_sequences=True, recurrent_dropout=0.2))
model.add(Dropout(dropout))
model.add(GRU(gru_node, recurrent_dropout=0.2))
model.add(Dropout(dropout))
model.add(Dense(256, activation='relu'))
model.add(Dense(nclasses, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
即使使用.predict,而不是.predict_classes,我也会遇到同样的错误
编辑:我正在使用以下代码调用方法
predicted = Build_Model_RNN_Text.predict_classes(X_test_Glove)
【问题讨论】:
-
我看不到您尝试调用
.predict_classes的位置,但从错误中可以明显看出您正尝试在function对象上这样做。请提供您遇到此错误时正在运行的代码。好像是语法错误。 -
我已经添加了调用方法的代码,请看一下
-
完全符合我的预期。你没有调用你的函数
Build_Model_RNN_Text,这意味着它没有返回任何东西。您需要调用您的函数以获取其输出。 -
你能告诉我我怎么不在这里调用函数吗?我无法理解,因为我以同样的方式调用了其他模型
-
您在 Python 中使用
()调用函数,例如my_function()。
标签: python deep-learning classification recurrent-neural-network