pytorch入门
张量
Tensor(张量)类似于NumPy的ndarray,但还可以在GPU上使用来加速计算。
#Tensor类似于Numpy的ndarry
from __future__ import print_function
import torch
x = torch.empty(5, 3)
print(x)
tensor([[9.6429e-39, 8.4490e-39, 9.6429e-39],
[9.2755e-39, 1.0286e-38, 9.0919e-39],
[8.9082e-39, 9.2755e-39, 8.4490e-39],
[1.0194e-38, 9.0919e-39, 8.4490e-39],
[8.9082e-39, 9.8265e-39, 1.0102e-38]])
#创建一个随机初始化矩阵
x = torch.rand(5, 3)
print(x)
tensor([[0.8368, 0.1550, 0.4556],
[0.9362, 0.9787, 0.8330],
[0.1501, 0.1925, 0.8110],
[0.4146, 0.4164, 0.5682],
[0.0263, 0.0798, 0.9334]])
#构造一个填满0且数据类型为long的矩阵
x = torch.zeros(5, 3, dtype = torch.long)
print(x)
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
#直接从数据构造张量
x = torch.tensor([5.5, 3])
print(x)
tensor([5.5000, 3.0000])
#根据已有的tensor建立新的tensor,除非用户提供新的值,否则这些方法将重用输入张量的属性,例如dtype等:
x = x.new_ones(5, 3, dtype = torch.double)
print(x)
x = torch.randn_like(x) #重载dtype
print(x)
x = torch.randn_like(x, dtype = torch.float) #结果size一致
print(x)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[ 0.0117, 0.2046, 0.8263],
[-1.2948, 0.9051, -0.5022],
[-0.3790, -0.9440, -0.3733],
[ 0.1626, 0.2243, -0.0229],
[ 0.0166, -0.4217, 0.8421]], dtype=torch.float64)
tensor([[ 0.0382, -1.0561, 1.0479],
[-1.4501, 0.4194, 0.0094],
[-0.1582, 1.1844, 1.0386],
[-0.0085, -1.6122, -0.4958],
[-1.4283, 0.7018, -0.7364]])
print(x.size())
torch.Size([5, 3])
注意:
torch本质上还是tuple,所以支持tuple的一切操作。
运算
主要介绍张量加法的四种方法。
#张量的四种加法
## 1.
y = torch.rand(5, 3)
print(x + y)
tensor([[ 0.7636, -0.7149, 1.1278],
[-1.0066, 0.8335, 0.9429],
[ 0.6623, 2.0257, 1.1513],
[ 0.9628, -0.7707, -0.3799],
[-0.8568, 1.0595, -0.6945]])
## 2.
print(torch.add(x, y))
tensor([[ 0.7636, -0.7149, 1.1278],
[-1.0066, 0.8335, 0.9429],
[ 0.6623, 2.0257, 1.1513],
[ 0.9628, -0.7707, -0.3799],
[-0.8568, 1.0595, -0.6945]])
## 3.
#result = torch.empty(5, 3)
result = torch.empty(6, 4)
torch.add(x, y, out = result) #将输出结果赋给某个参数
print(result)
tensor([[ 0.7636, -0.7149, 1.1278],
[-1.0066, 0.8335, 0.9429],
[ 0.6623, 2.0257, 1.1513],
[ 0.9628, -0.7707, -0.3799],
[-0.8568, 1.0595, -0.6945]])
## 4.
y.add_(x)
print(y)
tensor([[ 0.7636, -0.7149, 1.1278],
[-1.0066, 0.8335, 0.9429],
[ 0.6623, 2.0257, 1.1513],
[ 0.9628, -0.7707, -0.3799],
[-0.8568, 1.0595, -0.6945]])
注意:任何一个in-place改变张量的操作后面都固定一个_。例如x.copy_(y)、x.t_()将更改x
print(x)
print(x[:,1]) #索引操作
tensor([[ 0.0382, -1.0561, 1.0479],
[-1.4501, 0.4194, 0.0094],
[-0.1582, 1.1844, 1.0386],
[-0.0085, -1.6122, -0.4958],
[-1.4283, 0.7018, -0.7364]])
tensor([-1.0561, 0.4194, 1.1844, -1.6122, 0.7018])
#利用torch.view():变换为指定维度
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # 某一维度为-1说明是由其他维度决定
print(x.size(), y.size(), z.size())
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
x = torch.randn(1)
print(x)
print(x.item())
x = torch.randn(5)
print(x)
#print(x.item()) #打印某个元素的value
print(x[0].item())
tensor([-0.6409])
-0.640923261642456
tensor([-0.2946, 0.0541, 0.3561, 1.1394, 0.1953])
-0.2946498692035675
桥接NumPy
将一个Torch张量转换为一个NumPy数组是轻而易举的事情,反之亦然。
Torch张量和NumPy数组将共享它们的底层内存位置,因此当一个改变时,另外也会改变。
将torch的Tensor转化为NumPy数组
a = torch.ones(5)
print(a)
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
print(b)
[1. 1. 1. 1. 1.]
#观察Torch是如何自动改变Numpy数组的值的
a.add_(1)
print(a)
print(b)
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
将NumPy数组转化为Torch张量
#观察NumPy数组是如何自动改变Torch张量的
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out = a)
print(a)
print(b)
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
if torch.cuda.is_available():
device = torch.device("cuda")
y = torch.ones_like(x, device = device)
x = x.to(device)
z = x + y
print(z)
print(z.to("cpu", torch.double))
tensor([0.7054, 1.0541, 1.3561, 2.1394, 1.1953], device='cuda:0')
tensor([0.7054, 1.0541, 1.3561, 2.1394, 1.1953], dtype=torch.float64)
测试一下CPU和GPU的速度差异
from time import perf_counter
X = torch.rand(1000, 10000)
Y = torch.rand(10000, 10000)
start = perf_counter()
X.mm(Y)
end = perf_counter()
time_1 = end - start
print(type(time_1))
print("CPU运算时间:%s" %time_1)
<class 'float'>
CPU运算时间:0.9247130999999627
X = X.cuda()
Y = Y.cuda()
start = perf_counter()
X.mm(Y)
end = perf_counter()
time_2 = end - start
print("GPU运算时间:%s" %time_2)
print("GPU比CPU运算时间快%s倍" %str(time_1/time_2))
GPU运算时间:0.0004142999999885433
GPU比CPU运算时间快2061.7195269699837倍
// A code block
你好