torch.ge()

参考torch.ge() - 云+社区 - 腾讯云

  • 函数作用
    torch.ge(a,b)比较a,b的大小,a为张量,b可以为和a相同形状的张量,也可以为一个常数。

  • 代码示例

>>> import torch
>>> a=torch.Tensor([[1,2,3],[4,5,6]])
>>> a
tensor([[1., 2., 3.],
        [4., 5., 6.]])
>>> b=torch.Tensor([[2,2,2],[4,5,7]])
>>> b
tensor([[2., 2., 2.],
        [4., 5., 7.]])
>>> torch.ge(a,b)      #逐个元素比较
tensor([[0, 1, 1],
        [1, 1, 0]], dtype=torch.uint8)
>>> c=torch.randn(4,4)
>>> c
tensor([[ 0.3021,  0.5238, -1.2107,  1.3706],
        [-0.0715, -0.1180,  0.2854,  0.2061],
        [ 0.7414,  0.2233, -0.4926, -0.6218],
        [-0.0471,  1.1931,  0.8465, -0.5087]])
>>> d=torch.randn(4,4)
>>> d
tensor([[-1.9698,  1.8576,  0.2773,  0.6412],
        [ 1.0153,  0.5828,  0.8718, -0.1757],
        [ 0.7880,  0.8339, -0.5220, -0.3703],
        [ 1.3132,  1.4456,  0.7565, -1.1955]])
>>> torch.ge(c,d)
tensor([[1, 0, 0, 1],
        [0, 0, 0, 1],
        [0, 0, 1, 0],
        [0, 0, 1, 1]], dtype=torch.uint8)
>>> torch.ge(d,1)               #和1比较大小
tensor([[0, 1, 0, 0],
        [1, 0, 0, 0],
        [0, 0, 0, 0],
        [1, 1, 0, 0]], dtype=torch.uint8)

猜你喜欢

转载自blog.csdn.net/weixin_36670529/article/details/113733651
Ge
今日推荐