pytorch clamp()使用

参考:https://pytorch.org/docs/stable/generated/torch.clamp.html#torch.clamp

使用格式

torch.clamp(input, min, max, out=None)→ Tensor

在最大值和最小值之间对input进行裁剪
y i = { min ⁡  if  x i < min ⁡ x i  if  m i n ≤ x i ≤ max ⁡ max ⁡  if  x i > max ⁡ \mathrm{y}_{\mathrm{i}}=\left\{\begin{array}{ll} \min & \text { if } \mathrm{x}_{\mathrm{i}}<\min \\ \mathrm{x}_{\mathrm{i}} & \text { if } \mathrm{min} \leq \mathrm{x}_{\mathrm{i}} \leq \max \\ \max & \text { if } \mathrm{x}_{\mathrm{i}}>\max \end{array}\right. yi=minximax if xi<min if minximax if xi>max

Pytorch代码

>>> a = torch.randn(4)
>>> a
tensor([-1.7120,  0.1734, -0.0478, -0.0922])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000,  0.1734, -0.0478, -0.0922])

另一种使用方式(代码来自faster_rcnn)

cls_bbox[..., 0] = (cls_bbox[..., 0]).clamp(min=0, max=width)
cls_bbox[..., 2] = (cls_bbox[..., 2]).clamp(min=0, max=width)
cls_bbox[..., 1] = (cls_bbox[..., 1]).clamp(min=0, max=height)
cls_bbox[..., 3] = (cls_bbox[..., 3]).clamp(min=0, max=height)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值