pytorch loss

参考文献:

loss 测试

import torch

from torch.autograd import Variable

'''

参考文献: https://blog.csdn.net/zhangxb35/article/details/72464152?utm_source=itdadao&utm_medium=referral

如果 reduce = False,那么 size_average 参数失效,直接返回向量形式的 loss;

如果 reduce = True,那么 loss 返回的是标量

如果 size_average = True,返回 loss.mean();

如果 size_average = True,返回 loss.sum();

'''

# nn.L1Loss: loss(input, target)=|input-target|

if False:

loss_fn = torch.nn.L1Loss(reduce=True, size_average=False)

input = torch.autograd.Variable(torch.randn(3, 4))

target = torch.autograd.Variable(torch.randn(3, 4))

loss = loss_fn(input, target)

print(input)

print(target)

print(loss)

print(input.size(), target.size(), loss.size())

# nn.SmoothL1Loss  在(-1, 1)上是平方loss, 其他情况是L1 loss

if False:

loss_fn = torch.nn.SmoothL1Loss(reduce=False, size_average=False)

input = torch.autograd.Variable(torch.randn(3, 4))

target = torch.autograd.Variable(torch.randn(3, 4))

loss = loss_fn(input, target)

print(input)

print(target)

print(loss)

print(input.size(), target.size(), loss.size())

# nn.MSELoss 均方损失函数

if False:

loss_fn = torch.nn.MSELoss(reduce=False, size_average=False)

input = torch.autograd.Variable(torch.randn(3, 4))

target = torch.autograd.Variable(torch.randn(3, 4))

loss = loss_fn(input, target)

print(input)

print(target)

print(loss)

print(input.size(), target.size(), loss.size())

# nn.BCELoss

if False:

import torch.nn.functional as F

loss_fn = torch.nn.BCELoss(reduce=False, size_average=False)

input = torch.autograd.Variable(torch.randn(3, 4))

target = torch.autograd.Variable(torch.FloatTensor(3, 4).random_(2))

loss = loss_fn(F.sigmoid(input), target)

print(input, input.shape)

print(F.sigmoid(input))

print(target, target.shape)

print(loss, loss.shape)

# nn.CrossEntropyLoss

if False:

weight = torch.Tensor([1, 2, 1, 1, 10])

loss_fn = torch.nn.CrossEntropyLoss(reduce=False, size_average=False, weight=None)

input = Variable(torch.randn(3, 5)) # (batch_size, C)

target = Variable(torch.LongTensor(3).random_(5))

loss = loss_fn(input, target)

print(input)

print(target)

print(loss)

# nn.NLLLoss 负的 log likehood loss损失.用于训练一个n类分类器.

if False:

m = nn.LogSoftmax()

loss = nn.NLLLoss()

# input is of size nBatch x nClasses = 3 x 5

input = Variable(torch.randn(3, 5), requires_grad=True)

# each element in target has to have 0 <= value < nclasses

target = Variable(torch.LongTensor([1, 0, 4]))

output = loss(m(input), target)

print(output)

# nn.LLLoss2d 对于图片的 negtive log likehood loss.计算每个像素的NLL loss

if False:

m = nn.Conv2d(16, 32, (3, 3)).float()

loss = nn.NLLLoss2d()

# input is of size nBatch x nClasses x height x width

input = Variable(torch.randn(3, 16, 10, 10))

# each element in target has to have 0 <= value < nclasses

target = Variable(torch.LongTensor(3, 8, 8).random_(0, 4))

output = loss(m(input), target)

print(m(input))

print(output)

# nn.MultiLabelMarginLoss input : x --> (N, c), y --> (N, c)其中y是 LongTensor, 且其元素为类别的index

if False:

x=Variable(torch.randn(3, 4))

y=Variable(torch.LongTensor(3, 4).random_(4))

loss=torch.nn.MultiLabelMarginLoss()

output = loss(x, y)

print(output)

# nn.MultiLabelSoftMarginLoss 与MultiLableMarginLoss相同,区别在于y的类型是FloatTensor

if False:

x = Variable(torch.randn(3, 10))

y = Variable(torch.FloatTensor(3, 10).random_(10))

loss = torch.nn.MultiLabelSoftMarginLoss()

output = loss(x, y)

print(output)

# nn.MultiMarginLoss 适用于多分类模型

if True:

x = Variable(torch.randn(3, 10))

y = Variable(torch.LongTensor(3).random_(10))

loss = torch.nn.MultiMarginLoss()

output=loss(x, y)

print(output)

pytorch查看loss曲线_pytorch loss总结与测试相关推荐

  1. pytorch 画loss曲线_Pytorch练习amp;#8211;绘制Loss曲线 - 易采站长站

    绘制Loss曲线 b站课程链接:https://www.bilibili.com/video/BV1Y7411d7Ys?p=2 import绘制曲线的库和numpy库 import numpy as ...

  2. pytorch 画loss曲线_Pytorch使用tensorboardX可视化。超详细!!!

    1 引言 我们都知道tensorflow框架可以使用tensorboard这一高级的可视化的工具,为了使用tensorboard这一套完美的可视化工具,未免可以将其应用到Pytorch中,用于Pyto ...

  3. pytorch 查看当前学习率_pytorch调整模型训练的学习率

    简介 torch.optim.lr_scheduler :提供了一些方法可以基于epochs的值来调整学习率. torch.optim.lr_scheduler.ReduceLROnPlateau : ...

  4. pytorch 查看当前学习率_pytorch实现查看当前学习率

    在pytorch训练过程中可以通过下面这一句代码来打印当前学习率 print(net.optimizer.state_dict()['param_groups'][0]['lr']) 补充知识:Pyt ...

  5. pytorch绘制并显示loss曲线和acc曲线,LeNet5识别图像准确率

    我用的是Anaconda3 ,用spyder编写pytorch的代码,在Anaconda3中新建了一个pytorch的虚拟环境(虚拟环境的名字就叫pytorch). 以下内容仅供参考哦~~ 1.首先打 ...

  6. Visdom数据可视化工具绘制Loss曲线

    目录 Visdom介绍 Loss可视化代码编写 视频链接 Visdom介绍 Visdom是FaceBook公司开发的一款开源数据可视化工具,以其简单易用的功能,很快成为PyTorch的一个数据可视化工 ...

  7. Caffe—根据log日志绘制loss曲线和accuracy

    本文在此只讲述Ubuntu16.04下 caffe训练日志绘制loss曲线以及accuracy 如果是windows平台直接跳转文末 caffe中其实已经自带了这样的小工具 caffe-master/ ...

  8. Python之plt.show()不显示图片画loss曲线教程

    目录 plt.show()之后不显示图片的问题 画loss曲线教程 plt.show()之后不显示图片的问题 1. 如果系统为windows等系统,那就看别的教程吧(因为我没有遇到过这类问题),比如: ...

  9. Keras构建前馈神经网络并使用callbacks输出acc以及loss曲线(训练接、验证集)及效果可视化

    Keras构建前馈神经网络并使用callbacks输出acc以及loss曲线(训练接.验证集)及效果可视化 在每个training/epoch/batch结束时,如果我们想执行某些任务,例如模型缓存. ...

最新文章

  1. ListView style
  2. Leangoo敏捷看板管理 6.3.8
  3. PCL点云库学习笔记 点云的欧式聚类
  4. bootstraptable获得所有行_“安全超新星”集结出道,共创交通美好未来 2020广汽本田安全中国行首届道路安全创新大赛成功举办...
  5. IOS开发 Block的学习
  6. 为ASP.NET MVC扩展异步Action功能(下)
  7. wxWidgets:wxRibbonButtonBar类用法
  8. Golang 的跨平台交叉编译浅析
  9. 第二章16位和32位微处理器(2)——一些操作时序与中断
  10. 华为java开发面试难不难,秀出天际!
  11. 小程序中的css样式有哪些,微信小程序中css样式media标签
  12. Numpy知识点:np.expand_dims()np.argmax()
  13. mysql查询游标_数据库查询,游标。
  14. 大型网站的架构设计问题----大型高并发高负载网站的系统架构[转]
  15. Java内存模型——《深入理解Java虚拟机》笔记
  16. 混淆矩阵 python实现
  17. STM32CubeMx-SPI读写W25QXXX
  18. Fiddle 抓包小白一步带过超详细教程(含汉化)
  19. python怎么读数据_Python如何读取数据
  20. ABV指标与通达信的主力进出指标分析

热门文章

  1. Hello,Views(六)下拉框Spinner(附源码)
  2. 程序员达到高效率的一种境界
  3. Linux下基于 Docker 搭建 MySQL 主从复制(1 Master+2 Slave)
  4. 线性代数:线性组合学习笔记
  5. Luogu P2309 loidc,卖卖萌
  6. [wordpress]更新插件时,免去FTP操作
  7. Ripple_vJZ
  8. Hadoop简介 (资源)
  9. 解决eclipse 文件更新不自动刷新的问题
  10. 厦门信息集团与EMC战略合作共建智慧厦门