torch.add、sub、mul、div、matmul、mm、@、pow、exp、log、sqrt运算

69 篇文章 8 订阅
a = torch.rand(2, 3)
b = torch.rand(2, 3)
print(a)
print(b)

add 加法运算

import torch

# 加法运算的几种方式
print('a + b\n', a + b)
print('a.add(b)\n', a.add(b))
print('torch.add(a, b)\n', torch.add(a, b))
# 仅这种方式会改变a的值,即把结果保存到a中
print('a.add_(b)\n', a.add_(b))
print(a)

结果:

tensor([[0.9993, 0.9160, 0.4045],
        [0.6335, 0.2120, 0.6178]])
tensor([[0.8361, 0.6041, 0.1181],
        [0.5700, 0.5781, 0.6998]])
a + b
 tensor([[1.8354, 1.5201, 0.5226],
        [1.2035, 0.7901, 1.3176]])
a.add(b)
 tensor([[1.8354, 1.5201, 0.5226],
        [1.2035, 0.7901, 1.3176]])
torch.add(a, b)
 tensor([[1.8354, 1.5201, 0.5226],
        [1.2035, 0.7901, 1.3176]])
a.add_(b)
 tensor([[1.8354, 1.5201, 0.5226],
        [1.2035, 0.7901, 1.3176]])
tensor([[1.8354, 1.5201, 0.5226],
        [1.2035, 0.7901, 1.3176]])

sub 减法:

import torch

a = torch.rand(2, 3)
b = torch.rand(2, 3)
print(a)
print(b)
print('减法操作')
print('a\n', a)
print('a-b\n', a-b)
print('a.sub(b)\n', a.sub(b))
print('torch.sub(a, b)\n', torch.sub(a, b))
print('a.sub_(b)\n', a.sub_(b))
print(a)

结果:

tensor([[0.8540, 0.5175, 0.9498],
        [0.8799, 0.8251, 0.7269]])
tensor([[0.2576, 0.6812, 0.9487],
        [0.1363, 0.7312, 0.4687]])
减法操作
a
 tensor([[0.8540, 0.5175, 0.9498],
        [0.8799, 0.8251, 0.7269]])
a-b
 tensor([[ 0.5964, -0.1637,  0.0011],
        [ 0.7437,  0.0939,  0.2582]])
a.sub(b)
 tensor([[ 0.5964, -0.1637,  0.0011],
        [ 0.7437,  0.0939,  0.2582]])
torch.sub(a, b)
 tensor([[ 0.5964, -0.1637,  0.0011],
        [ 0.7437,  0.0939,  0.2582]])
a.sub_(b)
 tensor([[ 0.5964, -0.1637,  0.0011],
        [ 0.7437,  0.0939,  0.2582]])
tensor([[ 0.5964, -0.1637,  0.0011],
        [ 0.7437,  0.0939,  0.2582]])

mul 乘法:

import torch

a = torch.rand(2, 3)
b = torch.rand(2, 3)
print(a)
print(b)
print('乘法: 对应位置上的元素相乘')
print('a\n', a)
print('a*b\n', a*b)
print('torch.mul(a, b)\n', torch.mul(a, b))
print('a.mul(b)\n', a.mul(b))
print('a.mul_(b)\n', a.mul_(b))
print('a', a)

结果:


tensor([[0.6888, 0.6880, 0.8884],
        [0.9059, 0.8689, 0.5366]])
tensor([[0.5977, 0.1775, 0.9614],
        [0.4969, 0.8030, 0.6663]])
乘法: 对应位置上的元素相乘
a
 tensor([[0.6888, 0.6880, 0.8884],
        [0.9059, 0.8689, 0.5366]])
a*b
 tensor([[0.4117, 0.1221, 0.8541],
        [0.4501, 0.6977, 0.3575]])
torch.mul(a, b)
 tensor([[0.4117, 0.1221, 0.8541],
        [0.4501, 0.6977, 0.3575]])
a.mul(b)
 tensor([[0.4117, 0.1221, 0.8541],
        [0.4501, 0.6977, 0.3575]])
a.mul_(b)
 tensor([[0.4117, 0.1221, 0.8541],
        [0.4501, 0.6977, 0.3575]])
a tensor([[0.4117, 0.1221, 0.8541],
        [0.4501, 0.6977, 0.3575]])

div 除法

import torch

a = torch.rand(2, 3)
b = torch.rand(2, 3)
print(a)
print(b)
print('除法')
print('a\n', a)
print('a/b\n', a/b)
print('torch.div(a, b)\n', torch.div(a, b))
print('a.div(b)\n', a.div(b))
print('a.div_(b)\n', a.div_(b))
print('a', a)

结果:

tensor([[0.3184, 0.7036, 0.5756],
        [0.5146, 0.4399, 0.1367]])
tensor([[0.9408, 0.1757, 0.7494],
        [0.0198, 0.7022, 0.8607]])
除法
a
 tensor([[0.3184, 0.7036, 0.5756],
        [0.5146, 0.4399, 0.1367]])
a/b
 tensor([[ 0.3385,  4.0043,  0.7680],
        [25.9892,  0.6265,  0.1588]])
torch.div(a, b)
 tensor([[ 0.3385,  4.0043,  0.7680],
        [25.9892,  0.6265,  0.1588]])
a.div(b)
 tensor([[ 0.3385,  4.0043,  0.7680],
        [25.9892,  0.6265,  0.1588]])
a.div_(b)
 tensor([[ 0.3385,  4.0043,  0.7680],
        [25.9892,  0.6265,  0.1588]])
a tensor([[ 0.3385,  4.0043,  0.7680],
        [25.9892,  0.6265,  0.1588]])

@、matmul、mm矩阵运算

import torch
# 矩阵运算 matmul
a = torch.ones(2, 1)
b = torch.ones(1, 2)
print('a @ b\n', a @ b)
print('a.matmul(b)\n', a.matmul(b))
print('torch.matmul(a, b)\n', torch.matmul(a, b))
print('torch.mm(a, b)\n', torch.mm(a, b))
print('a.mm(b)\n', a.mm(b))

结果:


a @ b
 tensor([[1., 1.],
        [1., 1.]])
a.matmul(b)
 tensor([[1., 1.],
        [1., 1.]])
torch.matmul(a, b)
 tensor([[1., 1.],
        [1., 1.]])
torch.mm(a, b)
 tensor([[1., 1.],
        [1., 1.]])
a.mm(b)
 tensor([[1., 1.],
        [1., 1.]])

matmul高维矩阵运算

import torch
# 高维运算的时候要注意前边的维度一定要一样,后边的两个维度要和二维的一样即:
# 第一个的最后一个维度的数字,要和第二个的倒数第二个的数字一样
a = torch.ones(1, 2, 3, 4)
b = torch.ones(1, 2, 4, 3)
print(torch.matmul(a, b))
print(torch.matmul(a, b).shape)

结果:

tensor([[[[4., 4., 4.],
          [4., 4., 4.],
          [4., 4., 4.]],

         [[4., 4., 4.],
          [4., 4., 4.],
          [4., 4., 4.]]]])
torch.Size([1, 2, 3, 3])

pow指数运算

import torch
print('指数运算:即a中的每个元素的n次方 ')
a = torch.tensor([1, 2])
print('a\n', a)
print('torch.pow(a, 3)\n', torch.pow(a, 3))
print('a.pow(3)\n', a.pow(3))
print('a**3\n', a**3)
print('a.pow_(3)\n', a.pow_(3))
print('a\n', a)

结果:


指数运算:即a中的每个元素的n次方 
a
 tensor([1, 2])
torch.pow(a, 3)
 tensor([1, 8])
a.pow(3)
 tensor([1, 8])
a**3
 tensor([1, 8])
a.pow_(3)
 tensor([1, 8])
a
 tensor([1, 8])

exp的运算

import torch
print('e的运算:即 e的n次方 ')
# 注意必须要是float类型的否则会报错
a = torch.tensor([1, 2], dtype=torch.float32)
print('a\n', a)
print('torch.exp(a)\n', torch.exp(a))
print('torch.exp_(a)\n', torch.exp_(a))
a = torch.tensor([1, 2] , dtype=torch.float32)
print('a.exp()\n', a.exp())
print('a.exp_()', a.exp_())
print('a\n', a)

结果:


e的运算:即 e的n次方 
a
 tensor([1., 2.])
torch.exp(a)
 tensor([2.7183, 7.3891])
torch.exp_(a)
 tensor([2.7183, 7.3891])
a.exp()
 tensor([2.7183, 7.3891])
a.exp_() tensor([2.7183, 7.3891])
a
 tensor([2.7183, 7.3891])

log运算

import torch
print('log运算: log以1为底的')
a = torch.tensor([1, 2], dtype=torch.float32)
print('a\n', a)
print('torch.log(a)\n', torch.log(a))
print('torch.log_(a)', torch.log_(a))
a = torch.tensor([1, 2], dtype=torch.float32)
print('a.log()\n', a.log())
print('a.log_()\n', a.log_())
print('a\n', a)

结果:

log运算: log以1为底的
a
 tensor([1., 2.])
torch.log(a)
 tensor([0.0000, 0.6931])
torch.log_(a) tensor([0.0000, 0.6931])
a.log()
 tensor([0.0000, 0.6931])
a.log_()
 tensor([0.0000, 0.6931])
a
 tensor([0.0000, 0.6931])

sqrt开平方

import torch
print('开平方')
a = torch.tensor([4, 5], dtype=torch.float32)
print('a\n', a)
print('torch.sqrt(a)\n', torch.sqrt(a))
print('torch.sqrt_(a)\n', torch.sqrt_(a))
print('a\n', a)
a = torch.tensor([4, 5], dtype=torch.float32)
print('a.sqrt()\n', a.sqrt())
print('a.sqrt_()\n', a.sqrt_())
print('a\n', a)

结果:

开平方
a
 tensor([4., 5.])
torch.sqrt(a)
 tensor([2.0000, 2.2361])
torch.sqrt_(a)
 tensor([2.0000, 2.2361])
a
 tensor([2.0000, 2.2361])
a.sqrt()
 tensor([2.0000, 2.2361])
a.sqrt_()
 tensor([2.0000, 2.2361])
a
 tensor([2.0000, 2.2361])
  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: torch.mmtorch.matmul都是PyTorch中用于矩阵乘法的函数,但是它们的输入和输出有所不同。 torch.mm只能用于两个二维矩阵的乘法,即两个矩阵的维度分别为(m,n)和(n,p),输出的矩阵维度为(m,p)。 而torch.matmul可以用于多个矩阵的乘法,且支持广播机制,即可以对不同维度的矩阵进行乘法。输入的矩阵可以是任意维度的张量,输出的矩阵维度也可以是任意维度的张量,但是要满足矩阵乘法的规则。 因此,torch.matmultorch.mm更加灵活,可以处理更多的矩阵乘法情况。 ### 回答2: torch.mmtorch.matmul是在PyTorch中用于矩阵乘法运算的两个函数。 torch.mm函数的输入是两个二维张量,它计算它们的矩阵积,并返回一个二维张量作为输出。它的代码实现中,使用了BLAS(基本线性代数子程序)库中的矩阵乘法实现,能够利用底层CPU的多线程进行加速,因此在大规模矩阵计算时,torch.mm的速度明显快于非优化的for循环实现。 而torch.matmul函数则适用于多维张量之间的矩阵乘法。通常情况下,它的第一个输入张量需要是至少二维的,第二个输入张量可以是二维或者更高维度的,两个张量将按照向量积的规则进行广播。也就是说,如果第一个张量是形状为(m, n)的矩阵,第二个张量是(m, n, p)的张量,那么它们的矩阵乘积将得到一个形状为(m, p)的矩阵。值得注意的是,当输入张量维度超过二维的时候,torch.matmul函数不一定等价于torch.mm函数。 总的来说,torch.mmtorch.matmul是实现矩阵乘法计算的两个强大函数,torch.mm适用于计算两个二维张量的矩阵积,torch.matmul则适用于处理更高维度的多维张量之间的向量积。在实际使用时,需要根据具体任务需求来选择使用哪个函数。 ### 回答3: torch.mmtorch.matmul都是PyTorch中的矩阵乘法函数,可以用来进行矩阵乘法运算。但是,它们之间还是存在一些细微的差别的。 torch.mm是一个比较早的矩阵乘法函数,它的操作对象必须是两个二维矩阵。torch.mm函数相当于numpy中的dot函数,会将两个矩阵进行标准的矩阵乘法运算,输出结果也是一个二维矩阵。而且,torch.mm只能进行二维矩阵的乘法运算,如果其中有一个矩阵是三维矩阵,则会报错。 与torch.mm不同的是,torch.matmul可以进行任意维度的矩阵乘法运算。具体来说,它根据输入的两个矩阵的维度特性,自动选择最合适的矩阵乘法方式。这种方式既包括标准的矩阵乘法,也包括一些特殊情况下矩阵乘法的优化方式。此外,torch.matmul还支持广播机制,也就是说可以与高维的元素运算。 总的来说,torch.mmtorch.matmul都是PyTorch中的矩阵乘法函数,可以满足大部分的矩阵乘法求解需求,但是它们适用的场景有所不同。当输入数据都是二维矩阵的时候,建议使用torch.mm;而当需要进行任意维度的矩阵乘法时,或者需要支持广播操作时,则可以使用torch.matmul
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值