前请提要
Pytorch学习笔记(一)--Tensor和Variable
一.Pycharm
- 在上次pytorch的学习中是使用文本编辑的方式运行pytorch代码,缺少IDE的许多快捷键,于是本次学习前安装好了pycharm.
- 安装首先在官网下载对应的安装包,随后进行破解(这里网上教程很多,在此不再赘述)
- 进入pycharm页面之后需要导入对应的torch包,如下图打开对应的选项卡,选择add,笔者的torch包没有在anaconda的目录下,所以选择了对应的python3,如果安装了anaconda就选择对应的目录
- pycharm里在在edit->font 中可以调节字体的大小
- 选中代码按下ctrl+/可以注释快速注释代码 ctrl+F5快速运行
二.autograd(自动求导)
1. 简单情况自动求导
- 例一
import torch
from torch.autograd import Variable
x = Variable(torch.Tensor([3]),requires_grad=True)
y = x + 2
z = y ** 2 + 3
print("x=3 z=")
print(z)
z.backward()
print("x=3,z对x求导")
print(x.grad)
运行结果:
tensor([2., 3.], requires_grad=True)
tensor([[0., 0.]])
- 例二
x = Variable(torch.randn(10,20),requires_grad=True)
y = Variable(torch.randn(10, 5),requires_grad=True)
w = Variable(torch.randn(20, 5),requires_grad=True)
out = torch.mean(y - torch.matmul(x,w)) #矩阵乘法A=(m,n) B=(n,k) C=A*B=(m,k)
out.backward()
print(x.grad)
tensor([10.])
tensor([[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116],
[ 0.0207, -0.0222, 0.0032, -0.0017, 0.0777, -0.0293, 0.0058, 0.0518,
0.0615, -0.0786, -0.0213, -0.0042, 0.0795, -0.0344, 0.0648, 0.0586,
-0.0092, -0.0342, 0.0373, 0.1116]])
2. 复杂情况自动求导
- 例三
m = Variable(torch.Tensor([[2,3]]),requires_grad=True)
n = Variable(torch.zeros(1,2))
print("m=",m)
print("n=",n)
n[0,0] = m[0,0] ** 2
n[0,1] = m[0,1] ** 3
print("通过m中的值计算n中的值",n)
n.backward(torch.ones_like(n)) # 将 (w0, w1) 取成 (1, 1)
print(m.grad)
这里遇到了一个小问题,开始自己编写时候少了一个'[]'Variable(torch.Tensor([2,3]),报错too many indices for tensor of dimension 1
m= tensor([[2., 3.]], requires_grad=True)
n= tensor([[0., 0.]])
通过m中的值计算n中的值 tensor([[ 4., 27.]], grad_fn=)
tensor([[ 4., 27.]])
通过自动求导我们得到了梯度是 4 和 27,我们可以验算一下
∂n/∂m0=w0∂n0/∂m0+w1∂n1/∂m0=2m0+0=2×2=4
∂n/∂m1=w0∂n0/∂m1+w1∂n1/∂m1=0+3m21=3×32=27
通过验算我们可以得到相同的结果
3. 多次自动求导
过调用 backward 我们可以进行一次自动求导,如果我们再调用一次 backward,会发现程序报错,没有办法再做一次。这是因为 PyTorch 默认做完一次自动求导之后,计算图就被丢弃了,所以两次自动求导需要手动设置一个东西.
x = Variable(torch.FloatTensor([3]),requires_grad=True)
y = x * 2 + x ** 2 + 3
print(y)
#设置retain_graph来保留计算图
y.backward(retain_graph=True)
print(x.grad)
y.backward()
print(x.grad)
tensor([18.]
tensor([8.])
tensor([16.])
三.dynamic-graph(动态图)
import torch
first_counter = torch.Tensor([0])
second_counter = torch.Tensor([10])
while(first_counter < second_counter)[0]:
first_counter += 2
second_counter += 1
print(first_counter)
print(second_counter)
tensor([20.])
tensor([20.])