本文是DEEP LEARNING WITH PYTORCH: A 60 MINUTE BLITZ的學(xué)習(xí)筆記
教程主要分為以下4個部分:
- TENSORS (張量)
- A GENTLE INTRODUCTION TO TORCH.AUTOGRAD (Autograd自動求導(dǎo))
- NEURAL NETWORKS (神經(jīng)網(wǎng)絡(luò))
- TRAINING A CLASSIFIER (訓(xùn)練一個分類器)
下面開始介紹第一部分:
張量(Tensors)是一種特殊的數(shù)據(jù)結(jié)構(gòu)喘漏,類似于數(shù)組(arrays)和矩陣(matrices)芥喇。在PyTorch中安皱,我們使用張量來編碼模型的輸入和輸出,以及模型的參數(shù)确憨。
除Tensors可以在GPU或其他專門的硬件上運行以加速計算外,其他非常類似于NumPy的ndarrays。
import torch
import numpy as np
1. Tensor Initialization
1.1 Directly from data
Tensors can be created directly from data. The data type is automatically inferred.
data = [[1, 2], [3, 4]]
x_data = torch.tensor(data)
1.2 From a NumPy array
Tensors can be created from NumPy arrays.
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
1.3 From another tensor
The new tensor retains the properties (shape, datatype) of the argument tensor, unless explicitly overridden.
x_ones = torch.ones_like(x_data) # retains the properties of x_data
print(f"Ones Tensor: \n {x_ones} \n")
x_rand = torch.rand_like(x_data, dtype=torch.float) # overrides the datatype of x_data
print(f"Random Tensor: \n {x_rand} \n")
我們發(fā)現(xiàn)蜒蕾,默認情況下新變量x_ones與參數(shù)張量x_data的數(shù)據(jù)類型確實相同及汉,除非對參數(shù)dtype進行顯式指定沮趣。
1.4 With random or constant values
shape
is a tuple of tensor dimensions. In the functions below, it determines the dimensionality of the output tensor
shape = (2, 3,)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)
print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor}")
2. Tensor Attributes
Tensor attributes describe their shape, datatype, and the device on which they are stored.
tensor = torch.rand(3, 4)
print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device}")
3. Tensor Operations
Over 100 tensor operations, including transposing, indexing, slicing, mathematical operations, linear algebra, random sampling...
# We move our tensor to the GPU if available
if torch.cuda.is_available():
tensor = tensor.to('cuda')
print(f"Device tensor is stored on: {tensor.device}")
3.1 Standard numpy-like indexing and slicing:
tensor = torch.ones(4, 4)
tensor[:,1] = 0
print(tensor)
3.2 Joining tensors
use torch.cat
to concatenate a sequence of tensors along a given dimension
t1 = torch.cat([tensor, tensor, tensor], dim=1)
print(t1)
我們發(fā)現(xiàn)張量t1確實是3個張量tensor按第1軸即列(參數(shù)dim=1)拼接在一起
3.3 Multiplying tensors
# This computes the element-wise product
print(f"tensor.mul(tensor) \n {tensor.mul(tensor)} \n")
# Alternative syntax:
print(f"tensor * tensor \n {tensor * tensor}")
This computes the matrix multiplication between two tensors:
print(f"tensor.matmul(tensor.T) \n {tensor.matmul(tensor.T)} \n")
# Alternative syntax:
print(f"tensor @ tensor.T \n {tensor @ tensor.T}")
上圖的前者相當(dāng)于常規(guī)的向量化(兩個矩陣之間的對應(yīng)相同位置元素相乘),后者是矩陣乘法坷随,相當(dāng)于矩陣與其轉(zhuǎn)置相乘房铭,在這里即是驻龟。
3.4 In-place operations
Operations that have a _
suffix are in-place. For example: x.copy_(y), x.t_(), will change x.
In-place operations save some memory, but can be problematic when computing derivatives because of an immediate loss of history. Hence, their use is discouraged.
4. Bridge with NumPy
Tensors on the CPU and NumPy arrays can share their underlying memory locations, and changing one will change the other.
4.1 Tensor to NumPy array
t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")
t.add_(1)
print(f"t: {t}")
print(f"n: {n}")
A change in the tensor reflects in the NumPy array.
4.2 NumPy array to Tensor
n = np.ones(5)
t = torch.from_numpy(n)
np.add(n, 1, out=n)
print(f"t: {t}")
print(f"n: {n}")
很明顯,vice versa缸匪。
總結(jié)一下用到的API:
函數(shù):tensor(), from_numpy(), ones_like(), rand_like(), rand(), ones(), zeros(), cuda.is_available(), cat(), stack()
tensor的屬性和方法:shape, dtype, device, T, to(), mul(), matmul(), add_(), numpy(), copy_(), t_()
特殊符號:*, @
參考:
- https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html
- https://pytorchbook.cn/chapter2/2.1.1.pytorch-basics-tensor/
- https://www.youtube.com/watch?v=u7x8RXwLKcA
- https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/3dbbd6931d76adb0dc37d4e88b328852/tensor_tutorial.ipynb