【问题标题】:How to interpret and transform the values predicted by Keras classifier?如何解释和转换 Keras 分类器预测的值?
【发布时间】:2020-04-29 05:54:44
【问题描述】:

我正在训练我的 Keras 模型,以使用提供的数据参数来预测它是否会成功,并且它将以 0 表示否,1 表示是的方式表示。但是,当我尝试预测它时,我得到的值是浮动的。

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

我使用下面的数据尝试了 one-hot 编码。

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 是什么意思?

【问题讨论】:

    标签: python tensorflow machine-learning keras neural-network


    【解决方案1】:

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

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

    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],您可以将值四舍五入:

    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]) .

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

    【讨论】:

      猜你喜欢
      • 2016-05-06
      • 1970-01-01
      • 2021-12-18
      • 2020-08-04
      • 2020-03-03
      • 2020-07-06
      • 2020-08-12
      • 2019-06-15
      • 2018-02-17
      相关资源
      最近更新 更多