当前位置:网站首页>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)
边栏推荐
- Leetcode takes out the least number of magic beans
- Good documentation
- Moco V2 literature research [self supervised learning]
- College Students' innovation project management system
- Practice of tdengine in TCL air conditioning energy management platform
- The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
- Timescaledb 2.5.2 release, time series database based on PostgreSQL
- A label colorful navigation bar
- [understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
- Tucson will lose more than $400million in the next year
猜你喜欢
Spark SQL learning bullet 2
Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Design of kindergarten real-time monitoring and control system
Prometheus monitors the correct posture of redis cluster
A label making navigation bar
Character painting, I use characters to draw a Bing Dwen Dwen
Exploration of short text analysis in the field of medical and health (I)
Marubeni Baidu applet detailed configuration tutorial, approved.
TCP security of network security foundation
随机推荐
openresty ngx_lua執行階段
【LeetCode】501. Mode in binary search tree (2 wrong questions)
Asp+access campus network goods trading platform
openresty ngx_lua执行阶段
"C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Rabbit MQ message sending of vertx
. Net starts again happy 20th birthday
Talk about the things that must be paid attention to when interviewing programmers
Icu4c 70 source code download and compilation (win10, vs2022)
使用druid连接MySQL数据库报类型错误
Talk about the things that must be paid attention to when interviewing programmers
Pytest (5) - assertion
【LeetCode】110. Balanced binary tree (2 brushes of wrong questions)
The MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!
The most powerful new household god card of Bank of communications. Apply to earn 2100 yuan. Hurry up if you haven't applied!
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
Action News
The phenomenology of crypto world: Pioneer entropy
Design of kindergarten real-time monitoring and control system