Tensorflow - 如何计算多类分类问题的损失权重并将其应用于损失函数?

时间:2017-11-17 09:57:25

标签: python tensorflow

我有一个不平衡的数据集,用于训练我的神经网络。与我计算的类对应的数据集中的标签数量如下:

training_data_classnums = [1480, 1104, 1261, 942, 2612, 1109, 355, 355]

我使用Softmax来计算损失函数中每个类的概率:

training_data_classnums = [1480, 1104, 1261, 942, 2612, 1109, 355, 355]
class_sum = sum(training_data_classnums)
training_data_weights = [x / class_sum for x in training_data_classnums]
print("class_weights {}".format(training_data_weights))
...
onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=8)
class_weight = tf.constant(training_data_weights)
weighted_logits = tf.multiply(logits, class_weight)
tf.logging.info("logits_weighted {} {}".format(weighted_logits.get_shape(), logits.get_shape()))
loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=weighted_logits)

这是计算每个类别权重的正确方法吗?这是将其应用于损失计算的正确方法吗? (我使用tf.Estimator

0 个答案:

没有答案