torch.stack

news/2024/9/22 23:21:28

看一下stack的直观解释,动词可以简单理解为:把……放成一堆、把……放成一摞。
torch.stack方法用于沿着一个新的维度 join(也可称为cat)一系列的张量(可以是2个张量或者是更多),它会插入一个新的维度,并让张量按照这个新的维度进行张量的cat操作。值得注意的是:张量序列中的张量必须要有相同的shape和dimension。

import torch
ogfW = 50
fW = ogfW // 10 #5
ogfH = 40
fH = ogfH // 10 ##4
print("====>>xs"*8)
xs = torch.linspace(0, ogfW - 1, fW, dtype=torch.float).view(1, fW).expand(fH, fW)
print(torch.linspace(0, ogfW - 1, fW, dtype=torch.float))
print(torch.linspace(0, ogfW - 1, fW, dtype=torch.float).view(1, fW))
print(xs)print("====>>ys"*8)
ys = torch.linspace(0, ogfH - 1, fH, dtype=torch.float).view(fH, 1).expand(fH, fW)
print(torch.linspace(0, ogfH - 1, fH, dtype=torch.float))
print(torch.linspace(0, ogfH - 1, fH, dtype=torch.float).view(fH, 1))
print(ys)
print("====>>frustum"*8)
print("===>>>shape xs=", xs.shape)
print("===>>>shape ys=", ys.shape)
frustum = torch.stack((xs, ys), -1)
print("===>>>shape frustum=", frustum.shape)
print(frustum)
====>>xs====>>xs====>>xs====>>xs====>>xs====>>xs====>>xs====>>xs
tensor([ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000])
tensor([[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000]])
tensor([[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000],[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000],[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000],[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000]])
====>>ys====>>ys====>>ys====>>ys====>>ys====>>ys====>>ys====>>ys
tensor([ 0., 13., 26., 39.])
tensor([[ 0.],[13.],[26.],[39.]])
tensor([[ 0.,  0.,  0.,  0.,  0.],[13., 13., 13., 13., 13.],[26., 26., 26., 26., 26.],[39., 39., 39., 39., 39.]])
====>>frustum====>>frustum====>>frustum====>>frustum====>>frustum====>>frustum====>>frustum====>>frustum
===>>>shape xs= torch.Size([4, 5])
===>>>shape ys= torch.Size([4, 5])
===>>>shape frustum= torch.Size([4, 5, 2])
tensor([[[ 0.0000,  0.0000],[12.2500,  0.0000],[24.5000,  0.0000],[36.7500,  0.0000],[49.0000,  0.0000]],[[ 0.0000, 13.0000],[12.2500, 13.0000],[24.5000, 13.0000],[36.7500, 13.0000],[49.0000, 13.0000]],[[ 0.0000, 26.0000],[12.2500, 26.0000],[24.5000, 26.0000],[36.7500, 26.0000],[49.0000, 26.0000]],[[ 0.0000, 39.0000],[12.2500, 39.0000],[24.5000, 39.0000],[36.7500, 39.0000],[49.0000, 39.0000]]])Process finished with exit code 0

3维

import torch
D = 3
ogfW = 50
fW = ogfW // 10 #5
ogfH = 40
fH = ogfH // 10 ##4
ds = torch.arange(*[-6,-3,1], dtype=torch.float).view(-1, 1, 1).expand(-1, fH, fW)xs = torch.linspace(0, ogfW - 1, fW, dtype=torch.float).view(1, 1, fW).expand(D, fH, fW)
print(torch.linspace(0, ogfW - 1, fW, dtype=torch.float))
print(torch.linspace(0, ogfW - 1, fW, dtype=torch.float).view(1, 1, fW))
print(xs)ys = torch.linspace(0, ogfH - 1, fH, dtype=torch.float).view(1, fH, 1).expand(D, fH, fW)
print("=="*20)
print(torch.linspace(0, ogfH - 1, fH, dtype=torch.float))
print(torch.linspace(0, ogfH - 1, fH, dtype=torch.float).view(1, fH, 1))
print(ys)
print("==>>"*20)
print("===>>>shape xs=", xs.shape)
print("===>>>shape ys=", ys.shape)
frustum = torch.stack((xs, ys, ds), -1)
print("===>>>shape frustum=", frustum.shape)
print(frustum)
tensor([ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000])
tensor([[[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000]]])
tensor([[[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000],[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000],[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000],[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000]],[[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000],[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000],[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000],[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000]],[[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000],[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000],[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000],[ 0.0000, 12.2500, 24.5000, 36.7500, 49.0000]]])
========================================
tensor([ 0., 13., 26., 39.])
tensor([[[ 0.],[13.],[26.],[39.]]])
tensor([[[ 0.,  0.,  0.,  0.,  0.],[13., 13., 13., 13., 13.],[26., 26., 26., 26., 26.],[39., 39., 39., 39., 39.]],[[ 0.,  0.,  0.,  0.,  0.],[13., 13., 13., 13., 13.],[26., 26., 26., 26., 26.],[39., 39., 39., 39., 39.]],[[ 0.,  0.,  0.,  0.,  0.],[13., 13., 13., 13., 13.],[26., 26., 26., 26., 26.],[39., 39., 39., 39., 39.]]])
==>>==>>==>>==>>==>>==>>==>>==>>==>>==>>==>>==>>==>>==>>==>>==>>==>>==>>==>>==>>
===>>>shape xs= torch.Size([3, 4, 5])
===>>>shape ys= torch.Size([3, 4, 5])
===>>>shape frustum= torch.Size([3, 4, 5, 3])
tensor([[[[ 0.0000,  0.0000, -6.0000],[12.2500,  0.0000, -6.0000],[24.5000,  0.0000, -6.0000],[36.7500,  0.0000, -6.0000],[49.0000,  0.0000, -6.0000]],[[ 0.0000, 13.0000, -6.0000],[12.2500, 13.0000, -6.0000],[24.5000, 13.0000, -6.0000],[36.7500, 13.0000, -6.0000],[49.0000, 13.0000, -6.0000]],[[ 0.0000, 26.0000, -6.0000],[12.2500, 26.0000, -6.0000],[24.5000, 26.0000, -6.0000],[36.7500, 26.0000, -6.0000],[49.0000, 26.0000, -6.0000]],[[ 0.0000, 39.0000, -6.0000],[12.2500, 39.0000, -6.0000],[24.5000, 39.0000, -6.0000],[36.7500, 39.0000, -6.0000],[49.0000, 39.0000, -6.0000]]],[[[ 0.0000,  0.0000, -5.0000],[12.2500,  0.0000, -5.0000],[24.5000,  0.0000, -5.0000],[36.7500,  0.0000, -5.0000],[49.0000,  0.0000, -5.0000]],[[ 0.0000, 13.0000, -5.0000],[12.2500, 13.0000, -5.0000],[24.5000, 13.0000, -5.0000],[36.7500, 13.0000, -5.0000],[49.0000, 13.0000, -5.0000]],[[ 0.0000, 26.0000, -5.0000],[12.2500, 26.0000, -5.0000],[24.5000, 26.0000, -5.0000],[36.7500, 26.0000, -5.0000],[49.0000, 26.0000, -5.0000]],[[ 0.0000, 39.0000, -5.0000],[12.2500, 39.0000, -5.0000],[24.5000, 39.0000, -5.0000],[36.7500, 39.0000, -5.0000],[49.0000, 39.0000, -5.0000]]],[[[ 0.0000,  0.0000, -4.0000],[12.2500,  0.0000, -4.0000],[24.5000,  0.0000, -4.0000],[36.7500,  0.0000, -4.0000],[49.0000,  0.0000, -4.0000]],[[ 0.0000, 13.0000, -4.0000],[12.2500, 13.0000, -4.0000],[24.5000, 13.0000, -4.0000],[36.7500, 13.0000, -4.0000],[49.0000, 13.0000, -4.0000]],[[ 0.0000, 26.0000, -4.0000],[12.2500, 26.0000, -4.0000],[24.5000, 26.0000, -4.0000],[36.7500, 26.0000, -4.0000],[49.0000, 26.0000, -4.0000]],[[ 0.0000, 39.0000, -4.0000],[12.2500, 39.0000, -4.0000],[24.5000, 39.0000, -4.0000],[36.7500, 39.0000, -4.0000],[49.0000, 39.0000, -4.0000]]]])Process finished with exit code 0










部分转载于:
https://blog.csdn.net/dongjinkun/article/details/132590205

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/63530.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Java反序列化调用链分析系列 | URLDNS链

URLDNS链 URLDNS链是java通过反序列化发起dns请求的利用链。一般用于测试反序列化漏洞。 该链比较简单,利用链也比较短。 其中入口类为 HashMap,执行类为URLStreamHandler的hashCode()方法。 整个调用链如下: HashMap.readObject() HashMap.putVal() HashMap.hash()URL.hash…

控制请求并发数量:p-limit 源码解读

p-limit 是一个控制请求并发数量的库,他的整体代码不多,思路挺好的,很有学习价值; 举例 当我们同时发起多个请求时,一般是这样做的 Promise.all([requestFn1,requestFn2,requestFn3 ]).then(res =>{})或者 requestFn1() requestFn2() requestFn3()而使用 p-limit 限制并…

程序员职业发展之路思考:工程师的等级阶梯

德雷福斯模型:新手到专家 德雷福斯模型(Dreyfus model)是在 1980 年,Dreyfus 兄弟共同提出的技能习得模型。 它是一个技能习得的阶梯模型,也可以用来考察行业技术能手的分级。该模型由上而下分成:专家、精通者、胜任者、高级新手、新手五个等级,越到上面人数占比越少。新…

2024 人工智能学习内容

第六组思维导图:图形的认识

04. 流程控制

一、流程控制流程控制就是用来控制程序运行中各语句执行顺序的语句。基本的流程结构为:顺序结构,分支结构(或称选择结构),循环结构。顺序结构:程序自上到下执行,中间没有任何判断和跳转; 分支结构:根据条件,选择性的执行某段代码,有 if……else 和 switch……case 两…

CentOS 7 虚拟机连接网络

CentOS 7 虚拟机连接网络 检查网络 ping www.baidu.com切换 root 用户 su查看网卡名 ip addr激活网卡 vim /etc/sysconfig/network-scripts/ifcfg-ens33重启网络 service network restart

execve

目录glibc glibc execve() 执行由 pathname 指定的程序。这会导致当前正在被调用进程运行的程序被一个新程序替换,且该新程序会重新初始化栈、堆,以及(已初始化和未初始化的)数据段。

freeRTOS源码解析4--tasks.c 5

4.2.13 继续任务--vTaskResume 接口:void vTaskResume( TaskHandle_t xTaskToResume )形参1:xTaskToResume ,想要继续的任务handle; 首先是vTaskResume调用的一个内部函数:static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ),用于检查任务是否是挂起…