1. import torch
  2. from torch.autograd import Variable
  3.  
  4. # Variable in torch is to build a computational graph,
  5. # but this graph is dynamic compared with a static graph in Tensorflow or Theano.
  6. # So torch does not have placeholder, torch can just pass variable to the computational graph.
  7.  
  8. tensor = torch.FloatTensor([[1,2],[3,4]]) # build a tensor
  9. variable = Variable(tensor, requires_grad=True) # build a variable, usually for compute gradients
  10.  
  11. print(tensor) # [torch.FloatTensor of size 2x2]
  12. print(variable) # [torch.FloatTensor of size 2x2]
  13.  
  14. # till now the tensor and variable seem the same.
  15. # However, the variable is a part of the graph, it's a part of the auto-gradient.
  16.  
  17. t_out = torch.mean(tensor*tensor) # x^2
  18. v_out = torch.mean(variable*variable) # x^2
  19. print(t_out)
  20. print(v_out) # 7.5
  21.  
  22. v_out.backward() # backpropagation from v_out
  23. # v_out = 1/4 * sum(variable*variable)
  24. # the gradients w.r.t the variable, d(v_out)/d(variable) = 1/4*2*variable = variable/2
  25. print(variable.grad)
  26. '''
  27. 0.5000 1.0000
  28. 1.5000 2.0000
  29. '''
  30.  
  31. print(variable) # this is data in variable format
  32. """
  33. Variable containing:
  34. 1 2
  35. 3 4
  36. [torch.FloatTensor of size 2x2]
  37. """
  38.  
  39. print(variable.data) # this is data in tensor format
  40. """
  41. 1 2
  42. 3 4
  43. [torch.FloatTensor of size 2x2]
  44. """
  45.  
  46. print(variable.data.numpy()) # numpy format
  47. """
  48. [[ 1. 2.]
  49. [ 3. 4.]]
  50. """

pytorch之 Variable的更多相关文章

  1. Pytorch之Variable求导机制

    自动求导机制是pytorch中非常重要的性质,免去了手动计算导数,为构建模型节省了时间.下面介绍自动求导机制的基本用法. #自动求导机制 import torch from torch.autogra ...

  2. Pytorch Tensor, Variable, 自动求导

    2018.4.25,Facebook 推出了 PyTorch 0.4.0 版本,在该版本及之后的版本中,torch.autograd.Variable 和 torch.Tensor 同属一类.更确切地 ...

  3. PyTorch的Variable已经不需要用了!!!

    转载自:https://blog.csdn.net/rambo_csdn_123/article/details/119056123 Pytorch的torch.autograd.Variable今天 ...

  4. pytorch 2 variable 变量

    import torch from torch.autograd import Variable tensor = torch.FloatTensor([[1, 2], [3, 4]]) variab ...

  5. pytorch: Variable detach 与 detach_

    pytorch 的 Variable 对象中有两个方法,detach和 detach_ 本文主要介绍这两个方法的效果和 能用这两个方法干什么. detach 官方文档中,对这个方法是这么介绍的. 返回 ...

  6. pytorch使用总结

    loss的获取 在看别人代码的时候发现都是 loss=net.loss train_loss+=loss.data[0]#train_loss用于累加梯度 在想为什么不直接使用loss呢,因为pyto ...

  7. PyTorch基础——词向量(Word Vector)技术

    一.介绍 内容 将接触现代 NLP 技术的基础:词向量技术. 第一个是构建一个简单的 N-Gram 语言模型,它可以根据 N 个历史词汇预测下一个单词,从而得到每一个单词的向量表示. 第二个将接触到现 ...

  8. Pytorch1.3源码解析-第一篇

    pytorch$ tree -L 1 . ├── android ├── aten ├── benchmarks ├── binaries ├── c10 ├── caffe2 ├── CITATIO ...

  9. 『PyTorch』第五弹_深入理解autograd_上:Variable属性方法

    在PyTorch中计算图的特点可总结如下: autograd根据用户对variable的操作构建其计算图.对变量的操作抽象为Function. 对于那些不是任何函数(Function)的输出,由用户创 ...

随机推荐

  1. 探究公钥、私钥、对称加密、非对称加密、hash加密、数字签名、数字证书、CA认证、https它们究竟是什么,它们分别解决了通信过程的哪些问题。

    一.准备 1. 角色:小白.美美.小黑. 2. 剧情:小白和美美在谈恋爱:小黑对美美求而不得.心生怨念,所以从中作梗. 3. 需求:小白要与美美需通过网络进行通信,联络感情,所以必须保证通信的安全性. ...

  2. 【UEFI】---记录一次debug过程中的调试经验

    最近在调试一次SMBIOS的动态更新以及I2c设备的配置读取时,遇到了很多问题,特此总结: 1. 第一个是调试一个I2c设备的时候,遇到了一个很奇怪的问题,也由此问题总结了下SMBUS模块的知识,如下 ...

  3. MongoDB 官方文档中的 aggregate 例子当中的 $sum: 1 , 这里的 1 起什么作用?

    按照 group 的条件, 满足一条就加1, db.getCollection('user_login_info').aggregate( [ {$project:{account_id:" ...

  4. 【转】程序员"青春饭"问题之我见

    1. 问题描述问题1: 什么是程序员?在本文中程序员的定义为: 拥有编程技能,在IT.互联网公司打工的IT从业人员.程序员与很多行业最大的不同是该行业的形成时间短:1954年第一台计算机才诞生,而中医 ...

  5. AVR单片机教程——LCD1602

    本文隶属于AVR单片机教程系列.   显示屏 开发板套件里有两块屏幕,大的是LCD(液晶显示),小的是OLED(有机发光二极管).正与你所想的相反,短小精悍的比较贵,而本讲的主题--LCD1602-- ...

  6. Excel.Application class

    https://docs.microsoft.com/en-us/javascript/api/excel/excel.application?view=office-js Represents th ...

  7. 创建dynamics CRM client-side (三) - Execution Context

    Execution Context 在代码执行的时候定义了event  context. 当form或者grid发生event时候传递了execution context. 可以在event hand ...

  8. Python3-ORM-Sqlalchemy

    目录: ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 多对多关系 表结构设计作业 1. ORM介绍 orm英文全称object relational mapping, ...

  9. Web自动化测试项目(四)测试报告

    测试报告生成 使用HTMLTestRunner 生成测试报告 本文使用的 HTMLTestRunner 来源于github: https://github.com/githublitao/HTMLTe ...

  10. 痞子衡嵌入式:嵌入式里堆栈原理及其纯C实现

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是嵌入式里堆栈原理及其纯C实现. 今天给大家分享的这篇还是2016年之前痞子衡写的技术文档,花了点时间重新编排了一下格式.栈这种结构在嵌入式 ...