codecamp

TensorFlow损失(contrib)

用于神经网络的损失操作.

注意:默认情况下,所有损失都将收集到 GraphKeys.LOSSES 集中。

所有的损失函数都采取一对预测和基准真实标签,从中计算损失。假设这两个张量的形状是 [batch_size,d1,... dN] 的形式,其中 batch_size 是批次中的样品数量,而 d1... dN 是其余尺寸。

在多次损失功能训练时,通常会调整个人损失的相对优势。这是通过权重传递给损失函数的参数重新调整损失来执行的。例如,如果我们训练 log_loss 和 sum_of_squares_loss,并且我们希望 log_loss 惩罚是 sum_of_squares_loss 的两倍,我们将实现:

# Explicitly set the weight.
tf.contrib.losses.log(predictions, labels, weight=2.0)

# Uses default weight of 1.0
tf.contrib.losses.mean_squared_error(predictions, labels)

# All the losses are collected into the `GraphKeys.LOSSES` collection.
losses = tf.get_collection(tf.GraphKeys.LOSSES)

在指定标量损失的同时,整个批处理中的损失可能会重新计算,我们有时希望重新调整每个批处理样本的损失。例如,如果我们有一些比较重要的例子可以让我们得到正确的结果,那么我们可能想要损失更多的其他错误的样本。在这种情况下,我们可以提供长度的权重向量 batch_size,导致批处理中每个样本的损失由相应的权重元素缩放。例如,考虑分类问题的情况,我们希望最大化我们的准确性,但我们特别有兴趣获得高精度的特定类:

inputs, labels = LoadData(batch_size=3)
logits = MyModelPredictions(inputs)

# Ensures that the loss for examples whose ground truth class is `3` is 5x
# higher than the loss for all other examples.
weight = tf.multiply(4, tf.cast(tf.equal(labels, 3), tf.float32)) + 1

onehot_labels = tf.one_hot(labels, num_classes=5)
tf.contrib.losses.softmax_cross_entropy(logits, onehot_labels, weight=weight)

最后,在某些情况下,我们可能希望为每个可衡量的值指定不同的损失。例如,如果我们执行每像素深度预测或每像素去噪,则单个批次样本具有P值,其中P是图像中的像素数。对于许多损失,可测量值的数量与预测和标签张量中的元素数量相匹配。对于其他的,例如 softmax_cross_entropy 和 cosine_distance,损失函数减小输入的维数,以产生每个可测量值的张量。例如,softmax_cross_entropy 作为维度 [batch_size,num_classes] 的输入预测和标签,但可测量值的数量为 [batch_size]。因此,当通过权重张量以指定每个可测量值的不同损失时,

对于具体的例子,考虑某些地面真值深度值缺失(由于捕获过程中的传感器噪声)的每像素深度预测的情况.在这种情况下,我们要为这些预测分配零权重给损失。

# 'depths' that are missing have a value of 0:
images, depths = LoadData(...)
predictions = MyModelPredictions(images)

weight = tf.cast(tf.greater(depths, 0), tf.float32)
loss  = tf.contrib.losses.mean_squared_error(predictions, depths, weight)

注意,当使用权重作为损失时,最终的平均值是通过将权重重新分配权重来计算的,然后除以非零样本的总数。对于任意一组权重,这可能不一定产生加权平均值。相反,在平均观测数量之前,它简单而透明地调整了每个元素的损失。例如,如果由损失函数计算的损失是数组[4,1,2,3],权重是数组[1,0.5,3,9],那么平均损失是:

(4*1 + 1*0.5 + 2*3 + 3*9) / 4

然而,利用单个损失函数和任意权重集合,仍然可以容易地创建损失函数,使得所得到的损失是相对于各个预测误差的加权平均值:

images, labels = LoadData(...)
predictions = MyModelPredictions(images)

weight = MyComplicatedWeightingFunction(labels)
weight = tf.div(weight, tf.size(weight))
loss = tf.contrib.losses.mean_squared_error(predictions, depths, weight)

  • tf.contrib.losses.absolute_difference 
  • tf.contrib.losses.add_loss
  • tf.contrib.losses.hinge_loss
  • tf.contrib.losses.compute_weighted_loss
  • tf.contrib.losses.cosine_distance
  • tf.contrib.losses.get_losses 
  • tf.contrib.losses.get_regularization_losses
  • tf.contrib.losses.get_total_loss 
  • tf.contrib.losses.log_loss 
  • tf.contrib.losses.mean_pairwise_squared_error 
  • tf.contrib.losses.mean_squared_error
  • tf.contrib.losses.sigmoid_cross_entropy 
  • tf.contrib.losses.softmax_cross_entropy 
  • tf.contrib.losses.sparse_softmax_cross_entropy

以下是不推荐使用的:mean_pairwise_squared_error 和 mean_squared_error.

  • tf.contrib.losses.sum_of_pairwise_squares
  • tf.contrib.losses.sum_of_squares
TensorFlow 线性代数库(contrib)
TensorFlow 数学函数
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

TensorFlow 函数介绍

TensorFlow 函数模块:tf

TensorFlow的image模块

TensorFlow使用之tf.io

TensorFlow使用之tf.keras

TensorFlow函数教程:tf.keras.applications

TensorFlow函数教程:tf.keras.backend

TensorFlow使用之tf.metrics

TensorFlow使用之tf.nn

TensorFlow使用之tf.python_io

TensorFlow 功能函数

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }