标签: cross-entropy

在TensorFlow中单词logits的含义是什么?

在下面的TensorFlow函数中,我们必须在最后一层中提供人工神经元的激活.我明白了 但我不明白为什么它被称为logits?这不是一个数学函数吗?

loss_function = tf.nn.softmax_cross_entropy_with_logits(
     logits = last_layer,
     labels = target_output
)
Run Code Online (Sandbox Code Playgroud)

machine-learning neural-network deep-learning tensorflow cross-entropy

191
推荐指数
9
解决办法
8万
查看次数

sparse_softmax_cross_entropy_with_logits和softmax_cross_entropy_with_logits有什么区别?

我最近遇到了tf.nn.sparse_softmax_cross_entropy_with_logits,我无法弄清楚与tf.nn.softmax_cross_entropy_with_logits相比有什么不同.

是训练矢量唯一的区别y必须是独热编码使用时sparse_softmax_cross_entropy_with_logits

阅读API,我找不到任何其他差异softmax_cross_entropy_with_logits.但是为什么我们需要额外的功能呢?

如果提供单热编码训练数据/向量,不应softmax_cross_entropy_with_logits产生相同的结果sparse_softmax_cross_entropy_with_logits吗?

neural-network tensorflow softmax cross-entropy

99
推荐指数
3
解决办法
5万
查看次数

什么是交叉熵?

我知道有很多解释是什么__CODE__,但我仍然感到困惑.

它只是一种描述损失函数的方法吗?然后,我们可以使用例如梯度下降算法来找到最小值.或者整个过程还包括找到最小算法?

machine-learning cross-entropy

81
推荐指数
2
解决办法
4万
查看次数

如何选择张量流中的交叉熵损失?

分类问题,例如逻辑回归或多项逻辑回归,优化了交叉熵损失.通常,交叉熵层遵循softmax层,其产生概率分布.

在tensorflow中,至少有十几种不同的交叉熵损失函数:

  • tf.losses.softmax_cross_entropy
  • tf.losses.sparse_softmax_cross_entropy
  • tf.losses.sigmoid_cross_entropy
  • tf.contrib.losses.softmax_cross_entropy
  • tf.contrib.losses.sigmoid_cross_entropy
  • tf.nn.softmax_cross_entropy_with_logits
  • tf.nn.sigmoid_cross_entropy_with_logits
  • ...

哪个只适用于二进制分类,哪个适用于多类问题?你何时应该使用sigmoid而不是softmax?如何在sparse功能与别人不同,为什么仅是它softmax

相关(更多数学导向)讨论:交叉熵丛林.

machine-learning neural-network logistic-regression tensorflow cross-entropy

76
推荐指数
3
解决办法
4万
查看次数

Tensorflow sigmoid和cross entropy vs sigmoid_cross_entropy_with_logits

当试图用sigmoid激活函数得到交叉熵时,两者之间存在差异

  1. loss1 = -tf.reduce_sum(p*tf.log(q), 1)
  2. loss2 = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(labels=p, logits=logit_q),1)

但是当使用softmax激活功能时,它们是相同的.

以下是示例代码:

import tensorflow as tf

sess2 = tf.InteractiveSession()
p = tf.placeholder(tf.float32, shape=[None, 5])
logit_q = tf.placeholder(tf.float32, shape=[None, 5])
q = tf.nn.sigmoid(logit_q)
sess.run(tf.global_variables_initializer())

feed_dict = {p: [[0, 0, 0, 1, 0], [1,0,0,0,0]], logit_q: [[0.2, 0.2, 0.2, 0.2, 0.2], [0.3, 0.3, 0.2, 0.1, 0.1]]}
loss1 = -tf.reduce_sum(p*tf.log(q),1).eval(feed_dict)
loss2 = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(labels=p, logits=logit_q),1).eval(feed_dict)

print(p.eval(feed_dict), "\n", q.eval(feed_dict))
print("\n",loss1, "\n", loss2)
Run Code Online (Sandbox Code Playgroud)

classification machine-learning tensorflow cross-entropy sigmoid

45
推荐指数
1
解决办法
3万
查看次数

为什么Cross Entropy方法优于Mean Squared Error?在什么情况下这不起作用?

尽管上述两种方法都提供了更好的分数以更好地接近预测,但仍然优选交叉熵.是在每种情况下还是有一些特殊情况我们更喜欢交叉熵而不是MSE?

machine-learning backpropagation neural-network mean-square-error cross-entropy

34
推荐指数
3
解决办法
3万
查看次数

TensorFlow SparseCategoricalCrossentropy 如何工作?

我试图理解 TensorFlow 中的这个损失函数,但我不明白。它是SparseCategoricalCrossentropy。所有其他损失函数都需要相同形状的输出和标签,而这个特定的损失函数不需要。

源代码:

import tensorflow as tf;

scce = tf.keras.losses.SparseCategoricalCrossentropy();
Loss = scce(
  tf.constant([ 1,    1,    1,    2   ], tf.float32),
  tf.constant([[1,2],[3,4],[5,6],[7,8]], tf.float32)
);
print("Loss:", Loss.numpy());
Run Code Online (Sandbox Code Playgroud)

错误是:

InvalidArgumentError: Received a label value of 2 which is outside the valid range of [0, 2).  
Label values: 1 1 1 2 [Op:SparseSoftmaxCrossEntropyWithLogits]
Run Code Online (Sandbox Code Playgroud)

如何为损失函数 SparseCategoricalCrossentropy 提供适当的参数?

machine-learning deep-learning tensorflow cross-entropy loss-function

16
推荐指数
2
解决办法
1万
查看次数

Pytorch:交叉熵损失中的权重

我试图通过一个实际的例子来理解 CrossEntropyLoss 中的权重是如何工作的。所以我首先运行标准 PyTorch 代码,然后手动运行。但损失并不相同。

from torch import nn
import torch
softmax=nn.Softmax()
sc=torch.tensor([0.4,0.36])
loss = nn.CrossEntropyLoss(weight=sc)
input = torch.tensor([[3.0,4.0],[6.0,9.0]])
target = torch.tensor([1,0])
output = loss(input, target)
print(output)
>>1.7529
Run Code Online (Sandbox Code Playgroud)

现在进行手动计算,首先对输入进行 softmax:

print(softmax(input))
>>
tensor([[0.2689, 0.7311],
        [0.0474, 0.9526]])
Run Code Online (Sandbox Code Playgroud)

然后正确类别概率的负对数并乘以各自的权重:

((-math.log(0.7311)*0.36) - (math.log(0.0474)*0.4))/2
>>
0.6662
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?

python cross-entropy pytorch

16
推荐指数
2
解决办法
3万
查看次数

softmax_cross_entropy_with_logits 的 PyTorch 等价

我想知道 TensorFlow 是否有等效的 PyTorch 损失函数softmax_cross_entropy_with_logits

tensorflow cross-entropy pytorch

14
推荐指数
2
解决办法
1万
查看次数

关于tf.nn.softmax_cross_entropy_with_logits_v2

我注意到tf.nn.softmax_cross_entropy_with_logits_v2(labels, logits)主要执行3个操作:

  1. 将softmax应用于logits(y_hat)以对其进行标准化:y_hat_softmax = softmax(y_hat).

  2. 计算交叉熵损失: y_cross = y_true * tf.log(y_hat_softmax)

  3. 对实例的不同类求和: -tf.reduce_sum(y_cross, reduction_indices=[1])

这里借来的代码完美地证明了这一点.

y_true = tf.convert_to_tensor(np.array([[0.0, 1.0, 0.0],[0.0, 0.0, 1.0]]))
y_hat = tf.convert_to_tensor(np.array([[0.5, 1.5, 0.1],[2.2, 1.3, 1.7]]))

# first step
y_hat_softmax = tf.nn.softmax(y_hat)

# second step
y_cross = y_true * tf.log(y_hat_softmax)

# third step
result = - tf.reduce_sum(y_cross, 1)

# use tf.nn.softmax_cross_entropy_with_logits_v2
result_tf = tf.nn.softmax_cross_entropy_with_logits_v2(labels = y_true, logits = y_hat)

with tf.Session() as sess:
    sess.run(result)
    sess.run(result_tf)
    print('y_hat_softmax:\n{0}\n'.format(y_hat_softmax.eval()))
    print('y_true: \n{0}\n'.format(y_true.eval()))
    print('y_cross: \n{0}\n'.format(y_cross.eval()))
    print('result: …
Run Code Online (Sandbox Code Playgroud)

python machine-learning tensorflow softmax cross-entropy

14
推荐指数
1
解决办法
1万
查看次数