码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • torch.Tensor详解


    目录

        • torch.Tensor详解
          • 参考:
          • 提供的数据类型:
          • 初始化:
            • 2、设置类型和设备
            • 3、可以使用 Python 的索引和切片符号访问和修改张量的内容
            • 4、使用 torch.Tensor.item() 从包含单个值的张量中获取 Python 数字:
            • 5、requires_grad=True自动梯度计算

    torch.Tensor详解

    参考:

    torch.Tensor — PyTorch 1.12 documentation

    提供的数据类型:

    Data typedtypeCPU tensorGPU tensor
    32-bit floating pointtorch.float32 or torch.floattorch.FloatTensortorch.cuda.FloatTensor
    64-bit floating pointtorch.float64 or torch.doubletorch.DoubleTensortorch.cuda.DoubleTensor
    16-bit floating point [1]torch.float16 or torch.halftorch.HalfTensortorch.cuda.HalfTensor
    16-bit floating point [2]torch.bfloat16torch.BFloat16Tensortorch.cuda.BFloat16Tensor
    32-bit complextorch.complex32 or torch.chalf
    64-bit complextorch.complex64 or torch.cfloat
    128-bit complextorch.complex128 or torch.cdouble
    8-bit integer (unsigned)torch.uint8torch.ByteTensortorch.cuda.ByteTensor
    8-bit integer (signed)torch.int8torch.CharTensortorch.cuda.CharTensor
    16-bit integer (signed)torch.int16 or torch.shorttorch.ShortTensortorch.cuda.ShortTensor
    32-bit integer (signed)torch.int32 or torch.inttorch.IntTensortorch.cuda.IntTensor
    64-bit integer (signed)torch.int64 or torch.longtorch.LongTensortorch.cuda.LongTensor
    Booleantorch.booltorch.BoolTensortorch.cuda.BoolTensor
    quantized 8-bit integer (unsigned)torch.quint8torch.ByteTensor/
    quantized 8-bit integer (signed)torch.qint8torch.CharTensor/
    quantized 32-bit integer (signed)torch.qint32torch.IntTensor/
    quantized 4-bit integer (unsigned)torch.quint4x2torch.ByteTensor/

    除了编码常见的类型,还有几种不常见的类型:

    1、16-bit floating point[1]:使用 1 个符号、5 个指数和 10 个有效位。 当精度很重要以牺牲范围为代价时很有用。

    2、16-bit floating point[2]:使用 1 个符号、8 个指数和 7 个有效位。 当范围很重要时很有用,因为它具有与 float32 相同数量的指数位

    3、quantized 4-bit integer (unsigned):量化的 4 位整数存储为 8 位有符号整数。 目前仅在 EmbeddingBag 运算符中支持。

    不指定类型的话,默认是:torch.FloatTensor

    初始化:

    1、使用列表或者序列:

    import torch
    
    torch.tensor([[1., -1.], [1., -1.]])
    torch.tensor(np.array([[1, 2, 3], [4, 5, 6]]))
    
    • 1
    • 2
    • 3
    • 4

    注意:torch.tensor会拷贝数据,如果在改变requires_grad时避免拷贝数据,需要使用requires_grad_() or detach()。如果在使用numpy array初始化想避免拷贝,需要使用torch.as_tensor()

    2、设置类型和设备
    >>> torch.zeros([2, 4], dtype=torch.int32)
    tensor([[ 0,  0,  0,  0],
            [ 0,  0,  0,  0]], dtype=torch.int32)
    >>> cuda0 = torch.device('cuda:0')
    >>> torch.ones([2, 4], dtype=torch.float64, device=cuda0)
    tensor([[ 1.0000,  1.0000,  1.0000,  1.0000],
            [ 1.0000,  1.0000,  1.0000,  1.0000]], dtype=torch.float64, device='cuda:0')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    3、可以使用 Python 的索引和切片符号访问和修改张量的内容
    >>> x = torch.tensor([[1, 2, 3], [4, 5, 6]])
    >>> print(x[1][2])
    tensor(6)
    >>> x[0][1] = 8
    >>> print(x)
    tensor([[ 1,  8,  3],
            [ 4,  5,  6]])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    4、使用 torch.Tensor.item() 从包含单个值的张量中获取 Python 数字:
    >>> x = torch.tensor([[1]])
    >>> x
    tensor([[ 1]])
    >>> x.item()
    1
    >>> x = torch.tensor(2.5)
    >>> x
    tensor(2.5000)
    >>> x.item()
    2.5
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    5、requires_grad=True自动梯度计算
    >>> x = torch.tensor([[1., -1.], [1., 1.]], requires_grad=True)
    >>> out = x.pow(2).sum()
    >>> out.backward()
    >>> x.grad
    tensor([[ 2.0000, -2.0000],
            [ 2.0000,  2.0000]])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    [七夕节]——html炫酷七夕情人节表白动画特效
    【LeetCode-235】二叉搜索树的最近公共祖先
    Solidity基础语法代码2
    聊聊admin服务的架构模式
    mybatis报nvalid bound statement (not found)或者找不到xml文件
    【机器学习Q&A】余弦相似度、余弦距离、欧式距离以及机器学习中距离的含义
    QT之QLineEdit的用法
    centos php 启动命令
    NGINX 域名及IP白名单拦截,Referer防盗链设置
    vue3+vite+pinia+axios+mock+ElementPlus:登录,动态路由,存储,网络(mock) js非ts纯前端
  • 原文地址:https://blog.csdn.net/KPer_Yang/article/details/126295538
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号