[英]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. 我正在训练一个模型,其输出是一个大小为19的softmax层。当我尝试使用model.predict(x)
,对于每个输入,我得到的是19个类中的概率分布。 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? 我尝试了model.predict_classes
,得到了一个大小为x
的numpy数组,每个输出等于0.我怎样才能得到一个热输出的输出?
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. 因此, predcit_classes
的文档在predcit_classes
是误导性的,因为如果仔细检查它的实现 ,你会发现它只适用于二进制分类。 In order to solve your problem you may use the numpy
library (basically - a function argmax
) in a following way: 为了解决您的问题,您可以通过以下方式使用numpy
库(基本上 - 函数argmax
):
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: 为了获得单热矢量 - 您可以通过以下方式使用keras
内置函数to_categorical
:
import numpy as np
from keras.utils.np_utils import to_categorical
classes_one_hot = to_categorical(np.argmax(model.predict(x), axis = 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.