随笔记录——pytorch一元通用函数

1、获取一维tensor中的最大值
import torch
a = torch.randn(1, 3)
a
Out[4]: tensor([[-0.6893, -0.3273,  0.6810]])
torch.max(a)
Out[5]: tensor(0.6810)

1.1、 二维或者更多维数据中,获取各个维度的最大值
a = torch.randn(4, 4)
a
Out[7]: 
tensor([[ 0.0531,  0.1278,  0.0252, -0.7423],
        [ 0.5913,  0.3381, -0.0945,  1.9293],
        [-0.0192, -0.3136, -0.9444, -0.4563],
        [-1.4126, -2.3428, -0.0474, -1.0039]])
torch.max(a, 1)
Out[8]: 
torch.return_types.max(
values=tensor([ 0.1278,  1.9293, -0.0192, -0.0474]),
indices=tensor([1, 3, 0, 2]))
torch.max(a, 0)
Out[9]: 
torch.return_types.max(
values=tensor([0.5913, 0.3381, 0.0252, 1.9293]),
indices=tensor([1, 1, 0, 1]))
torch.max(a)
Out[10]: tensor(1.9293)

torch.max(a, 0).indices
Out[11]: tensor([1, 1, 0, 1])
2、测试输入中的所有元素是否计算为True
torch.all(a.bool())
Out[64]: tensor(True)
a = torch.rand(1, 2)
a
Out[66]: tensor([[0.5181, 0.9654]])
torch.all(a.bool())
Out[67]: tensor(True)
a = torch.arange(0, 3)
a
Out[69]: tensor([0, 1, 2])
torch.all(a)
Out[70]: tensor(False)
2.1、 二维或者更多维数据中,判断是否全为true
a = torch.arange(12).reshape((4, 3)).bool()
a
Out[84]: 
tensor([[False,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True]])
torch.all(a, dim=1)
Out[85]: tensor([False,  True,  True,  True])
torch.all(a, dim=0)
Out[86]: tensor([False,  True,  True])
torch.all(a, dim=1, keepdim=True)
Out[87]: 
tensor([[False],
        [ True],
        [ True],
        [ True]])
  • input: 输入的tensor
  • dim: 聚合的维度
  • keepdim: 是否保持维度
3、计算tensor的均值
tensor([-0.0867, -0.1155,  1.0731])
torch.mean(a)
Out[94]: tensor(0.2903)
3.1、多维tensor计算均值
a = torch.randn(4, 4)
a
Out[96]: 
tensor([[ 1.6702, -0.0506,  0.0056, -0.9968],
        [ 0.8423,  1.4097, -0.2560,  0.2021],
        [-2.5357,  0.9478, -0.0252,  1.9307],
        [ 1.1350, -1.3580,  1.5683, -0.6136]])
torch.mean(a, 1)
Out[97]: tensor([0.1571, 0.5495, 0.0794, 0.1829])
torch.mean(a, 1, True)
Out[98]: 
tensor([[0.1571],
        [0.5495],
        [0.0794],
        [0.1829]])
4、返回输入值的中值
a = torch.randn(1, 3)
a
Out[102]: tensor([[-0.1196,  0.6667, -0.0947]])
torch.median(a)
Out[103]: tensor(-0.0947)
4.1 在多维tensor中获取中值
a = torch.randn(4, 5)
a
Out[105]: 
tensor([[ 0.7206,  2.0982,  0.4741, -0.6925,  0.1256],
        [ 0.9775, -3.5454, -0.1150, -2.2993, -0.2888],
        [ 0.3592, -0.2771, -0.6156, -0.4904, -0.7169],
        [-0.2024, -0.1461,  0.8557,  0.3229,  1.9360]])
torch.median(a)
Out[106]: tensor(-0.1461)
torch.median(a, 1, keepdim=True)
Out[107]: 
torch.return_types.median(
values=tensor([[ 0.4741],
        [-0.2888],
        [-0.4904],
        [ 0.3229]]),
indices=tensor([[2],
        [4],
        [3],
        [3]]))

pytorch中的一元通用函数

名称描述
argmax返回输入张量中所有元素的最大值的索引。
argmin返回展平张量或沿维度的最小值的索引。
all测试输入中的所有元素是否计算为True。
any测试输入中的任何元素是否计算为True。
max返回输入张量中所有元素的最大值。
min返回输入张量中所有元素的最小值。
mean返回输入张量中所有元素的平均值。
median返回输入值的中值。
prod返回输入张量中所有元素的乘积。
quantile计算沿维度dim的每一行输入张量的第q个分位数。

待续。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值