资讯详情

【Tensorflow实战系列】Tensorflow: 怎么写L2 loss

📅 2026/10/22 6:47:46 | 华诺云谱 👁 阅读
【Tensorflow实战系列】Tensorflow: 怎么写L2 loss
最近真的写的都是非常基础的小点也是希望自己能够好好积累。在训练中我们通常会定义各种各样的loss那么最简单的一个loss就是L2的distance这个要怎么写呢假设我们有两个feature一个是我们predict出来的我们假设为pred另外一个是我们得到的ground_truth, 假设为gtprednp.random.randint(1,5,[2,5,5,3])print(pred,pred.transpose(0,3,1,2))gtnp.random.randint(2,8,[2,5,5,3])print(pred,pred.transpose(0,3,1,2))tf_squaretf.square(pred-gt)tf_reduce_sumtf.reduce_sum(tf_square,axis3,keepdimsTrue)tf_reduce_sum_2tf.reduce_sum(tf_reduce_sum,axis[1,2,3])tf_reduce_meantf.reduce_mean(tf_reduce_sum_2,axis0)sesstf.Session()print(tf_square: ,np.squeeze(sess.run(tf_square)))print(tf_reduce_sum: ,np.squeeze(sess.run(tf_reduce_sum)))print(tf_reduce_sum_2: ,np.squeeze(sess.run(tf_reduce_sum_2)))print(tf_reduce_mean: ,np.squeeze(sess.run(tf_reduce_mean)))# use the tensorflow oprestf.nn.l2_loss((pred-gt).astype(np.float32))print(tf.nn.l2_loss: ,np.squeeze(sess.run(res)))写的比较复杂了其实可以写在一行里面但是看的可能不是很清楚。下面是展现的结果其实这些只要自己用个简单的小例子就能理解的很透彻了pred [[[[2 1 1 4 1] [3 1 4 2 3] [2 3 2 3 2] [2 2 4 4 3] [3 1 2 4 4]] [[2 2 2 1 3] [4 2 2 2 2] [3 2 4 3 2] [2 2 4 2 3] [3 3 2 3 2]] [[3 2 1 4 2] [3 3 2 3 2] [2 4 4 3 1] [2 3 1 3 1] [3 2 1 4 4]]] [[[4 3 2 1 2] [2 3 1 2 3] [4 4 4 2 2] [3 1 4 4 1] [4 1 1 3 4]] [[2 3 2 4 2] [3 3 2 4 3] [3 1 4 3 1] [3 3 3 4 3] [4 4 2 2 1]] [[3 4 4 2 2] [3 3 2 1 2] [4 2 1 2 1] [1 2 4 2 3] [1 2 1 2 3]]]] pred [[[[2 1 1 4 1] [3 1 4 2 3] [2 3 2 3 2] [2 2 4 4 3] [3 1 2 4 4]] [[2 2 2 1 3] [4 2 2 2 2] [3 2 4 3 2] [2 2 4 2 3] [3 3 2 3 2]] [[3 2 1 4 2] [3 3 2 3 2] [2 4 4 3 1] [2 3 1 3 1] [3 2 1 4 4]]] [[[4 3 2 1 2] [2 3 1 2 3] [4 4 4 2 2] [3 1 4 4 1] [4 1 1 3 4]] [[2 3 2 4 2] [3 3 2 4 3] [3 1 4 3 1] [3 3 3 4 3] [4 4 2 2 1]] [[3 4 4 2 2] [3 3 2 1 2] [4 2 1 2 1] [1 2 4 2 3] [1 2 1 2 3]]]] tf_square: [[[[ 0 4 4] [16 0 0] [ 9 0 36] [ 0 9 4] [ 9 16 9]] [[ 0 4 1] [25 9 1] [ 1 9 4] [25 1 1] [ 9 9 25]] [[25 0 25] [ 9 4 9] [25 1 4] [ 1 1 0] [ 4 25 25]] [[ 9 1 1] [16 25 1] [ 0 0 25] [ 4 16 9] [ 1 4 1]] [[ 1 1 0] [ 4 9 4] [25 25 1] [ 1 1 9] [ 1 1 4]]] [[[ 0 1 0] [ 4 9 4] [16 16 1] [ 9 4 1] [ 9 1 9]] [[25 0 9] [ 4 4 0] [ 9 16 1] [ 0 0 36] [16 16 0]] [[ 0 4 9] [ 1 4 1] [ 0 1 16] [ 9 9 1] [ 0 36 16]] [[ 9 1 25] [16 0 1] [ 4 16 1] [ 0 1 4] [36 0 9]] [[ 4 4 25] [25 4 4] [25 9 25] [16 0 25] [ 4 9 9]]]] tf_reduce_sum: [[[ 8 16 45 13 34] [ 5 35 14 27 43] [50 22 30 2 54] [11 42 25 29 6] [ 2 17 51 11 6]] [[ 1 17 33 14 19] [34 8 26 36 32] [13 6 17 19 52] [35 17 21 5 45] [33 33 59 41 22]]] tf_reduce_sum_2: [598 638] tf_reduce_mean: 618 tf.nn.l2_loss: 618.0可以看到我们写了好多行使用tf的接口一行就搞定啦(⊙v⊙)嗯今日份小总结水平有限能力一般。
📝

华诺云谱内容团队

资深建站顾问 · 行业研究员

10年+企业数字化服务经验,专注智能建站、SEO优化与品牌营销,持续输出建站技巧、行业洞察与营销干货,已帮助5000+企业实现数字化增长。

你可能需要的服务

订阅华诺云谱资讯周报

每周一封,精选建站技巧、SEO与营销干货,直达邮箱。已有 8,000+ 企业主订阅,助你少走弯路。

↑