当前位置:网站首页>Pytoch learning notes -- Summary of common functions of pytoch 1
Pytoch learning notes -- Summary of common functions of pytoch 1
2022-07-25 15:41:00 【whut_ L】
Catalog
2-set() Functions and sorted() function
3-DataLoader() Functions and Dataset class
5- Maximum pooling (max_pool2d) And average pooling (avg_pool2d) function
1-torch.randn() function
import torch
batch_size = 1
seq_len = 3
input_size = 4
inputs = torch.randn(seq_len, batch_size, input_size) torch.randn() The function is used to generate a group with an average of 0, The variance of 1( Standard normal distribution ) The random number . Examples are as follows :
import torch
print(torch.randn(3, 2, 3, 3))
torch.randn(seq_len, batch_size, input_size): The first parameter seq_len Represents sequence length , In the example, the sequence length is 3; The second parameter batch_size Indicates the batch size , The batch size in the example is 2; The third parameter input_size Is the dimension of the input vector , The example is (3, 3).( stay RNN Can be understood as : Example , share 3 A sequence of , Each sequence is divided into 2 batch , The dimension of each batch is 3*3.)
#####################################
#####################################
2-set() Functions and sorted() function
self.country_list = list(sorted(set(self.countries))) # set() duplicate removal , Delete duplicate data ; sorted() Sort set() Function is used to delete duplicate data elements ;sorted() Used for sorting elements , Examples are as follows :
a = ['china', 'china', 'japan']
print(list(set(a)))
print(list(sorted(set(a))))
because ‘c’ < 'j', therefore ‘china’ be ranked at ‘japan’ front .
#####################################
#####################################
3-DataLoader() Functions and Dataset class
from torchvision import datasets
from torch.utils.data import DataLoader, Dataset
batch_size = 64
transform = transforms.Compose([
transforms.ToTensor(), # take shape by (H, W, C) Of img To shape by (C, H, W) Of tensor, Normalize each value to [0,1]
transforms.Normalize((0.1307, ), (0.3081, )) # Data standardization by channel
])
train_dataset = datasets.MNIST(root = '../dataset/mnist/', train = True, download = True, transform = transform)
train_loader = DataLoader(train_dataset, shuffle = True, batch_size = batch_size)
test_dataset = datasets.MNIST(root = '../dataset/mnist/', train = False, download = True, transform = transform)
test_loader = DataLoader(test_dataset, shuffle = False, batch_size = batch_size)DataLoader() The data set imported by the function is Dataset type ,shuffle Indicates whether the data set is disturbed .
#####################################
#####################################
4-.t() function
.t() The delta function is going to Tensor Transposition , Examples are as follows :
import torch
input = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(input)
print(input.t())
#####################################
#####################################
5- Maximum pooling (max_pool2d) And average pooling (avg_pool2d) function
import torch
import torch.nn.functional as F
input = torch.tensor([[[1, 2, 3, 1], [4, 5, 6, 1], [7, 8, 9, 1]]]).unsqueeze(0).float() # unsqueeze(0) In the 0 Add a dimension before the dimension
print(input.size())
output = F.max_pool2d(input, kernel_size = (1, 4))
print(output)
max_pool2d(): Maximum pooling operation . According to the set core size , Select the largest element value . Example , The nuclear size is (1,4), It can be understood as picking out the maximum element value of each line .
It should be noted that :unsqueeze(0) The function of is in the 0 Expand a dimension before the dimension , therefore input Of size by (1, 1, 3, 4).
###
import torch
import torch.nn.functional as F
input = torch.randn(1, 1, 4, 4)
print(input.size())
print(input)
output = F.avg_pool2d(input, kernel_size = (2, 2))
print(output)
avg_pool2d(): Average pooling operations . According to the set core size , Calculate the average value of the elements in the core .
The role of pooling : Dimension reduction ; Suppress noise , Reduce information redundancy ; Improve the scale invariance of the model 、 Rotation does not deform ; Reduce the amount of model calculation ; Prevent over fitting .
边栏推荐
- Leetcode - 225 implements stack with queue
- GAMES101复习:变换
- Pytorch学习笔记-刘二老师RNN高级篇-代码注释及结果
- Brain racking CPU context switching
- LeetCode - 362 敲击计数器(设计)
- The difference between VaR, let and Const
- Flex layout
- window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
- C # fine sorting knowledge points 10 generic (recommended Collection)
- Solve the vender-base.66c6fc1c0b393478adf7.js:6 typeerror: cannot read property 'validate' of undefined problem
猜你喜欢

Games101 review: Transformation

活动回顾|7月6日安远AI x 机器之心系列讲座第2期|麻省理工教授Max Tegmark分享「人类与AI的共生演化 」

MySQL—常用SQL语句整理总结

Solve the vender-base.66c6fc1c0b393478adf7.js:6 typeerror: cannot read property 'validate' of undefined problem

Leetcode - 707 design linked list (Design)

LeetCode - 362 敲击计数器(设计)

JVM - classloader and parental delegation model

Geogle Colab笔记1--运行Geogle云端硬盘上的.py文件

Leetcode - 622 design cycle queue (Design)

带你创建你的第一个C#程序(建议收藏)
随机推荐
ZOJ - 4114 flipping game DP, reasonable state representation
CF750F1-思维dp
CF566A-贪心+字典树
Games101 review: linear algebra
LeetCode - 225 用队列实现栈
理解“平均负载”
为什么PrepareStatement性能更好更安全?
伤透脑筋的CPU 上下文切换
Flex 布局
Matlab randInt, matlab randInt function usage "recommended collection"
Leetcode - 380 o (1) time to insert, delete and get random elements (design hash table + array)
Are you ready to break away from the "involution circle"?
p4552-差分
MySQL—常用SQL语句整理总结
IDEA—点击文件代码与目录自动同步对应
C#精挑整理知识要点12 异常处理(建议收藏)
对this对象的理解
解决vender-base.66c6fc1c0b393478adf7.js:6 TypeError: Cannot read property ‘validate‘ of undefined问题
你准备好脱离“内卷化怪圈”了吗?
Cf566a greed + dictionary tree