当前位置:网站首页>Pytoch foundation - (2) mathematical operation of tensor
Pytoch foundation - (2) mathematical operation of tensor
2022-07-06 03:27:00 【Up and down black】
Catalog
Two 、 Maximum 、 minimum value 、 mean value 、 The absolute value 、 Sort
3、 ... and 、tensor The index of
One 、 Basic operation
import torch
x = torch.tensor([2, 2, 2], dtype=torch.float32)
y = torch.tensor([3, 4, 5], dtype=torch.float32)
- Add
out1 = torch.add(x, y)
print(out1)
- Subtraction
out2 = torch.sub(x, y)
print(out2)
- division
out3 = torch.div(x, y)
print(out3)
- Multiplication
out4 = torch.mul(x, y)
print(out4)
- Matrix multiplication
a = torch.rand((2, 5))
b = torch.rand((5, 3))
out5 = torch.mm(a, b)
print(out5)
print(out5.shape)
- Batch matrix multiplication
batch = 16
c1 = 5
c2 = 10
c3 = 20
x1 = torch.rand(batch, c1, c2) # 16*5*10
x2 = torch.rand(batch, c2, c3) # 16*10*20
out6 = torch.bmm(x1, x2) # 16*5*20
print(out6.shape)
- Index
out7 = x.pow(3)
print(out7)
- Matrix index
x = torch.tensor([[1, 1], [1, 1]]) # (2*2) The number of rows and columns must be the same
out8 = x.matrix_power(2)
print(out8) # 2*2
- broadcasting
x1 = torch.rand((1, 3))
x2 = torch.rand((3, 3))
out9 = x1 -x2
print(out9)
Two 、 Maximum 、 minimum value 、 mean value 、 The absolute value 、 Sort
import torch
x = torch.tensor([[-1, 2, 3], [4, 5, 6]], dtype=torch.float32)
y = torch.tensor([[1, 1, 2], [0, 10, 9]], dtype=torch.float32)
- Maximum 、 minimum value 、 mean value
values, indice = torch.max(x, dim=0) # dim=0 The column ,1 Said line
print(values) # Value of maximum value
print(indice) # Index of maximum value
values, indice = torch.min(x, dim=0) # dim=0 The column ,1 Said line
print(values) # The minimum value
print(indice) # Index of minimum value
values, indice = torch.mean(x, dim=0)
print(values)
print(indice)
# Find the maximum 、 Index of minimum value
out1 = torch.argmax(x, dim=0)
out2 = torch.argmin(x, dim=0)
- The absolute value
out = torch.abs(x)
print(out)
- Sort
values, indice = torch.sort(x, dim=1, descending=False) # descending=False Expressing ascending order
print(values)
print(indice)
3、 ... and 、tensor The index of
- Simple operation
x = torch.tensor([1, 4, 5, 6, 0, 8, 6, 1, 4, 5])
print(x[0]) # First element
print(x[1: 6]) # Output No 2 One to the first 6 Elements
x = torch.randn((3, 10)) # size 3x10
print(x[0]) # Output No 1 All numbers of rows , size 1x10
print(x[0, :]) # size 1x10
print(x[:, 0]) # Output No 1 All the numbers in the column , size 10x1
x = torch.randn((4, 10))
rows = [1, 3]
colums = [2, 9]
print(x[rows, colums]) # (2, 3) (4, 10)
- Conditional
x = torch.tensor([1, 4, 5, 6, 0, 8, 6, 1, 4, 5])
print(torch.where(x > 5, x, x/2)) # x>5 Time output x, Otherwise output x/2
- other
print(x.unique()) # Output non repeating elements
print(x.numel()) # Output x The number of elements in
边栏推荐
- Shell pass parameters
- The next industry outlet: NFT digital collection, is it an opportunity or a foam?
- Lua uses require to load the shared library successfully, but the return is Boolean (always true)
- Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
- How to do function test well
- 记录一下逆向任务管理器的过程
- MADDPG的pythorch实现——(1)OpenAI MADDPG环境配置
- ASU & OSU | model based regularized off-line meta reinforcement learning
- Polymorphic day02
- February 14, 2022 Daily: Google long article summarizes the experience of building four generations of TPU
猜你喜欢
IPv6 comprehensive experiment
Buuctf question brushing notes - [geek challenge 2019] easysql 1
Suggestions for new engineer team members
记录一下逆向任务管理器的过程
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
Pointer for in-depth analysis (problem solution)
NR modulation 1
SAP ALV颜色代码对应颜色(整理)
施努卡:3d视觉检测应用行业 机器视觉3d检测
Research on cooperative control of industrial robots
随机推荐
Analyze 菜单分析
施努卡:什么是视觉定位系统 视觉系统如何定位
jsscript
Pelosi: Congress will soon have legislation against members' stock speculation
Getting started with applet cloud development - getting user search content
给新人工程师组员的建议
Selenium share
多项目编程极简用例
Leetcode problem solving -- 173 Binary search tree iterator
Crazy, thousands of netizens are exploding the company's salary
Differences and application scenarios between resulttype and resultmap
【RISC-V】外部中断
Brush questions in summer -day3
Idea push rejected solution
Leetcode problem solving -- 98 Validate binary search tree
Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
下一个行业风口:NFT 数字藏品,是机遇还是泡沫?
An article about liquid template engine
NR modulation 1
Image super-resolution using deep convolutional networks(SRCNN)解读与实现