当前位置:网站首页>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 .
边栏推荐
- Solve the vender-base.66c6fc1c0b393478adf7.js:6 typeerror: cannot read property 'validate' of undefined problem
- JVM knowledge brain map sharing
- Leetcode - 303 area and retrieval - array immutable (design prefix and array)
- C#精挑整理知识要点12 异常处理(建议收藏)
- LeetCode - 379 电话目录管理系统(设计)
- IDEA—点击文件代码与目录自动同步对应
- 二进制补码
- 数据系统分区设计 - 分区与二级索引
- 盒子躲避鼠标
- window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
猜你喜欢
随机推荐
SQL cultivation manual from scratch - practical part
带你创建你的第一个C#程序(建议收藏)
我想问下变量配置功能是只能在SQL模式下使用吗
LeetCode - 677 键值映射(设计)*
GAMES101复习:变换
No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
ZOJ - 4114 Flipping Game-dp,合理状态表示
Qtime definition (manual waste utilization is simple and beautiful)
2019浙江省赛C-错排问题,贪心
The difference between VaR, let and Const
LeetCode - 225 用队列实现栈
伤透脑筋的CPU 上下文切换
2021上海市赛-D-卡特兰数变种,dp
GAMES101复习:线性代数
Pytorch学习笔记--SEResNet50搭建
In depth: micro and macro tasks
JS URLEncode function
Cf750f1 thinking DP
分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
JVM知识脑图分享









