学习笔记427—Python Keras 报错AttributeError: 'Sequential' object has no attribute 'predict_classes'解决方法
Python Keras 报错AttributeError: 'Sequential' object has no attribute 'predict_classes'解决方法
本文文要介绍Python中,使用 Keras 执行yhat_classes = model.predict_classes(X_test)代码报错:AttributeError: 'Sequential' object has no attribute 'predict_classes'解决方法。
示例代码:
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)
问题原因:
在TensorFlow 2.6版本中删除了这个predict_classes
函数。
可以使用如下代码:
predict_x=model.predict(X_test) classes_x=np.argmax(predict_x,axis=1)
参考链接:https://www.cjavapy.com/article/2239/