当前位置:网站首页>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 .
边栏推荐
- 自定义注解校验API参数电话号
- LeetCode - 359 日志速率限制器 (设计)
- Pytorch学习笔记-刘二老师RNN高级篇-代码注释及结果
- GAMES101复习:线性代数
- Gary marcus: learning a language is more difficult than you think
- MySQL - user and permission control
- PAT甲级题目目录
- MySQL - Summary of common SQL statements
- 2022-07-25日报:微软提出CodeT:代码生成新SOTA,20个点的性能提升
- Take you to create your first C program (recommended Collection)
猜你喜欢

Games101 review: linear algebra

带你创建你的第一个C#程序(建议收藏)

No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac

Idea eye care settings

Use cpolar to build a business website (how to buy a domain name)

Leetcode - 232 realize queue with stack (design double stack to realize queue)

为什么PrepareStatement性能更好更安全?

ML - natural language processing - Basics

LeetCode - 707 设计链表 (设计)

LeetCode - 622 设计循环队列 (设计)
随机推荐
LeetCode - 641 设计循环双端队列(设计)*
使用cpolar建立一个商业网站(如何购买域名)
Deadlock gossip
数据系统分区设计 - 分区与二级索引
See a lot of blinking pictures on apps, especially the member page
The difference between mouseover and mouseenter
对this对象的理解
Leetcode - 622 design cycle queue (Design)
Pat grade a 1152 Google recruitment (20 points)
matlab randint,Matlab的randint函数用法「建议收藏」
User defined annotation verification API parameter phone number
GAMES101复习:三维变换
CF685B-求有根树每颗子树的重心
How to realize page inclusion
HDU3873-有依赖的最短路(拓扑排序)
理解“平均负载”
LeetCode - 362 敲击计数器(设计)
Are you ready to break away from the "involution circle"?
Solve the vender-base.66c6fc1c0b393478adf7.js:6 typeerror: cannot read property 'validate' of undefined problem
ML - natural language processing - Basics