Pytorch基础——(2)张量(tensor)的数学运算

目录

一、基本运算

二、最大值、最小值、均值、绝对值、排序

三、tensor的索引


一、基本运算

import torch

x = torch.tensor([2, 2, 2], dtype=torch.float32)
y = torch.tensor([3, 4, 5], dtype=torch.float32)
  • 加法
out1 = torch.add(x, y)
print(out1)
  • 减法
out2 = torch.sub(x, y)
print(out2)
  • 除法
out3 = torch.div(x, y)
print(out3)

  •  乘法
out4 = torch.mul(x, y)
print(out4)
  • 矩阵乘法
a = torch.rand((2, 5))
b = torch.rand((5, 3))

out5 = torch.mm(a, b)
print(out5)
print(out5.shape)
  • 批量矩阵乘法
batch = 16
c1 = 5
c2 = 10
c3 = 20

x1 = torch.rand(batch, c1, c2)  # 16*5*10
x2 = torch.rand(batch, c2, c3)  # 16*10*20

out6 = torch.bmm(x1, x2)  # 16*5*20
print(out6.shape)
  • 指数
out7 = x.pow(3)
print(out7)
  • 矩阵指数
x = torch.tensor([[1, 1], [1, 1]])  # (2*2)必须行列数相同
out8 = x.matrix_power(2)
print(out8)  # 2*2
  • broadcasting
x1 = torch.rand((1, 3))
x2 = torch.rand((3, 3))

out9 = x1 -x2
print(out9)

二、最大值、最小值、均值、绝对值、排序

import torch

x = torch.tensor([[-1, 2, 3], [4, 5, 6]], dtype=torch.float32)
y = torch.tensor([[1, 1, 2], [0, 10, 9]], dtype=torch.float32)
  • 最大值、最小值、均值
values, indice = torch.max(x, dim=0)   # dim=0表示列,1表示行
print(values)  # 最大值的值
print(indice)  # 最大值的索引

values, indice = torch.min(x, dim=0)   # dim=0表示列,1表示行
print(values)  # 最小值的值
print(indice)  # 最小值的索引

values, indice = torch.mean(x, dim=0)
print(values)
print(indice)
# 找到最大值、最小值的索引
out1 = torch.argmax(x, dim=0)
out2 = torch.argmin(x, dim=0)
  • 绝对值
out = torch.abs(x)
print(out)
  • 排序
values, indice = torch.sort(x, dim=1, descending=False)  # descending=False表示升序
print(values)
print(indice)

三、tensor的索引

  • 简单操作
x = torch.tensor([1, 4, 5, 6, 0, 8, 6, 1, 4, 5])
print(x[0])  # 第一个元素
print(x[1: 6])  # 输出第2个到第6个元素

x = torch.randn((3, 10))  # size 3x10
print(x[0])  # 输出第1行的所有数, size 1x10
print(x[0, :])  # size 1x10
print(x[:, 0])  # 输出第1列的所有数, size 10x1

x = torch.randn((4, 10))
rows = [1, 3]
colums = [2, 9]
print(x[rows, colums])  # (2, 3) (4, 10)
  •  带条件的
x = torch.tensor([1, 4, 5, 6, 0, 8, 6, 1, 4, 5])
print(torch.where(x > 5, x, x/2))   # x>5时输出x,否则输出x/2
  •  其他
print(x.unique())  # 输出不重复的元素
print(x.numel())  # 输出x中的元素个数
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyTorch提供了丰富的张量算术运算,可以对张量进行各种复杂的运算。你可以使用PyTorch的算术函数来执行加法、减法、乘法和除法操作。例如,你可以使用add()函数执行张量的加法运算,subtract()函数执行减法运算,multiply()函数执行乘法运算,divide()函数执行除法运算。 此外,PyTorch还支持复杂数的算术运算,通过安装"pytorch-complex-tensor"库,你可以使用该库中提供的ComplexTensor类来进行复杂数的模拟算术运算,该库支持渐变。你可以使用ComplexTensor类来创建初始张量,并在其中执行复杂的算术运算。 下面是一个使用PyTorch进行张量算术运算的例子: ``` import torch # 创建两个张量 a = torch.tensor([1, 2, 3]) b = torch.tensor([4, 5, 6]) # 执行加法运算 c = torch.add(a, b) # 执行减法运算 d = torch.subtract(a, b) # 执行乘法运算 e = torch.multiply(a, b) # 执行除法运算 f = torch.divide(a, b) # 输出结果 print(c) # tensor([5, 7, 9]) print(d) # tensor([-3, -3, -3]) print(e) # tensor([4, 10, 18]) print(f) # tensor([0.25, 0.4, 0.5]) ``` 通过使用PyTorch张量算术运算,你可以对张量进行各种复杂的运算,并得到所需的结果。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [[PyTroch系列-7]:PyTorch基础 - 张量的算术运算](https://blog.csdn.net/HiWangWenBing/article/details/119428023)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [pytorch-complex-tensor:Pytorch的非官方复张量和标量支持](https://download.csdn.net/download/weixin_42128537/18698908)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值