【发布时间】:2016-09-18 09:37:17
【问题描述】:
在 tensorflow 中使用 tf.nn.sparse_softmax_cross_entropy_with_logits,可以通过将类标签设置为 -1 来仅计算特定行的损失(否则它应该在 0->numclasses-1 的范围内)。
不幸的是,这会破坏梯度计算(如源 nn_ops.py 中的 cmets 所述)。
我想做的事情如下:
raw_classification_output1 = [0,1,0]
raw_classification_output2 = [0,0,1]
classification_output =tf.concat(0,[raw_classification_output1,raw_classification_output2])
classification_labels = [1,-1]
classification_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(classification_output,classification_labels)
total_loss = tf.reduce_sum(classification_loss) + tf.reduce_sum(other_loss)
optimizer = tf.train.GradientDescentOptimizer(1e-3)
grads_and_vars = optimizer.compute_gradients(total_loss)
changed_grads_and_vars = #do something to 0 the incorrect gradients
optimizer.apply_gradients(changed_grads_and_vars)
将这些渐变归零最直接的方法是什么?
【问题讨论】:
-
您可以使用clip_by_value 剪辑您的祖孙。
-
抱歉,我只是不确定您所说的“不正确”渐变是什么意思。带有“-1”的行的loss会为零,也就是说你的参数在最终loss中没有生效,那为什么不为零呢?
标签: python tensorflow