简体   繁体   中英

Keras model.predict for multinomial logistic regression

I'm training a model whose output is a softmax layer of size 19. When I try model.predict(x) , for each input, I get what appears to be a probability distribution across the 19 classes. I tried model.predict_classes , and got a numpy array of the size of x , with each output equal to 0. How can I get one hot vectors for the output?

So a documentation of predcit_classes is somehow misleading because if you check carefully its implementation , you'll find out that it works only for binary classification. In order to solve your problem you may use the numpy library (basically - a function argmax ) in a following way:

import numpy as np
classes = np.argmax(model.predict(x), axis = 1)

.. in order to get an array with a class number for each example. In order to get a one-hot vector - you might use a keras built-in function to_categorical in a following manner:

import numpy as np
from keras.utils.np_utils import to_categorical
classes_one_hot = to_categorical(np.argmax(model.predict(x), axis = 1))

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