码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • pytorch的tensor创建和操作记录


    目录

    一、Tensr

    1、Tensor属性

    2、创建

    (1)  

    Creation Ops

     (2) 其它

    二、Tensor基本操作

    Indexing, Slicing, Joining, Mutating Ops

    三、Random sampling

    四、数学运算

    1、按元素操作 

    (1)加减乘除、绝对值

    (2)指数、对数、幂运算、开方运算

    (3)三角&反三角函数运算函数运算

    (4)双曲线反双曲线运算

      (5) 其他常用操作

    (6) 

    BLAS and LAPACK Operations


    一、Tensr

    1、Tensor属性

    1. numel # Returns the total number of elements in the input tensor.
    2. repeat_interleaves
    3. >>> a = torch.randn(1, 2, 3, 4, 5)
    4. >>> torch.numel(a)
    5. 120
    6. >>> a = torch.zeros(4,4)
    7. >>> torch.numel(a)
    8. 16
    9. >>> x = torch.tensor([1, 2, 3])
    10. >>> x.repeat_interleave(2)
    11. tensor([1, 1, 2, 2, 3, 3])
    12. >>> y = torch.tensor([[1, 2], [3, 4]])
    13. >>> torch.repeat_interleave(y, 2)
    14. tensor([1, 1, 2, 2, 3, 3, 4, 4])
    15. >>> torch.repeat_interleave(y, 3, dim=1)
    16. tensor([[1, 1, 1, 2, 2, 2],
    17. [3, 3, 3, 4, 4, 4]])
    18. >>> torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0)
    19. tensor([[1, 2],
    20. [3, 4],
    21. [3, 4]])
    22. >>> torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0, output_size=3)
    23. tensor([[1, 2],
    24. [3, 4],
    25. [3, 4]]

    2、创建

    前言 · PyTorch实用教程(第二版)

    (1)  

    • Creation Ops

     (2) 其它

    1. >>> torch.eye(3)
    2. tensor([[ 1., 0., 0.],
    3. [ 0., 1., 0.],
    4. [ 0., 0., 1.]])

    二、Tensor基本操作

    Indexing, Slicing, Joining, Mutating Ops

    1. tensor.stack()
    2. tensor.cat()
    3. torch.squeeze()
    4. torch.unsqueeze()
    5. >>> x = torch.zeros(2, 1, 2, 1, 2)
    6. >>> x.size()
    7. torch.Size([2, 1, 2, 1, 2])
    8. >>> y = torch.squeeze(x)
    9. >>> y.size()
    10. torch.Size([2, 2, 2])
    11. >>> y = torch.squeeze(x, 0)
    12. >>> y.size()
    13. torch.Size([2, 1, 2, 1, 2])
    14. >>> y = torch.squeeze(x, 1)
    15. >>> y.size()
    16. torch.Size([2, 2, 1, 2])

    三、Random sampling

    1. bernoulli
    2. multinomial
    3. normal
    4. poisson
    5. rand
    6. rand_like
    7. randint
    8. randint_like
    9. randn
    10. randn_like
    11. randperm
    12. # In-place random sampling
    13. torch.Tensor.bernoulli_()
    14. torch.Tensor.cauchy_()
    15. torch.Tensor.exponential_()
    16. torch.Tensor.geometric_()
    17. torch.Tensor.log_normal_()
    18. torch.Tensor.normal_()
    19. torch.Tensor.random_()
    20. torch.Tensor.uniform_()

    四、数学运算

    1、按元素操作 

    torch — PyTorch 1.12 documentationhttps://pytorch.org/docs/stable/torch.html#pointwise-ops

    (1)加减乘除、绝对值

    1. add
    2. sub # subtract, Alias for torch.sub().
    3. mul # multiply, Alias for torch.mul().
    4. div # divide , Alias for torch.div().
    5. abs # absolute, Alias for torch.abs()

    加法举例:

    1. import torch
    2. a_list = [[1, -2, 3], [-4, 5, -6]]
    3. a_tensor = torch.tensor(list1)
    4. print(a_tensor)
    5. # output:
    6. tensor([[ 1, -2, 3],
    7. [-4, 5, -6]])
    8. # 加法操作
    9. # ops 1
    10. a_tensor.add(10)
    11. # output1
    12. tensor([[11, 8, 13],
    13. [ 6, 15, 4]])
    14. # ops 2
    15. b_tensor = torch.ones_like(a_tensor)
    16. a_tensor.add(b_tensor, alpha=19) # alpha=1(默认),a_tensor = alpha*b_tensor + a_tensor
    17. # output1
    18. tensor([[20, 17, 22],
    19. [15, 24, 13]])

    (2)指数、对数、幂运算、开方运算

    (3)三角&反三角函数运算函数运算

    (4)双曲线反双曲线运算

      (5) 其他常用操作

    1. clamp 
    2. reshape
    3. view
    1. clamp # clip Alias for torch.clamp().
    2. a = torch.randn(4)
    3. # output1
    4. tensor([-1.7120, 0.1734, -0.0478, -0.0922])
    5. torch.clamp(a, min=-0.5, max=0.5)
    6. # output2
    7. tensor([-0.5000, 0.1734, -0.0478, -0.0922])

    (6) 

    • BLAS and LAPACK Operations

    1. # torch.bmm
    2. >>> input = torch.randn(10, 3, 4)
    3. >>> mat2 = torch.randn(10, 4, 5)
    4. >>> res = torch.bmm(input, mat2)
    5. >>> res.size()
    6. torch.Size([10, 3, 5])

    参考:   torch.clamp — PyTorch 1.12 documentation 

    PyTorch:view() 与 reshape() 区别详解_地球被支点撬走啦的博客-CSDN博客_reshape和view

    torch.repeat_interleave — PyTorch 1.12 documentation 

  • 相关阅读:
    uniapp 使用svg
    mysql---squid代理服务器
    solana 入门 1
    大数据测试用例分析
    解决电脑显示找不到msvcp140_CODECVT_IDS.dll文件的办法
    Mono创始人 Miguel de Icaza今天离开微软
    mybatis循环插入
    2021年03月 Scratch(二级)真题解析#中国电子学会#全国青少年软件编程等级考试
    On Moving Object Segmentation from Monocular Video with Transformers 论文阅读
    实验2:Numpy手写多层神经网络
  • 原文地址:https://blog.csdn.net/Tanqy1997/article/details/126083448
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号