如何解决为什么我在编写自己的表达式时得到的结果与使用 tensorflow 的方法不同?
我正在学习逻辑回归,我想在通过梯度下降最小化过程中计算交叉熵损失函数的值是多少,但是当我使用 tensorflow 的 sigmoid_cross_entropy_with_logits 函数时,我得到的结果与我自己得到的结果不同表达。
这是一个例子:
import numpy as np
import tensorflow as tf
pred = np.array([[0.2],[0.3],[0.4]])
test_y = np.array([[0.5],[0.6],[0.7]])
print(tf.nn.sigmoid_cross_entropy_with_logits(logits = pred,labels = test_y))
print(-test_y * tf.math.log(pred) - (1-test_y) * tf.math.log(1-pred))
输出:
tf.Tensor(
[[0.69813887]
[0.67435524]
[0.63301525]],shape=(3,1),dtype=float64)
tf.Tensor(
[[0.91629073]
[0.86505366]
[0.7946512 ]],dtype=float64)
谁能给我解释一下这有什么问题?我查看了有关其功能的 tensorflow 文档,看起来它应该与我的表达式完全相同。
解决方法
在计算交叉熵损失之前,您忘记采用预测的 sigmoid pred
:
-test_y * tf.math.log(tf.math.sigmoid(pred)) - (1-test_y) * tf.math.log(1-tf.math.sigmoid(pred))
tf.Tensor(
[[0.69813887]
[0.67435524]
[0.63301525]],shape=(3,1),dtype=float64)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。