Pytorch中torch.max()函数的使用

it2023-08-30  71

torch.max(input, dim, keepdim=False, out=None) -> (Tensor, LongTensor) max函数需要注意的是,它是一个过载函数,函数参数不同函数的功能和返回值也不同。 input是一个tensor. 当max函数中有维数参数的时候,它的返回值为两个,一个为最大值,另一个为最大值的索引. 对于dim参数,以一个二维数据输入为例,当dim为0时,max()返回的是每列的最大值以及各列最大值的索引;当dim为1时,max()返回的是每行的最大值以及各行最大值的索引。

a = t.randn(3, 4) print(a) print('维度为0:') print(t.max(a, 0)) print('维度为1:') print(t.max(a, 1))

输出为:

tensor([[-1.2851, 1.2206, -0.2578, 1.4101], [ 0.6201, -0.8447, 0.0455, -1.0944], [ 0.8037, -1.2278, -0.3627, 0.0303]]) 维度为0: torch.return_types.max( values=tensor([0.8037, 1.2206, 0.0455, 1.4101]), indices=tensor([2, 0, 1, 0])) 维度为1: torch.return_types.max( values=tensor([1.4101, 0.6201, 0.8037]), indices=tensor([3, 0, 0]))
最新回复(0)