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:
- Python 3.7 Error: AttributeError: ‘str‘ object has no attribute ‘decode‘ [How to Solve]
- [Modified] AttributeError: ‘socket‘ object has no attribute ‘ioctl‘ python linux
- How to Solve Python AttributeError: ‘module’ object has no attribute ‘xxx’
- How to Solve Python AttributeError: ‘dict’ object has no attribute ‘item’
- [Solved] python Error: AttributeError: ‘NoneType‘ object has no attribute ‘split‘
- [2021-10-05] Python Error: AttributeError: ‘NoneType‘ object has no attribute ‘append‘
- Python Openyxl Error: AttributeError: ‘int‘ object has no attribute ‘upper‘ [How to Solve]
- [Solved] Python Selenium Error: AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_xpath‘
- Python writes DICOM file (attributeerror: ‘filemetadataset’ object has no attribute ‘transfersyntax uid’ solution)
- [Solved] AttributeError: ‘_IncompatibleKeys‘ object has no attribute ‘parameters‘
- [Solved] AttributeError: ‘_IncompatibleKeys’ object has no attribute
- [Solved] AttributeError: ‘DataParallel‘ object has no attribute ‘save‘
- [Solved] yolov5-6.0 ERROR: AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
- Python AttributeError: module ‘tensorflow‘ has no attribute ‘InteractiveSession‘
- [Solved] AttributeError: ‘Manager‘ object has no attribute ‘get_by_natural_key‘
- AttributeError: DatetimeProperties object has no attribute
- [Solved] AttributeError: DataFrame object has no attribute’xxx’
- [Solved] AttributeError: ‘NoneType‘ object has no attribute ‘append‘
- [Solved] AttributeError: ‘DataFrame‘ object has no attribute ‘tolist‘
- [Solved] AttributeError: ‘str‘ object has no attribute ‘decode‘