首页 文章

蒙面张量的损失

提问于
浏览
8

假设我有类似的logits

[[4.3, -0.5, -2.7, 0, 0], [0.5, 2.3, 0, 0, 0]]

显然,第一个例子中的最后两个和第二个例子中的最后三个被掩盖,不应该影响损失和梯度计算 . 如何计算此logits和相应标签之间的交叉熵损失?为了理智,这个例子的标签可以是这样的

[[1, 0, 0, 0, 0], [0, 1, 0, 0, 0]]

(一个问题:在logits上的Softmax,然后是log,也适用于被屏蔽的零,并且tf的交叉熵方法也将考虑这些元素的损失 . )

(另外,你可以考虑这样的问题:我在批处理中有不同长度的logits,即我的logits分别为eg.1和eg.2的长度为3和2 . 后面跟着标签 . )

3 回答

  • 2

    我最终做的是以下内容:

    import tensorflow as tf
    import numpy as np
    prelim_scores=tf.constant([[4.3, -0.5, -2.7, 0, 0],[0.5, 2.3, 0, 0, 0]])
    mask=tf.constant([[True,True,True,False,False],[True,True,False,False,False]])
    dummy_scores = tf.ones_like(prelim_scores) * -99999.0  # the base matrix to choose from if dummy relation
    scores = tf.where(mask, prelim_scores,dummy_scores)  # [B, MAX_NUM_ACTIONS]
    a=tf.nn.softmax(scores)
    with tf.Session() as sess:
       print(sess.run(a))
    

    结果是:

    [[9.9094123e-01 8.1551941e-03 9.0362143e-04 0 0]

    [1.4185105e-01 8.5814887e-01 0 0 0]]

    信用额度:here

  • 0

    掩盖交叉熵损失是图书馆所涵盖的常见操作 . 它实际上处理了更一般的权重概念;提供用于屏蔽的二进制权重 .

    mask = tf.equal(logits, 0) # as in the OP
    weights = tf.to_float(mask) # convert to (0, 1) weights
    loss = tf.losses.softmax_cross_entropy(labels, logits, weights)
    

    不要通过实际计算输出的softmax然后交叉熵来计算softmax交叉熵,你会失去同时执行它的计算精度和稳定性 .

  • 2

    你可以做:

    import tensorflow as tf
    logits = [[4.3, -0.5, -2.7, 0, 0], [0.5, 2.3, 0, 0, 0]]
    labels = [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0]]
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=labels))
    

相关问题