PyTorch基础(20)-- torch.gt() / torch.ge() / torch.le() / torch.lt()方法

小明 2025-05-06 14:27:56 7

一、前言

嗯……���近遇到的奇奇怪怪的方法很多了,学无止境啊!学不完啊,根本学不完!本篇文章介绍四个方法:torch.gt()、torch.ge()、torch.le()和torch.lt()方法,由于这四个方法很相似,所以放到一块解析了!

函数作用
torch.gt(intput, other)input > other
torch.ge(intput, other)input ≥ other
torch.le(intput, other)input ≤ other
torch.lt(intput, other)input

二、方法解析

由于方法类似,我们只取一个进行详细解析,其它三个只是比较的方式不同而已,用法完全相同。

2.1 torch.gt()

按照惯例,先对函数的参数及return做一个简单的解释。函数的作用:元素级的比较input是否大于other,如果input>other,那input当前位置的元素为True;否则,当前位置的元素为False。

argument

  • input:要比较的tensor
  • other:可以是一个float类型的数,或者是可以与input进行广播的tensor

    return

    • 返回一个boolean类型的tensor

      2.2 torch.ge()

      作用:判断input是否大于等于other。

      2.3 torch.le()

      作用:判断input是否小于等于other。

      2.4 torch.lt()

      作用:判断input是否小于other。

      三、案例分析

      3.1 torch.gt()

      • 案例1
        t = torch.tensor([[1,2],[3,1]])
        other = 1
        torch.gt(t, 1)
        
        • 运行结果
          tensor([[False,  True],
                  [ True, False]])
          
          • 案例2
            t = torch.tensor([[1,2],[3,1]])
            other = torch.tensor([[2,1],[1,2]])
            torch.gt(t, other)
            
            • 运行结果
              tensor([[False,  True],
                      [ True, False]])
              

              3.2 torch.ge()

              • 案例1
                t = torch.tensor([[1,2],[3,1]])
                other = 1
                torch.ge(t, 1)
                
                • 运行结果
                  tensor([[True, True],
                          [True, True]])
                  
                  • 案例2
                    t = torch.tensor([[1,2],[3,1]])
                    other = torch.tensor([[2,1],[1,2]])
                    torch.ge(t, other)
                    
                    • 运行结果
                      tensor([[False,  True],
                              [ True, False]])
                      

                      3.3 torch.le()

                      • 案例1
                        t = torch.tensor([[1,2],[3,1]])
                        other = 1
                        torch.le(t, 1)
                        
                        • 运行结果
                          tensor([[ True, False],
                                  [False,  True]])
                          
                          • 案例2
                            t = torch.tensor([[1,2],[3,1]])
                            other = torch.tensor([[2,1],[1,2]])
                            torch.le(t, other)
                            
                            • 运行结果
                              tensor([[ True, False],
                                      [False,  True]])
                              

                              3.4 torch.lt()

                              • 案例1
                                t = torch.tensor([[1,2],[3,1]])
                                other = 1
                                torch.lt(t, 1)
                                
                                tensor([[False, False],
                                        [False, False]])
                                
                                • 案例2
                                  t = torch.tensor([[1,2],[3,1]])
                                  other = torch.tensor([[2,1],[1,2]])
                                  torch.lt(t, other)
                                  
                                  tensor([[ True, False],
                                          [False,  True]])
                                  

                                  参考文献

                                  [1]torch.gt()方法官方解释

                                  [2]torch.ge()方法官方解释

                                  [3]torch.le()方法官方解释

                                  [4]torch.lt()方法官方解释

                                  今天的你,学会了吗?

The End
微信