Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关于tf.nn.softmax\u cross\u entropy\u与\u logits\u v2_Python_Tensorflow_Machine Learning_Softmax_Cross Entropy - Fatal编程技术网

Python 关于tf.nn.softmax\u cross\u entropy\u与\u logits\u v2

Python 关于tf.nn.softmax\u cross\u entropy\u与\u logits\u v2,python,tensorflow,machine-learning,softmax,cross-entropy,Python,Tensorflow,Machine Learning,Softmax,Cross Entropy,我注意到tf.nn.softmax\u cross\u entropy\u with\u logits\u v2(标签、登录)主要执行3个操作: 将softmax应用于logits(y\u hat)以使其正常化:y\u hat\u softmax=softmax(y\u hat) 计算交叉熵损失:y\u cross=y\u true*tf.log(y\u hat\u softmax) 实例不同类上的求和:-tf.reduce\u Sum(y\u交叉,reduce\u索引=[1]) 从中借用的代码

我注意到
tf.nn.softmax\u cross\u entropy\u with\u logits\u v2(标签、登录)
主要执行3个操作:

  • 将softmax应用于logits(y\u hat)以使其正常化:
    y\u hat\u softmax=softmax(y\u hat)

  • 计算交叉熵损失:
    y\u cross=y\u true*tf.log(y\u hat\u softmax)

  • 实例不同类上的求和:
    -tf.reduce\u Sum(y\u交叉,reduce\u索引=[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: \n{0}\n'.format(result.eval()))
        print('result_tf: \n{0}'.format(result_tf.eval()))
    
    输出:

    y_hat_softmax:
    [[0.227863   0.61939586 0.15274114]
    [0.49674623 0.20196195 0.30129182]]
    
    y_true: 
    [[0. 1. 0.]
    [0. 0. 1.]]
    
    y_cross: 
    [[-0.         -0.4790107  -0.        ]
    [-0.         -0.         -1.19967598]]
    
    result: 
    [0.4790107  1.19967598]
    
    result_tf: 
    [0.4790107  1.19967598]
    
    然而,一个热标签包括0或1,因此这种二元情况的交叉熵公式如下所示,如和所示:

    我在下一个单元格中为这个公式编写代码,结果与上面不同。我的问题是哪一个更好或正确?根据这个公式,tensorflow也有计算交叉熵的函数吗

    y_true = np.array([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
    y_hat_softmax_from_tf = np.array([[0.227863, 0.61939586, 0.15274114], 
                                  [0.49674623, 0.20196195, 0.30129182]])
    comb = np.dstack((y_true, y_hat_softmax_from_tf))
    #print(comb)
    
    print('y_hat_softmax_from_tf: \n{0}\n'.format(y_hat_softmax_from_tf))
    print('y_true: \n{0}\n'.format(y_true))
    
    def cross_entropy_fn(sample):
        output = []
        for label in sample:
            if label[0]:
                y_cross_1 = label[0] * np.log(label[1])
            else:
                y_cross_1 = (1 - label[0]) * np.log(1 - label[1])
            output.append(y_cross_1)
        return output
    
    y_cross_1 = np.array([cross_entropy_fn(sample) for sample in comb])
    print('y_cross_1: \n{0}\n'.format(y_cross_1))
    
    result_1 = - np.sum(y_cross_1, 1)
    print('result_1: \n{0}'.format(result_1))
    
    输出

    y_hat_softmax_from_tf: 
    [[0.227863   0.61939586 0.15274114]
    [0.49674623 0.20196195 0.30129182]]
    
    y_true: 
    [[0. 1. 0.]
    [0. 0. 1.]]
    
    y_cross_1: 
    [[-0.25859328 -0.4790107  -0.16574901]
    [-0.68666072 -0.225599   -1.19967598]]
    
    result_1: 
    [0.90335299 2.11193571]
    

    你的公式是正确的,但它只适用于二元分类。tensorflow中的演示代码分为3类。这就像拿苹果和桔子做比较。您也提到了它:

    此公式通常用于一个输出预测两个类的网络(通常为1的正类成员资格和0的负类成员资格)。在这种情况下,我可能只有一个值-你可能会失去i的总和

    这两个公式(二元交叉熵与多项式交叉熵)之间的差异以及每一个公式适用时的差异在中有很好的描述


    第二个问题的答案是肯定的,有这样一个函数叫做。请参阅上述问题。

    在官方文档中要小心:警告:此op需要无标度的logits,因为它在内部对logits执行softmax以提高效率。不要使用softmax的输出调用此op,因为它将产生不正确的结果。似乎y不应该被传递给softmax函数。此V2与上一个V2有什么区别?我可以用新的V2替换代码吗?当我运行tf.nn.softmax\u cross\u entropy\u的tf 1.9代码时,我收到了一条不推荐使用的消息(…)