- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下表
来自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 中找出一个整数的 log10,但我得到了一个错误,比如数学域错误 我的代码是这样的w=math.log10(q*q1)/math.log10(2) 其中 q1,q2 是整数 是的
舍入小数 在 NumPy 中,主要有五种方法来舍入小数: 截断 去除小数部分,并返回最接近零的浮点数。使用 trunc() 和 fix() 函数。 示例: import numpy as n
我有一个数值范围为 0 到 100 的 slider 。 我想将它们映射到 100 到 10,000,000 的范围内。 我在网上看到过一些函数,但它们都是用 C++ 编写的。我需要它在 Javasc
我想请用户输入一个整数(N),然后显示他/她输入的整数的 10 对数。我已经成功计算了 10 对数,但不知道如何像下面这样显示它: Write in an Integer: 455666 455666
我将 x 轴设置为对数刻度。最大值为10000,最小值为1。 GraphPane mypane = zedgraphcontrol.GraphPane; mypane.XAxis.Type = Axi
我正在尝试编写一个快速算法来计算 log gamma function 。目前我的实现看起来很幼稚,只是迭代了 1000 万次来计算 gamma 函数的对数(我还使用 numba 来优化代码)。 im
这个问题在这里已经有了答案: How to show minor tick labels on log-scale with Matplotlib (2 个答案) 关闭 7 年前。 将行 plt.y
抱歉标题不好 ;) 我正在尝试重新创建我在其他一些工作中遇到的 matlab 图,但我不太了解他们使用的比例。 y轴增量如下(从上往下[+ve y]): 0.9999,0.999,0.99,0.9,0
由于 1000 的以 10 为底的对数是 3,您可能期望 Math::log(1000, 10) 返回 3。相反,它返回 2.9999999999999996。 这是因为 Ruby 中的 float
我对对数 X 轴有疑问。阈值大于 0,x 的最小值为 1,并且所有 X 值都大于 0。并且仍然给我相同的错误 Can't plot zero or subzero values on a logari
我需要在我的应用程序中实现折线图,我想使用 MPAndroidChart。问题是 y 轴上的值将介于 1 和 1x10^-12 之间。这就是为什么我需要在该轴上的对数 View 。 有没有办法用那个库
我正在尝试按照 Logarithmic slider 中的示例进行操作. 这是我使用的代码: Timeline._MIN_PER_MINUTE = 1; Timeline._MIN_PER_HOUR
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我尝试为对数 y 轴绘制条形图。不幸的是,如果我将 y 轴设置为对数,则不再有条形图。我该怎么做才能实现这一目标?是否可以在 bar-function 中设置引用点(默认似乎为零)? 我的代码是: i
所以我一直在努力掌握 Big Oh 的计算方法。我觉得我已经掌握了基础知识,但对看似非常简单的计算感到困惑。所以如果下面的计算有很大的 O(n log n)(我真的希望我至少做对了)改变循环的顺序对复
我知道二维绘图的 semilogx 和 semilogy。 SURF 和 MESH 有什么等价物吗? 最佳答案 如上述链接所述,要将所有三个轴设置为对数刻度,请使用 set(gca, 'XScale'
这看起来很简单,但我在用 Ruby 计算 log (Base 5) 时遇到了问题。 显然标准的 base-10 日志工作正常: >> value = Math::log(234504) => 12.3
这段代码是用 C 语言根据 pollard 的对数 rho 算法(来自 wiki)编写的。在此代码中,如果我输入 alpha=2、beta=5、N=1019,则必须返回 a=681、b=378、A=3
有了this question之后通过指向 an external site 的链接回答,我意识到我解决了一个问题,只是为了得到另一个问题:在对数刻度上,MESH 和 SURF 函数的 C=Z 参数不
我正在尝试解决 the SPOJ problem PGCD , 它询问最大公约数表中出现了多少个素数。 我想到的第一个想法是先通过筛分生成素数。 然后,对于每个素数 p,查看有多少对(a,b),其中
我是一名优秀的程序员,十分优秀!