• empty、arange、linspace、rand/randn、normal/uniform_、randperm、加减乘除幂对数、取整/余、比较运算


    一、tensor基础函数

    1. 基本函数

    函数功能
    Tensor(*size)创建tensor
    Tensor(data)将数据转化为tensor
    ones(*size)全1
    zeros(*size)全0
    zeros_like(tensor)/ones_like(tensor)创建一个与参数相同shape且元素全为0/全为1的tensor
    eye(*size)单位tensor
    torch.empty(*size)创建一个未被初始化数值的tensor(值不一定是0),tensor的大小是由size确定
    arange(s, e, step)返回一个1维张量,取包含在[s, e)在内的间隔为steps的点
    linspace(s, e, step=100)返回一个1维张量,取包含在[s, e]在内的均匀间隔的step个点
    rand/randn(*size)均匀分布:从区间[0, 1)的均匀分布中抽取的一组随机数 / 标准正态分布:从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的一组随机数
    normal(mean, std)/uniform_(from, to)正态分布:从指定均值和标准差的正态分布中抽取的一组随机数 / 均匀分布:从from到to的均匀分布中随机抽样数值进行填充
    randperm(n)将[0, n)随机打乱后获得的数字序列
    import torch
    
    print(torch.Tensor([[1, 2], [3, 4]]))	# 创建
    print(torch.Tensor(3, 2))
    print(torch.zeros(2, 2))	
    print(torch.ones(2, 2))
    print(torch.eye(2, 2))
    print(torch.empty([2, 2]))
    a = torch.Tensor([[1, 2], [3, 4]])
    print(torch.zeros_like(a))
    print(torch.ones_like(a))
    print(torch.rand(2, 2))
    print(torch.randn(2, 2))
    print(torch.normal(torch.randn(1, 3), torch.rand(1, 3)))
    print(torch.linspace(1, 5, 5))
    print(torch.arange(1, 5, 1))
    print(torch.Tensor(2, 2).uniform_(-1, 1))
    print(torch.randperm(10))
    
    # 输出
    tensor([[1., 2.],			# 创建
            [3., 4.]])
    tensor([[ 7.3908e+22,  1.4764e-41],
            [-6.2287e-23,  4.5916e-41],
            [ 0.0000e+00,  0.0000e+00]])
    tensor([[0., 0.],			# zeros
            [0., 0.]])
    tensor([[1., 1.],			# ones
            [1., 1.]])
    tensor([[1., 0.],			# eye
            [0., 1.]])
    tensor([[1.4013e-45, 0.0000e+00],	# empty
            [1.6393e+19, 4.5916e-41]])
    tensor([[0., 0.],			# zeros_like
            [0., 0.]])
    tensor([[1., 1.],			# ones_like
            [1., 1.]])
    tensor([[0.4374, 0.2534],		# rand
            [0.1902, 0.1513]])
    tensor([[ 1.8772,  0.9770],		# randn
            [ 0.7970, -0.5733]])
    tensor([[-0.2111,  0.9958,  0.2982]])	# normal
    tensor([1., 2., 3., 4., 5.])	# linspace
    tensor([1, 2, 3, 4])		# arange
    tensor([[-0.3002, -0.6143],		# uniform_
            [ 0.7185, -0.2010]])
    tensor([3, 5, 8, 2, 6, 9, 1, 4, 0, 7])	# randperm
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    2. 其他函数

    • torch.abs():绝对值函数
    • torch.sign():符号函数
    • torch.sigmoid():sigmoid函数

    二、torch运算函数

    1. tensor的算数运算

    ①加减乘除

    加为add函数,减为sub函数,乘(element wise:对应元素相乘)为mul函数,除为div函数

    不过对于add_(),sub_(),mul_(),div_() 而言会对原来的操作数a进行修改

    a, b = torch.tensor(5), torch.tensor(3)
    print(a + b)		# 加减乘除只需要修改对应函数与符号
    print(torch.add(a, b))
    print(a.add(b))
    print(a.add_(b))
    print(a)
    
    # 输出
    tensor(8)
    tensor(8)
    tensor(8)
    tensor(8)
    tensor(8)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    ②幂运算

    print(a ** b)			# 普通幂运算同加减乘除
    print(torch.pow(a, b))
    print(a.pow(b))
    print(a.pow_(b))
    
    print(torch.exp(a))		# 以e为底的指数函数
    print(a.exp())		
    print(a.exp_())			
    
    print(torch.sqrt(a))		# 开根号
    print(a.sqrt())			
    print(a.sqrt_())			# 同add_()函数
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    ③矩阵运算

    二维来说,矩阵乘法包括torch.mm()、torch.matmul()、@,且这里 没有mm()_、matmul()_的方法

    print(torch.mm(a, b))		# 开根号
    print(torch.matmul(a, b))		# 开根号
    print(a @ b)		# 开根号
    print(a.matmul(b))		# 开根号
    print(a.mm(b))		# 开根号
    
    • 1
    • 2
    • 3
    • 4
    • 5

    对于高维的Tensor (dim>2),定义其矩阵乘法仅在最后的两个维度上进行二维运算,并要求 前面的维度必须保持一致 ,且操作函数只有 torch.matmul()

    a, b = torch.rand(1, 2, 3), torch.rand(1, 3, 7)
    
    print(torch.matmul(a, b))		# 开根号
    print(a.matmul(b))		# 开根号
    
    • 1
    • 2
    • 3
    • 4

    ④对数运算

    log2和log10的函数同log函数的用法

    print(torch.log(a))			# e为底的对数函数
    print(a.log())
    print(a.log_())
    
    • 1
    • 2
    • 3

    ⑤取整/余

    ~可以为torch或操作数

    函数作用
    %取余
    ~.floor()下取整
    ~.ceil()上取整
    ~.round()四舍五入
    ~.trunc()只取整数部分
    ~.frac()只取小数部分
    print(a)
    print(torch.floor(a))
    print(a.floor())
    print(torch.ceil(a))
    print(a.ceil())
    print(torch.round(a))
    print(a.round())
    print(torch.trunc(a))
    print(a.trunc())
    print(torch.frac(a))
    print(a.frac())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2. 比较运算

    torch.equal()返回只返回一个布尔值,而torch.eq()和其他函数是返回一个内容为true或false的tensor

    ge为great than or equal;lt为Less than,nt为not equal
    在这里插入图片描述
    本文只用于个人学习与记录,侵权立删

  • 相关阅读:
    Openssl教程
    ubuntu的键盘F1~F12没有反应/出现问题(被系统强制为功能键了)
    2023年四川省安全员B证证模拟考试题库及四川省安全员B证理论考试试题
    【RocketMQ】sendDefaultImpl call timeout 问题及其解决办法
    电力电子转战数字IC——题目合集+持续更新进度
    Win11校园网无法连接怎么办?Win11连接不到校园网的解决方法
    小程序iPhonex适配
    Sudowrite:基于人工智能的AI写作文章生成工具
    AspNetCore 成长杂记(一):JWT授权鉴权之生成JWT(其二)
    重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
  • 原文地址:https://blog.csdn.net/weixin_45969777/article/details/126297343