torch.stack

看一下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)
print("===>>>ds" * 5)
print(torch.arange(*[-6,-3,1], dtype=torch.float))
print(torch.arange(*[-6,-3,1], dtype=torch.float).view(-1, 1, 1))
print(ds)

print("===>>>xs" * 5)
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("===>>>ys" * 5)
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 ds=", ds.shape)
print("===>>>shape xs=", xs.shape)
print("===>>>shape ys=", ys.shape)
frustum = torch.stack((xs, ys, ds), -1)
print("===>>>shape frustum=", frustum.shape)
print(frustum)

===>>>ds===>>>ds===>>>ds===>>>ds===>>>ds
tensor([-6., -5., -4.])
tensor([[[-6.]],

        [[-5.]],

        [[-4.]]])
tensor([[[-6., -6., -6., -6., -6.],
         [-6., -6., -6., -6., -6.],
         [-6., -6., -6., -6., -6.],
         [-6., -6., -6., -6., -6.]],

        [[-5., -5., -5., -5., -5.],
         [-5., -5., -5., -5., -5.],
         [-5., -5., -5., -5., -5.],
         [-5., -5., -5., -5., -5.]],

        [[-4., -4., -4., -4., -4.],
         [-4., -4., -4., -4., -4.],
         [-4., -4., -4., -4., -4.],
         [-4., -4., -4., -4., -4.]]])
===>>>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]],

        [[ 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]]])
===>>>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.]],

        [[ 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 ds= torch.Size([3, 4, 5])
===>>>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

相关推荐
桥田智能1 分钟前
工博会动态 | 来8.1馆 看桥田如何玩转全场
人工智能·机器人·自动化
深度学习的奋斗者1 分钟前
YOLOv8+注意力机制+PyQt5玉米病害检测系统完整资源集合
python·深度学习·yolo
—你的鼬先生2 分钟前
从零开始使用树莓派debian系统使用opencv4.10.0进行人脸识别(保姆级教程)
python·opencv·debian·人脸识别·二维码识别·opencv安装
斯派的曼3 分钟前
学习C++的第七天!
开发语言·c++·学习
街 三 仔4 分钟前
【C语言零基础入门篇 - 15】:单链表
c语言·开发语言
苏格拉没有底1115 分钟前
数据结构——顺序表、链表
c语言·开发语言·数据结构·笔记·学习·算法·链表
Eric.Lee202117 分钟前
数据集-目标检测系列-口罩检测数据集 mask>> DataBall
人工智能·目标检测·计算机视觉·数据集·口罩检测
Pandaconda22 分钟前
【计算机网络 - 基础问题】每日 3 题(二十三)
开发语言·网络·笔记·后端·计算机网络·面试·职场和发展
界面开发小八哥25 分钟前
如何用LightningChart Python实现地震强度数据可视化应用程序?
开发语言·python·信息可视化
野生派蒙25 分钟前
IDEA 关闭自动补全功能(最新版本)
java·开发语言·ide·后端·学习·intellij-idea