![](/img/trans.png)
[英]Keras AttributeError: 'Sequential' object has no attribute 'predict_classes'
[英]Even when using Sequential model, I am getting “AttributeError: 'Model' object has no attribute 'predict_classes' ”
正如在這個問題中提到的,我們需要順序 model 來使用.predict_classes
我正在使用這個 model 但仍然得到
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)
該錯誤是由於您沒有調用 function 以獲得其 output 的事實引起的。 簡單地做
predicted = Build_Model_RNN_Text(<<args>>).predict_classes(X_test_Glove)
您需要將<<args>>
替換為 function 所需的 arguments。 似乎您原本打算將Build_Model_RNN_Text
改為class
?
無論哪種方式,您究竟希望它如何工作,因為您沒有提供所需的 arguments word_index
、 embeddings_index
和nclasses
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.