文章目录
前言
pytorch
用了一段时间,后来又不用了,所以重新捡起pytorch
再学一遍。
本次的资料是来自官方教程的A 60 Minute Blitz,最基础的资料,本文是基于该资料的一些总结和概括。
Tensor
Tensor
在 pytorch可以看成是numpy.array
的扩展,并且tensor
可以在GPU
上运行使用。
1.创建方法
torch.tensor()
: 接受list, np.arraytorch.*_like
: 如torch.zero_like(x)
,和x同形[tensor].new_[*]
: 如x.new_ones()
,和x同dtype
和device
2.大小
print(x.size())
3.运算
- 直接使用运算符:
+
torch.add(x,y, out = result)
: 可以指定返回的tensor
,但是它必须已经被创建。[tensor].add(y)
: 不改变tensor
的值,返回和。[tensor].add_(y)
: 将改变tensor
的值,tensor
值变为和。以_
结尾的改变x
的函数是in-place的。
4.切片
tensor
和numpy.array
和list
拥有相同的切片规则。
5.resize
使用view
函数,类似numpy
的reshape
。
x = torch.randn(5,4)
print(x.view(-1, 5))
6.tensor和array的关系
The Torch Tensor and NumPy array will share their underlying memory locations (if the Torch Tensor is on CPU), and changing one will change the other.
tensor和array 共享内存(如果tensor在cpu上),所以 改变一个影响另一个。
- tensor -> array:
使用numpy()
函数
x = torch.randn(5,4)
a = x.numpy()
print(a)
a += np.random.randn(5,4)
print(x) #这里x的值改变了
- array -> tensor:
使用from_numpy()
函数
import numpy as np
a = np.array([3])
b = torch.from_numpy(a)
a = np.add(a, 1, out = a)
print(a)
print(b)
All the Tensors on the CPU except a CharTensor support converting to NumPy and back.
除CharTensor的其他CPU上的tensor都支持array互转。
7.x.item()
如果x
是一个数字的tensor,x.item()
可以得到一个python
的数字。
8.Tensor on CUDA
- 在创建函数中指定
device=torch.device("cuda")
直接创建CUDA上的Tensor x = x.to(device=torch.device("cude"))
使用to
函数,注意不是in-place
操作
9.其他操作
torch
还包含很多其他的Tensor
的操作。
如:
squeeze()
去掉大小为1的维度unsqueeze()
在指定维度之后增加一个维度,大小为1transpose(dim1, dim2)
两个维度互换
AutoGrad 自动求导机制
自动求导机制是pytorch
的核心模块。
AutoGrad in Tensor
Tensor
(如x
)在创建时指定requires_grad=True
,会记录之后对它的计算。当完成从x
到另一个Tensor
(如z
)的计算时,我们调用z.backward()
方法,torch
会沿着所有x
到z
的计算路径,通过反向传播(链式求导法则)计算这之间所有requires_grad=True
的Tensor
的导数,并存储在x.grad
变量里面。
注意:
- 得到的导数
x.grad
的大小和x
相同。 - 调用
.backward()
方法时,参数为空时Tensor
必须是标量(size=(1)),一般是loss
。当Tensor
是向量时,必须传入一个和Tensor
大小相同的gradient
(相当于链式求导的中间)。
x = torch.randn([