如何解释和转换Keras分类器预测的值

如何解释和转换Keras分类器预测的值

本文介绍了如何解释和转换Keras分类器预测的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在训练我的Keras模型,以预测是否可以通过提供的数据参数进行拍摄,并以0表示否"和1表示是"的方式表示.但是,当我尝试对其进行预测时,我得到的值是浮动的.

I'm training my Keras model to predict whether, with the provided data parameter, it will make a shot or not and it will represent in such a way that 0 means no and 1 means yes. However, when I try to predict it I got values that are float.

我尝试使用与训练数据完全相同的数据来获得1,但它不起作用.

I've tried using the data that is exactly the same as train data to get 1 but it does not work.

我使用下面的数据尝试了一次热编码.

I used the data below to tried the one-hot encoding.

https://github.com /eijaz1/Deep-Learning-in-Keras-Tutorial/blob/master/keras_tutorial.ipynb

import pandas as pd
from keras.utils import to_categorical
from keras.models import load_model
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
#read in training data
train_df_2 = pd.read_csv('diabetes_data.csv')

#view data structure
train_df_2.head()

#create a dataframe with all training data except the target column
train_X_2 = train_df_2.drop(columns=['diabetes'])

#check that the target variable has been removed
train_X_2.head()

#one-hot encode target column
train_y_2 = to_categorical(train_df_2.diabetes)

#vcheck that target column has been converted
train_y_2[0:5]

#create model
model_2 = Sequential()

#get number of columns in training data
n_cols_2 = train_X_2.shape[1]

#add layers to model
model_2.add(Dense(250, activation='relu', input_shape=(n_cols_2,)))
model_2.add(Dense(250, activation='relu'))
model_2.add(Dense(250, activation='relu'))
model_2.add(Dense(2, activation='softmax'))

#compile model using accuracy to measure model performance
model_2.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
early_stopping_monitor = EarlyStopping(patience=3)
model_2.fit(train_X_2, train_y_2, epochs=30, validation_split=0.2, callbacks=[early_stopping_monitor])


train_dft = pd.read_csv('diabetes_data - Copy.csv')
train_dft.head()

test_y_predictions = model_2.predict(train_dft)
print(test_y_predictions)

我想得到

[[0,1]
[1,0]]

但是,我得到了

[[0.8544417  0.14555828]
 [0.9312985  0.06870154]]

此外,有人可以向我解释这个值0.8544417是什么意思吗?

Additionally, can anyone explain to me what does this value 0.8544417 mean?

推荐答案

实际上,您可以将顶部带有softmax分类器的模型的输出解释为类的置信度得分或概率(因为softmax函数将值归一化这样它们将为正数且总和为1).因此,当您为模型提供真实标签[1, 0]时,这意味着此样本属于类别1,概率为1,而属于类别2,概率为零.因此,在训练过程中,优化过程会尝试尽可能接近该标签,但是它永远不会完全达到[1,0](实际上由于softmax,它可能会接近[0.999999, 0.000001],但从不[1,0]).

Actually, you may interpret the output of a model with a softmax classifier at the top as the confidence scores or probabilities of classes (because the softmax function normalizes the values such that they would be positive and have a sum of 1). So, when you provide the model with a true label of [1, 0] this means that this sample belongs to class 1 with probability of 1, and it belongs to class 2 with probability of zero. Therefore, during training the optimization process tries to get as close as possible to that label, but it would never exactly reach [1,0] (actually due to softmax it might get as close as [0.999999, 0.000001], but never [1, 0]).

但这不是问题,因为我们有兴趣尽可能地接近并且知道具有最高概率的类别,并将其视为模型的预测.而且,您可以通过最大可能地找到类的索引来轻松地做到这一点:

But that is not a problem, because we are interested to get just close enough and know the class with the highest probability and consider that as the prediction of the model. And you can easily do that by finding the index of the class with maximum probability:

import numpy as np

preds = model.predict(some_data)
class_preds = np.argmax(preds, axis=-1) # e.g. for [max,min] it gives 0, for [min,max] it gives 1

此外,如果您出于任何原因有兴趣将预测值转换为[0,1]或[1,0],则可以将值取整:

Further, if you are interested to convert predictions to either [0,1] or [1,0] for any reason, you can just round the values:

import numpy as np

preds = model.predict(some_data)
round_preds = np.around(preds)   # this would convert [0.87, 0.13] to [1., 0.]

注意:四舍五入仅适用于两个类别,而当您拥有两个以上类别时,则不能四舍五入(例如,[0.3,0.4,0.3]会在四舍五入后变为[0,0,0])

Note: rounding only works properly with two classes, and not when you have more than two classes (e.g. [0.3, 0.4, 0.3] would become [0, 0, 0] after rounding).

注释2:由于您是使用Keras的顺序API创建模型的,因此作为上述argmax方法的替代方法,您可以直接使用model.predict_classes(some_data),这将为您提供完全相同的输出

Note 2: Since you are creating the model using Sequential API of Keras, then as an alternative to argmax approach described above you can directly use model.predict_classes(some_data) which gives you the exact same output.

这篇关于如何解释和转换Keras分类器预测的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 18:45