tensor.max()

该博客介绍了如何在PyTorch中对三维张量在不同维度进行比较,以获取每个维度的最大值及其索引。通过示例代码展示了`score.max(dim)`函数在batch、列和行维度上的应用,详细解释了每一步的结果含义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tensor在不同维度比较大小,并取最大值

# 给定一个三维矩阵--[batch_size, n_tags, n_tags]
import torch

score = torch.tensor([[ [1,2,7],
                        [4,8,6],
                        [3,5,9]],
                      [ [8,1,3],
                        [6,9,4],
                        [2,5,7]]])
best_score, best_dst = score.max(0)
print(best_score)
print(best_dst)
print('---------------------------')
best_score, best_dst = score.max(1)
print(best_score)
print(best_dst)
print('---------------------------')
best_score, best_dst = score.max(2)
print(best_score)
print(best_dst)
print('---------------------------')
'''
score.max(0)在batch维度-第0维度进行比较:
tensor([[8, 2, 7],
        [6, 9, 6],
        [3, 5, 9]])
tensor([[1, 0, 0],
        [1, 1, 0],
        [0, 1, 0]])
如[1,2,7]和[8,1,3]相比较,得到[8, 2, 7],对应batch位置为[1, 0, 0]。
'''

'''
score.max(1)在第1维度进行比较:
tensor([[4, 8, 9],
        [8, 9, 7]])
tensor([[1, 1, 2],
        [0, 1, 2]])
如	[[1,2,7],
     [4,8,6],
     [3,5,9]]中的[1,4,3]、[2,8,5]和[7,6,9]相比较,得到[4,8,9],对应位置为[1, 1, 2]
'''

'''
score.max(2)在第2维度进行比较:
tensor([[7, 8, 9],
        [8, 9, 7]])
tensor([[2, 1, 2],
        [0, 1, 2]])
如	[[1,2,7],
     [4,8,6],
     [3,5,9]]中的[1,2,7]、[4,8,6]和[3,5,9]相比较,得到[7,8,9],对应位置为[2, 1, 2]
'''
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值