This article is about using Keras in Python to execute yhat_classes = model.predict_classes(X_test) code with an error: AttributeError: ‘Sequential’ object has no attribute ‘predict_classes’ solution.
model = Sequential()
model.add(Dense(24, input_dim=13, activation='relu'))
model.add(Dense(18, activation='relu'))
model.add(Dense(6, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
-
history = model.fit(X_train, y_train, batch_size = 256, epochs = 10, verbose = 2, validation_split = 0.2)
-
score, acc = model.evaluate(X_test, y_test,verbose=2, batch_size= 256)
print('test accuracy:', acc)
-
yhat_classes = model.predict_classes(X_test)
Cause of problem:
This predict was removed in tensorflow version 2.6_ Classes function.
Reference documents: https://keras.rstudio.com/reference/predict_proba.html#details
The following codes can be used:
predict_x=model.predict(X_test)
classes_x=np.argmax(predict_x,axis=1)
Or
You can also try tensorflow 2.5 or other versions to solve this problem.
Using tensorflow version 2.5, there may be the following warning messages:
tensorflow\python\keras\engine\sequential.py:455: UserWarning: model.predict_classes() is deprecated and will be removed after 2021-01-01. Please use instead:* np.argmax(model.predict(x), axis=-1), if your model does multi-class classification (e.g. if it uses a softmax last-layer activation).* (model.predict(x) > 0.5).astype("int32"), if your model does binary classification (e.g. if it uses a sigmoid last-layer activation).
Read More:
- To solve the problem that the loss of verification set of resnet50 pre-training model remains unchanged
- [Solved] Error occurred when finalizing GeneratorDataset iterator: Failed precondition: Python interpreter st
- Mxnet.gluon Load Pre Training
- [Solved] AttributeError: module ‘tensorflow._api.v2.train‘ has no attribute ‘AdampOptimizer‘
- Keras import a custom metric model error: unknown metric function: Please ensure this object is passed to`custom_object‘
- [Solved] R Error: Python module tensorflow.keras was not found.
- [Solved] ValueError: Error when checking input: expected conv2d_input to have 4 dimensions
- [Solved] RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the
- Python RuntimeError: Expected 4-dimensional input for 4-dimensional weight [32, 1, 5, 5]
- Python: How to Processe “return multiple values”
- How to Solve keras load_model() Error
- `Model.XXX` is not supported when the `Model` instance was constructed with eager mode enabled
- [Solved] KeyError: ‘Transformer/encoderblock_0/MultiHeadDotProductAttention_1/query\\kernel is
- AttributeError: can‘t set attribute [How to Solve]
- from keras.preprocessing.text import Tokenizer error: AttributeError: module ‘tensorflow.compat.v2‘ has..
- Conda Install Library Error: failed with initial frozen solve. Retrying with flexible solve.
- AttributeError: lower not found (NLP extracted tfidf features)
- Python Kmeans Error: ConvergenceWarning: Number of distinct clusters (99) found smaller than n_clusters (100).
- [Solved] RuntimeError: Numpy is not available (Associated Torch or Tensorflow)
- [Solved] Pyg load dataset Error: attributeerror [pytorch geometry]