2017-04-19 80 views
1

我在SO中的问题与this question相同。然而,当我尝试使用probas_to_classes()效用函数,**它已经丢失在当前代码:获取Keras预测函数的类标签订单

"""Numpy-related utilities.""" 
from __future__ import absolute_import 

import numpy as np 


def to_categorical(y, num_classes=None): 
    """Converts a class vector (integers) to binary class matrix. 

    E.g. for use with categorical_crossentropy. 

    # Arguments 
     y: class vector to be converted into a matrix 
      (integers from 0 to num_classes). 
     num_classes: total number of classes. 

    # Returns 
     A binary matrix representation of the input. 
    """ 
    y = np.array(y, dtype='int').ravel() 
    if not num_classes: 
     num_classes = np.max(y) + 1 
    n = y.shape[0] 
    categorical = np.zeros((n, num_classes)) 
    categorical[np.arange(n), y] = 1 
    return categorical 


def normalize(x, axis=-1, order=2): 
    """Normalizes a Numpy array. 

    # Arguments 
     x: Numpy array to normalize. 
     axis: axis along which to normalize. 
     order: Normalization order (e.g. 2 for L2 norm). 

    # Returns 
     A normalized copy of the array. 
    """ 
    l2 = np.atleast_1d(np.linalg.norm(x, order, axis)) 
    l2[l2 == 0] = 1 
    return x/np.expand_dims(l2, axis) 

你有没有为了得到与输出相关的类的任何其他替代品该模型?

回答

0

只需在softmax函数的输出上使用numpy的argmax即可获得最大概率的类。这将返回范围[0,N-1]中的类ID,其中N是类的数量。

pred = model.predict(data here)[0] 
classes = np.argmax(pred) 
+3

谢谢你的回答。但是,我不知道我的类在输出中的顺序。另外,我的计划是通过模型获得前两名的猜测。你能帮我么? – noobalert

0

由马蒂亚斯作为正确地呈现,你应该使用np.argmax功能

但因为你通常批次,输入处理您的预测产量将最有可能是一个矩阵。您可以通过逐个应用argmax来处理它,但我认为使用axis参数会更好。

简而言之:

predictions = model.predict(Input) 
classes = np.argmax(predictions, axis=1) 

在不那么短,可运行的代码,你可以测试:

from __future__ import print_function 
import keras 
import numpy as np 
from keras.datasets import mnist 



(x_train, y_train), (x_test, y_test) = mnist.load_data() 
num_classes = 10 
y_test_cat = keras.utils.to_categorical(y_test, num_classes) 
print(y_test) 
print(np.argmax(y_test_cat,axis=1)) 

error = y_test-np.argmax(y_test_cat,axis=1) 

all_zero = not np.any(error) 

print (all_zero) 

说明:

首先这些keras和numpy的进口和打印功能(因为为什么不)

from __future__ import print_function 
import keras 
import numpy as np 
from keras.datasets import mnist 

然后加载MNIST数据

(x_train, y_train), (x_test, y_test) = mnist.load_data() 

之后,改变你的目标类一个热编码与to_categorical

y_test_cat = keras.utils.to_categorical(y_test, num_classes) 

然后返回到你需要的类:

print(np.argmax(y_test_cat,axis=1)) 

在这个例子中,y_test_cat将会是你的model.predict()函数的输出,所以这就是你如何将它传递给argmax来recov呃从最高概率预测开始。

现在,为了确保我们的类“预测”完全是原始类(因为它们应该是“预测”已经是正确的类),计算误差。和印刷

error = y_test-np.argmax(y_test_cat,axis=1) 

all_zero = not np.any(error) 

print (all_zero) 
+0

何时使用argmax轴作为1和-1?什么是不同的情况? –

0

一个更好的选择是使用sklearn的标签编码器,其被设计用于精确地该目的。

>>> from sklearn.preprocessing import LabelEncoder() 
>>> le = LabelEncoder() 
>>> le.fit_tranform([1, 2, 2, 6]) 
array([0, 0, 1, 2]) 
>>> le.inverse_transform([0, 0, 1, 2]) 
[1, 2, 2, 6] 

本质上讲,这可以用于任何集合(包括那些含有非数字)转换成一个整数的映射映射可在训练过程之后,为了干净地类别标签相关联被颠倒的方式。请注意,在keras模型中,您可以使用predict_class函数直接获取转换后的类标签(此时您可以执行inverse_transform),或者如果您想直接从多类输出向量转到 - 这就是例如,您可以使用Numpy的argmax,正如其他人与编码器一起提到的那样:

true_labels = le.inverse_transform(list(map(lambda x: np.argmax(x))))