V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
jingous
V2EX  ›  机器学习

交叉熵损失函数之前是否需要加 softmax 函数?

  •  
  •   jingous · 2019-04-09 18:54:49 +08:00 · 3570 次点击
    这是一个创建于 1815 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在我的理解里面,交叉熵( cross-entry)损失函数中的 log 能够抵消 softmax 函数中的 exp.使得梯度能够更好的反向传播。
    在看 niftyNet 代码的时候,发现如下内容:
    在__init__中:

        if data_loss_function_name.startswith('cross_entropy') \
                or 'xent' in data_loss_function_name:
            tf.logging.info(
                'Cross entropy loss function calls '
                'tf.nn.sparse_softmax_cross_entropy_with_logits '
                'which always performs a softmax internally.')
            self._softmax = False
    

    在 layer_op 中

                    if self._softmax:
                        pred_b = tf.cast(pred_b, dtype=tf.float32)
                        pred_b = tf.nn.softmax(pred_b)
    

    为什么损失函数是 cross_entropy 的时候,不需要进行 softmax?

    4 条回复    2019-04-10 10:18:10 +08:00
    dinghow
        1
    dinghow  
       2019-04-09 19:33:06 +08:00
    ![]( https://dinghow.site/2018/11/28/Udacity-DL-note1/dl-2.png)
    ![]( https://dinghow.site/2018/11/28/Udacity-DL-note1/dl-1.png)
    应该是要加的,利用 cross_entropy 来计算 softmax 层输出与 label 的 loss
    dinghow
        2
    dinghow  
       2019-04-09 19:38:41 +08:00
    @dinghow 囧,至于楼主的疑问,我也母鸡,期待有大神来解答
    tinywhale
        3
    tinywhale  
       2019-04-10 07:12:28 +08:00
    我记得 tf.nn.sparse_softmax_cross_entropy_with_logits 已经包括了 softmax
    jingous
        4
    jingous  
    OP
       2019-04-10 10:18:10 +08:00
    @tinywhale 似乎是这样的,谢谢。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1035 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:26 · PVG 06:26 · LAX 15:26 · JFK 18:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.