I am trying to train an NLP Neural Machine Translation model and in that code I'm using sequential model of Keras. I want to predict the output in the form of classes but as i am using Tensorflow 2.7.0 and the predict_classes() has now been depreciated, how should i go around it? Here's the code snippet -:
model = load_model('model.h1.24_jan_19')
preds = model.predict_classes(testX.reshape((testX.shape[0],testX.shape[1])))
And here's the error that i'm getting -:
AttributeError Traceback (most recent call last)
in () 1 model = load_model('model.h1.24_jan_19') ----> 2 preds = model.predict_classes(testX.reshape((testX.shape[0],testX.shape[1])))
AttributeError: 'Sequential' object has no attribute 'predict_classes'
CodePudding user response:
I dont think there is a direct replacement, you need to use the output of model.predict()
and manually map them onto a list of class labels, then pick the one with highest confidence.
A simple example:
classes = ['a', 'b']
results = model.predict(testX.reshape((testX.shape[0],testX.shape[1])))
# the results here will be a n array of confidence level, if the last layer of your model uses the softmax activation function.
class_predicted = classes[np.argmax(predictions)] # this line gets the index of the highest confidence class and select from the classes list.
Beware that the order of labels in the classes list must be same as your input label, so you should double check on that. If you are using tensorflow APIs like tf.data.dataset
, then you can use the dataset.class_names
attribute to access the class list.