import torch
from torch.autograd import Variable # Variable in torch is to build a computational graph,
# but this graph is dynamic compared with a static graph in Tensorflow or Theano.
# So torch does not have placeholder, torch can just pass variable to the computational graph. tensor = torch.FloatTensor([[1,2],[3,4]]) # build a tensor
variable = Variable(tensor, requires_grad=True) # build a variable, usually for compute gradients print(tensor) # [torch.FloatTensor of size 2x2]
print(variable) # [torch.FloatTensor of size 2x2] # till now the tensor and variable seem the same.
# However, the variable is a part of the graph, it's a part of the auto-gradient. t_out = torch.mean(tensor*tensor) # x^2
v_out = torch.mean(variable*variable) # x^2
print(t_out)
print(v_out) # 7.5 v_out.backward() # backpropagation from v_out
# v_out = 1/4 * sum(variable*variable)
# the gradients w.r.t the variable, d(v_out)/d(variable) = 1/4*2*variable = variable/2
print(variable.grad)
'''
0.5000 1.0000
1.5000 2.0000
''' print(variable) # this is data in variable format
"""
Variable containing:
1 2
3 4
[torch.FloatTensor of size 2x2]
""" print(variable.data) # this is data in tensor format
"""
1 2
3 4
[torch.FloatTensor of size 2x2]
""" print(variable.data.numpy()) # numpy format
"""
[[ 1. 2.]
[ 3. 4.]]
"""

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. 一个由"2020年1月7日 京东出现的重大 Bug 漏洞"引起的思考...

    2020年1月7日,京东由于优惠券设置错误,导致大量产品以0元或者超低价成交,并且发货.网传小家电被薅24万件,损失损失金额高达7000多万.很多网友表示收到货了,在网上晒出到货截图.下面为购买截图: ...

  2. 搭建自己的Online Judge

    前言 很多人对于做题有点厌烦,但是,如果让你出题给别人做那么可能会很有意思.可是,出题只能出在一些别人的OJ上,甚至只能在自己的Word文档里出.今天我教大家一个厉害点的,叫做搭建自己的Online ...

  3. 【转】vue1.0与2.0的一些区别

    一.生命周期钩子的差别 vue1.0的生命周期如下: vue2.0的生命周期如下: 用一张表格来做对比: 二.代码片段 在vue1.0中可以在template编写时出现: <template&g ...

  4. typescript 第一弹

    typescript官网: http://typescriptlang.org typescript 在线运行环境: http://www.typescriptlang.org/play/index. ...

  5. Qt使用QAxObject快速批量读取Excel内容

    网上各种教程用的方法主要是如下这一句: QAxObject * range = worksheet->querySubObject("Cells(int,int)", 1, ...

  6. 分享一下我在mysql5.6+mysql8数据库安装过程中的一些坑!

    Mysql5.6安装 下载好安装包后,在bin目录下用cmd打开,输入mysqld install [服务名]新建个服务 在windows+r输入services.msc即可查看服务 怎样使用mysq ...

  7. Centos与Ubuntu

    共同点 1.两个系统都分别有桌面系统与服务器系统,不过ubuntu的桌面从外观上来看要比centos的漂亮 不同点 1.centos中新建的普通用户是没有sudo权限的,如果想让普通用户拥有sudo权 ...

  8. linux下误清用户/home下的文件怎么办?

    2016-08-19 10:38:10   有时候我们不小心把home目录下的用户目录删除了,出现上图情况,每行开头直接变成-bash-3.2$这种形式而不是[lyp@centos7 ~]$这种,这时 ...

  9. markdown时序图语法

    语法 - 代表实线 , 主动发送消息,比如 request请求 > 代表实心箭头 , 同步消息,比如 AJAX 的同步请求 -- 代表虚线,表示返回消息,spring Controller re ...

  10. Java并发专栏

    1. Java并发 2. 守护线程与非守护线程 3. 为什么启动线程用start()而不用run()? 4. Java线程join方法总结 5. 生产者与消费者 6. wait.notify/noti ...