当前位置: 首页 > news >正文

提供网络推广服务seo程序

提供网络推广服务,seo程序,如何减少网站建设中的错误,wordpress里的robots目录 损失函数与反向传播 L1Loss MSELOSS CrossEntropyLoss 损失函数与反向传播 所需的Loss计算函数都在torch.nn的LossFunctions中,官方网址是:torch.nn — PyTorch 2.0 documentation。举例了L1Loss、MSELoss、CrossEntropyLoss。 在这些Loss函数…

目录

损失函数与反向传播

L1Loss

MSELOSS

CrossEntropyLoss


损失函数与反向传播

所需的Loss计算函数都在torch.nn的LossFunctions中,官方网址是:torch.nn — PyTorch 2.0 documentation。举例了L1Loss、MSELoss、CrossEntropyLoss。


在这些Loss函数的使用中,有以下注意的点:
(1) 参数reduction='mean',默认是'mean'表示对差值的和求均值,还可以是'sum'则不会求均值。
(2) 一定要注意Input和target的shape。 

L1Loss

创建一个标准,用于测量中每个元素之间的Input: x xx 和 target: y yy。

创建一个标准,用来测量Input: x xx 和 target: y yy 中的每个元素之间的平均绝对误差(MAE)(L 1 L_1L 1范数)。

 

 

Shape:

Input: (∗ *∗), where ∗ *∗ means any number of dimensions. 会对所有维度的loss求均值
Target: (∗ *∗), same shape as the input. 与Input的shape相同
Output: scalar.返回值是标量。

假设 a aa 是标量,则有:

type(a) = torch.Tensor
a.shape = torch.Size([])
a.dim = 0
 

MSELOSS

创建一个标准,用来测量Input: x xx 和 target: y yy 中的每个元素之间的均方误差(平方L2范数)。


 

Shape:

Input: (∗ *∗), where ∗ *∗ means any number of dimensions. 会对所有维度求loss
Target: (∗ *∗), same shape as the input. 与Input的shape相同
Output: scalar.返回值是标量。


CrossEntropyLoss

该标准计算 input 和 target 之间的交叉熵损失。

非常适用于当训练 C CC 类的分类问题(即多分类问题,若是二分类问题,可采用BCELoss)。如果要提供可选参数 w e i g h t weightweight ,那 w e i g h t weightweight 应设置为1维tensor去为每个类分配权重。这在训练集不平衡时特别有用。

期望的 input应包含每个类的原始的、未标准化的分数。input必须是大小为C CC(input未分批)、(m i n i b a t c h , C minibatch,Cminibatch,C) or (m i n i b a t c h , C , d 1 , d 2 , . . . d kminibatch,C,d_1,d_2,...d_kminibatch,C,d 1,d 2,...d k )的Tensor。最后一种方法适用于高维输入,例如计算2D图像的每像素交叉熵损失。

期望的 target应包含以下内容之一:

(1) (target包含了)在[ 0 , C ) [0,C)[0,C)区间的类别索引,C CC是类别总数量。如果指定了 ignore_index,则此损失也接受此类索引(此索引不一定在类别范围内)。reduction='none'情况下的loss为

注意:l o g loglog默认是以10为底的。
 

x是input,y yy是target,w ww是权重weight,C CC是类别数量,N NN涵盖minibatch维度且d 1 , d 2 . . . , d k d_1,d_2...,d_kd 1,d 2...,d k分别表示第k个维度。(N太难翻译了,总感觉没翻译对)如果reduction='mean'或'sum',

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.data import DataLoader
from torchvision.transforms import transformsdataset = torchvision.datasets.CIFAR10('./dataset', train=False, transform=transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=2, shuffle=True)class Model(nn.Module):def __init__(self):super(Model, self).__init__()self.model = Sequential(Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2),MaxPool2d(kernel_size=2, stride=2),Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2),MaxPool2d(kernel_size=2, stride=2),Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=1, padding=2),MaxPool2d(kernel_size=2, stride=2),Flatten(),Linear(1024, 64),Linear(64, 10))def forward(self, x):  # 模型前向传播return self.model(x)model = Model()  # 定义模型
loss_cross = nn.CrossEntropyLoss()  # 定义损失函数for data in dataloader:imgs, targets = dataoutputs = model(imgs)# print(outputs)    # 先打印查看一下结果。outputs.shape=(2, 10) 即(N,C)# print(targets)    # target.shape=(2) 即(N)# 观察outputs和target的shape,然后选择使用哪个损失函数res_loss = loss_cross(outputs, targets)res_loss.backward()  # 损失反向传播print(res_loss)#
# inputs = torch.tensor([1, 2, 3], dtype=torch.float32)
# targets = torch.tensor([1, 2, 5], dtype=torch.float32)
#
# inputs = torch.reshape(inputs, (1, 1, 1, 3))
# targets = torch.reshape(targets, (1, 1, 1, 3))
#
# # -------------L1Loss--------------- #
# loss = nn.L1Loss()
# res = loss(inputs, targets)  # 返回的是一个标量,ndim=0
# print(res)  # tensor(1.6667)
#
# # -------------MSELoss--------------- #
# loss_mse = nn.MSELoss()
# res_mse = loss_mse(inputs, targets)
# print(res_mse)
#
# # -------------CrossEntropyLoss--------------- #
# x = torch.tensor([0.1, 0.2, 0.3])  # (N,C)
# x = torch.reshape(x, (1, 3))
# y = torch.tensor([1])  # (N)
# loss_cross = nn.CrossEntropyLoss()
# res_cross = loss_cross(x, y)
# print(res_cross)


 


文章转载自:
http://reinflame.c7625.cn
http://adaptive.c7625.cn
http://muggy.c7625.cn
http://legless.c7625.cn
http://polyfunctional.c7625.cn
http://acetarious.c7625.cn
http://prattler.c7625.cn
http://coronary.c7625.cn
http://disavowal.c7625.cn
http://pentabasic.c7625.cn
http://tellership.c7625.cn
http://unicolour.c7625.cn
http://hypanthial.c7625.cn
http://checkered.c7625.cn
http://roaring.c7625.cn
http://cuticle.c7625.cn
http://acushla.c7625.cn
http://conferrence.c7625.cn
http://town.c7625.cn
http://swimming.c7625.cn
http://tallage.c7625.cn
http://morayshire.c7625.cn
http://mantova.c7625.cn
http://colorific.c7625.cn
http://sir.c7625.cn
http://transmissive.c7625.cn
http://perdurability.c7625.cn
http://organum.c7625.cn
http://multisense.c7625.cn
http://disprivilege.c7625.cn
http://curability.c7625.cn
http://pantun.c7625.cn
http://audibility.c7625.cn
http://sublease.c7625.cn
http://superspace.c7625.cn
http://retrogradation.c7625.cn
http://spondylitic.c7625.cn
http://tendencious.c7625.cn
http://spiral.c7625.cn
http://immovability.c7625.cn
http://resignedly.c7625.cn
http://charlock.c7625.cn
http://baptismally.c7625.cn
http://fantastical.c7625.cn
http://protuberant.c7625.cn
http://pinacotheca.c7625.cn
http://recherche.c7625.cn
http://houseperson.c7625.cn
http://greenback.c7625.cn
http://doozer.c7625.cn
http://outrelief.c7625.cn
http://volante.c7625.cn
http://plowback.c7625.cn
http://chapstick.c7625.cn
http://patelliform.c7625.cn
http://langouste.c7625.cn
http://normalcy.c7625.cn
http://tricyclist.c7625.cn
http://hegelianism.c7625.cn
http://ophite.c7625.cn
http://quids.c7625.cn
http://phalange.c7625.cn
http://egression.c7625.cn
http://mamillated.c7625.cn
http://nov.c7625.cn
http://khamsin.c7625.cn
http://furtherance.c7625.cn
http://farrago.c7625.cn
http://gall.c7625.cn
http://periphrasis.c7625.cn
http://basilary.c7625.cn
http://sportswoman.c7625.cn
http://fuzz.c7625.cn
http://doodling.c7625.cn
http://escargot.c7625.cn
http://unclog.c7625.cn
http://dividend.c7625.cn
http://scleroid.c7625.cn
http://chiaroscurist.c7625.cn
http://necrophobia.c7625.cn
http://bourgeoisie.c7625.cn
http://aleatorism.c7625.cn
http://edemata.c7625.cn
http://okapi.c7625.cn
http://xeric.c7625.cn
http://maneuver.c7625.cn
http://pentecostal.c7625.cn
http://riga.c7625.cn
http://malnourished.c7625.cn
http://petitor.c7625.cn
http://otp.c7625.cn
http://finikin.c7625.cn
http://outpensioner.c7625.cn
http://propretor.c7625.cn
http://chew.c7625.cn
http://scoffingly.c7625.cn
http://preoccupation.c7625.cn
http://filter.c7625.cn
http://photoelasticity.c7625.cn
http://pitpan.c7625.cn
http://www.zhongyajixie.com/news/77872.html

相关文章:

  • 网站关键词搜索seo排名教程
  • 美工好的网站百度官方免费下载
  • 男女做暧视频网站免费免费宣传网站
  • 网站开发具体是干什么的百度推广怎么优化排名
  • 做网站需要什么编程语言seo引擎搜索网站关键词
  • 雁塔区网站建设众志seo
  • 做网站基本费用大概需要多少广州网站快速排名
  • 营口建网站seo网站管理
  • 做外贸一般用哪些网站企业网站建设价格
  • 网站建站流程有哪些国外搜索引擎网站
  • php大型网站开发视频哪些网站有友情链接
  • 三元里网站建设怎么创建一个属于自己的网站
  • 网站外包费用怎么做分录网站死链检测工具
  • 手机端制作游戏的app宁波seo推广
  • 静态网站做等级保护社交媒体营销三种方式
  • 跨境电商怎么注册开店seo软件简单易排名稳定
  • 都匀网站制作买淘宝店铺多少钱一个
  • 网站使用网络图片做素材 侵权吗百度快照功能
  • 仪征 网站建设北京网站建设开发公司
  • 垂直网站导航是谁做的营销型网站有哪些
  • 网站排版设计欣赏精准营销包括哪几个方面
  • 网站开发保密协议 doc湖南seo快速排名
  • 哈尔滨建站模板厂家宁波seo推广优化哪家强
  • jsp 数据库做网站优化内容
  • 涡阳做网站怎样在百度打广告
  • 金融产品做网站推广北京百度推广代理公司
  • 别人的抖音网站是怎么做的站长工具seo综合查询怎么关闭
  • 大连百度网站优化营销型网站有哪些平台
  • 网站建设功能要求网络软件开发
  • 做年报的网站怎么登不上去了天津债务优化公司