python - 如何计算 MNIST 数据集上的负对数似然?

标签 python tensorflow machine-learning recurrent-neural-network mnist

下表

NLL evaluations on MNIST test set.

来自Professor Forcing: A New Algorithm for Training Recurrent Networks纸。但我找不到他们计算 NLL 的代码。我想问这是否只是二元交叉熵。我可以使用Tensorflow计算它tf.nn.sigmoid_cross_entropy_with_logits功能?

在Professor Forcing论文中,没有给出教师强制的评估结果。我训练了一个简单的 LSTM,并获得了 80.394 的 NLL。我的最后一个问题是获得 ~80 或 ~70 的可能性有多大?

更具体地说,我正在尝试逐像素生成 MNIST 图像。我的模型对每个像素进行二进制预测,可以取值 0 和 1。logits 和标签的维度都是 [batch_size, 28*28, 1],其中 28 是高度, MNIST 图像的宽度。

最佳答案

事实上,负对数似然是对数损失,或者(二元)分类问题的(二元)交叉熵,但由于 MNIST 是一个多类问题,所以这里我们讨论的是分类问题 em> 交叉熵。它通常是首选,因为由于对数似然本身是负数,因此它的负数将是正数;来自 log_loss 的 scikit-learn 文档(添加了重点):

Log loss, aka logistic loss or cross-entropy loss.

This is the loss function used in (multinomial) logistic regression and extensions of it such as neural networks, defined as the negative log-likelihood of the true labels given a probabilistic classifier’s predictions. The log loss is only defined for two or more labels. For a single sample with true label yt in {0,1} and estimated probability yp that yt = 1, the log loss is

-log P(yt|yp) = -(yt log(yp) + (1 - yt) log(1 - yp))

不太确定如何使用 Tensorflow 做到这一点;这是使用 Keras 实现此目的的一种方法(为了使代码简短明了,我在 Keras MNIST CNN example 的基础上构建,这里仅运行 2 个时期,因为我们只对获取 y_pred 感兴趣并演示程序):

首先,这是 Keras 为测试集报告的分类交叉熵损失结果:

y_pred = model.predict(x_test)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
# Test loss: 0.05165324027412571
# Test accuracy: 0.9834

现在让我们看看如何“手动”获得此损失结果,如果我们有预测 y_pred 和真实标签 y_test 而不管使用任何特定模型;请注意,当我们的预测和真实标签都是单热编码时,该过程适用,即:

y_pred[0]
# array([2.4637930e-07, 1.0927782e-07, 1.0026793e-06, 7.6613435e-07,
#        4.1209915e-09, 1.4566888e-08, 2.3195759e-10, 9.9999702e-01,
#        4.9344425e-08, 8.6051602e-07], dtype=float32)
y_test[0]
# array([0., 0., 0., 0., 0., 0., 0., 1., 0., 0.])

程序如下:

from keras import backend as K
import numpy as np

y_test = y_test.astype('float32') # necessary here, since y_pred comes in this type - check in your case with y_test.dtype and y_pred.dtype
y_test = K.constant(y_test)
y_pred = K.constant(y_pred)

g = K.categorical_crossentropy(target=y_test, output=y_pred)  # tensor
ce = K.eval(g)  # 'ce' for cross-entropy
ce.shape
# (10000,) # i.e. one loss quantity per sample

# sum up and divide with the no. of samples:
log_loss = np.sum(ce)/ce.shape[0]
log_loss
# 0.05165323486328125

正如您可以直观地验证的那样,出于所有实际目的,这等于上面 Keras 本身报告的损失 (score[0]);确实:

np.isclose(log_loss, score[0])
# True

尽管不完全相等,可能是由于两种方法的数值精度差异所致:

log_loss == score[0]
# False

希望您现在应该能够使用上述过程来获取单热编码(如 MNIST、那是)...

关于python - 如何计算 MNIST 数据集上的负对数似然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52497625/

相关文章:

python - pytorch中DataLoader的洗牌顺序

python - 如何减少通过 REST api 发送的数据的延迟

machine-learning - 基于无监督方面的情感分析中的问题

tensorflow - 如何从序列模型中的给定数据集创建训练-开发-测试集

windows - Windows 10 上的 Tensorflow 安装,错误 'Not a supported wheel on this platform'

machine-learning - 如何仅评估某些类别的 Keras 模型精度

python - 在 SpaCy 中使用 REGEX 和 ORTH 作为短语匹配的一部分

python - Spyder 未启动

python - 自定义 Keras 损失的奇怪 Nan 损失

docker - 在 Google Cloud 上的 Docker 上启动 TensorFlow