现在位置: 首页 > PyTorch 教程 > 正文

PyTorch torch.aminmax 函数


Pytorch torch 参考手册 Pytorch torch 参考手册

torch.aminmax 是 PyTorch 中用于返回张量的最小值和最大值的函数。

函数定义

torch.aminmax(input, dim=None, keepdim=False)

使用示例

实例

import torch

x = torch.tensor([[1, 3, 2], [4, 1, 3]])

# 返回所有元素的最小值和最大值
mm = torch.aminmax(x)
print("最小值:", mm[0])
print("最大值:", mm[1])

# 沿 dim=0 最小值和最大值
mm0 = torch.aminmax(x, dim=0)
print("dim=0 最小值:", mm0[0])
print("dim=0 最大值:", mm0[1])

# 沿 dim=1 最小值和最大值
mm1 = torch.aminmax(x, dim=1)
print("dim=1 最小值:", mm1[0])
print("dim=1 最大值:", mm1[1])

输出结果为:

最小值: tensor(1)
最大值: tensor(4)
dim=0 最小值: tensor([1, 1, 2])
dim=0 最大值: tensor([4, 3, 3])
dim=1 最小值: tensor([1, 1])
dim=1 最大值: tensor([3, 4])

Pytorch torch 参考手册 Pytorch torch 参考手册