pytorch中的torch.gt()函数解读

函数作用

torch.gt(a,b)函数比较a中元素大于(这里是严格大于)b中对应元素,大于则为1,不大于则为0,这里a为Tensor,b可以为与a的size相同的Tensor或常数。

代码示例

>>> import torch
>>> a=torch.randn(2,4)
>>> a
tensor([[-0.5466,  0.9203, -1.3220, -0.7948],
        [ 2.0300,  1.3090, -0.5527, -0.1326]])
>>> b=torch.randn(2,4)
>>> b
tensor([[-0.0160, -0.3129, -1.0287,  0.5962],
        [ 0.3191,  0.7988,  1.4888, -0.3341]])
>>> torch.gt(a,b)            #得到a中比b中元素大的位置
tensor([[0, 1, 0, 0],
        [1, 1, 0, 1]], dtype=torch.uint8)
>>> torch.gt(b,a)           #b中比a中大
tensor([[1, 0, 1, 1],
        [0, 0, 1, 0]], dtype=torch.uint8)
>>> torch.gt(a,1)
tensor([[0, 0, 0, 0],
        [1, 1, 0, 0]], dtype=torch.uint8)
>>> c=torch.Tensor([[1,2,3],[4,5,6]])
>>> d=torch.Tensor([[1,1,3],[5,5,5]])
>>> torch.gt(c,d)                   #必须是严格大于才为1
tensor([[0, 1, 0],
        [0, 0, 1]], dtype=torch.uint8)

更多参考:

http://www.voidcn.com/search/gdxsar
https://vimsky.com/examples/detail/python-method-torch.gt.html
https://www.cnblogs.com/lqchen/p/12346748.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值