即使使用顺序模型,我也会得到“AttributeError:“model”对象没有属性“predict_classes”

2024-09-23 04:32:07 发布

您现在位置:Python中文网/ 问答频道 /正文

正如在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_classesget时,我也会得到相同的错误

编辑:我正在使用以下代码调用方法

predicted = Build_Model_RNN_Text.predict_classes(X_test_Glove)

Tags: addnodeindexmodellenembeddingpredictmatrix
1条回答
网友
1楼 · 发布于 2024-09-23 04:32:07

错误是由于您没有调用函数以获取其输出。干脆

predicted = Build_Model_RNN_Text(<<args>>).predict_classes(X_test_Glove)

您需要用函数所需的参数替换<<args>>。看起来你是想让Build_Model_RNN_Text变成class

不管是哪种方式,由于您没有提供所需的参数word_indexembeddings_indexnclasses,因此您到底希望它如何工作

相关问题 更多 >