简体   繁体   中英

Even when using Sequential model, I am getting “AttributeError: 'Model' object has no attribute 'predict_classes' ”

As mentioned in this question that we need sequential model to use .predict_classes I am using this model but still getting

AttributeError: 'function' object has no attribute 'predict_classes' 

error. I am using following code

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

Even when using .predict , instead of .predict_classes get I am getting same error

EDIT: I am using following code to call method

predicted = Build_Model_RNN_Text.predict_classes(X_test_Glove)

The error is caused by the fact that you were not calling your function in order to get its output. Simply do

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

Where you need to replace <<args>> with the required arguments for your function. It seems like maybe you had intended for Build_Model_RNN_Text to be a class instead?

Either way, how exactly were you expecting this to work, as you were not providing the required arguments word_index , embeddings_index , and nclasses ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM