【ML笔记】PyTorch Essential(前置知识)

以下是notebook直接转换成markdown,在Google Colab上可以正常执行。
可以在这里直接进入👉https://colab.research.google.com/drive/1oSCI36tAQq9gfTaHmG5PyDwcPzA1P_pl?usp=sharing
内容为pytorch和ML的入门经典内容,各位可以康一康哈,但是主要以笔记为主,因此会有些中英参半

Basic

1
2
3
import torch
ones = torch.ones(size=(3, 4));
ones
1
2
zeros = ones * 0;
zeros
1
torch.arange(1,11)
1
torch.arange(1,101,2)
1
torch.rand(size=(10,10,10))
1
2
one_to_ten = torch.range(1,11);
one_to_ten
1
2
ten_zeros = torch.zeros_like(one_to_ten);
ten_zeros
1
2
3
RAND = torch.rand(size=(5,5));
RAND
RAND.dtype
1
2
3
4
5
6
float_32_tensor = torch.tensor([3.0, 6.0, 9.0],
dtype = None,
device = None,
requires_grad=False)
float_16_tensor = float_32_tensor.type(torch.float16)
float_16_tensor
1
float_16_tensor * float_32_tensor
1
2
3
int_32_tensor = torch.tensor([3, 6, 9],
dtype = torch.int32)
int_32_tensor.dtype
1
2
some_tensor = torch.rand(3, 4)
some_tensor
1
2
3
4
some_tensor.dtype
some_tensor.size()
some_tensor.shape
some_tensor.device
1
some_tensor.dtype
1
some_tensor.size()
1
some_tensor.shape
1
2
tensor = torch.tensor([1, 2, 3])
tensor + 1090
1
tensor *= 10
1
tensor
1

Matrix Multiplication

1
2
3
import torch
test = torch.rand(2,4)
test
1
2
3
4
5
tensor_a = torch.rand(3,4)
tensor_b = torch.rand(3,4)
tensor_b_t = tensor_b.T
tensor_c = torch.matmul(tensor_a, tensor_b_t)
print(tensor_b, '\n', tensor_b_t)
1

Tensor Aggregation

1
2
3
import torch
x = torch.arange(10,101,10)
x
1
2
# max & min
x.max(), torch.max(x), x.min(), torch.min(x)
1
2
3
# mean(平均数)
# 注意数据类型是浮点
torch.mean(x.type(torch.float)), x.type(torch.float).mean()
1
2
# sum
x.sum(), torch.sum(x)
1
x.argmin(), x.argmax()
1
x[x.argmax()], x[x.argmin()]

Reshaping, Stacking, Squeezing and Unsqueezing

1
2
3
import torch
x = torch.arange(1., 28.)
x
1
2
3
4
5
6
# reshape(重新构造tensor)
# 需要元素个数正确,e.g. 1*9=9
# 可能与原张量共享内存
x_reshape = x.reshape(3, 3, 3)
x_reshape[0, 0, 0] = 100
x_reshape, x
1
2
3
4
5
# change view
# view与原张量共享内存
z = x.view(3, 3, 3)
z[0,0,0] = 100
z, x
1
2
3
4
# stack
x = torch.arange(1., 10.)
x_stack = torch.hstack([x, x, x, x])
x_stack
1
2
3
4
5
6
7
8
# squeeze, unsqueeze
# 其中,unsqueeze的第二个穿传参n代表在n与n+1中插入一个维度
x = torch.arange(1., 10.)
a = torch.unsqueeze(x, 0)
x = torch.rand(2, 1, 2, 1, 4)
a = torch.squeeze(x, (-1, -2))
a = torch.unsqueeze(x, 1)
a.shape
1
2
3
4
5
6
7
# permute
# 用于调整
x = torch.tensor([[1, 1, 1],
[2, 2, 2]])
# a = torch.permute(x, (5, 4, 3, 2, 1, 0))
a = torch.permute(x, (1, 0))
a
1

Index

1
2
3
import torch
x = torch.arange(1, 28).reshape(3, 3, 3)
x[:, 0, :]

Reproducibility

1
2
import torch
torch.rand(3, 3)
1
2
3
4
5
6
7
8
# manual_seed只在一个代码块中起作用
torch.manual_seed(2233)
a = torch.rand(3, 3)

torch.manual_seed(2233)
b = torch.rand(3, 3)
print(a, '\n', b)
print(a == b)
1

GPU Accessing & Using

1
2
3
import torch
import numpy as np
torch.cuda.is_available()
1
2
device = "cuda" if torch.cuda.is_available() else "cpu"
torch.cuda.device_count(), device
1
2
a = torch.rand(3, 3)
a = a.to(device)
1
2
# numpy只在CPU上可用
a.cpu().numpy()
1


【ML笔记】PyTorch Essential(前置知识)
https://学习.fun/ml-note/pytorch-essentials/
Author
Stephen Zeng
Posted on
July 31, 2024
Licensed under