1. 程式人生 > >tensorflow: 損失函式(Losses Functions) 探究

tensorflow: 損失函式(Losses Functions) 探究

損失函式定義

Losses

The loss ops measure error between two tensors, or between a tensor and zero. These can be used for measuring accuracy of a network in a regression task or for regularization purposes (weight decay).

tf.nn.l2_loss
tf.nn.log_poisson_loss

即:

Losses

損失運算 用於測量兩個張量之間或張量與0之間的誤差。 這些可以用於測量回歸任務中的網路的精確度,或用於正則化的目的(權重衰減)。

tf.nn.l2_loss
tf.nn.log_poisson_loss

l2_loss

tf.nn.l2_loss

l2_loss( t, name=None ) Defined in tensorflow/python/ops/gen_nn_ops.py.

See the guide: Neural Network > Losses

L2 Loss.

Computes half the L2 norm of a tensor without the sqrt:

output = sum(t ** 2) / 2

Args:
t: A Tensor. Must be one of the following types: half, float32,
float64. Typically 2-D, but may have any dimensions. name: A name for
the operation (optional).

Returns:
A Tensor. Has the same type as t. 0-D.

易得 l2_loss( t, name=None ) 等同於 output = sum(t ** 2) / 2

實驗思路

  1. 新建三個shape為 [10, 5, 1] 的張量,一個全0,一個全1,還有一個全2;
  2. 用 tf.nn.l2_loss 分別計算出 tf.nn.l2_loss(a-b)、tf.nn.l2_loss(a-c);
  3. 用 sum(t ** 2) / 2 分別計算出 (((0-1) ** 2) * 50) / 2、l_2 = (((0-2) ** 2) * 50) / 2;
  4. 如果對應結果相同,則驗證了該公式。

實驗原始碼

自己編寫程式碼進行驗證:

import tensorflow as tf
import numpy as np


a = np.zeros(shape=[10, 5, 1], dtype=np.float32)
b = np.ones(shape=[10, 5, 1], dtype=np.float32)
c = b * 2

# tf.nn.l2_loss(a-b) = sum((a-b)**2) / 2
loss_1 = tf.Session().run(tf.nn.l2_loss(a-b))
loss_2 = tf.Session().run(tf.nn.l2_loss(a-c))

l_1 = (((0-1) ** 2) * 50) / 2
l_2 = (((0-2) ** 2) * 50) / 2

print 'tf.nn.l2_loss(a-b) = ', loss_1
print 'tf.nn.l2_loss(a-c) = ', loss_2

assert loss_1 == l_1 and loss_2 == l_2

成功驗證了公式:

tf.nn.l2_loss(a-b) =  25.0
tf.nn.l2_loss(a-c) =  100.0