当前位置:网站首页>Torch learning summary
Torch learning summary
2022-06-30 09:37:00 【A grain of sand in the vast sea of people】
Catalog
4. torch.squeeze And torch.unsqueeze
1. torch.clamp()
torch.clamp(input, min, max, out=None) → Tensor
Enter input The tensor of each element is clamped to the interval [min,max][min,max], And return the result to a new tensor .
The operation is defined as follows :
| min, if x_i < min
y_i = | x_i, if min <= x_i <= max
| max, if x_i > max
Example code
import torch
a=torch.randint(low=0,high=10,size=(10,1))
print(a)
a=torch.clamp(a,3,6)
print(a)Running results
tensor([[9],
[0],
[4],
[7],
[8],
[0],
[7],
[9],
[2],
[4]])
tensor([[6],
[3],
[4],
[6],
[6],
[3],
[6],
[6],
[3],
[4]])2. torch.gather()
effect : Collect the value of the specified position of the specific dimension entered
Example code
import torch
input = [
[2, 3, 4, 5, 0, 0],
[1, 4, 3, 0, 0, 0],
[4, 2, 2, 5, 7, 0],
[1, 0, 0, 0, 0, 0]
]
input = torch.tensor(input)
# Be careful index The type of
length = torch.LongTensor([[4],[3],[5],[1]])
#index The reason is that 1, Because the sequence dimension is from 0 Start calculated
out = torch.gather(input, 1, length-1)
prin(out)
Running results
tensor([[5],
[3],
[7],
[1]])3. torch.bmm
Calculate two tensor Matrix multiplication of ,torch.bmm(a,b),tensor a Of size by (b,h,w),tensor b Of size by (b,w,h), Pay attention to two tensor The dimension of must be 3.
Example code
import torch
c = cc=torch.randn((2,2,5))
print(c)
d = torch.reshape(cc,(2,5,2))
print(d)
e = torch.bmm(c, d)
print(e)Running results
tensor([[[-1.9362, 0.9502, 0.4412, 0.2745, -1.0118],
[ 0.0709, 1.4860, 0.9381, -0.6637, 1.0663]],
[[-0.8230, -0.6033, 0.4908, 0.6017, 0.7760],
[ 0.7727, -0.4746, -0.3565, -0.5635, 1.3207]]])
tensor([[[-1.9362, 0.9502],
[ 0.4412, 0.2745],
[-1.0118, 0.0709],
[ 1.4860, 0.9381],
[-0.6637, 1.0663]],
[[-0.8230, -0.6033],
[ 0.4908, 0.6017],
[ 0.7760, 0.7727],
[-0.4746, -0.3565],
[-0.5635, 1.3207]]])
tensor([[[ 4.8012, -2.3691],
[-2.1249, 1.0561]],
[[ 0.0393, 1.3231],
[-1.6222, 0.9178]]])
Process finished with exit code 0
4. torch.squeeze And torch.unsqueeze
torch.squeeze() This function mainly compresses the dimensions of data , Remove the dimension as 1 Dimension of , For example, a row or a column , One row, three columns (1,3) After removing the first dimension of one, the number becomes (3) That's ok .squeeze(a) Will be a All of them are 1 Delete the dimension of . Not for 1 The dimension of has no effect .a.squeeze(N) Just get rid of a The dimension specified in is one . Another form is b=torch.squeeze(a,N) a Remove the dimension whose specified fixed dimension is one .
torch.unsqueeze() This function is mainly used to expand the data dimension . Add a dimension of one to the specified position , For example, there was originally three lines of data (3), stay 0 When you add one dimension to the position of, it becomes a row and three columns (1,3).a.squeeze(N) Is in the a In N Add a dimension of 1 Dimensions . Another form is b=torch.squeeze(a,N) a Is in the a In N Add a dimension of 1 Dimensions
Example code
import torch
x = torch.zeros(3,2,4,1,2,1)# dimension of 3*2*4*1*2
print(x.size()) # torch.Size([3, 2, 4, 1, 2, 1])
print(x.shape)
y = torch.squeeze(x) # Returns a tensor with all the dimensions of input of size 1 removed.
print(y.size()) # torch.Size([3, 2, 4, 2])
print(y.shape)
z = torch.unsqueeze(y,dim=0)# Add a dimension of 1 in the 0th position
print(z.size()) # torch.Size([1, 3, 2, 4, 2])
print(z.shape)
z = torch.unsqueeze(y,dim=1)# Add a dimension of 1 in the 1st position
print(z.size()) # torch.Size([3, 1, 2, 4, 2])
print(z.shape)
z = torch.unsqueeze(y,dim=2)# Add a dimension of 1 in the 2nd position
print(z.size()) # torch.Size([3, 2, 1, 4, 2])
print(z.shape)
y = torch.squeeze(x,dim=0) # remove the 0th position of 1 (no 1)
print('dim=0', y.size()) # torch.Size([3, 2, 4, 1, 2, 1])
print('dim=0', y.shape)
y = torch.squeeze(x, dim=1) # remove the 1st position of 1 (no 1)
print('dim=1', y.size()) # torch.Size([3, 2, 4, 1, 2, 1])
print('dim=1', y.shape)
y = torch.squeeze(x, dim=2) # remove the 2nd position of 1 (no 1)
print('dim=2', y.size()) # torch.Size([3, 2, 4, 1, 2])
print('dim=2', y.shape)
y = torch.squeeze(x, dim=3) # remove the 3rd position of 1 (yes)
print('dim=3', y.size()) # torch.Size([3, 2, 4, 2])
print('dim=3', y.shape)
y = torch.squeeze(x, dim=4) # remove the 4th position of 1 (no 1)
print('dim=4', y.size()) # torch.Size([3, 2, 4, 1, 2, 1])
print('dim=4', y.shape)
y = torch.squeeze(x, dim=5) # remove the 5th position of 1 (yes)
print('dim=5', y.size()) # torch.Size([3, 2, 4, 1, 2])
print('dim=5', y.shape)
y = torch.squeeze(x, dim=6) # RuntimeError: Dimension out of range (expected to be in range of [-6, 5], but got 6)
print('dim=6', y.size())
print('dim=6', y.shape)
5. torch.expand
return tensor A new view of , A single dimension expands to a larger size . tensor It can also be expanded to higher dimensions , New dimensions will be attached to the front . expand tensor There is no need to allocate new memory , Just create a new one tensor The view of , Among them, through stride Set to 0, One dimension will be extended to higher dimensions . Any one-dimensional can be extended to any number without allocating new memory .
Example code
import torch
x = torch.Tensor([[1], [2], [3]])
print(x.size())
y = x.expand(3, 4)
print(x.size())
print(y.size())
print(x)
print(y)result
torch.Size([3, 1])
torch.Size([3, 1])
torch.Size([3, 4])
tensor([[1.],
[2.],
[3.]])
tensor([[1., 1., 1., 1.],
[2., 2., 2., 2.],
[3., 3., 3., 3.]])边栏推荐
- Handwriting sorter component
- Mysq database remote connection error, remote connection is not allowed
- Tablet PC based ink handwriting recognition input method
- JVM family
- Tutorial for beginners of small programs day01
- So the toolbar can still be used like this? The toolbar uses the most complete parsing. Netizen: finally, you don't have to always customize the title bar!
- ACM intensive training graph theory exercise 3 in the summer vacation of 2020 [problem solving]
- 单片机 MCU 固件打包脚本软件
- Distributed ID
- Flutter 0001, environment configuration
猜你喜欢

Handwriting sorter component

Tutorial for beginners of small programs day01

单片机 MCU 固件打包脚本软件

So the toolbar can still be used like this? The toolbar uses the most complete parsing. Netizen: finally, you don't have to always customize the title bar!

Idea setting automatic package Guide

Summary of Android knowledge points and common interview questions

5. Messager framework and imessager interface

JVM tuning tool commands (notes)

Express の post request

Numpy (time date and time increment)
随机推荐
Pit encountered by fastjason
ABAP-时间函数
So the toolbar can still be used like this? The toolbar uses the most complete parsing. Netizen: finally, you don't have to always customize the title bar!
Cronexpression expression explanation and cases
utils session&rpc
MySQL knowledge summary (useful for thieves)
OCX child thread cannot trigger event event (forward)
QR code generation and analysis
Create thread pool demo
Generate directory in markdown
Enum demo
八大排序(二)
小程序手持弹幕的原理及实现(uni-app)
float
(zero) most complete JVM knowledge points
JVM tuning tool commands (notes)
Function simplification principle: save if you can
Splice and slice functions of JS
11.自定义hooks
Express の Hello World