前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensorflow: 损失函数(Losses Functions) 探究

tensorflow: 损失函数(Losses Functions) 探究

作者头像
JNingWei
发布2018-09-28 15:56:22
1.4K0
发布2018-09-28 15:56:22
举报
文章被收录于专栏:JNing的专栏JNing的专栏

损失函数定义

From Tensorflow - Losses

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

From tf.nn.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. 如果对应结果相同,则验证了该公式。

实验源码

自己编写代码进行验证:

代码语言:javascript
复制
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

成功验证了公式:

代码语言:javascript
复制
tf.nn.l2_loss(a-b) =  25.0
tf.nn.l2_loss(a-c) =  100.0


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年09月04日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 损失函数定义
  • l2_loss
    • 实验思路
      • 实验源码
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档