PyTorch 张量数据类型

  • 【数据类型】Python 与 PyTorch 常见数据类型对应:

    a.type() 获取数据类型,用 isinstance(a, 目标类型) 进行类型合法化检测

    python3 复制代码
    >>> import torch
    >>> a = torch.randn(2,3)
    >>> a
    tensor([[-1.7818, -0.2472, -2.0684],
            [ 0.0117,  1.4698, -0.9359]])
    >>> a.type()  ## 获取数据类型
    'torch.FloatTensor'
    >>> isinstance(a, torch.FloatTensor)  ## 类型合法化检测
    True
    >>> 
  • 【什么是张量】标量与张量:用 a.dim(), a.shape 或者 a.size() 查看 dim 为 0 是标量,否则是张量

    python3 复制代码
    >>> import torch
    >>>
    >>> a = torch.tensor(1) 
    >>> a
    tensor(1)
    >>> a.dim()
    >>> 0  ## 标量
    
    >>> a = torch.Tensor([1]) 
    >>> a
    tensor([1.])
    >>> a.dim()
    >>> 1  ## 张量
  • 【生成张量】常见方法如下:

    • 常见随机方法:torch.randn(shape), torch.rand(shape), torch.randint(min, max, shape), torch.rand_like(a), torch.normal(mean, std) ... 具体示例如下

    • Dim 1 / rank 1: 以 size 2 为例

      python3 复制代码
      >>> a = torch.randn(2)   ## 随机,
      >>> a: tensor([1.4785, 0.6089])
      
      >>> a = torch.Tensor(2)   ## 接收维度, unintialized 不推荐
      >>> a: tensor([5.4086e+26, 4.5907e-41])
      >>> a = torch.Tensor([1,2])   ## 同 torch.tensor([1,2]) 接收具体数据
      >>> a: tensor([1, 2])
      
      >>> a = torch.from_numpy(np_data)  ## 数据维持不变,类型一一对应
      
      >>> a = torch.full([2],7)   ## 全部填充为一样的值   
      >>> a: tensor([7, 7])
      
      >>> a = torch.arange(0,10)   ## arange
      >>> a: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
      
      >>> a = torch.linspace(0,10, steps=4)   ##  
      >>> a: tensor([ 0.0000,  3.3333,  6.6667, 10.0000])
      >>> a = torch.logspace(0,10, steps=4) 
      >>> a: tensor([1.0000e+00, 2.1544e+03, 4.6416e+06, 1.0000e+10])
    • Dim 2 / rank 2: 以 size [2,3] 为例

      python3 复制代码
      >>> a = torch.randn(2, 3)   ## 随机 
      >>> a: tensor([[ 2.0631, -1.7011,  0.6375],
      	    	   [-1.2104, -1.3341, -0.8187]])
      
      >>> a = torch.Tensor(2, 3)   ## 接收维度, unintialized 不推荐   
      >>> a: tensor([[-0.2438, -0.9554, -0.4694],
      			   [ 0.8636,  1.6497, -0.8862]])
      >>> a = torch.Tensor([[1,2,3],[4,5,6]])  ## 同 torch.tensor([[1,2,3],[4,5,6]]) 接收具体数据 
      >>> a: tensor([[1., 2., 3.],
             		   [4., 5., 6.]])
      
      >>> a = torch.from_numpy(np_data)  ## 数据维持不变,类型一一对应
      
      >>> a = torch.full([2,3],7)  ## 全部填充为一样的值
      >>> a: tensor([[7, 7, 7],
                     [7, 7, 7]])

      ...


相关推荐
05候补工程师1 分钟前
[实战复盘] 拒绝 AI 屎山!我从设计模式中学到的“调教”AI 新范式
人工智能·python·设计模式·ai·ai编程
飞Link17 分钟前
垂直领域 AI 的曙光:GPT-Rosalind 如何重塑生命科学与药物研发?
人工智能·gpt
一只数据集32 分钟前
全尺寸人形机器人灵巧手力觉触觉数据集-2908条ROSbag数据覆盖14大应用场景深度解析
大数据·人工智能·算法·机器人
火山引擎开发者社区42 分钟前
火山引擎全面支持 Milvus 2.6 版本:更快、更省、更稳
人工智能
cczixun1 小时前
OpenAI连发GPT-5.5系列:免费版幻觉大降,安全版能力飙升,千亿融资估值直冲8520亿美元
人工智能·gpt·安全
飞Link1 小时前
商汤 SenseNova 6.7 Flash-Lite 深度评测:原生多模态 Agent 的“降本增效”终极方案?
人工智能
飞Link1 小时前
OpenAI 与微软“非排他性”协议解读:AI 云计算市场将迎来百家争鸣?
人工智能·microsoft·云计算
徐健峰2 小时前
GPT-image-2 热门玩法实战(三):AI 手办/Action Figure — 把自己变成盒装玩具
人工智能
扑兔AI2 小时前
B2B销售线索挖掘效率提升的技术实践:基于工商公开数据的客源筛选与竞品分析架构
大数据·人工智能·架构
亚鲁鲁2 小时前
00-目录
人工智能