学习PyTorch:60分钟快速入门
60分钟快速入门1
张量
在Pytorch中,神经网络的输入、输出以及网络的参数等数据,都是使用张量来进行描述。
张量可以在GPU上运算。
import torch
import numpy as np
张量初始化
1. 直接生成张量(通过列表)
data = [[1,2],[3,4]]
x_data = torch.tensor(data)
x_data:
tensor([[1, 2],
[3, 4]])
2. 通过Numpy数组来生成张量
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
x_np:
tensor([[1, 2],
[3, 4]], dtype=torch.int32)
3. 通过已有张量来生成新张量
x_ones = torch.ones_like(x_data)
x_rand = torch.rand_like(x_data,dtype = torch.float) # 0-1
x_rand:
tensor([[0.6000, 0.5929],
[0.4534, 0.3422]])
4. 通过指定数据维度生成张量
shape = (2,3)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(2,3)
rand_tensor, ones_tensor, zeros_tensor:
tensor([[0.7257, 0.3637, 0.5007],
[0.0514, 0.5631, 0.4413]])
tensor([[1., 1., 1.],
[1., 1., 1.]])
tensor([[0., 0., 0.],
[0., 0., 0.]])
张量属性:
tensor = torch.rand(3,4)
1. 张量的维数
tensor.shape
out:
torch.Size([3, 4])
2. 数据类型
tensor.dtype
out:
torch.float32
3. 存储设备(CPU或GPU)
tensor.device
out:
device(type='cpu')
张量运算
将tensor导入GPU
tensor = torch.rand(3,4)
if torch.cuda.is_available():
tensor = tensor.to('cuda')
1. 索引和切片
tensor = torch.ones(3,4)
tensor[:,0] = 0 #将第0列数据赋值为0
tensor:
tensor([[0., 1., 1., 1.],
[0., 1., 1., 1.],
[0., 1., 1., 1.]])
2. 拼接
tensor = torch.ones(3,4)
tensor1 = torch.zeros(3,4)
tensor2 = torch.rand(3,4)
t1 = torch.cat([tensor,tensor1,tensor2], dim=0)
t2 = torch.cat([tensor,tensor1,tensor2], dim=1)
t1, t2:
tensor([[1.0000, 1.0000, 1.0000, 1.0000],
[1.0000, 1.0000, 1.0000, 1.0000],
[1.0000, 1.0000, 1.0000, 1.0000],
[0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000],
[0.6261, 0.4629, 0.6310, 0.1371],
[0.0655, 0.7317, 0.4580, 0.2239],
[0.3346, 0.8897, 0.3560, 0.6030]])
tensor([[1.0000, 1.0000, 1.0000, 1.0000,
0.0000, 0.0000, 0.0000, 0.0000,
0.6261, 0.4629, 0.6310, 0.1371],
[1.0000, 1.0000, 1.0000, 1.0000,
0.0000, 0.0000, 0.0000, 0.0000,
0.0655, 0.7317, 0.4580, 0.2239],
[1.0000, 1.0000, 1.0000, 1.0000,
0.0000, 0.0000, 0.0000, 0.0000,
0.3346, 0.8897, 0.3560, 0.6030]])
3. 元素乘法
tensor = torch.ones(3,4)
tensor1 = torch.zeros(3,4)
tensor2 = torch.rand(3,4)
m_tensor = tensor.mul(tensor2)
m_tensor1 = tensor*tensor2
m_tensor == m_tensor1:
tensor([[0.6261, 0.4629, 0.6310, 0.1371],
[0.0655, 0.7317, 0.4580, 0.2239],
[0.3346, 0.8897, 0.3560, 0.6030]])
4. 矩阵乘法
mm_tensor = tensor.matmul(tensor2.T) # .T为转置,为了满足矩阵乘法要求
mm_tensor1 = tensor @ tensor2.T
mm_tensor == mm_tensor1:
tensor([[1.8571, 1.4792, 2.1833],
[1.8571, 1.4792, 2.1833],
[1.8571, 1.4792, 2.1833]])
5. 自动赋值,方法后以_为后缀
自动赋值可减少内存,但是在求导时会丢失中间过程,导致出现问题。
不鼓励使用自动赋值。
tensor = torch.ones(3,4)
tensor.add_(4)
tensor:
tensor([[5., 5., 5., 5.],
[5., 5., 5., 5.],
[5., 5., 5., 5.]])
Tensor与Numpy转化
互相转化后共用内存,修改其中一个另一个也会随之改变。
1. torch -> ndarray
tch = torch.ones(4)
nar = t.numpy()
张量元素数值+1:
tch.add_(1)
tch:
tensor([2., 2., 2., 2.])
nar:
array([2., 2., 2., 2.], dtype=float32)
nar元素数值+1
np.add(nar,1,out=ndarray)
nar:
array([3., 3., 3., 3.], dtype=float32)
tch:
tensor([3., 3., 3., 3.])
tch与nar数值上相等,只是形式不同。
2. ndarray -> torch
nar = np.ones(4)
tch = torch.from_numpy(nar)
tch:
tensor([1., 1., 1., 1.], dtype=torch.float64)