CNN基础知识 || 均方差损失函数

定义

在深度学习中,做回归任务时使用的loss多为均方差。公式为

其中:Batch为样本数量,M为网络输出层的元素的个数

 

实现

  • loss = tf.nn.l2_loss(x, x')  *2.0/ (Batch*M)
  • loss = tf.losses.mean_squared_error(x, x')
  • loss = tf.reduce_mean((x - x')**2)
  • loss = tf.reduce_mean(tf.aquare(x - x'))    (与上面的同意)

对于tf.nn.l2_loss(),数学表达式为 output = sum(t**2)/2

 

测试

认为a中有3个数据,计算a和b的均方差

import tensorflow as tf

a= [[1.0,2.0,3.0,5.0],[1.0,2.0,3.0,7.0],[1.0,2.0,3.0,2.0]]
b= [[2.0,4.0,3.0,7.0],[2.0,4.0,3.0,5.0],[2.0,4.0,3.0,5.0]]
c = tf.convert_to_tensor(a)
d = tf.convert_to_tensor(b)

loss1 = tf.nn.l2_loss(c-d) *2/(3*4)
loss2 = tf.reduce_mean((c-d)**2)
loss3 = tf.reduce_mean(tf.square(c-d))
loss4 = tf.losses.mean_squared_error(c,d)

with tf.Session() as sess:
    print(sess.run(loss1))    # 2.6666667
    print(sess.run(loss2))    # 2.6666667
    print(sess.run(loss3))    # 2.6666667
    print(sess.run(loss4))    # 2.6666667

上面四种表达结果是一样的

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值