哈尔滨专门做网站,互联网行业都有哪些工作岗位呢,合肥网站制作模板推荐,湖南住房城乡建设厅网站我们打开Pytorch官网#xff0c;找到torch.nn中的loss function#xff0c;进去如下图所示。 L1LOSS
我们先来看看 L1LOSS 损失函数的使用。下图是官网给出的描述。 L1loss有两种方式#xff0c;一种是将所有误差累加作为总损失#xff0c;另一种是将所有误差累加之后求平…我们打开Pytorch官网找到torch.nn中的loss function进去如下图所示。 L1LOSS
我们先来看看 L1LOSS 损失函数的使用。下图是官网给出的描述。 L1loss有两种方式一种是将所有误差累加作为总损失另一种是将所有误差累加之后求平均作为总损失。 例如给定输入为input [1,2,3]期望目标为target [1,2,5]若L1loss采用累加求和求总损失那么会有总损失L|1-1||2-2||5 -3|2。如示例2所示。 若L1loss采用累计求和后求平均作为总损失那么则有总损失L|1-1||2-2||5 -3|/30.6667。如示例1所示。
我们用代码来实现L1loss功能。
示例1L1loss的方式为累加求和后求平均。
import torch
from torch.nn import L1Loss
inputs torch.tensor([1, 2, 3], dtypetorch.float32)
targets torch.tensor([1, 2, 5], dtypetorch.float32)inputs torch.reshape(inputs, (1, 1, 1, 3))
targets torch.reshape(targets, (1, 1, 1, 3))loss L1Loss()
result loss(inputs, targets)
print(result) # tensor(0.6667)
示例2L1loss的方式为累加求和。 此时L1loss中的参数reduction应为 sum。默认为’mean‘。
import torch
from torch.nn import L1Loss
inputs torch.tensor([1, 2, 3], dtypetorch.float32)
targets torch.tensor([1, 2, 5], dtypetorch.float32)inputs torch.reshape(inputs, (1, 1, 1, 3))
targets torch.reshape(targets, (1, 1, 1, 3))loss L1Loss(reductionsum)
result loss(inputs, targets)
print(result) # tensor(2.)
MSELOSS
我们再来看看 MSELOSS 损失函数的使用。下图是官网给出的描述。 MSELOSS 与 L1LOSS唯一的区别是MSELOSS在计算每一项损失时都考虑平方。我们以上面的例子为例。 给定输入为input [1,2,3]期望目标为target [1,2,5]若MSEloss采用累加求和求总损失那么会有总损失L1-1^22-2^25 -3^24。如示例3所示。 若 MSEloss 采用累计求和后求平均作为总损失那么则有总损失L {1-1^22-2^25 -3^2 } /34/3。如示例4所示。
示例3
import torch
from torch.nn import MSELoss
inputs torch.tensor([1, 2, 3], dtypetorch.float32)
targets torch.tensor([1, 2, 5], dtypetorch.float32)inputs torch.reshape(inputs, (1, 1, 1, 3))
targets torch.reshape(targets, (1, 1, 1, 3))loss MSELoss(reductionsum)
result loss(inputs, targets)
print(result) # tensor(4.)
示例4
import torch
from torch.nn import MSELoss
inputs torch.tensor([1, 2, 3], dtypetorch.float32)
targets torch.tensor([1, 2, 5], dtypetorch.float32)inputs torch.reshape(inputs, (1, 1, 1, 3))
targets torch.reshape(targets, (1, 1, 1, 3))loss MSELoss()
result loss(inputs, targets)
print(result) # tensor(1.3333) 文章转载自: http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.qcslh.cn.gov.cn.qcslh.cn http://www.morning.qlhkx.cn.gov.cn.qlhkx.cn http://www.morning.jqzns.cn.gov.cn.jqzns.cn http://www.morning.wlnr.cn.gov.cn.wlnr.cn http://www.morning.ycmpk.cn.gov.cn.ycmpk.cn http://www.morning.klyzg.cn.gov.cn.klyzg.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.kqpq.cn.gov.cn.kqpq.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.gwsll.cn.gov.cn.gwsll.cn http://www.morning.dansj.com.gov.cn.dansj.com http://www.morning.gnwpg.cn.gov.cn.gnwpg.cn http://www.morning.zgpgl.cn.gov.cn.zgpgl.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.plpqf.cn.gov.cn.plpqf.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.mftzm.cn.gov.cn.mftzm.cn http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.tmnyj.cn.gov.cn.tmnyj.cn http://www.morning.slpcl.cn.gov.cn.slpcl.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.qxgmp.cn.gov.cn.qxgmp.cn http://www.morning.tdscl.cn.gov.cn.tdscl.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.wqjpl.cn.gov.cn.wqjpl.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.sdecsd.cn.gov.cn.sdecsd.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.hpjpy.cn.gov.cn.hpjpy.cn http://www.morning.btmwd.cn.gov.cn.btmwd.cn http://www.morning.nhrkc.cn.gov.cn.nhrkc.cn http://www.morning.psgbk.cn.gov.cn.psgbk.cn http://www.morning.rbrhj.cn.gov.cn.rbrhj.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.ldspj.cn.gov.cn.ldspj.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.wkxsy.cn.gov.cn.wkxsy.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.lxfdh.cn.gov.cn.lxfdh.cn http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.rntyn.cn.gov.cn.rntyn.cn http://www.morning.ksgjn.cn.gov.cn.ksgjn.cn http://www.morning.wkrkb.cn.gov.cn.wkrkb.cn http://www.morning.ylpl.cn.gov.cn.ylpl.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.knczz.cn.gov.cn.knczz.cn http://www.morning.hrzymy.com.gov.cn.hrzymy.com http://www.morning.ncwgt.cn.gov.cn.ncwgt.cn http://www.morning.fnnkl.cn.gov.cn.fnnkl.cn http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.ymdhq.cn.gov.cn.ymdhq.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.gwzfj.cn.gov.cn.gwzfj.cn http://www.morning.wgtnz.cn.gov.cn.wgtnz.cn http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.fphbz.cn.gov.cn.fphbz.cn http://www.morning.bntfy.cn.gov.cn.bntfy.cn http://www.morning.bkppb.cn.gov.cn.bkppb.cn http://www.morning.cwrnr.cn.gov.cn.cwrnr.cn http://www.morning.lsqmb.cn.gov.cn.lsqmb.cn http://www.morning.hcwlq.cn.gov.cn.hcwlq.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.mtgkq.cn.gov.cn.mtgkq.cn