当前位置:网站首页>Pytorch基础——(2)张量(tensor)的数学运算
Pytorch基础——(2)张量(tensor)的数学运算
2022-07-06 03:18:00 【七上八下的黑】
目录
一、基本运算
import torch
x = torch.tensor([2, 2, 2], dtype=torch.float32)
y = torch.tensor([3, 4, 5], dtype=torch.float32)- 加法
out1 = torch.add(x, y)
print(out1)- 减法
out2 = torch.sub(x, y)
print(out2)- 除法
out3 = torch.div(x, y)
print(out3)- 乘法
out4 = torch.mul(x, y)
print(out4)- 矩阵乘法
a = torch.rand((2, 5))
b = torch.rand((5, 3))
out5 = torch.mm(a, b)
print(out5)
print(out5.shape)- 批量矩阵乘法
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)- 指数
out7 = x.pow(3)
print(out7)- 矩阵指数
x = torch.tensor([[1, 1], [1, 1]]) # (2*2)必须行列数相同
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)二、最大值、最小值、均值、绝对值、排序
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)- 最大值、最小值、均值
values, indice = torch.max(x, dim=0) # dim=0表示列,1表示行
print(values) # 最大值的值
print(indice) # 最大值的索引
values, indice = torch.min(x, dim=0) # dim=0表示列,1表示行
print(values) # 最小值的值
print(indice) # 最小值的索引
values, indice = torch.mean(x, dim=0)
print(values)
print(indice)# 找到最大值、最小值的索引
out1 = torch.argmax(x, dim=0)
out2 = torch.argmin(x, dim=0)- 绝对值
out = torch.abs(x)
print(out)- 排序
values, indice = torch.sort(x, dim=1, descending=False) # descending=False表示升序
print(values)
print(indice)三、tensor的索引
- 简单操作
x = torch.tensor([1, 4, 5, 6, 0, 8, 6, 1, 4, 5])
print(x[0]) # 第一个元素
print(x[1: 6]) # 输出第2个到第6个元素
x = torch.randn((3, 10)) # size 3x10
print(x[0]) # 输出第1行的所有数, size 1x10
print(x[0, :]) # size 1x10
print(x[:, 0]) # 输出第1列的所有数, size 10x1
x = torch.randn((4, 10))
rows = [1, 3]
colums = [2, 9]
print(x[rows, colums]) # (2, 3) (4, 10)- 带条件的
x = torch.tensor([1, 4, 5, 6, 0, 8, 6, 1, 4, 5])
print(torch.where(x > 5, x, x/2)) # x>5时输出x,否则输出x/2- 其他
print(x.unique()) # 输出不重复的元素
print(x.numel()) # 输出x中的元素个数边栏推荐
- [ruoyi] ztree custom icon (iconskin attribute)
- 记录一下逆向任务管理器的过程
- 1003 emergency (25 points), "DIJ deformation"
- 如何做好功能测试
- 电机控制反Park变换和反Clarke变换公式推导
- 出现Permission denied的解决办法(750权限谨慎使用)
- Linear regression and logistic regression
- Exness foreign exchange: the governor of the Bank of Canada said that the interest rate hike would be more moderate, and the United States and Canada fell slightly to maintain range volatility
- 这些不太会
- [kubernetes series] learn the exposed application of kubernetes service security
猜你喜欢

【RISC-V】外部中断

【指针训练——八道题】

MySQL advanced notes

JS音乐在线播放插件vsPlayAudio.js

Crazy, thousands of netizens are exploding the company's salary
![Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]](/img/3b/385d19e51340ecd6281df47b39f40c.png)
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]

Getting started with applet cloud development - getting user search content

SAP ALV单元格级别设置颜色

XSS challenges bypass the protection strategy for XSS injection

Web security SQL injection vulnerability (1)
随机推荐
My C language learning records (blue bridge) -- files and file input and output
IPv6 comprehensive experiment
Leetcode problem solving -- 173 Binary search tree iterator
[concept] Web basic concept cognition
3857墨卡托坐标系转换为4326 (WGS84)经纬度坐标
How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
Jenkins basic knowledge ----- detailed explanation of 03pipeline code
深入刨析的指针(题解)
【RISC-V】外部中断
tcpdump: no suitable device found
11. Container with the most water
Handwriting database client
Idea push rejected solution
华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】
Explore pointers and pointer types in depth
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]
适合程序员学习的国外网站推荐
Who is the winner of PTA
Getting started with applet cloud development - getting user search content
Summary of Bible story reading