torch.ge()与nn.masked_select()使用

1.torch.ge(inputother*out=None) → Tensor

Computes input≥other element-wise. 返回的是bool tensor,shape与input相同

Parameters

  • input (Tensor) – the tensor to compare

  • other (Tensor or float) – the tensor or value to compare

Keyword Arguments

out (Tensoroptional) – the output tensor.

Returns

A boolean tensor that is True where input is greater than or equal to other and False elsewhere

2.nn.masked_select()

配合torch.ge使用,用于筛选出满足ge条件的input中的数据

Returns a new 1-D tensor which indexes the input tensor according to the boolean mask mask which is a BoolTensor.

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable.

Parameters

  • input (Tensor) – the input tensor.

  • mask (BoolTensor) – the tensor containing the binary mask to index with

Keyword Arguments

out (Tensoroptional) – the output tensor.

import torch
​
x = torch.randn([3, 4])
print(x)
# 将x中的每一个元素与0.5进行比较
# 当元素大于等于0.5返回True,否则返回False
mask = x.ge(0.5)
print(mask)
print(torch.masked_select(x, mask))

​
'''
tensor([[ 1.2001,  1.2968, -0.6657, -0.6907],
        [-2.0099,  0.6249, -0.5382,  1.4458],
        [ 0.0684,  0.4118,  0.1011, -0.5684]])
tensor([[ True,  True, False, False],
        [False,  True, False,  True],
        [False, False, False, False]])
tensor([1.2001, 1.2968, 0.6249, 1.4458])
'''

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值