python - 为什么softmax交叉熵损失在张量流中永远不会给出零值?
问题描述
我在张量流中做一个神经网络,我使用 softmax_cross_entropy 来计算损失,我正在做测试并注意它永远不会给出零值,即使我比较相同的值,这是我的代码
labels=[1,0,1,1]
with tf.Session() as sess:
onehot_labels=tf.one_hot(indices=labels,depth=2)
logits=[[0.,1.],[1.,0.],[0.,1.],[0.,1.]]
print(sess.run(onehot_labels))
loss=tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels,logits=logits)
print(sess.run(loss))
我得到这个
[[0. 1.]
[1. 0.]
[0. 1.]
[0. 1.]]
0.31326166
为什么不是零??
解决方案
马蒂亚斯的帖子是正确的。以下代码给出与您的代码相同的结果
labels=[1,0,1,1]
with tf.Session() as sess:
onehot_labels=tf.one_hot(indices=labels,depth=2)
logits=[[0.,1.],[1.,0.],[0.,1.],[0.,1.]]
print(sess.run(onehot_labels))
probabilities = tf.nn.softmax(logits=logits)
# cross entropy
loss = -tf.reduce_sum(onehot_labels * tf.log(probabilities)) / len(labels)
print(sess.run(loss))
推荐阅读
- python - 无法根据脚本的启动方式和位置导入包
- php - PHP:我可以使用内置的 curl 函数通过 gunzip 管道 curl 输出吗?
- awk - 是否可以漂亮地打印 Awk 的代码?
- python - 命名组中的非捕获组
- c++ - 如果我将 unsigned int 分配给 signed int,是否会有未初始化的位
- ssh - 使用 SSH 私钥的连接问题
- css - Css 选择器:选择属性值不起作用
- java - 从终端使用 acm.program 包运行 java 脚本
- python - 有没有办法在字典中编辑任意数量的列表?
- java - 从静态方法中读取 getServletContext()