clamp¶
Documentation¶
-
treetensor.torch.
clamp
(input, *args, **kwargs)[source]¶ Clamp all elements in
input
into the range [min
,max
].Examples:
>>> import torch >>> import treetensor.torch as ttorch >>> ttorch.clamp(ttorch.tensor([-1.7120, 0.1734, -0.0478, 2.0922]), min=-0.5, max=0.5) tensor([-0.5000, 0.1734, -0.0478, 0.5000]) >>> ttorch.clamp(ttorch.tensor({ ... 'a': [-1.7120, 0.1734, -0.0478, 2.0922], ... 'b': {'x': [[-0.9049, 1.7029, -0.3697], [0.0489, -1.3127, -1.0221]]}, ... }), min=-0.5, max=0.5) <Tensor 0x7fbf5332a7c0> ├── a --> tensor([-0.5000, 0.1734, -0.0478, 0.5000]) └── b --> <Tensor 0x7fbf5332a880> └── x --> tensor([[-0.5000, 0.5000, -0.3697], [ 0.0489, -0.5000, -0.5000]])
Torch Version Related
This documentation is based on torch.clamp in torch v1.9.0+cu102. Its arguments’ arrangements depend on the version of pytorch you installed.
If some arguments listed here are not working properly, please check your pytorch’s version with the following command and find its documentation.
1 | python -c 'import torch;print(torch.__version__)' |
The arguments and keyword arguments supported in torch v1.9.0+cu102 is listed below.
Description From Torch v1.9.0+cu102¶
-
torch.
clamp
(input, min=None, max=None, *, out=None) → Tensor¶ Clamps all elements in
input
into the range [min
,max
]. Letting min_value and max_value bemin
andmax
, respectively, this returns:\[y_i = \min(\max(x_i, \text{min\_value}_i), \text{max\_value}_i)\]If
min
isNone
, there is no lower bound. Or, ifmax
isNone
there is no upper bound.Note
If
min
is greater thanmax
torch.clamp(..., min, max)
sets all elements ininput
to the value ofmax
.- Args:
input (Tensor): the input tensor. min (Number or Tensor, optional): lower-bound of the range to be clamped to max (Number or Tensor, optional): upper-bound of the range to be clamped to
- Keyword args:
out (Tensor, optional): the output tensor.
Example:
>>> 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]) >>> min = torch.linspace(-1, 1, steps=4) >>> torch.clamp(a, min=min) tensor([-1.0000, 0.1734, 0.3333, 1.0000])