当前位置:网站首页>2021 Li Hongyi machine learning (2): pytorch
2021 Li Hongyi machine learning (2): pytorch
2022-07-05 02:38:00 【Three ears 01】
2021 Li hongyi machine learning (2):PyTorch
1 Basic content
1.1 produce tensor
import torch
import numpy as np
x = torch.tensor([[1, -1], [-1, 1]])
y = torch.from_numpy(np.array([[1, -1], [-1, 1]]))
x, y
(tensor([[ 1, -1],
[-1, 1]]),
tensor([[ 1, -1],
[-1, 1]], dtype=torch.int32))
1.2 squeeze Compress dimensions
x = torch.zeros([1, 2, 3])
y = x.squeeze(0)
x, y, x.shape, y.shape
(tensor([[[0., 0., 0.],
[0., 0., 0.]]]),
tensor([[0., 0., 0.],
[0., 0., 0.]]),
torch.Size([1, 2, 3]),
torch.Size([2, 3]))
1.3 unsqueeze Exhibition dimension
x = torch.zeros([2, 3])
y = x.unsqueeze(1) # dim = 1
z = x.unsqueeze(2) # dim = 2
x, y, z, x.shape, y.shape, z.shape
(tensor([[0., 0., 0.],
[0., 0., 0.]]),
tensor([[[0., 0., 0.]],
[[0., 0., 0.]]]),
tensor([[[0.],
[0.],
[0.]],
[[0.],
[0.],
[0.]]]),
torch.Size([2, 3]),
torch.Size([2, 1, 3]),
torch.Size([2, 3, 1]))
1.4 transpose Transposition
x = torch.zeros([2, 3])
y = x.transpose(0, 1)
x.shape, y.shape
(torch.Size([2, 3]), torch.Size([3, 2]))
1.5 cat Appoint dimension Connect multiple tensor
x = torch.zeros(2,1,3)
y = torch.zeros(2,3,3)
z = torch.zeros(2,2,3)
w = torch.cat([x, y, z], dim=1)
w.shape
torch.Size([2, 6, 3])
1.6 Calculate the gradient

2 neural network

2.1 Read data


The two are inclusive relations .
2.2 torch.nn
layer = torch.nn.Linear(32, 64)
layer.weight.shape, layer.bias.shape
(torch.Size([64, 32]), torch.Size([64]))
nn.Sigmoid()
nn.ReLU()
nn.MSELoss() # Mostly used for linear regression
nn.CrossEntropyLoss() # It's mostly used for classification

2.3 torch.optim
SGD:
torch.optin.sGD(params, lr , momentum = 0)
3 The whole process
3.1 training


3.2 validation

3.3 testing

4 Download and load
4.1 Save
torch.save( model.state_dict(), path)
4.2 Load
ckpt = torch.load(path)
model.load_state_dict(ckpt)
边栏推荐
- 使用druid连接MySQL数据库报类型错误
- Richview trvunits image display units
- "C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
- Learn game model 3D characters, come out to find a job?
- Go RPC call
- Design of kindergarten real-time monitoring and control system
- ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
- Application and Optimization Practice of redis in vivo push platform
- Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
- Icu4c 70 source code download and compilation (win10, vs2022)
猜你喜欢

【附源码】基于知识图谱的智能推荐系统-Sylvie小兔

Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘

Summary and practice of knowledge map construction technology

Naacl 2021 | contrastive learning sweeping text clustering task

Asynchronous and promise

Design and implementation of campus epidemic prevention and control system based on SSM

Yuan universe also "real estate"? Multiple second-hand trading websites block metauniverse keywords

Introduce reflow & repaint, and how to optimize it?

Grub 2.12 will be released this year to continue to improve boot security

8. Commodity management - commodity classification
随机推荐
[Yu Yue education] National Open University spring 2019 0505-22t basic nursing reference questions
Learn game model 3D characters, come out to find a job?
Process scheduling and termination
Design and implementation of high availability website architecture
The database and recharge are gone
Application and Optimization Practice of redis in vivo push platform
【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)
openresty ngx_ Lua variable operation
Openresty ngx Lua Execution stage
tuple and point
Go RPC call
When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
Timescaledb 2.5.2 release, time series database based on PostgreSQL
官宣!第三届云原生编程挑战赛正式启动!
ELFK部署
Yolov5 model training and detection
【LeetCode】222. The number of nodes of a complete binary tree (2 mistakes)
【LeetCode】110. Balanced binary tree (2 brushes of wrong questions)
如何做一个炫酷的墨水屏电子钟?
Traditional chips and AI chips