码农知识堂 - 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 

  • 相关阅读:
    深度学习推理显卡设置
    unity模型制作(终章)
    Git的安装与环境配置
    linux运维笔记:Linux文件系统
    50etf期权的隐含波动率是什么意思?最通俗易懂的解释!
    用HTML+CSS+JS做一个漂亮简单的公司网站(JavaScript期末大作业)
    [附源码]SSM计算机毕业设计音乐网站JAVA
    Appium+Java多台设备连接时实现设备切换
    基于先验激光雷达地图的2D-3D线特征单目定位
    YOLO家族再度升级——阿里达摩院DAMO-YOLO重磅来袭
  • 原文地址:https://blog.csdn.net/Tanqy1997/article/details/126083448
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    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号