码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • pytorch: Tensor.new_zeros使用场景,type_as函数, torch.stack()使用方法


    文章目录

        • 1. Tensor.new_zeros(size, dtype=None, device=None, requires_grad=False) → Tensor
        • 12 type_as函数
        • 3. torch.stack()

    1. Tensor.new_zeros(size, dtype=None, device=None, requires_grad=False) → Tensor

    创建一个tensor基于另一个tensor,目的是什么呢?
    答:创建与原tensor 相同类型,相同device的tensor.

    Tensor.new_zeros(size, dtype=None, device=None, requires_grad=False) → Tensor
    Returns a Tensor of size size filled with 0. 
    By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.
    
    • 1
    • 2
    • 3

    12 type_as函数

    torch.Tensor的类型转换函数.

    def type_as(self, tensor): # real signature unknown; restored from __doc__
        """
        type_as(tensor) -> Tensor
        
        Returns this tensor cast to the type of the given tensor.
        
        This is a no-op if the tensor is already of the correct type. This is
        equivalent to ``self.type(tensor.type())``
        
        Args:
            tensor (Tensor): the tensor which has the desired type
        """
        return _te.Tensor(*(), **{})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    一般情况下常常使用tensor后调用的方法:

    比如 .double()
    .float()
    .byte()
    .int()
    等

    3. torch.stack()

    torch.cat() 函数连接两个tensor, 沿着设定的维度增加尺寸。 参考 文章

    torch.stack() 沿着一个新维度对输入张量序列进行连接。 序列中所有的张量都应该为相同形状。
    浅显说法:把多个2维的张量凑成一个3维的张量;多个3维的凑成一个4维的张量…以此类推,也就是在增加新的维度进行堆叠。

    # 假设是时间步T1的输出
    T1 = torch.tensor([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])
    # 假设是时间步T2的输出
    T2 = torch.tensor([[10, 20, 30],
              [40, 50, 60],
              [70, 80, 90]])
    
    print(torch.stack((T1,T2),dim=0).shape)
    print(torch.stack((T1,T2),dim=1).shape)
    print(torch.stack((T1,T2),dim=2).shape)
    print(torch.stack((T1,T2),dim=3).shape)
    # outputs:
    torch.Size([2, 3, 3])
    torch.Size([3, 2, 3])
    torch.Size([3, 3, 2])
    '选择的dim>len(outputs),所以报错'
    IndexError: Dimension out of range (expected to be in range of [-3, 2], but got 3)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    比如一种使用方法,增加一个维度时一个列表的tensor连接在一起

    dir = Path('/home/ww/dataset/REDS/val_sharp_bicubic/X4/012/')
    frames = [read_image(os.path.join(dir, f'{i:08d}.png')) for i in range(10)]
    
    input = torch.stack(frames)
    
    • 1
    • 2
    • 3
    • 4

    每个图像是 3 * h * w
    stack后 是 10 * 3 * h * w

    [1]https://blog.csdn.net/xinjieyuan/article/details/105205326

  • 相关阅读:
    小白学习spring第四天
    防止安卓崩溃的工具类
    std::format 如何实现编译期格式检查
    python中的exec()、eval()以及complie()
    网络规划设计师上午真题及解析(2019)
    文心大模型写——网课点播系统(项目需求)
    RocketMQ NameServer 概览
    计算机mfc140.dll文件缺失的修复方法分析,一键修复mfc140.dll
    Gartner发布报告揭秘微软数据安全功能和许可
    git项目管理中如何fork别人的代码以及如何拉取最新的源项目代码
  • 原文地址:https://blog.csdn.net/tywwwww/article/details/127445841
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号