当前位置:网站首页>记录贴:pytorch学习Part2
记录贴:pytorch学习Part2
2022-08-08 17:33:00 【安联之夜】
记录贴:pytorch学习Part2
一、合并与切割
#cat
a = torch.rand(4,32,8)
b = torch.rand(5,32,8)
torch.cat([a,b],dim=0)#对第一个维度进行合并,其中其他维度大小需要一致
#stack
a = torch.rand(32,8)
b = torch.rand(32,8)
torch.stack([a,b],dim=0)#两组数据进行堆叠,同时增加一个维度
#split
a = torch.rand(32,8)
c = torch.stack([a,a,a,a],dim=0)
aa,bb = c.split([3,1],dim=0)#把c拆成两份
aa,bb = c.split(2,dim=0)
#chunk
aa,bb = c.chunk(2,dim=0)
二、基本运算
a = torch.rand(3,4)
b = torch.rand(3,4)
torch.all(torch.eq(a+b,torch.add(a,b)))
torch.all(torch.eq(a-b,torch.sub(a,b)))
torch.all(torch.eq(a*b,torch.mul(a,b)))
torch.all(torch.eq(a/b,torch.div(a,b)))
#矩阵乘法
a = torch.tensor([[3.,3.],[3.,3.]])
b = torch.ones(2,2)
torch.mm(a,b)
torch.matmul(a,b)
[email protected]
#次方
a = torch.full([2,2],3)
a.pow(2)
aa = a**2
aa.sqrt()#平方
aa.rsqrt()#平方根倒数
a = torch.exp(torch.ones(2,2))
torch.log(a)
torch.log2(a)
a = torch.tensor(3.14)
a.floor()#向下取整
a.ceil()#向上取整
a.trunc()#取整数
a.frac()#取小数
a.round()#四舍五入
grad = torch.rand(2,3)
grad.max()
grad.median()
grad.clamp(0,10)#超过这个范围的赋值为0或10
三、数据统计
#范数
a = torch.full([8],1.)
b = a.view(2,4)
c = a.view(2,2,2)
a.norm(1)
b.norm(1,dim = 1)
a.prod()#a所有元素的乘积
a.argmax()
a.argmin()
a.max(dim=1,keepdim=True)
a = torch.randn(4,10)
a.topk(4,dim=1)#第一个维度里,最大的四个元素,并返回位置
a.kthvalue(8,dim=1) #第第一个维度里,找到第8小的元素,并返回位置
a > 0
torch.gt(a,0)
四、其他筛选操作
cond = torch.tensor([[0.6769,0.7271],[0.8884,0.4163]])
a = torch.tensor([[0.,0.],[0.,0.]])
torch.where(cond>0.5,a,b)#大于0.5返回a的元素,否则返回b
边栏推荐
猜你喜欢

dp, dpi, px knowledge supplement

盘点检索任务中的损失函数

The latest research from PNAS: 81% problem solving rate, neural network Codex opens the door to the world of advanced mathematics

Cuda Anaconda tensorflow 版本对应

spark学习笔记(八)——sparkSQL概述-定义/特点/DataFrame/DataSet

vlan同步—VTP通告

【开源教程2】疯壳·开源编队无人机-硬件资源简介

grpc服务发现&负载均衡

迁移学习(Transfer Learning)的背景、历史

同态加密:正则嵌入、理想格和RLWE问题
随机推荐
新版松鼠as换源操作
XDOJ-统计正整数个数
LeetCode(剑指 Offer)- 21. 调整数组顺序使奇数位于偶数前面
同态加密:正则嵌入、理想格和RLWE问题
LeetCode_二叉树_中等_515.在每个树行中找最大值
【云图说】第252期 初识云速建站服务
PNAS最新研究:81%解题率,神经网络 Codex 推开高等数学世界大门
[Paper Reading] RAL 2022: Receding Moving Object Segmentation in 3D LiDAR Data Using Sparse 4D Convolutions
Debug和Release的区别
L2-021 点赞狂魔 (25 分)
arm交叉编译
数据库分析与优化
Obtain - 64 [chances] : the soldier, subtlety also - 5 - read sun tzu - melee meter
离线安装 Anaconda + TensorFlow
L2-009 抢红包 (25 分)(结构体+自定义排序)
盘点检索任务中的损失函数
L2-011 玩转二叉树 (25 分) (二叉树)
开源一夏 | 疫情期间闲来无事,我自制了一个按钮展示框特效来展示我的博客
dp, dpi, px knowledge supplement
List<String>用空串替换null值,并且都加上单引号,并且转为字符串用,分割