torch.where()函数解读

  • 函数作用
    torch.where()函数的作用是按照一定的规则合并两个tensor类型。
  • 代码示例
>>> import torch
>>> a=torch.randn(3,5)
>>> b=torch.ones(3,5)
>>> a
tensor([[-0.0310,  1.5895,  1.6003, -1.7584,  1.1478],
        [ 0.6773,  0.7763,  0.5024,  0.4952,  0.4198],
        [ 1.5132,  0.5185,  0.2956, -0.6312, -1.4787]])
>>> b
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])
>>> torch.where(a>0,a,b)      # 满足条件返回a, 不满足条件返回b
tensor([[1.0000, 1.5895, 1.6003, 1.0000, 1.1478],
        [0.6773, 0.7763, 0.5024, 0.4952, 0.4198],
        [1.5132, 0.5185, 0.2956, 1.0000, 1.0000]])

你可能感兴趣的:(python)