首页 > 其他分享 >torch.stack

torch.stack

时间:2024-09-22 23:12:24浏览次数:1  
标签:36.7500 24.5000 torch 12.2500 49.0000 0.0000 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)

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

标签:36.7500,24.5000,torch,12.2500,49.0000,0.0000,stack
From: https://www.cnblogs.com/yanghailin/p/18426072

相关文章

  • Stack Overflow 2023 年开发者调查报告!
    StackOverflow发布了2023年开发者调查报告,据称共计超过9万名开发者参与了此次调查。完整报告包含了受访开发者画像,以及关于开发技术、AI、职业、社区等方面的内容。本文主要介绍关于开发技术和AI的部分。懒人目录:最流行编程语言:JavaScript最“赚钱”编程语言......
  • llm-app-stack
    llm-app-stackhttps://github.com/a16z-infra/llm-app-stackakaEmergingArchitecturesforLLMApplicationsThisisalistofavailabletools,projects,andvendorsateachlayeroftheLLMappstack.  LlamaIndexvsLangChainhttps://www.datacamp.com......
  • stack - queue
    1.容器适配器(1)什么是适配器?适配器是一种设计模式(设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结),该种模式是将一个类的接口转换成客户希望的另外一个接口(2) STL标准库中stack和queue的底层结构 虽然stack和queue中也可以存......
  • torch模型量化方法总结
    0.概述模型训练完成后的参数为float或double类型,而装机(比如车载)后推理预测时,通常都会预先定点(量化)为int类型参数,相应的推理的精度会有少量下降,但不构成明显性能下降,带来的结果是板端部署的可能性,推理的latency明显降低,本文对torch常用的量化方法进行总结作为记录。1.模型量化......
  • 【已解决 含代码调试分析】pytorch的维度,为什么计算loss是0维度的,0维度是是什么?作用是
    嘿,你能搜索到这个问题,说明你说一个认真学习的同学,这个问题的细节值得思考。欢迎收藏,会持续更新。请仔细看后面的调试界面。三维维度很好理解,就是只管的认为是长宽高,你能看出下面的计算结果吗?importtorchdim_3=torch.randn(1,2,3)dim_2=torch.randn(1,2)dim_1......
  • openstack-cinder
    cinder概念cinder是OpenStack块存储服务,它的主要功能包括:卷管理:cinder可以创建,删除,扩容和缩小卷。管理员可以通过api或者命令行接口创建卷,指定卷的大小,类型,名称和描述等信息。当需要扩容或缩小卷时,管理员可以通过api或者命令行接口进行操作。卷快照:cinder支持卷的快照,......
  • fastsam_pytorch图像分割算法模型
    FastSAM论文FastSegmentAnything模型结构以yolov8-seg的instancesegmentation为基础,检测时集成instancesegmentation分支,主要分为两步全实例分割(allinstanceSegmentation)和基于prompt的mask输出(Prompt-guidedSelection),仅使用了2%的SA-1B数据集便得到了差不多的......
  • 《深度学习》—— PyTorch的介绍及PyTorch的CPU版本安装
    文章目录一、PyTorch的简单介绍二、pytorch的CPU版本安装三、torch、torchvision、torchaudio三个库的介绍一、PyTorch的简单介绍PyTorch是一个由FacebookAI实验室开发的深度学习框架,它基于Python,并提供了高效的GPU加速和灵活的模型定义能力。1.PyTorch的基本特点......
  • torch.distributed.DistNetworkError: The server socket has failed to listen on an
    解决方案是在torchrun中添加参数--master_port改变masterport。且注意这个参数一定要加在要跑的文件即src/entry_point/train.py之前,否则会被忽略。引用:https://juejin.cn/post/7260668104752775228我的代码是:torchrun--nproc_per_node1--master_port29501-mtraining.......
  • PyQt5 使用 QStackedWidget 实现轮播展示动画,但是却疯狂闪烁的解决办法
    PyQt5使用QStackedWidget实现轮播展示动画,但是却疯狂闪烁的解决办法上篇说到,上篇见这里我们可能会遇到,当把鼠标移动到"下一页"和"上一页"按钮,又或者是Qlabel标签页时,就会疯狂闪烁,于是在这里换另一种方案,解决这个问题代码结构本文基于上篇,上篇见这里修改而来,全部代码......