Pytorch
基于Python的科学计算包
- 作为Numpy的替代品,可以使用GPU的强大计算能力
- 提供强大的灵活性和高速的深度学习研究平台
Tensors(张量)
Tensors与Numpy的ndarray类似,但是Tensors可以使用GPU计算
# 创建一个5x5矩阵,未初始化
import torch
x = torch.empty(5,5)
x
tensor([[9.9184e-39, 9.0000e-39, 1.0561e-38, 1.0653e-38, 4.1327e-39],
[8.9082e-39, 9.8265e-39, 9.4592e-39, 1.0561e-38, 9.7347e-39],
[9.1837e-39, 8.4490e-39, 9.5511e-39, 1.0469e-38, 8.7245e-39],
[8.9082e-39, 1.0653e-38, 8.4490e-39, 1.1112e-38, 9.2755e-39],
[9.5511e-39, 9.1837e-39, 1.0561e-38, 9.1837e-39, 9.6429e-39]])
# 创建随机初始化矩阵
x = torch.randn(2, 3)
x
tensor([[ 0.1443, 0.4900, 0.0978],
[-0.6755, 0.1361, -0.1632]])
# 创建0填充矩阵,类型long
x = torch.zeros(2, 3, dtype=torch.long)
x
tensor([[0, 0, 0],
[0, 0, 0]])
# 创建tensor并使用现有数据
x = torch.tensor([5.5, 3, 2])
x
tensor([5.5000, 3.0000, 2.0000])
# 根据现有的张量创建张量,可设置新的值进行覆盖
x = x.new_ones(3, 2, dtype=torch.double)
print(x)
y = torch.rand_like(x, dtype=torch.float)
print(y)
tensor([[1., 1.],
[1., 1.],
[1., 1.]], dtype=torch.float64)
tensor([[0.5680, 0.2010],
[0.9147, 0.4793],
[0.3011, 0.6881]])
# 获取size
x.size()
torch.Size([3, 2])
注:使用size()方法与Numpy的shape属性相同,张量也支持reshape属性,“torch.size返回的是touple类型,所以支持touple操作”
# 加法
y = torch.rand(3,2, dtype=torch.double)
print(x + y)
tensor([[1.8921, 1.0456],
[1.2846, 1.1511],
[1.6023, 1.4341]], dtype=torch.float64)
torch.add(x, y)
tensor([[1.8921, 1.0456],
[1.2846, 1.1511],
[1.6023, 1.4341]], dtype=torch.float64)
res = torch.empty(3, 2, dtype=torch.double)
torch.add(x, y, out=res)
res
tensor([[1.8921, 1.0456],
[1.2846, 1.1511],
[1.6023, 1.4341]], dtype=torch.float64)
y.add_(x)
tensor([[1.8921, 1.0456],
[1.2846, 1.1511],
[1.6023, 1.4341]], dtype=torch.float64)
注:任何以_结尾的操作都会用结果替换变量
x[:, -1]
tensor([1., 1., 1.], dtype=torch.float64)
# view:可以改变张量的维度和大小
x = torch.randn(2, 3)
y = x.view(6)
z = x.view(-1, 6)
print(x.size(), y.size(), z.size())
torch.Size([2, 3]) torch.Size([6]) torch.Size([1, 6])
# .item()
x = torch.randn(1)
print(x)
print(x.item())
tensor([-1.3655])
-1.365473747253418
Numpy转换
a = torch.ones(5)
a
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
b
array([1., 1., 1., 1., 1.], dtype=float32)
a.add_(1)
a
tensor([2., 2., 2., 2., 2.])
b
array([2., 2., 2., 2., 2.], dtype=float32)
# from_numpy
import numpy as np
a = np.ones(4)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
[2. 2. 2. 2.]
tensor([2., 2., 2., 2.], dtype=torch.float64)
cuda张量
- torch.cuda.is_avaliable() 判断设备是否有cuda可用,torch.device将张量移动到设备中
if torch.cuda.is_available():
device = torch.device('cuda') # CUDA设备
y = torch.ones_like(x, device=device) # 从GPU创建张量
x = x.to(device) # 将张量移动到cuda中
z = x + y
print(z)
print(z.to('cpu'), torch.double) # .to 可对变量做更改
tensor([-0.3655], device='cuda:0')
tensor([-0.3655]) torch.float64