下面以常用的softmax_cross_entropy() 函数为例,做一个详细介绍。
tf.losses.softmax_cross_entropy(
onehot_labels, # 注意此处参数名就叫 onehot_labels
logits,
weights=1.0,
label_smoothing=0,
scope=None,
loss_collection=tf.GraphKeys.LOSSES,
reduction=Reduction.SUM_BY_NONZERO_WEIGHTS
)
Args:
- onehot_labels: [batch_size, num_classes] one_hot类型的label.
- logits: [batch_size, num_classes] 神经网络的logits输出. 这两个参数都没什么好解释的,最基本的知识。
- weights: 为可选参数,首先它是tensor 然后它可以是一个标量(此时作为loss的系数),也可以是一个[batch_size]的向量(看源码我个人理解就是对一个batch的样本进行加权,不知道有什么意义,因为一般来说训练样本的输入顺序是随机的,即每一个batch的样本全部都是随机的,这中情况下这个加权没有任何意义了)
- label_smoothing: 这个参数如果设置大于0,则对label进行平滑,平滑的公式为:
- new_onehot_labels = onehot_labels*(1-label_smoothing) + label_smoothing/num_classes
- scope: 命名空间
- loss_collection: 指定loss集合。
- reduction: 指定应用到这个loss的reduction类型.
- NONE: Un-reduced weighted losses with the same shape as input.
- SUM: Scalar sum of weighted losses.
- MEAN: Scalar ‘SUM’ divided by sum of weights.
- SUM_OVER_BATCH_SIZE: Scalar ‘SUM’ divided by number of elements in losses.
- SUM_OVER_NONZERO_WEIGHTS: Scalar ‘SUM’ divided by number of non-zero weights.
- SUM_BY_NONZERO_WEIGHTS: Same as ‘SUM_OVER_NONZERO_WEIGHTS’(注意:在损失函数中weights=1.0, 对应这里的weighted_sum_by_nonzero_weights,配合起来就是求和)
reduction一般都是使用SUM_BY_NONZERO_WEIGHTS,不过需要注意的是设置这个参数的时候是:reduction=tf.losses.Reduction.MEAN