如何使用sparse_softmax_cross_entropy_with_logits在Tensorflow中实现加权交叉熵损失 [英] How can I implement a weighted cross entropy loss in tensorflow using sparse_softmax_cross_entropy_with_logits
问题描述
我开始使用tensorflow(来自Caffe),并且使用的损失是 sparse_softmax_cross_entropy_with_logits
。该函数接受 0,1,... C-1
之类的标签,而不是onehot编码。现在,我想根据类标签使用权重;我知道,如果我使用 softmax_cross_entropy_with_logits
(一种热编码),则可以使用矩阵乘法来完成,是否可以用进行相同的处理sparse_softmax_cross_entropy_with_logits
?
I am starting to use tensorflow (coming from Caffe), and I am using the loss sparse_softmax_cross_entropy_with_logits
. The function accepts labels like 0,1,...C-1
instead of onehot encodings. Now, I want to use a weighting depending on the class label; I know that this could be done maybe with a matrix multiplication if I use softmax_cross_entropy_with_logits
(one hot encoding), Is there any way to do the same with sparse_softmax_cross_entropy_with_logits
?
推荐答案
import tensorflow as tf
import numpy as np
np.random.seed(123)
sess = tf.InteractiveSession()
# let's say we have the logits and labels of a batch of size 6 with 5 classes
logits = tf.constant(np.random.randint(0, 10, 30).reshape(6, 5), dtype=tf.float32)
labels = tf.constant(np.random.randint(0, 5, 6), dtype=tf.int32)
# specify some class weightings
class_weights = tf.constant([0.3, 0.1, 0.2, 0.3, 0.1])
# specify the weights for each sample in the batch (without having to compute the onehot label matrix)
weights = tf.gather(class_weights, labels)
# compute the loss
tf.losses.sparse_softmax_cross_entropy(labels, logits, weights).eval()
这篇关于如何使用sparse_softmax_cross_entropy_with_logits在Tensorflow中实现加权交叉熵损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!