当前位置:网站首页>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)
边栏推荐
- Uniapp navigateto jump failure
- Video display and hiding of imitation tudou.com
- [download white paper] does your customer relationship management (CRM) really "manage" customers?
- [Yu Yue education] National Open University spring 2019 0505-22t basic nursing reference questions
- Vb+access hotel service management system
- [200 opencv routines] 99 Modified alpha mean filter
- Hmi-30- [motion mode] the module on the right side of the instrument starts to write
- Word processing software
- Zabbix
- Design and implementation of kindergarten management system
猜你喜欢
Design and implementation of campus epidemic prevention and control system based on SSM
Go RPC call
The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!
Video display and hiding of imitation tudou.com
Security level
Action News
openresty ngx_lua执行阶段
【LeetCode】110. Balanced binary tree (2 brushes of wrong questions)
Visual studio 2019 set transparent background (fool teaching)
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
随机推荐
Last week's hot review (2.7-2.13)
问题解决:AttributeError: ‘NoneType‘ object has no attribute ‘append‘
TCP security of network security foundation
Three properties that a good homomorphic encryption should satisfy
Timescaledb 2.5.2 release, time series database based on PostgreSQL
GFS分布式文件系统
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
GFS distributed file system
[illumination du destin - 38]: Ghost Valley - chapitre 5 Flying clamp - one of the Warnings: There is a kind of killing called "hold Kill"
Introduce reflow & repaint, and how to optimize it?
Leetcode takes out the least number of magic beans
RichView TRVUnits 图像显示单位
ELK日志分析系统
Visual explanation of Newton iteration method
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
CAM Pytorch
Practice of tdengine in TCL air conditioning energy management platform
Design of KTV intelligent dimming system based on MCU
tuple and point
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience