MultiClass Keras Classifier 预测输出意义

MultiClass Keras Classifier prediction output meaning

我有一个使用 Scikit-Learn 的 Keras 包装器构建的 Keras 分类器 API。神经网络有10个输出节点,训练数据全部使用one-hot encoding表示。

根据 Tensorflow documentationpredict 函数输出 (n_samples,) 的形状。当我拟合 514541 个样本时,该函数返回一个形状为 (514541, ) 的数组,数组的每个条目的范围为 0 到 9。

由于我有十个不同的输出,每个条目的数值是否与我在训练矩阵中编码的结果完全对应?

即如果我 y_train 的单热编码的索引 5 表示 "orange",预测值 5 是否意味着神经网络预测 "orange"?

这是我的模型示例:

model = Sequential()
model.add(Dropout(0.2, input_shape=(32,) ))

model.add(Dense(21, activation='selu'))
model.add(Dropout(0.5))

model.add(Dense(10, activation='softmax'))

你的问题有些问题。

The neural network has 10 output nodes, and the training data is all represented using one-hot encoding.

由于您的网络有 10 个输出节点,并且您的标签是单热编码的,因此您的模型的输出也应该是 10 维的,并且也是热编码的,即形状 (n_samples, 10)。此外,由于您对最后一层使用 softmax 激活,因此 10 维输出的每个元素都应在 [0, 1] 中,并解释为输出属于各自的概率 (one-hot编码)class.

According to Tensorflow documentation, the predict function outputs a shape of (n_samples,).

令人费解的是为什么你提到 Tensorflow,而你的模型显然是 Keras 模型;你应该参考Keras sequential APIpredict方法。

When I fitted 514541 samples, the function returned an array with shape (514541, ), and each entry of the array ranged from 0 to 9.

如果发生类似的事情,那一定是由于您的代码后面的部分没有在此处显示;在任何情况下,想法都是从每个 10 维网络输出中找到具有最高值的 argument(因为它们被解释为概率,直观的是具有最高值的元素值将是最有可能的)。换句话说,在你的代码中某处必须有这样的东西:

pred = model.predict(x_test)
y = np.argmax(pred, axis=1) # numpy must have been imported as np

这将给出一个形状为 (n_samples,) 的数组,每个 y 一个介于 0 和 9 之间的整数,正如您报告的那样。

i.e. if index 5 of my one-hot encoding of y_train represents "orange", does a prediction value of 5 mean that the neural network predicted "orange"?

只要以上成立,是的。