PyTorch入门教程-01-张量tensor和autograd

PyTorch最好的入门方法就是阅读官网教程:60min blitz
看完之后,我记录了自己的笔记和最常用的代码,形成这篇教程。我将结合网上的其他资料,总结一下PyTorch的各个模块及功能,介绍大概的知识结构和用法框架。教程的第一部分,介绍关于PyTorch张量和自动计算梯度相关的基础知识。

1. Tensor张量

PyTorch中的张量类似于numpy的ndarrays多维数组。

# Number
t1 = torch.tensor(4.)
t1 = torch.FloatTensor([4])
t1.dtype
# Vector
t2 = torch.tensor([1., 2, 3, 4])
# Matrix
t3 = torch.tensor([[5., 6], 
                   [7, 8], 
                   [9, 10]])
matrix = torch.randn(3, 3) 
matrix.t() #转置矩阵
# 3-dimensional array
t4 = torch.tensor([
    [[11, 12, 13], 
     [13, 14, 15]], 
    [[15, 16, 17], 
     [17, 18, 19.]]])   
t1.shape   

numpy array和torch tensor可以相互转换。

# Convert the numpy array to a torch tensor.
x = np.array([[1, 2], [3, 4.]])
y = torch.from_numpy(x)    
# Convert a torch tensor to a numpy array
z = y.numpy()       

2. 数学运算

# numpy
data = [-1, -2, 1, 2]
np.abs(data)
np.sin(data)
np.mean(data)
# torch
tensor = torch.FloatTensor(data) # 32-bit floating point
torch.abs(tensor)
torch.sin(tensor)
torch.mean(tensor)

矩阵乘法:

# numpy
data = [[1,2], [3,4]]
np.matmul(data, data)
data = np.array(data)
data.dot(data)
# torch
tensor = torch.FloatTensor(data)
torch.mm(tensor, tensor)
torch.dot(torch.Tensor([4, 2]), torch.Tensor([3, 1]))

3. 梯度的自动计算

PyTorch能自动计算 y 的梯度(即微分), 当参数wbrequires_grad 设定为 True

# Create tensors.
x = torch.tensor(3.)
w = torch.tensor(4., requires_grad=True)
b = torch.tensor(5., requires_grad=True)
# Arithmetic operations
y = w * x + b
# Compute derivatives
y.backward()

y 的梯度被保存在相应张量的.grad 属性中。

# Display gradients
print('dy/dx:', x.grad) # None, because x doesn't have requires_grad set to True
print('dy/dw:', w.grad) # tensor(3.)
print('dy/db:', b.grad) # tensor(1.)

除了数值型变量,矩阵也可以计算梯度。

w = torch.randn(2, 3, requires_grad=True)
b = torch.randn(2, requires_grad=True)
def model(x): # inputs size:(5,3)
    return x @ w.t() + b
preds = model(inputs) # preds size: (5,2)
diff = preds- targets
mse_loss = torch.sum(diff * diff) / diff.numel()
mse_loss.backward()
print(w.grad) # 和w的size相同

更多相关资料:
https://blog.csdn.net/qq_41214997/article/details/89300950
https://www.jianshu.com/p/d66319506dd7

https://pytorch.org/docs/stable/tensors.html
https://pytorch.org/docs/master/notes/autograd.html

https://jovian.ai/aakashns/01-pytorch-basics
https://jhui.github.io/2018/02/09/PyTorch-Basic-operations/. https://dev.to/pk500/how-to-perform-basic-matrix-operations-with-pytorch-tensor-2jkk

你可能感兴趣的:(PyTorch入门教程,pytorch,神经网络,python,深度学习,人工智能)