当前位置:网站首页>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)
边栏推荐
- Design and implementation of high availability website architecture
- Process scheduling and termination
- Abacus mental arithmetic test
- Spoon inserts and updates the Oracle database, and some prompts are inserted with errors. Assertion botch: negative time
- 【LeetCode】106. Construct binary tree from middle order and post order traversal sequence (wrong question 2)
- Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
- Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
- Collection of gmat750 wrong questions
- Exploration of short text analysis in the field of medical and health (I)
- College Students' innovation project management system
猜你喜欢

【LeetCode】111. Minimum depth of binary tree (2 brushes of wrong questions)

Pytest (5) - assertion

Icu4c 70 source code download and compilation (win10, vs2022)

A label colorful navigation bar

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

Character painting, I use characters to draw a Bing Dwen Dwen

Application and Optimization Practice of redis in vivo push platform

Missile interception -- UPC winter vacation training match

如何做一个炫酷的墨水屏电子钟?

Openresty ngx Lua Execution stage
随机推荐
Tucson will lose more than $400million in the next year
Grub 2.12 will be released this year to continue to improve boot security
February database ranking: how long can Oracle remain the first?
Official announcement! The third cloud native programming challenge is officially launched!
Last words record
. Net starts again happy 20th birthday
Introduce reflow & repaint, and how to optimize it?
Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
2022/02/13
STL container
Kotlin - 协程 Coroutine
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
TCP security of network security foundation
Openresty ngx Lua Execution stage
Can you really learn 3DMAX modeling by self-study?
Visual explanation of Newton iteration method
ELFK部署
When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
Binary tree traversal - middle order traversal (golang)