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)
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页