当前位置:网站首页>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)
边栏推荐
- A label colorful navigation bar
- . Net starts again happy 20th birthday
- 【微服务|SCG】Filters的33种用法
- Binary tree traversal - middle order traversal (golang)
- "C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
- Tucson will lose more than $400million in the next year
- openresty ngx_ Lua variable operation
- 低度酒赛道进入洗牌期,新品牌如何破局三大难题?
- [download white paper] does your customer relationship management (CRM) really "manage" customers?
- Summary and practice of knowledge map construction technology
猜你喜欢

Bumblebee: build, deliver, and run ebpf programs smoothly like silk

Design and implementation of high availability website architecture

【LeetCode】404. Sum of left leaves (2 brushes of wrong questions)

Action News

A label colorful navigation bar

官宣!第三届云原生编程挑战赛正式启动!

Security level

Practical case of SQL optimization: speed up your database

Missile interception -- UPC winter vacation training match

Matrixone 0.2.0 is released, and the fastest SQL computing engine is coming
随机推荐
打破信息茧房-我主动获取信息的方法 -#3
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
RichView TRVStyle MainRVStyle
Avoid material "minefields"! Play with super high conversion rate
Application and Optimization Practice of redis in vivo push platform
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
A label colorful navigation bar
Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
[download white paper] does your customer relationship management (CRM) really "manage" customers?
Summary and practice of knowledge map construction technology
College Students' innovation project management system
Hmi-31- [motion mode] solve the problem of picture display of music module
使用druid連接MySQL數據庫報類型錯誤
Richview trvunits image display units
返回二叉树中两个节点的最低公共祖先
Subject 3 how to turn on the high beam diagram? Is the high beam of section 3 up or down
D3js notes
Openresty ngx Lua Execution stage
Prometheus monitors the correct posture of redis cluster