tensorflow - 为什么 Pytorch 和 Tensorflow 中交叉熵的实现不同?

标签 tensorflow neural-network pytorch precision

我正在阅读 Pytorch 和 Tensorflow 中的交叉熵文档。我知道他们正在修改交叉熵的幼稚实现来解决潜在的数字溢出/下溢。但是,我无法理解这些修改是如何提供帮助的。
implementation of Cross Entropy in Pytorch遵循以下逻辑 -
enter image description here
哪里enter image description here是 softmax 分数和 enter image description here是原始分数。
这似乎不能解决问题,因为 enter image description here也会导致数值溢出。
现在,我们将它与 Tensorflow 的实现进行对比(我得到了 from a discussion in Github 。这可能是完全错误的)-
enter image description here是所有 k 个原始 logit 分数的向量。
enter image description here
虽然这解决了上溢的问题,但它遇到了下溢的问题,因为 enter image description here 可能是这将导致一个更小的 enter image description here
有人可以帮我理解这里发生了什么吗?

最佳答案

为了社区的利益,通过结合评论部分的答案来回答这里。
由于您已经在 中解决了数字溢出的问题。 PyTorch ,这是通过减去最大值来处理的,如下所示( from here )。

scalar_t z = std::exp(input_data[d * dim_stride] - max_input);
谈到 TensorFlow 的交叉熵实现,下溢问题并不是那么重要,因为它在数值上被忽略,而占主导地位的大值。

关于tensorflow - 为什么 Pytorch 和 Tensorflow 中交叉熵的实现不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63657247/

相关文章:

python - LSTM - 一段时间后预测相同的常数值

python - Keras:改变学习率

python - 神经网络模型

machine-learning - 什么时候应该运行 wandb.watch 以便权重和偏差正确跟踪参数和梯度?

python - torch 如何在几乎为零的时间内将两个 10000*10000 矩阵相乘?为什么速度变化如此之大,从 349 毫秒下降到 999 微秒?

pytorch - 用不同形状的张量计算余弦距离

python - 属性错误 : module 'keras.utils' has no attribute 'Sequence'

python - 模块 'tensorflow' 没有属性 'log'

python - 我的分类器损失很大,准确率始终为 0

python - Pytorch自定义数据集: ValueError: some of the strides of a given numpy array are negative