Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.mint.clamp

mindspore.mint.clamp(input, min=None, max=None) Tensor[source]

Clamps tensor values between the specified minimum value and maximum value.

Limits the value of input to a range, whose lower limit is min and upper limit is max .

outi={max if inputimaxinputi if min<inputi<maxmin if inputimin

Note

  • min and max cannot be None at the same time;

  • When min is None and max is not None, the elements in Tensor larger than max will become max;

  • When min is not None and max is None, the elements in Tensor smaller than min will become min;

  • If min is greater than max, the value of all elements in Tensor will be set to max;

  • The data type of input, min and max should support implicit type conversion and cannot be bool type.

Parameters
  • input (Tensor) – Input data, which type is Tensor. Tensors of arbitrary dimensions are supported.

  • min (Union(Tensor, float, int), optional) – The minimum value. Default: None .

  • max (Union(Tensor, float, int), optional) – The maximum value. Default: None .

Returns

Tensor, a clipped Tensor. The data type and shape are the same as input.

Raises
  • ValueError – If both min and max are None.

  • TypeError – If the type of input is not Tensor.

  • TypeError – If the type of min is not in None, Tensor, float or int.

  • TypeError – If the type of max is not in None, Tensor, float or int.

Supported Platforms:

Ascend

Examples

>>> # case 1: the data type of input is Tensor
>>> import mindspore
>>> from mindspore import Tensor, mint
>>> import numpy as np
>>> min_value = Tensor(5, mindspore.float32)
>>> max_value = Tensor(20, mindspore.float32)
>>> input = Tensor(np.array([[1., 25., 5., 7.], [4., 11., 6., 21.]]), mindspore.float32)
>>> output = mint.clamp(input, min_value, max_value)
>>> print(output)
[[ 5. 20.  5.  7.]
 [ 5. 11.  6. 20.]]
>>> # case 2: the data type of input is number
>>> import mindspore
>>> from mindspore import Tensor, mint
>>> import numpy as np
>>> min_value = 5
>>> max_value = 20
>>> input = Tensor(np.array([[1., 25., 5., 7.], [4., 11., 6., 21.]]), mindspore.float32)
>>> output = mint.clamp(input, min_value, max_value)
>>> print(output)
[[ 5. 20.  5.  7.]
 [ 5. 11.  6. 20.]]