PyTorch 的基本使用
1. 环境搭建
安装 python:
PyTorch官网: https://pytorch.org/
pip3 install torch==1.8.1+cpu torchvision==0.9.1+cpu torchaudio===0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
检查是否安装成功
import torch
torch.__version__
在 PyCharm 中添加 torch 和普通添加 numpy 等一样
2. 基础
2.1 Tensors 的含义
Tensors 是一种特殊的数据结构,与 arrays 和 matrices 非常相似。在 PyTorch 中,我们使用张量对模型的输入和输出以及模型的参数进行编码。
上面是官方所解释的意思,可以得出,Tensors 是 PyTorch 运算的基础。
2.2 Tensors 和 Numpy 中的数组的关系
官方也指出,Tensors 和 Numpy 中 ndarrays 非常相似,了解 Numpy 中 ndarrays 可以直接上手 Tensors。
而且 Tensors 和 Numpy 中的数组具有底层内存共享,意味着不需要进行复制直接就可以相互转化。
import torch
import numpy as np
# Tensor
data = [[1, 2], [3, 4]]
x_data = torch.tensor(data)
print(f"Tensor: \n {x_data} \n")
# numpy 数组直接转化为 Tensor
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
print(f"Tensor from numpy: \n {x_np} \n")
一个新的 Tensors 可以从另一个 Tensors 获取,其中新的 Tensors 与原来的 Tensors 中的参数不会改变,除非被明确重写。
明确重写,这里可以理解为 Tensors 被改变了。
# retainsthe properties of x_data
x_ones = torch.ones_like(x_data)
print(f"Ones Tensor: \n {x_ones} \n")
# overrides the datatype of x_data
x_rand = torch.rand_like(x_data, dtype=torch.float)
print(f"Random Tensor: \n {x_rand} \n")
2.3 Tensors 带有随机和常量值以及其属性
Tensors 带有随机和常量值
# 带有随机和不变的值
shape = (2, 3)
# 随机值
rand_tensor = torch.rand(shape)
# 全是 1
ones_tensor = torch.ones(shape)
# 全是 0
zeros_tensor = torch.zeros(shape)
# 对角矩阵
eye_tensor = torch.eye(2, 3)
print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor} \n")
print(f"Eye Tensor: \n {eye_tensor} \n")
Tensors 的属性
# 属性
tensor = torch.rand(3, 4)
print(f"Size of tensor: {tensor.size()}")
print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device} \n")
2.4 Tensors 操作
Tensors 操作有百余种,例如算术、线性代数、矩阵操作(转置、索引、切片)、采样等。 每个操作都可以在 GPU 上运行,其效率高于 CPU。
# We move our tensor to the GPU if available
print(f"Datatype of tensor: {torch.cuda.is_available()} \n")
if torch.cuda.is_available():
tensor = tensor.to('cuda')
2.4.1 Tensors 矩阵操作
Tensors 的索引和切片,与 Numpy 简直一模一样。
# Standard numpy-like indexing and slicing
tensor = torch.ones(4, 4)
# 获取行
print('First row: ', tensor[0])
# 获取列
print('First column: ', tensor[:, 0])
# 获取列
print('Last column:', tensor[..., -1])
# 改变值
tensor[:, 1] = 0
print(tensor)
Tensors 链接
torch.cat() 方法中 dim=1 按行拼接
torch.cat() 方法中 dim=0 按列拼接
# Joiningtensors torch.cat()
horizontal = torch.cat([tensor, tensor, tensor], dim=1)
print(horizontal)
vertical = torch.cat([tensor, tensor, tensor], dim=0)
print(vertical)
torch.stack(tensors, dim=0, *, out=None) → Tensor
所有的 tensors 具有相同的函数。
# Joining tensors torch.stack() 0 <= dim <= len()
tensor = torch.tensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
print(tensor.shape)
output = torch.stack([tensor, tensor], dim=0)
print(output, output.shape)
output = torch.stack([tensor, tensor], dim=1)
print(output, output.shape)
output = torch.stack([tensor, tensor], dim=2)
print(output, output.shape)
2.4.2 Tensors 算术操作
# 输出
def println(*key):
for k in key:
print(k)
乘法操作-矩阵乘法
tensor = torch.ones(3, 3)
# This computes the matrix multiplication between two tensors. y1, y2, y3 will have the same value
y1 = tensor @ tensor.T
y2 = tensor.matmul(tensor.T)
y3 = torch.rand_like(tensor)
println(y1, y2, y3)
torch.matmul(tensor, tensor.T, out=y3)
println(y1, y2, y3)
乘法操作-元素相乘
# This computes the element-wise product. z1, z2 will have the same value
tensor = torch.tensor([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
z1 = tensor * tensor
z2 = tensor.mul(tensor)
z3 = torch.rand_like(tensor, dtype=torch.float)
println(z1, z2, z3)
torch.mul(tensor, tensor, out=z3)
println(z1, z2, z3)
Tensors 总和计算
# 总和
agg = tensor.sum()
print(agg, type(agg))
# Tentor 转化为数值型
agg_item = agg.item()
print(agg_item, type(agg_item))
Tensors 中每个元素加上同一个数值,该操作属于 In-place 操作。
print(tensor, "\n")
tensor.add_(5)
print(tensor)
2.5 Tensors 与 NumPy array 的转化
下面的例子就可以看出内存是共享的。
Tensor 转 NumPy array
# Tensor to NumPy array
t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")
# A change in the tensor reflects in the NumPy array.
t.add_(1)
print(f"t: {t}")
print(f"n: {n}")
NumPy array 转 Tensor
# NumPy array to Tensor
n = np.ones(5)
t = torch.from_numpy(n)
print(f"t: {t}")
print(f"n: {n}")
# Changes in the NumPy array reflects in the tensor.
np.add(n, 1, out=n)
print(f"t: {t}")
print(f"n: {n}")