Source code for mindspore.ops.function.math_func

# Copyright 2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

"""Defines math operators with functional form."""

import math
from itertools import zip_longest
from collections import deque
import numpy as np
import mindspore.ops as ops
from mindspore.common import dtype as mstype
from mindspore.ops.primitive import constexpr
from mindspore.ops import operations as P
from mindspore.ops import composite as C
from mindspore.ops.operations._inner_ops import Cummin
from mindspore.ops.operations.math_ops import STFT
from mindspore.ops.operations.math_ops import ReduceStd
from mindspore.ops.operations.math_ops import Logit
from mindspore.ops.operations.math_ops import LuUnpack
from mindspore.nn import layer
from mindspore._checkparam import check_is_number
from mindspore._checkparam import Rel
from mindspore.ops.operations.math_ops import (
    Bernoulli,
    BesselI0,
    BesselI1,
    BesselJ0,
    BesselJ1,
    BesselK0,
    BesselK0e,
    BesselY0,
    BesselY1,
    BesselK1,
    BesselK1e,
    MatrixExp,
    MatrixSolve,
    Median,
    Orgqr,
    Renorm,
    Hypot,
    Heaviside,
    Lcm,
    Gcd,
    Sinc,
    SparseSegmentMean,
    InplaceUpdateV2,
)
from mindspore.common.tensor import Tensor
from mindspore._checkparam import Validator as validator
from mindspore.ops._primitive_cache import _get_cache_prim
from ..._c_expression import Tensor as Tensor_


@constexpr
def _make_tensor(val, dtype):
    """Returns the tensor with value `val` and dtype `dtype`."""
    return Tensor(val, dtype)


@constexpr
def get_x_shape(x_shape):
    s = 1
    for i in x_shape:
        s = s * i
    return (s,)


#####################################
# Public Operation Functions.
#####################################
absolute = P.Abs()
tensor_ceil = P.Ceil()
tensor_add = P.Add()
neg_tensor = P.Neg()
tensor_sub = P.Sub()
tensor_mul = P.Mul()
tensor_div = P.RealDiv()
tensor_floordiv = P.FloorDiv()
floordiv = tensor_floordiv
xdivy_ = P.Xdivy()
tensor_pow = P.Pow()
pows = tensor_pow
tensor_mod = P.FloorMod()
floormod = tensor_mod
tensor_exp = P.Exp()
tensor_expm1 = P.Expm1()
tensor_lt = P.Less()
tensor_le = P.LessEqual()
tensor_gt = P.Greater()
tensor_ge = P.GreaterEqual()
not_equal = P.NotEqual()

#####################################
# Private Operation Functions.
#####################################
addn_ = P.AddN()
log_ = P.Log()
floor_ = P.Floor()
logical_not_ = P.LogicalNot()
logical_or_ = P.LogicalOr()
logical_and_ = P.LogicalAnd()
sin_ = P.Sin()
sinc_ = Sinc()
cos_ = P.Cos()
tan_ = P.Tan()
asin_ = P.Asin()
acos_ = P.ACos()
atan_ = P.Atan()
sinh_ = P.Sinh()
cosh_ = P.Cosh()
tanh_ = P.Tanh()
asinh_ = P.Asinh()
acosh_ = P.Acosh()
atanh_ = P.Atanh()
bitwise_and_ = P.BitwiseAnd()
bitwise_or_ = P.BitwiseOr()
bitwise_xor_ = P.BitwiseXor()
inv_ = P.math_ops.Inv()
invert_ = P.Invert()
erf_ = P.Erf()
erfc_ = P.Erfc()
bessel_j1_ = BesselJ1()
bessel_j0_ = BesselJ0()
bessel_i0_ = BesselI0()
bessel_i0e_ = P.BesselI0e()
bessel_k0_ = BesselK0()
bessel_k0e_ = BesselK0e()
bessel_y0_ = BesselY0()
bessel_y1_ = BesselY1()
bessel_i1_ = BesselI1()
bessel_i1e_ = P.BesselI1e()
bessel_k1_ = BesselK1()
bessel_k1e_ = BesselK1e()
equal_ = P.Equal()
isfinite_ = P.IsFinite()
isnan_ = P.IsNan()
same_type_shape_ = P.SameTypeShape()
maximum_ = P.Maximum()
minimum_ = P.Minimum()
lerp_ = P.Lerp()
tensor_round_ = P.Round()
linspace_ = P.LinSpace()
matrix_determinant_ = P.MatrixDeterminant()
log_matrix_determinant_ = P.LogMatrixDeterminant()
matrix_exp_ = MatrixExp()
exp2_ = P.Pow()
truncate_div_ = P.TruncateDiv()
truncate_mod_ = P.TruncateMod()
trunc_ = P.Trunc()
sparse_segment_mean_ = SparseSegmentMean()
xlogy_ = P.Xlogy()
square_ = P.Square()
sqrt_ = P.Sqrt()


#####################################
# Element-wise Operation Functions.
#####################################

[docs]def addn(x): """ Computes addition of all input tensors element-wise. All input tensors must have the same shape. Args: x (Union(tuple[Tensor], list[Tensor])): A tuple or list composed of Tensor, the data type is `bool_ <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_ or `number <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_ . Returns: Tensor, has the same shape and dtype as each Tensor of `x`. Raises: TypeError: If `x` is neither tuple nor list. ValueError: If there are Tensors with different shapes in `x`. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> y = Tensor(np.array([4, 5, 6]), mindspore.float32) >>> output = ops.addn([x, y, x, y]) >>> print(output) [10. 14. 18.] """ return addn_(x)
[docs]def abs(x): r""" Returns absolute value of a tensor element-wise. .. math:: out_i = |x_i| Args: x (Tensor): The input tensor. The shape of tensor is :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape as the `x`. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([-1.0, 1.0, 0.0]), mindspore.float32) >>> output = ops.abs(x) >>> print(output) [1. 1. 0.] """ return absolute(x)
[docs]def add(x, y): r""" Adds two input tensors element-wise. .. math:: out_{i} = x_{i} + y_{i} .. note:: - Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. - The inputs must be two tensors or one tensor and one scalar. - When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them can be broadcast. - When the inputs are one tensor and one scalar, the scalar could only be a constant. Args: x (Union[Tensor, number.Number, bool]): The first input is a number.Number or a bool or a tensor whose data type is `number <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_ or `bool_ <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_. y (Union[Tensor, number.Number, bool]): The second input, when the first input is a Tensor, the second input should be a number.Number or bool value, or a Tensor whose data type is number or bool\_. When the first input is Scalar, the second input must be a Tensor whose data type is number or bool\_. Returns: Tensor, the shape is the same as the one of the input `x` , `y` after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs. Raises: TypeError: If `x` and `y` is not one of the following: Tensor, number.Number, bool. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> # case 1: x and y are both Tensor. >>> x = Tensor(np.array([1, 2, 3]).astype(np.float32)) >>> y = Tensor(np.array([4, 5, 6]).astype(np.float32)) >>> output = ops.add(x, y) >>> print(output) [5. 7. 9.] >>> # case 2: x is a scalar and y is a Tensor >>> x = Tensor(1, mindspore.int32) >>> y = Tensor(np.array([4, 5, 6]).astype(np.float32)) >>> output = ops.add(x, y) >>> print(output) [5. 6. 7.] >>> # the data type of x is int32, the data type of y is float32, >>> # and the output is the data format of higher precision float32. >>> print(output.dtype) Float32 """ return tensor_add(x, y)
[docs]def addcdiv(input_data, x1, x2, value): r""" Performs the element-wise division of tensor x1 by tensor x2, multiply the result by the scalar value and add it to input_data. .. math:: y[i] = input\_data[i] + value[i] * (x1[i] / x2[i]) Args: input_data (Tensor): The tensor to be added. x1 (Tensor): The numerator tensor. x2 (Tensor): The denominator tensor. value (Tensor): The multiplier for tensor x1/x2. Returns: Tensor, has the same shape and dtype as x1/x2. Raises: TypeError: If dtype of `x1`, `x2`, `value`, `input_data` is not tensor. ValueError: If `x1` could not be broadcast to a tensor with shape of `x2`. ValueError: If `value` could not be broadcast to tensors with shapes of `x1/x2`. ValueError: If `input_data` could not be broadcast to tensors with shapes of `value*(x1/x2)`. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> input_data = Tensor(np.array([1, 1, 1, 1]), mindspore.float32) >>> x1 = Tensor(np.array([1, 2, 3, 4]), mindspore.float32) >>> x2 = Tensor(np.array([4, 3, 2, 1]), mindspore.float32) >>> value = Tensor([1], mindspore.float32) >>> y = ops.addcdiv(input_data, x1, x2, value) >>> print(y) [1.25 1.6666667 2.5 5. ] """ return _get_cache_prim(P.Addcdiv)()(input_data, x1, x2, value)
[docs]def addcmul(input_data, x1, x2, value): r""" Performs the element-wise product of tensor x1 and tensor x2, multiply the result by the scalar value and add it to input_data. .. math:: output[i] = input\_data[i] + value[i] * (x1[i] * x2[i]) Args: input_data (Tensor): The tensor to be added. x1 (Tensor): The tensor to be multiplied. x2 (Tensor): The tensor to be multiplied. value (Tensor): The multiplier for tensor x1*x2. Returns: Tensor, has the same shape and dtype as x1*x2. Raises: TypeError: If dtype of `x1`, `x2`, `value`, `input_data` is not tensor. TypeError: If dtype of `input_data` is not one of: float32, float16, int32. TypeError: If dtype of `x1` or `x2` is not one of: float32, float16, int32. TypeError: If dtype of `value` is not one of: float32, float16, int32. ValueError: If `x1` could not be broadcast to a tensor with shape of `x2`. ValueError: If `value` could not be broadcast to tensors with shapes of `x1` * `x2`. ValueError: If `input_data` could not be broadcast to tensors with shapes of `value*(x1*x2)`. Supported Platforms: ``Ascend`` ``GPU`` Examples: >>> input_data = Tensor(np.array([1, 1, 1]), mindspore.float32) >>> x1 = Tensor(np.array([[1], [2], [3]]), mindspore.float32) >>> x2 = Tensor(np.array([[1, 2, 3]]), mindspore.float32) >>> value = Tensor([1], mindspore.float32) >>> y = ops.addcmul(input_data, x1, x2, value) >>> print(y) [[ 2. 3. 4.] [ 3. 5. 7.] [ 4. 7. 10.]] """ return _get_cache_prim(P.Addcmul)()(input_data, x1, x2, value)
def exp2(x): """ Computes the base two exponential function of input. Calculates ``2^x``. Args: x (Tensor): Input tensor. Returns: Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([2, 3, 4]), mindspore.float32) >>> output = ops.exp2(x) >>> print(output) [ 4. 8. 16.] """ tensor_2 = Tensor(np.array(2.0).astype(np.float32)) if x.dtype == mstype.float16: tensor_2 = Tensor(np.array(2.0).astype(np.float16)) return exp2_(tensor_2, x)
[docs]def argmin(x, axis=-1): """ Returns the indices of the minimum value of a tensor across the axis. If the shape of input tensor is :math:`(x_1, ..., x_N)`, the shape of the output tensor is :math:`(x_1, ..., x_{axis-1}, x_{axis+1}, ..., x_N)`. Args: x (Tensor): Input tensor. The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions. axis (int): Axis where the Argmin operation applies to. Default: -1. Returns: Tensor, indices of the min value of input tensor across the axis. Raises: TypeError: If `axis` is not an int. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> input_x = Tensor(np.array([2.0, 3.1, 1.2]), mindspore.float32) >>> index = ops.argmin(input_x) >>> print(index) 2 """ _argmin = _get_cache_prim(P.Argmin)(axis) return _argmin(x)
neg_tensor = P.Neg()
[docs]def neg(x): """ Returns a tensor with negative values of the input tensor element-wise. .. math:: out_{i} = - x_{i} Args: x (Tensor): The input tensor with a dtype of Number, its rank must be in [0, 7] inclusive. Returns: Tensor, has the same shape and dtype as input. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1, 2, -1, 2, 0, -3.5]), mindspore.float32) >>> output = ops.neg(x) >>> print(output) [-1. -2. 1. -2. 0. 3.5] """ return neg_tensor(x)
[docs]def ceil(x): r""" Rounds a tensor up to the closest integer element-wise. .. math:: out_i = \lceil x_i \rceil = \lfloor x_i \rfloor + 1 Args: x (Tensor): The input tensor with a dtype of float16 or float32, its rank must be in [0, 7] inclusive. Returns: Tensor, has the same shape as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16 or float32. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1.1, 2.5, -1.5]), mindspore.float32) >>> output = ops.ceil(x) >>> print(output) [ 2. 3. -1.] """ return tensor_ceil(x)
[docs]def round(x): r""" Returns half to even of a tensor element-wise. .. math:: out_i \approx x_i Args: x (Tensor): The input tensor. Returns: Tensor, has the same shape and type as the `x`. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.8, 1.5, 2.3, 2.5, -4.5]), mindspore.float32) >>> output = ops.round(x) >>> print(output) [ 1. 2. 2. 2. -4.] """ return tensor_round_(x)
[docs]def sub(x, y): r""" Subtracts the second input tensor from the first input tensor element-wise. .. math:: out_{i} = x_{i} - y_{i} .. note:: - Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. - The inputs must be two tensors or one tensor and one scalar. - When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them can be broadcast. - When the inputs are one tensor and one scalar, the scalar could only be a constant. Args: x (Union[Tensor, number.Number, bool]): The first input is a number.Number or a bool or a tensor whose data type is `number <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_ or `bool_ <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_. y (Union[Tensor, number.Number, bool]): The second input, when the first input is a Tensor, the second input should be a number.Number or bool value, or a Tensor whose data type is number or bool\_. When the first input is Scalar, the second input must be a Tensor whose data type is number or bool\_. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs. Raises: TypeError: If `x` and `y` is not a number.Number or a bool or a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1, 2, 3]), mindspore.int32) >>> y = Tensor(np.array([4, 5, 6]), mindspore.int32) >>> output = ops.sub(x, y) >>> print(output) [-3 -3 -3] """ return tensor_sub(x, y)
[docs]def mul(x, y): r""" Multiplies two tensors element-wise. .. math:: out_{i} = x_{i} * y_{i} .. note:: - Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. - The inputs must be two tensors or one tensor and one scalar. - When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them can be broadcast. - When the inputs are one tensor and one scalar, the scalar could only be a constant. Args: x (Union[Tensor, number.Number, bool]): The first input is a number.Number or a bool or a tensor whose data type is `number <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_ or `bool_ <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_. y (Union[Tensor, number.Number, bool]): The second input, when the first input is a Tensor, the second input should be a number.Number or bool value, or a Tensor whose data type is number or bool\_. When the first input is Scalar, the second input must be a Tensor whose data type is number or bool\_. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs. Raises: TypeError: If `x` and `y` is not one of the following: Tensor, number.Number, bool. ValueError: If `x` and `y` are not the same shape. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32) >>> y = Tensor(np.array([4.0, 5.0, 6.0]), mindspore.float32) >>> output = ops.mul(x, y) >>> print(output) [ 4. 10. 18.] """ return tensor_mul(x, y)
[docs]def div(x, y): """ Divides the first input tensor by the second input tensor in floating-point type element-wise. Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant. .. math:: out_{i} = x_{i} / y_{i} Args: x (Union[Tensor, Number, bool]): The first input is a number or a bool or a tensor whose data type is number or bool. y (Union[Tensor, Number, bool]): The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs. Raises: TypeError: If `x` and `y` is not one of the following: Tensor, Number, bool. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32) >>> y = Tensor(np.array([4.0, 5.0, 6.0]), mindspore.float32) >>> output = ops.div(x, y) >>> print(output) [0.25 0.4 0.5 ] """ return tensor_div(x, y)
[docs]def floor_div(x, y): """ Divides the first input tensor by the second input tensor element-wise and round down to the closest integer. Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant. .. math:: out_{i} = \\text{floor}( \\frac{x_i}{y_i}) where the :math:`floor` indicates the Floor operator, for more details, please refer to the Floor operator. Args: x (Union[Tensor, Number, bool]): The first input is a number or a bool or a tensor whose data type is number or bool. y (Union[Tensor, Number, bool]): The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs. Raises: TypeError: If neither `x` nor `y` is a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([2, 4, -1]), mindspore.int32) >>> y = Tensor(np.array([3, 3, 3]), mindspore.int32) >>> output = ops.floor_div(x, y) >>> print(output) [ 0 1 -1] """ return tensor_floordiv(x, y)
[docs]def pow(x, y): r""" Calculates the `y` power of each element in `x`. .. math:: out_{i} = x_{i} ^{ y_{i}} .. note:: - Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. - The inputs must be two tensors or one tensor and one scalar. - When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them can be broadcast. Args: x (Union[Tensor, number.Number, bool]): The first input is a number.Number or a bool or a tensor whose data type is `number <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_ or `bool_ <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_. y (Union[Tensor, number.Number, bool]): The second input, when the first input is a Tensor, the second input should be a number.Number or bool value, or a Tensor whose data type is number or bool\_. When the first input is Scalar, the second input must be a Tensor whose data type is number or bool\_. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs. Raises: TypeError: If `x` and `y` is not one of the following: Tensor, number.Number or bool. ValueError: If the shape of `x` and `y` are different. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) >>> y = 3.0 >>> output = ops.pow(x, y) >>> print(output) [ 1. 8. 64.] >>> >>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) >>> y = Tensor(np.array([2.0, 4.0, 3.0]), mindspore.float32) >>> output = ops.pow(x, y) >>> print(output) [ 1. 16. 64.] """ return tensor_pow(x, y)
[docs]def floor_mod(x, y): r""" Computes the remainder of division element-wise. It's a flooring divide. E.g. :math:`floor(x / y) * y + mod(x, y) = x`. Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be both bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant. .. math:: out_{i} =\text{floor}(x_{i} // y_{i}) where the :math:`floor` indicates the Floor operator, for more details, please refer to the Floor operator. .. warning:: - The input data does not support 0. - When the elements of input exceeds 2048 , the accuracy of operator cannot guarantee the requirement of double thousandths in the mini form. - Due to different architectures, the calculation results of this operator on NPU and CPU may be inconsistent. - If shape is expressed as (D1,D2... ,Dn), then D1\*D2... \*DN<=1000000,n<=8. Args: x (Union[Tensor, Number, bool]): The first input is a number or a bool or a tensor whose data type is number or bool. y (Union[Tensor, Number, bool]): The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision of the two inputs. Raises: TypeError: If neither `x` nor `y` is a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([2, 4, -1]), mindspore.int32) >>> y = Tensor(np.array([3, 3, 3]), mindspore.int32) >>> output = ops.floor_mod(x, y) >>> print(output) [2 1 2] """ return tensor_mod(x, y)
[docs]def exp(x): r""" Returns exponential of a tensor element-wise. .. math:: out_i = e^{x_i} Args: x (Tensor): The input tensor, its rank must be in [0, 7] inclusive. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) >>> output = ops.exp(x) >>> print(output) [ 2.718282 7.389056 54.598152] """ return tensor_exp(x)
[docs]def expm1(x): r""" Returns exponential then minus 1 of a tensor element-wise. .. math:: out_i = e^{x_i} - 1 Args: x (Tensor): The input tensor with a dtype of float16 or float32, its rank must be in [0, 7] inclusive. Returns: Tensor, has the same shape as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is neither float16 nor float32. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.0, 1.0, 2.0, 4.0]), mindspore.float32) >>> output = ops.expm1(x) >>> print(output) [ 0. 1.718282 6.389056 53.598152] """ return tensor_expm1(x)
[docs]def log(x): """ Returns the natural logarithm of a tensor element-wise. .. math:: y_i = log_e(x_i) .. note:: The dimension of the input Tensor on Ascend should be less than or equal to 8, and the dimension of the input Tensor on the CPU should be less than 8. .. warning:: If the input value of operator Log is within the range (0, 0.01] or [0.95, 1.05], the output accuracy may be affacted. Args: x (Tensor): Input Tensor of any dimension. The value must be greater than 0. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64 on GPU and CPU. TypeError: If dtype of `x` is not float16 or float32 on Ascend. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) >>> output = ops.log(x) >>> print(output) [0. 0.6931472 1.3862944] """ return log_(x)
[docs]def floor(x): r""" Rounds a tensor down to the closest integer element-wise. .. math:: out_i = \lfloor x_i \rfloor Args: x (Tensor): The input tensor, its rank must be in [0, 7] inclusive and data type must be float16, float32 or float64. Returns: Tensor, has the same shape as `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not in [float16, float32, float64]. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1.1, 2.5, -1.5]), mindspore.float32) >>> output = ops.floor(x) >>> print(output) [ 1. 2. -2.] """ return floor_(x)
[docs]def inplace_update(x, v, indices): """ Updates specified rows with values in `v`. Note: `indices` refers to the left-most dimension. Args: indices (Union[int, tuple], Tensor): Indices into the left-most dimension of `x`, and determines which rows of x to update with v. It is an int or tuple, whose value is in [0, the first dimension size of x). If the type is Tensor, it supports dynamic shape. Otherwise, it only supports static shape. x (Tensor): A tensor which to be inplace updated. It can be one of the following data types: float32, float16 and int32. v (Tensor): A tensor with the same type as `x` and the same dimension size as `x` except the first dimension, which must be the same as the size of `indices`. Returns: Tensor, with the same type and shape as the input `x`. Raises: TypeError: If `indices` is neither int nor tuple. TypeError: If `indices` is a tuple and its element is not an int. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> indices = (0, 1) >>> x = Tensor(np.array([[1, 2], [3, 4], [5, 6]]), mindspore.float32) >>> v = Tensor(np.array([[0.5, 1.0], [1.0, 1.5]]), mindspore.float32) >>> output = ops.inplace_update(x, v, indices) >>> print(output) [[0.5 1. ] [1. 1.5] [5. 6. ]] """ if not isinstance(indices, (Tensor, Tensor_)): inplace_update_inner = _get_cache_prim(P.InplaceUpdate)(indices) output = inplace_update_inner(x, v) else: inplace_update_inner = InplaceUpdateV2() output = inplace_update_inner(x, indices, v) return output
[docs]def inplace_add(x, v, indices): """ Adds `v` into specified rows of `x`. Computes `y` = `x`; y[i,] += `v`. Note: `indices` refers to the left-most dimension. Args: x (Tensor): The first input is a tensor whose data type is float16, float32, float64 or int32. :math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8. v (Tensor): The second input is a tensor that has the same dimension sizes as `x` except the first dimension, which must be the same as indices' size. It has the same data type with `x`. indices (Union[int, tuple]): Indices into the left-most dimension of `x`, and determines which rows of `x` to add with `v`. It is an integer or a tuple, whose value is in [0, the first dimension size of `x`). Returns: Tensor, has the same shape and dtype as `x`. Raises: TypeError: If `indices` is neither int nor tuple. TypeError: If `indices` is a tuple whose elements are not all int. ValueError: If the rank of `x` is not equal to the rank of `v`. ValueError: If the length of `indices` is not equal to `v.shape[0]`. ValueError: If the values of `indices` are not in range of `[0, x.shape[0])`. Supported Platforms: ``Ascend`` ``CPU`` Examples: >>> import numpy as np >>> import mindspore >>> from mindspore import Tensor, ops >>> indices = (0, 1) >>> x = Tensor(np.array([[1, 2], [3, 4], [5, 6]]), mindspore.float32) >>> input_v = Tensor(np.array([[0.5, 1.0], [1.0, 1.5]]), mindspore.float32) >>> output = ops.inplace_add(x, input_v, indices) >>> print(output) [[1.5 3. ] [4. 5.5] [5. 6. ]] """ inplace_add_inner = _get_cache_prim(P.InplaceAdd)(indices) return inplace_add_inner(x, v)
[docs]def inplace_sub(x, v, indices): r""" Subtracts `v` into specified rows of `x`. Computes :math:`y = x`; :math:`y[i,] -= input\_v`. Note: `indices` refers to the left-most dimension. Args: indices (Union[int, tuple]): Indices into the left-most dimension of `x`, and determines which rows of `x` to subtract with `v`. It is an int or tuple, whose value is in [0, the first dimension size of `x`). x (Tensor): The first input is a tensor whose data type is float16, float32, float64 or int32. :math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8. v (Tensor): The second input is a tensor who has the same dimension sizes as `x` except the first dimension, which must be the same as indices' size. It has the same data type with `x`. Returns: Tensor, has the same shape and dtype as `x`. Raises: TypeError: If `indices` is neither int nor tuple. TypeError: If `indices` is a tuple whose elements are not all int. ValueError: If the rank of `x` is not equal to the rank of `v`. ValueError: If the length of `indices` is not equal to `v.shape[0]`. ValueError: If the values of `indices` are not in range of `[0, x.shape[0])`. Supported Platforms: ``Ascend`` ``CPU`` Examples: >>> import numpy as np >>> import mindspore >>> from mindspore import Tensor, ops >>> indices = (0, 1) >>> x = Tensor(np.array([[1, 2], [3, 4], [5, 6]]), mindspore.float32) >>> input_v = Tensor(np.array([[0.5, 1.0], [1.0, 1.5]]), mindspore.float32) >>> output = ops.inplace_sub(x, input_v, indices) >>> print(output) [[0.5 1. ] [2. 2.5] [5. 6. ]] """ inplace_sub_inner = _get_cache_prim(P.InplaceSub)(indices) return inplace_sub_inner(x, v)
[docs]def logical_not(x): """ Computes the "logical NOT" of a tensor element-wise. .. math:: out_{i} = \\neg x_{i} Args: x (Tensor): The input tensor whose dtype is bool. :math:`(N,*)` where :math:`*` means,any number of additional dimensions. Returns: Tensor, the shape is the same as the `x`, and the dtype is bool. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not a bool. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([True, False, True]), mindspore.bool_) >>> output = ops.logical_not(x) >>> print(output) [False True False] """ return logical_not_(x)
[docs]def logical_or(x, y): """ Computes the "logical OR" of two tensors element-wise. Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one bool. When the inputs are two tensors, the shapes of them could be broadcast, and the data types of them must be bool. When the inputs are one tensor and one bool, the bool object could only be a constant, and the data type of the tensor must be bool. .. math:: out_{i} = x_{i} \\vee y_{i} Note: LogicalOr supports broadcasting. Args: x (Union[Tensor, bool]): The first input is a bool or a tensor whose data type is bool. y (Union[Tensor, bool]): The second input is a bool when the first input is a tensor or a tensor whose data type is bool. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is bool. Raises: TypeError: If neither `x` nor `y` is a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([True, False, True]), mindspore.bool_) >>> y = Tensor(np.array([True, True, False]), mindspore.bool_) >>> output = ops.logical_or(x, y) >>> print(output) [ True True True] """ return logical_or_(x, y)
[docs]def logical_and(x, y): r""" Computes the "logical AND" of two tensors element-wise. Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one bool. When the inputs are two tensors, the shapes of them could be broadcast, and the data types of them must be bool. When the inputs are one tensor and one bool, the bool object could only be a constant, and the data type of the tensor must be bool. .. math:: out_{i} = x_{i} \wedge y_{i} Note: LogicalAnd supports broadcasting. Args: x (Union[Tensor, bool]): The first input is a bool or a tensor whose data type is bool. y (Union[Tensor, bool]): The second input is a bool when the first input is a tensor or a tensor whose data type is bool. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is bool. Raises: TypeError: If neither `x` nor `y` is a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([True, False, True]), mindspore.bool_) >>> y = Tensor(np.array([True, True, False]), mindspore.bool_) >>> output = ops.logical_and(x, y) >>> print(output) [ True False False] """ return logical_and_(x, y)
[docs]def sin(x): r""" Computes sine of the input element-wise. .. math:: out_i = sin(x_i) Args: x (Tensor): The shape of tensor is :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape as `x`. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.62, 0.28, 0.43, 0.62]), mindspore.float32) >>> output = ops.sin(x) >>> print(output) [0.5810352 0.27635565 0.41687083 0.5810352] """ return sin_(x)
def sinc(x): r""" Computes the normalized sinc of input. .. math:: out_i = sinc(x_i) Args: x (Tensor): The shape of tensor is :math:`(x_1, x_2, ..., x_R)`. Returns: Tensor, has the same shape as the `x`. The dtype of output is float32 when dtype of `x` is in [uint8, uint8, uint16, int16, uint32, int32, uint64, int64, bool]. Otherwise output has the same dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``CPU`` Examples: >>> x = Tensor(np.array([0.62, 0.28, 0.43, 0.62]), mindspore.float32) >>> output = ops.sinc(x) >>> print(output) [0.47735003 0.8759357 0.7224278 0.47735003] """ return sinc_(x)
[docs]def cos(x): r""" Computes cosine of input element-wise. .. math:: out_i = cos(x_i) .. warning:: Currently support Float16, Float32 data type. If use Float64, there may be a problem of missing precision. Args: x (Tensor): The shape of tensor is :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape as `x`. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) >>> output = ops.cos(x) >>> print(output) [0.971338 0.6748758 0.95233357 0.9959527] """ return cos_(x)
[docs]def tan(x): r""" Computes tangent of `x` element-wise. .. math:: out_i = tan(x_i) Args: x (Tensor): The input Tensor, valid for any dimensions. Returns: Tensor, has the same shape as `x`. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``CPU`` ``GPU`` Examples: >>> x = Tensor(np.array([-1.0, 0.0, 1.0]), mindspore.float32) >>> output = ops.tan(x) >>> print(output) [-1.5574081 0. 1.5574081] """ return tan_(x)
[docs]def xlogy(x, y): r""" Computes the first input tensor multiplied by the logarithm of second input tensor element-wise. Returns zero when `x` is zero. .. math:: out_i = x_{i}\ln{y_{i}} Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant. .. warning:: - On Ascend, the data type of `x` and `y` must be float16 or float32. Args: x (Union[Tensor, number.Number, bool]): The first input is a number.Number or a bool or a tensor whose data type is `number <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_ or `bool_ <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_. y (Union[Tensor, number.Number, bool]): The second input is a number.Number or a bool when the first input is a tensor or a tensor whose data type is number or bool\_. When the first input is Scalar, the second input must be a Tensor whose data type is number or bool\_. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs. Raises: TypeError: If `x` and `y` is not a number.Number or a bool or a Tensor. TypeError: If dtype of `x` and 'y' is not in [float16, float32, float64, complex64, complex128]. ValueError: If `x` could not be broadcast to a tensor with shape of `y`. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([-5, 0, 4]), mindspore.float32) >>> y = Tensor(np.array([2, 2, 2]), mindspore.float32) >>> output = ops.xlogy(x, y) >>> print(output) [-3.465736 0. 2.7725887] """ return xlogy_(x, y)
[docs]def asin(x): r""" Computes arcsine of input tensors element-wise. .. math:: out_i = sin^{-1}(x_i) Args: x (Tensor): The shape of tensor is :math:`(N,*)` where :math:`*` means, any number of additional dimensions. The data type should be one of the following types: float16, float32, float64. Returns: Tensor, has the same shape and dtype as `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32, float64. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.74, 0.04, 0.30, 0.56]), mindspore.float32) >>> output = ops.asin(x) >>> print(output) [0.8330704 0.04001067 0.30469266 0.5943858 ] """ return asin_(x)
[docs]def acos(x): r""" Computes arccosine of input tensors element-wise. .. math:: out_i = cos^{-1}(x_i) Args: x (Tensor): The shape of tensor is :math:`(N,*)` where :math:`*` means, any number of additional dimensions. The data type should be one of the following types: float16, float32, float64. Returns: Tensor, has the same shape and dtype as `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.74, 0.04, 0.30, 0.56]), mindspore.float32) >>> output = ops.acos(x) >>> print(output) [0.737726 1.5307857 1.2661036 0.9764105] """ return acos_(x)
[docs]def atan(x): r""" Computes the trigonometric inverse tangent of the input element-wise. .. math:: out_i = tan^{-1}(x_i) Args: x (Tensor): The shape of tensor is :math:`(N,*)` where :math:`*` means, any number of additional dimensions. The data type should be one of the following types: float16, float32. Returns: A Tensor, has the same type as the input. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16 or float32. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1.0, 0.0]), mindspore.float32) >>> output = ops.atan(x) >>> print(output) [0.7853982 0. ] """ return atan_(x)
[docs]def sinh(x): r""" Computes hyperbolic sine of the input element-wise. .. math:: out_i = \sinh(x_i) Args: x (Tensor): The input tensor of hyperbolic sine function, its rank must be in [0, 7] inclusive. Returns: Tensor, has the same shape as `x`. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.62, 0.28, 0.43, 0.62]), mindspore.float32) >>> output = ops.sinh(x) >>> print(output) [0.6604918 0.28367308 0.44337422 0.6604918 ] """ return sinh_(x)
[docs]def cosh(x): r""" Computes hyperbolic cosine of input element-wise. .. math:: out_i = \cosh(x_i) Args: x (Tensor): The input tensor of hyperbolic cosine function, its rank must be in [0, 7] inclusive and data type must be float16, float32, float64, complex64 or complex128. Returns: Tensor, has the same shape as `x`. Raises: TypeError: If the dtype of `x` is not one of the following types: float16, float32, float64, complex64, complex128. TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) >>> output = ops.cosh(x) >>> print(output) [1.0289385 1.364684 1.048436 1.0040528] """ return cosh_(x)
[docs]def tanh(input_x): r""" Computes hyperbolic tangent of input element-wise. The Tanh function is defined as: .. math:: tanh(x_i) = \frac{\exp(x_i) - \exp(-x_i)}{\exp(x_i) + \exp(-x_i)} = \frac{\exp(2x_i) - 1}{\exp(2x_i) + 1}, where :math:`x_i` is an element of the input Tensor. Args: input_x (Tensor): Tensor of shape :math:`(N, *)`, where :math:`*` means, any number of additional dimensions, with float16 or float32 data type. Returns: Tensor, with the same type and shape as the `input_x`. Raises: TypeError: If dtype of `input_x` is neither float16 nor float32. TypeError: If `input_x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> input_x = Tensor(np.array([1, 2, 3, 4, 5]), mindspore.float32) >>> output = ops.tanh(input_x) >>> print(output) [0.7615941 0.9640276 0.9950547 0.9993293 0.9999092] """ return tanh_(input_x)
[docs]def asinh(x): r""" Computes inverse hyperbolic sine of the input element-wise. .. math:: out_i = \sinh^{-1}(input_i) Args: x (Tensor): The input tensor of inverse hyperbolic sine function, its rank must be in [0, 7] inclusive. Returns: Tensor, has the same shape and type as `x`. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([-5.0, 1.5, 3.0, 100.0]), mindspore.float32) >>> output = ops.asinh(x) >>> print(output) [-2.3124382 1.1947632 1.8184465 5.298342 ] """ return asinh_(x)
[docs]def acosh(x): r""" Computes inverse hyperbolic cosine of the inputs element-wise. .. math:: out_i = \cosh^{-1}(input_i) .. warning:: Given an input tensor x, the function computes inverse hyperbolic cosine of every element. Input range is [1, inf]. Args: x (Tensor): The input tensor of inverse hyperbolic cosine function, its rank must be in [0, 7] inclusive. Returns: Tensor, has the same shape and type as `x`. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1.0, 1.5, 3.0, 100.0]), mindspore.float32) >>> output = ops.acosh(x) >>> print(output) [0. 0.9624237 1.7627472 5.298292 ] """ return acosh_(x)
[docs]def atanh(x): r""" Computes inverse hyperbolic tangent of the input element-wise. .. math:: out_i = \tanh^{-1}(x_{i}) .. warning:: This is an experimental prototype that is subject to change and/or deletion. Args: x (Tensor): The shape of tensor is :math:`(N,*)` where :math:`*` means, any number of additional dimensions. The data type should be one of the following types: float16, float32. Returns: A Tensor, has the same type as the input. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16 or float32. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0, -0.5]), mindspore.float32) >>> output = ops.atanh(x) >>> print(output) [ 0. -0.54930615] """ return atanh_(x)
[docs]def atan2(x, y): r""" Returns arctangent of x/y element-wise. It returns :math:`\theta\ \in\ [-\pi, \pi]` such that :math:`x = r*\sin(\theta), y = r*\cos(\theta)`, where :math:`r = \sqrt{x^2 + y^2}`. Args of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. If they have different data types, the lower precision data type will be converted to the relatively highest precision data type. Args: x (Tensor): The input tensor. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. y (Tensor): The input tensor. It has the same shape with `x`. Returns: Tensor, the shape is the same as the one after broadcasting,and the data type is same as `x`. Raises: TypeError: If `x` or `y` is not a Tensor. RuntimeError: If the data type of `x` and `y` conversion of Parameter is required when data type conversion of Parameter is not supported. Supported Platforms: ``Ascend`` ``CPU`` ``GPU`` Examples: >>> x = Tensor(np.array([0, 1]), mindspore.float32) >>> y = Tensor(np.array([1, 1]), mindspore.float32) >>> output = ops.atan2(x, y) >>> print(output) [0. 0.7853982] """ _atan2 = _get_cache_prim(P.Atan2)() return _atan2(x, y)
[docs]def bitwise_and(x, y): r""" Returns bitwise `and` of two tensors element-wise. .. math:: out_i = x_{i} \wedge y_{i} Args of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. If they have different data types, the lower priority data type will be converted to the relatively highest priority data type. Args: x (Tensor): The input tensor with int16, int32 or uint16 data type. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. y (Tensor): The input tensor with same type as the `x`. Returns: Tensor, has the same type as the `x`. Raises: TypeError: If `x` or `y` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0, 0, 1, -1, 1, 1, 1]), mindspore.int16) >>> y = Tensor(np.array([0, 1, 1, -1, -1, 2, 3]), mindspore.int16) >>> output = ops.bitwise_and(x, y) >>> print(output) [ 0 0 1 -1 1 0 1] """ return bitwise_and_(x, y)
[docs]def bitwise_or(x, y): r""" Returns bitwise `or` of two tensors element-wise. .. math:: out_i = x_{i} \mid y_{i} Args of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. If they have different data types, the lower priority data type will be converted to the relatively highest priority data type. Args: x (Tensor): The input tensor with int16, int32 or uint16 data type. y (Tensor): The input tensor with same type as the `x`. Returns: Tensor, has the same type as the `x`. Raises: TypeError: If `x` or `y` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0, 0, 1, -1, 1, 1, 1]), mindspore.int16) >>> y = Tensor(np.array([0, 1, 1, -1, -1, 2, 3]), mindspore.int16) >>> output = ops.bitwise_or(x, y) >>> print(output) [ 0 1 1 -1 -1 3 3] """ return bitwise_or_(x, y)
[docs]def bitwise_xor(x, y): r""" Returns bitwise `xor` of two tensors element-wise. .. math:: out_i = x_{i} \oplus y_{i} Args of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. If they have different data types, the lower priority data type will be converted to the relatively highest priority data type. Args: x (Tensor): The input tensor with int16, int32 or uint16 data type. y (Tensor): The input tensor with same type as the `x`. Returns: Tensor, has the same type as the `x`. Raises: TypeError: If `x` or `y` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0, 0, 1, -1, 1, 1, 1]), mindspore.int16) >>> y = Tensor(np.array([0, 1, 1, -1, -1, 2, 3]), mindspore.int16) >>> output = ops.bitwise_xor(x, y) >>> print(output) [ 0 1 0 0 -2 3 2] """ return bitwise_xor_(x, y)
[docs]def inv(x): r""" Computes Reciprocal of input tensor element-wise. .. math:: out_i = \frac{1}{x_{i} } Args: x (Tensor): Tensor of any dimension. Must be one of the following types: float16, float32 or int32. Returns: Tensor, has the same type and shape as input shape value. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not one of float16, float32, int32. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.25, 0.4, 0.31, 0.52]), mindspore.float32) >>> output = ops.inv(x) >>> print(output) [4. 2.5 3.2258065 1.923077 ] """ return inv_(x)
[docs]def invert(x): r""" Flips all bits of input tensor element-wise. .. math:: out_i = \sim x_{i} Args: x (Tensor): The shape of tensor is :math:`(x_1, x_2, ..., x_R)`. The data type should be one of the following types: int16, uint16. Returns: Tensor, has the same shape as `x`. Raises: TypeError: If dtype of `x` is neither int16 nor uint16. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([25, 4, 13, 9]), mindspore.int16) >>> output = ops.invert(x) >>> print(output) [-26 -5 -14 -10] """ return invert_(x)
[docs]def erf(x): r""" Computes the Gauss error function of `x` element-wise. .. math:: erf(x)=\frac{2} {\sqrt{\pi}} \int\limits_0^{x} e^{-t^{2}} dt Args: x (Tensor): The input tensor of Gaussian error function. Its rank must be in [0, 7] inclusive and data type must be float16 or float32. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is neither float16 nor float32. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([-1, 0, 1, 2, 3]), mindspore.float32) >>> output = ops.erf(x) >>> print(output) [-0.8427168 0. 0.8427168 0.99530876 0.99997765] """ return erf_(x)
[docs]def erfc(x): r""" Computes the complementary error function of `x` element-wise. .. math:: erfc(x) = 1 - \frac{2} {\sqrt{\pi}} \int\limits_0^{x} e^{-t^{2}} dt Args: x (Tensor): The input tensor with a dtype of float16 or float32, its rank should be in [0, 7] inclusive. Returns: Tensor, has the same shape and dtype as `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16 or float32. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([-1, 0, 1, 2, 3]), mindspore.float32) >>> output = ops.erfc(x) >>> print(output) [1.8427168e+00 1.0000000e+00 1.5728319e-01 4.6912432e-03 2.2351742e-05] """ return erfc_(x)
[docs]def bessel_j0(x): r""" Computes the Bessel j0 function of x element-wise. Args: x (Tensor): The input tensor. The data type must be float16, float32 or float64. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.5, 1., 2., 4.]), mindspore.float32) >>> output = ops.bessel_j0(x) >>> print(output) [0.93846981 0.76519769 0.22389078 -0.39714981] """ return bessel_j0_(x)
[docs]def bessel_j1(x): r""" Computes the Bessel j1 function of x element-wise. Args: x (Tensor): The input tensor. The data type must be float16, float32 or float64. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.5, 1., 2., 4.]), mindspore.float32) >>> output = ops.bessel_j1(x) >>> print(output) [0.24226846 0.44005059 0.57672481 -0.06604333] """ return bessel_j1_(x)
[docs]def bessel_i0(x): r""" Computes the Bessel i0 function of x element-wise. Args: x (Tensor): The input tensor. The data type must be float16, float32 or float64. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([-1, -0.5, 0.5, 1]), mindspore.float32) >>> output = ops.bessel_i0(x) >>> print(output) [1.26606588 1.06348337 1.06348337 1.26606588] """ return bessel_i0_(x)
[docs]def bessel_i0e(x): r""" Computes the Bessel i0e function of x element-wise. Args: x (Tensor): The input tensor. The data type must be float16, float32 or float64. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([-1, -0.5, 0.5, 1]), mindspore.float32) >>> output = ops.bessel_i0e(x) >>> print(output) [0.46575961 0.64503527 0.64503527 0.46575961] """ return bessel_i0e_(x)
[docs]def bessel_k0(x): r""" Computes the Bessel k0 function of x element-wise. Args: x (Tensor): The input tensor. The data type must be float16, float32 or float64. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.5, 1., 2., 4.]), mindspore.float32) >>> output = ops.bessel_k0(x) >>> print(output) [0.92441907 0.42102444 0.11389387 0.01115968] """ return bessel_k0_(x)
[docs]def bessel_k0e(x): r""" Computes the Bessel k0e function of x element-wise. Args: x (Tensor): The input tensor. The data type must be float16, float32 or float64. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.5, 1., 2., 4.]), mindspore.float32) >>> output = ops.bessel_k0e(x) >>> print(output) [1.52410939 1.14446308 0.84156822 0.60929767] """ return bessel_k0e_(x)
[docs]def bessel_y0(x): r""" Computes the Bessel y0 function of x element-wise. Args: x (Tensor): The input tensor. The data type must be float16, float32 or float64. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.5, 1., 2., 4.]), mindspore.float32) >>> output = ops.bessel_y0(x) >>> print(output) [-0.44451874 0.08825696 0.51037567 -0.01694074] """ return bessel_y0_(x)
[docs]def bessel_y1(x): r""" Computes the Bessel y1 function of x element-wise. Args: x (Tensor): The input tensor. The data type must be float16, float32 or float64. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.5, 1., 2., 4.]), mindspore.float32) >>> output = ops.bessel_y1(x) >>> print(output) [-1.47147239 -0.78121282 -0.10703243 0.39792571] """ return bessel_y1_(x)
[docs]def linspace(start, stop, num): r""" Returns a Tensor whose value is `num` evenly spaced in the interval `start` and `stop` (including `start` and `stop`), and the length of the output Tensor is `num`. .. math:: \begin{aligned} &step = (stop - start)/(num - 1)\\ &output = [start, start+step, start+2*step, ... , stop] \end{aligned} Args: start (Tensor): Start value of interval. The tensor data type must be float32 or float64 and with shape of 0-D. stop (Tensor): Last value of interval. The tensor data type must be float32 or float64 and with shape of 0-D. num (Union[Tensor, int]): Number of ticks in the interval, inclusive of start and stop. Must be positive int number or 0D int32/int64 Tensor. Returns: Tensor, has the same dtype as `start`, and the shape of :math:`(num)`. Raises: TypeError: If `start` or `stop` is not a Tensor. TypeError: If dtype of `start` or dtype of `stop` is not float32 or float64. ValueError: If shape of `start` or shape of `stop` is not 0-D. TypeError: If `num` is not int or 0D int32/int64 Tensor. ValueError: If `num` is not positive int number. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> start = Tensor(1, mindspore.float32) >>> stop = Tensor(10, mindspore.float32) >>> num = 5 >>> output = ops.linspace(start, stop, num) >>> print(output) [ 1. 3.25 5.5 7.75 10. ] """ return linspace_(start, stop, num)
def matrix_determinant(x): r""" Computes the determinant of one or more square matrices. Args: x (Tensor): A matrix to be calculated. The matrix must be at least two dimensions, and the last two dimensions must be the same size. Data type must be float32, float64, complex64 or complex128. Returns: Tensor, The shape is :math:`x\_shape[:-2]`, the dtype is same as `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` not float32, float64, complex64 or complex128. ValueError: If the last two dimensions of `x` is not same size. ValueError: If the dimension of `x` is less than 2. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> input_x = Tensor(np.array([[[-4.5, -1.5], [7.0, 6.0]], [[2.5, 0.5], [3.0, 9.0]]]), mindspore.float32) >>> output = ops.matrix_determinant(input_x) >>> print(output) [-16.5 21. ] """ return matrix_determinant_(x) def matrix_exp(x): r""" Computes the matrix exponential of a square matrix. Supports batched inputs. .. math:: matrix\_exp(x) = \sum_{k=0}^{\infty} \frac{1}{k !} x^{k} \in \mathbb{K}^{n \times n} Args: x (Tensor): The shape of tensor is :math:`(*, n, n)` where * is zero or more batch dimensions. Must be one of the following types: float64, float32, float16, complex64, complex128. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If the dtype of `x` is not one of the following dtype: float16, float32, float64, complex64, complex128. ValueError: If the rank of `x` is less than 2. ValueError: If the last two dimensions of `x` are not equal. Supported Platforms: ``Ascend`` ``CPU`` Examples: >>> x = Tensor(np.array([[1, 2], [0, 1]]), mindspore.float32) >>> output = ops.matrix_exp(x) >>> print(output) [[2.7182817 5.436563 ] [0. 2.7182817]] """ return matrix_exp_(x) def log_matrix_determinant(x): r""" Computes the sign and the log of the absolute value of the determinant of one or more square matrices. Args: x (Tensor): A matrix to be calculated. The matrix must be at least two dimensions, and the last two dimensions must be the same size. Data type must be float32, float64, complex64 or complex128. Returns: Tensor, The signs of the log determinants. The shape is :math:`x\_shape[:-2]`, the dtype is same as `x`. Tensor, The absolute values of the log determinants. The shape is :math:`x\_shape[:-2]`, the dtype is same as `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` not float32, float64, complex64 or complex128. ValueError: If the last two dimensions of `x` is not same size. ValueError: If the dimension of `x` is less than 2. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> input_x = Tensor(np.array([[[-4.5, -1.5], [7.0, 6.0]], [[2.5, 0.5], [3.0, 9.0]]]), mindspore.float32) >>> sign, output = ops.log_matrix_determinant(input_x) >>> print(sign) [-1. 1.] >>> print(output) [2.80336046e+00 3.04452229e+00] """ return log_matrix_determinant_(x)
[docs]def matrix_solve(matrix, rhs, adjoint=False): r""" Solves systems of linear equations. .. math:: \begin{aligned} &matrix[..., M, M] * x[..., M, K] = rhs[..., M, K] \\ &adjoint(matrix[..., M, M]) * x[..., M, K] = rhs[..., M, K] \end{aligned} .. warning:: On GPU, if the matrix is irreversible, an error may be reported or an unknown result may be returned. Args: matrix (Tensor): The shape of tensor is :math:`[..., M, M]`. rhs (Tensor): The shape of tensor is :math:`[..., M, K]`. `rhs` must have the same dtype as `matrix`. adjoint(bool): Indicating whether to solve with matrix or its (block-wise) adjoint. Default: False. Returns: x (Tensor), The dtype and shape is the same as 'rhs'. Raises: TypeError: If adjoint is not the type of bool. TypeError: If the type of matrix is not one of the following dtype: mstype.float16, mstype.float32, mstype.float64, mstype.complex64, mstype.complex128. TypeError: If the type of `matrix` is not the same as that of `rhs`. ValueError: If the rank of `matrix` less than 2. ValueError: If the dimension of `matrix` is not the same as `rhs`. ValueError: If the inner-most 2 dimension of `matrix` is not the same. ValueError: If the inner-most 2 dimension of `rhs` does not match `matrix`. ValueError: If the `matrix` is irreversible. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> matrix = Tensor([[5, 4], [3, 1]], mindspore.float32) >>> rhs = Tensor([[7], [2]], mindspore.float32) >>> result = ops.matrix_solve(matrix, rhs) >>> print(result) [[0.14285707] [1.5714287 ]] """ matrix_solve_ = _get_cache_prim(MatrixSolve)(adjoint=adjoint) return matrix_solve_(matrix, rhs)
[docs]def truncate_div(x, y): """ Divides the first input tensor by the second input tensor element-wise for integer types, negative numbers will round fractional quantities towards zero. Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant. Note: Broadcasting is supported. Args: x(Union[Tensor, Number, bool]): The first input is a number, or a bool, or a tensor whose data type is number or bool. y(Union[Tensor, Number, bool]): The second input is a number, or a bool when the first input is a tensor, or a tensor whose data type is number or bool. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs. Raises: TypeError: If `x` and `y` is not one of the following: Tensor, Number, bool. Supported Platforms: ``Ascend`` ``CPU`` ``GPU`` Examples: >>> x = Tensor(np.array([2, 4, -1]), mindspore.int32) >>> y = Tensor(np.array([3, 3, 3]), mindspore.int32) >>> output = ops.truncate_div(x, y) >>> print(output) [0 1 0] """ return truncate_div_(x, y)
[docs]def truncate_mod(x, y): r""" Returns the remainder of division element-wise. Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant. .. warning:: - The input data does not support 0. - When the elements of input exceed 2048 , the accuracy of operator cannot guarantee the requirement of double thousandths in the mini form. - Due to different architectures, the calculation results of this operator on NPU and CPU may be inconsistent. - If shape is expressed as (D1,D2... ,Dn), then D1\*D2... \*DN<=1000000,n<=8. Args: x (Union[Tensor, numbers.Number, bool]): The first input is a number, or a bool, or a tensor whose data type is number or bool. y (Union[Tensor, numbers.Number, bool]): The second input is a number, or a bool when the first input is a tensor, or a tensor whose data type is number or bool. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision among the two inputs. Raises: TypeError: If neither `x` nor `y` is one of the following: Tensor, number, bool. TypeError: If neither `x` nor `y` is a Tensor. ValueError: If the shape `x` and `y` cannot be broadcasted to each other. Supported Platforms: ``Ascend`` ``CPU`` ``GPU`` Examples: >>> x = Tensor(np.array([2, 4, -1]), mindspore.int32) >>> y = Tensor(np.array([3, 3, 3]), mindspore.int32) >>> output = ops.truncate_mod(x, y) >>> print(output) [ 2 1 -1] """ return truncate_mod_(x, y)
[docs]def trunc(input_x): r""" Returns a new tensor with the truncated integer values of the elements of input. Args: input_x (Tensor): input_x is a tensor. Returns: Tensor, the same shape and data type as the input. Raises: TypeError: If `input_x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> input_x = Tensor(np.array([3.4742, 0.5466, -0.8008, -3.9079]),mindspore.float32) >>> output = ops.trunc(input_x) >>> print(output) [ 3. 0. 0. -3.] """ return trunc_(input_x)
def ldexp(x, other): """ Multiplies input by 2**:attr:other. .. math:: out_{i} = input_{i} * ( 2_{i} ^{ other} ) Note: Typically this function can create floating point numbers by multiplying mantissas in input with powers of intger 2 from the exponents in :attr:’other’. Args: x (Tensor): The input tensor. other (Tensor): A tensor of exponents, typically integers. Returns: out (Tensor, optional) : the output tensor. Raises: TypeError: If `x` is not a Tensor. TypeError: If `other` is not a Tensor. ValueError: If the size of tensor `x` and `other` are different at non-singleton dimension, and not equal to 1. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> import mindspore >>> import numpy as np >>> from mindspore import Tensor >>> from mindspore import ops >>> x = Tensor(np.array([1.]), mindspore.float32) >>> other = Tensor(np.array([1, 2, 3, 4]), mindspore.int32) >>> out = ops.ldexp(x, other) >>> print(out) [ 2. 4. 8. 16.] """ pow_ops = _get_cache_prim(P.Pow)() mul_ops = _get_cache_prim(P.Mul)() out = mul_ops(x, pow_ops(2.0, other)) return out def logit(x, eps=None): r""" Calculate the logit of a tensor element-wise. When eps is not None, element in 'x' is clamped to [eps, 1-eps]. When eps is None, input 'x' is not clamped. .. math:: \begin{align} y_{i} & = \ln(\frac{z_{i}}{1 - z_{i}}) \\ z_{i} & = \begin{cases} x_{i} & \text{if eps is None} \\ \text{eps} & \text{if } x_{i} \lt \text{eps} \\ x_{i} & \text{if } \text{eps} \leq x_{i} \leq 1 - \text{eps} \\ 1 - \text{eps} & \text{if } x_{i} \gt 1 - \text{eps} \end{cases} \end{align} Args: x (Tensor): The input tensor. eps (float, optional): The epsilon. The input clamp bound is defined as [eps, 1-eps]. Default: None. Returns: Tensor, with the same shape as the `x`. Raises: TypeError: If `eps` is not a float. TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``GPU`` Examples: >>> x = Tensor(np.array([0.1, 0.2, 0.3]).astype(np.float32)) >>> output = ops.logit(x, eps=1e-5) >>> print(output) [-2.1972246 -1.3862944 -0.8472978] """ if eps is None: eps = -1.0 logit_ = _get_cache_prim(Logit)(eps) return logit_(x) ##################################### # Comparison Operation Functions. #####################################
[docs]def less(x, y): r""" Computes the boolean value of :math:`x < y` element-wise. Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant. .. math:: out_{i} =\begin{cases} & \text{True, if } x_{i}<y_{i} \\ & \text{False, if } x_{i}>=y_{i} \end{cases} Args: x (Union[Tensor, Number, bool]): The first input is a number or a bool or a tensor whose data type is number or bool. y (Union[Tensor, Number, bool]): The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool. Returns: Tensor, the shape is the same as the one after broadcasting,and the data type is bool. Raises: TypeError: If `x` and `y` is not one of the following: Tensor, Number, bool. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1, 2, 3]), mindspore.int32) >>> y = Tensor(np.array([1, 1, 4]), mindspore.int32) >>> output = ops.less(x, y) >>> print(output) [False False True] """ return tensor_lt(x, y)
[docs]def le(x, y): r""" Computes the boolean value of :math:`x <= y` element-wise. .. math:: out_{i} =\begin{cases} & \text{True, if } x_{i}<=y_{i} \\ & \text{False, if } x_{i}>y_{i} \end{cases} .. note:: - Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. - The inputs must be two tensors or one tensor and one scalar. - When the inputs are two tensors, dtypes of them cannot be both bool , and the shapes of them can be broadcast. - When the inputs are one tensor and one scalar, the scalar could only be a constant. Args: x (Union[Tensor, number.Number, bool]): The first input is a number.Number or a bool or a tensor whose data type is `number <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_ or `bool_ <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_. y (Union[Tensor, number.Number, bool]): The second input, when the first input is a Tensor, the second input should be a number.Number or bool value, or a Tensor whose data type is number or bool\_. When the first input is Scalar, the second input must be a Tensor whose data type is number or bool\_. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is bool. Raises: TypeError: If neither `x` nor `y` is a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1, 2, 3]), mindspore.int32) >>> y = Tensor(np.array([1, 1, 4]), mindspore.int32) >>> output = ops.le(x, y) >>> print(output) [ True False True] """ return tensor_le(x, y)
[docs]def gt(x, y): r""" Compare the value of the input parameters :math:`x,y` element-wise, and the output result is a bool value. .. math:: out_{i} =\begin{cases} & \text{True, if } x_{i}>y_{i} \\ & \text{False, if } x_{i}<=y_{i} \end{cases} Note: - Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. - The inputs must be two tensors or one tensor and one scalar. - When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them can be broadcast. - When the inputs are one tensor and one scalar, the scalar could only be a constant. - Broadcasting is supported. - If the input Tensor can be broadcast, the low dimension will be extended to the corresponding high dimension in another input by copying the value of the dimension. Args: x (Union[Tensor, number.Number, bool]): The first input is a number.Number or a bool or a tensor whose data type is `number <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_ or `bool_ <https://www.mindspore.cn/docs/en/r1.10/api_python/mindspore.html#mindspore.dtype>`_ . y (Union[Tensor, number.Number, bool]): The second input, when the first input is a Tensor, the second input should be a number.Number or bool value, or a Tensor whose data type is number or bool\_. When the first input is Scalar, the second input must be a Tensor whose data type is number or bool\_. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is bool. Raises: TypeError: If neither `x` nor `y` is a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1, 2, 3]), mindspore.int32) >>> y = Tensor(np.array([1, 1, 4]), mindspore.int32) >>> output = ops.gt(x, y) >>> print(output) [False True False] """ return tensor_gt(x, y)
[docs]def ge(x, y): r""" Computes the boolean value of :math:`x >= y` element-wise. Note: - Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. - The inputs must be two tensors or one tensor and one scalar. - When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them can be broadcast. - When the inputs are one tensor and one scalar, the scalar could only be a constant. - Broadcasting is supported. - If the input Tensor can be broadcast, the low dimension will be extended to the corresponding high dimension in another input by copying the value of the dimension. .. math:: out_{i} =\begin{cases} & \text{True, if } x_{i}>=y_{i} \\ & \text{False, if } x_{i}<y_{i} \end{cases} Args: x (Union[Tensor, Number, bool]): The first input is a number or a bool or a tensor whose data type is number or bool. y (Union[Tensor, Number, bool]): The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is bool. Raises: TypeError: If neither `x` nor `y` is a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1, 2, 3]), mindspore.int32) >>> y = Tensor(np.array([1, 1, 4]), mindspore.int32) >>> output = ops.ge(x, y) >>> print(output) [True True False] """ return tensor_ge(x, y)
[docs]def equal(x, y): r""" Computes the equivalence between two tensors element-wise. .. math:: out_{i} =\begin{cases} & \text{True, if } x_{i} = y_{i} \\ & \text{False, if } x_{i} \ne y_{i} \end{cases} Note: - Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. - The inputs must be two tensors or one tensor and one scalar. - When the inputs are two tensors, the shapes of them could be broadcast. - When the inputs are one tensor and one scalar, the scalar could only be a constant. Args: x (Union[Tensor, Number]): The first input is a number or a tensor whose data type is number. y (Union[Tensor, Number]): The second input is a number when the first input is a tensor or a tensor whose data type is number. The data type is the same as the first input. Returns: Tensor, the shape is the same as the one after broadcasting,and the data type is bool. Raises: TypeError: If neither `x` nor `y` is a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> # case 1: The shape of two inputs are different >>> x = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> output = ops.equal(x, 2.0) >>> print(output) [False True False] >>> # case 2: The shape of two inputs are the same >>> x = Tensor(np.array([1, 2, 3]), mindspore.int32) >>> y = Tensor(np.array([1, 2, 4]), mindspore.int32) >>> output = ops.equal(x, y) >>> print(output) [ True True False] """ return equal_(x, y)
[docs]def ne(x, y): r""" Computes the non-equivalence of two tensors element-wise. Note: - Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. - The inputs must be two tensors or one tensor and one scalar. - When the inputs are two tensors, the shapes of them could be broadcast. - When the inputs are one tensor and one scalar, the scalar could only be a constant. - Broadcasting is supported. .. math:: out_{i} =\begin{cases} & \text{True, if } x_{i} \ne y_{i} \\ & \text{False, if } x_{i} = y_{i} \end{cases} Args: x (Union[Tensor, Number, bool]): The first input is a number or a bool or a tensor whose data type is number or bool. y (Union[Tensor, Number, bool]): The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool. Returns: Tensor, the shape is the same as the one after broadcasting,and the data type is bool. Raises: TypeError: If `x` and `y` is not one of the following: Tensor, Number, bool. TypeError: If neither `x` nor `y` is a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> output = ops.ne(x, 2.0) >>> print(output) [ True False True] >>> >>> x = Tensor(np.array([1, 2, 3]), mindspore.int32) >>> y = Tensor(np.array([1, 2, 4]), mindspore.int32) >>> output = ops.ne(x, y) >>> print(output) [False False True] """ return not_equal(x, y)
[docs]def approximate_equal(x, y, tolerance=1e-5): r""" Returns True if abs(x-y) is smaller than tolerance element-wise, otherwise False. .. math:: out_i = \begin{cases} & \text{ if } \left | x_{i} - y_{i} \right | < \text{tolerance},\ \ True \\ & \text{ if } \left | x_{i} - y_{i} \right | \ge \text{tolerance},\ \ False \end{cases} where `tolerance` indicates Acceptable maximum tolerance. Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. If they have different data types, the lower precision data type will be converted to the relatively highest precision data type. Args: x (Tensor): A tensor. Must be one of the following types: float32, float16. :math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8. y (Tensor): A tensor of the same type and shape as `x`. tolerance (float): The maximum deviation that two elements can be considered equal. Default: 1e-05. Returns: Tensor, the shape is the same as the shape of `x`, and the data type is bool. Raises: TypeError: If `tolerance` is not a float. RuntimeError: If the data type of `x`, `y` conversion of Parameter is given but data type conversion of Parameter is not supported. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> tol = 1.5 >>> x = Tensor(np.array([1, 2, 3]), mstype.float32) >>> y = Tensor(np.array([2, 4, 6]), mstype.float32) >>> output = ops.approximate_equal(Tensor(x), Tensor(y), tol) >>> print(output) [ True False False] """ return P.ApproximateEqual(tolerance)(x, y)
[docs]def isfinite(x): r""" Determines which elements are finite for each position. .. math:: out_i = \begin{cases} & \text{ if } x_{i} = \text{Finite},\ \ True\ \\ & \text{ if } x_{i} \ne \text{Finite},\ \ False \end{cases} Args: x (Tensor): The input tensor. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape of input, and the dtype is bool. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([np.log(-1), 1, np.log(0)]), mindspore.float32) >>> output = ops.isfinite(x) >>> print(output) [False True False] """ return isfinite_(x)
[docs]def isnan(x): r""" Determines which elements are NaN for each position. .. math:: out_i = \begin{cases} & \ True,\ \text{ if } x_{i} = \text{Nan} \\ & \ False,\ \text{ if } x_{i} \ne \text{Nan} \end{cases} where :math:`Nan` means not a number. Args: x (Tensor): The input tensor. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape of input, and the dtype is bool. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([np.log(-1), 1, np.log(0)]), mindspore.float32) >>> output = ops.isnan(x) >>> print(output) [ True False False] """ return isnan_(x)
[docs]def isclose(x1, x2, rtol=1e-05, atol=1e-08, equal_nan=False): """ Returns a new Tensor with boolean elements representing if each element of `x1` is “close” to the corresponding element of `x2`. Closeness is defined as: .. math:: ∣x1−x2∣ ≤ atol + rtol × ∣x2∣ Args: x1 (Tensor): First Tensor to compare, with data type belongs to float32, float16, int32. x2 (Tensor): Second Tensor to compare, with data type belongs to float32, float16, int32. rtol (float, optional): Relative tolerance. Default: 1e-05. atol (float, optional): Absolute tolerance. Default: 1e-08. equal_nan (bool, optional): If True, then two NaNs will be considered equal. Default: False. Returns: A bool Tensor, with the shape as broadcasted result of the input `x1` and `x2`. Raises: TypeError: If either of `x1` and `x2` is not Tensor. TypeError: If either of `x1` and `x2` is not float16, float32 or int32. TypeError: If either of `atol` and `rtol` is not float. TypeError: If `equal_nan` is not bool. TypeError: If the dtype of `x1` is not same as the `x2`. ValueError: If `x1` and `x2` can not be broadcast. ValueError: If either of `atol` and `rtol` is less than zero. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> input = Tensor(np.array([1.3, 2.1, 3.2, 4.1, 5.1]), mindspore.float16) >>> other = Tensor(np.array([1.3, 3.3, 2.3, 3.1, 5.1]), mindspore.float16) >>> output = ops.isclose(input, other) >>> print(output) [ True False False False True] """ is_close = _get_cache_prim(P.IsClose)(rtol=rtol, atol=atol, equal_nan=equal_nan) return is_close(x1, x2)
def isreal(x): """ Returns a new tensor with boolean elements representing whether each element of `x` is real-valued. All real value types are considered real numbers. A complex value is considered real when its imaginary part is 0. Inputs: - **x** (Tensor) - The input tensor. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Outputs: Tensor, has the same shape of input, and the dtype is bool. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> from mindspore import ops >>> x = Tensor([1, 1+1j, 2+0j], mstype.complex64) >>> output = ops.isreal(x) >>> print(output) [ True False True] """ if not isinstance(x, (Tensor, Tensor_)): raise TypeError("the input x must be Tensor!") # Note: Integral and Floating tensor values are always real ones_op = _get_cache_prim(P.Ones)() real_dtype = mstype.int_type + mstype.uint_type + mstype.float_type + (mstype.bool_,) if x.dtype in real_dtype: return ones_op(x.shape, mstype.bool_) imag_op = _get_cache_prim(P.Imag)() return imag_op(x) == 0
[docs]def same_type_shape(input_x, input_y): """ Checks whether the data type and shape of two tensors are the same. Args: input_x (Tensor): The shape of tensor is :math:`(x_1, x_2, ..., x_R)`. input_y (Tensor): The shape of tensor is :math:`(x_1, x_2, ..., x_S)`. Returns: Tensor, the shape of tensor is :math:`(x_1, x_2, ..., x_R)`, if data type and shape of `input_x` and `input_y` are the same. Raises: TypeError: If the data types of `input_x` and `input_y` are not the same. ValueError: If the shapes of `input_x` and `input_y` are not the same. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> input_x = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32) >>> input_y = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32) >>> output = ops.same_type_shape(input_x, input_y) >>> print(output) [[2. 2.] [2. 2.]] """ return same_type_shape_(input_x, input_y)
[docs]def maximum(x, y): """ Computes the maximum of input tensors element-wise. Note: - Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. - The inputs must be two tensors or one tensor and one scalar. - When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them could be broadcast. - When the inputs are one tensor and one scalar, the scalar could only be a constant. - Broadcasting is supported. - If one of the elements being compared is a NaN, then that element is returned. .. math:: output_i = max(x_i, y_i) Args: x (Union[Tensor, Number, bool]): The first input is a number or a bool or a tensor whose data type is number or bool. y (Union[Tensor, Number, bool]): The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs. Raises: TypeError: If `x` and `y` is not one of the following: Tensor, Number, bool. ValueError: If `x` and `y` are not the same shape. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> # case 1 : same data type >>> x = Tensor(np.array([1.0, 5.0, 3.0]), mindspore.float32) >>> y = Tensor(np.array([4.0, 2.0, 6.0]), mindspore.float32) >>> output = ops.maximum(x, y) >>> print(output) [4. 5. 6.] >>> # case 2 : different data type >>> x = Tensor(np.array([1.0, 5.0, 3.0]), mindspore.int32) >>> y = Tensor(np.array([4.0, 2.0, 6.0]), mindspore.float32) >>> output = ops.maximum(x, y) >>> print(output.dtype) Float32 """ return maximum_(x, y)
[docs]def minimum(x, y): r""" Computes the minimum of input tensors element-wise. Note: - Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. - The inputs must be two tensors or one tensor and one scalar. - When the inputs are two tensors, dtypes of them cannot be bool at the same time. - When the inputs are one tensor and one scalar, the scalar could only be a constant. - Shapes of them are supposed to be broadcast. - If one of the elements being compared is a NaN, then that element is returned. .. math:: output_i = min(x_i, y_i) Args: x (Union[Tensor, Number, bool]): The first input is a number or a bool or a tensor whose data type is number or bool. y (Union[Tensor, Number, bool]): The second input is a number or a bool when the first input is a tensor or a tensor whose data type is number or bool. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs. Raises: TypeError: If `x` and `y` is not one of the following: Tensor, Number, bool. ValueError: If `x` and `y` are not the same shape after broadcast. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> # case 1 : same data type >>> x = Tensor(np.array([1.0, 5.0, 3.0]), mindspore.float32) >>> y = Tensor(np.array([4.0, 2.0, 6.0]), mindspore.float32) >>> output = ops.minimum(x, y) >>> print(output) [1. 2. 3.] >>> # case 2 : different data type >>> x = Tensor(np.array([1.0, 5.0, 3.0]), mindspore.int32) >>> y = Tensor(np.array([4.0, 2.0, 6.0]), mindspore.float32) >>> output = ops.minimum(x, y) >>> print(output.dtype) Float32 """ return minimum_(x, y)
def median(x, global_median=False, axis=0, keep_dims=False): r""" Computes the median of input tensor. .. warning:: When attr `global_median` is True, the second output Tensor value is meaningless. Args: x (Tensor): The first input is a tensor whose data type is number. global_median (bool): Whether the output tensor is the global median of all input tensor elements or not. Default: False. axis (int): The dimension need to reduce. Default: 0. keep_dims (bool): Whether the output tensor need to retain `axis` dimension or not. Default: False. Returns: y (Tensor), has the same dtype as the `x`. If `global_median` is true, the `y` has only one element. If `keep_dims` is true, the `y` has the same shape as the `x` except the shape of `y` in dimension `axis` is size 1. Otherwise, the `y` lacks `axis` dimension than input. indices (Tensor), has the same shape as the `y`, but dtype is int64. Raises: TypeError: If dtype of `x` is not one of the following: int16, int32, int64, float32, float64. TypeError: If input `x` is not a Tensor. TypeError: If `global_median` is not a bool. TypeError: If `axis` is not a int. TypeError: If `keep_dims` is not a bool. ValueError: If `axis` is not in range of [-x.dim, x.dim-1]. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> # case 1 : common median compute >>> x = Tensor(np.array([[0.57, 0.11, 0.21],[0.38, 0.50, 0.57], [0.36, 0.16, 0.44]]).astype(np.float32)) >>> y = ops.median(x, global_median=False, axis=0, keep_dims=False) >>> print(y) (Tensor(shape=[3], dtype=Float32, value=[0.38, 0.16, 0.44]), Tensor(shape=[3], dtype=Int64, value=[1, 2, 2])) >>> # case 2 : global median compute >>> x = Tensor(np.array([1, 7, 6],[5, 1, 3],[9, 17, 1]), mindspore.int32) >>> y = ops.median(x, global_median=True) >>> print(y) (Tensor(shape=[1], dtype=Int32, value=[5]), Tensor(shape=[1], dtype=Int64, value=[1])) """ median_ = Median(global_median, axis, keep_dims) return median_(x) def orgqr(x, tau): r""" Computes the first :math:`N` columns of a product of Householder matrices. Take the case of input without batch as an example. The input x is a matrix of size :math:`(M, N)` after householder transformation. When the diagonal of x is set to 1, every colunm of lower triangular in x is denoted as :math:`w_j` for :math:`j` for :math:`j=1, \ldots, M`, this function returns the first :math:`N` columns of the matrix .. math:: H_{1} H_{2} \ldots H_{k} \quad \text { with } \quad H_{j}=\mathrm{I}_{M}-\tau_{j} w_{j} w_{j}^{\mathrm{H}} where :math:`\mathrm{I}_{M}` is the :math:`M`-dimensional identity matrix. And when :math:`w` is complex, :math:`w^{\mathrm{H}}` is the conjugate transpose, otherwise the transpose. The output matrix is the same size as the input matrix :math:`x`. Args: x (Tensor): Tensor of shape :math:`(*, M, N)`, indicating 2D or 3D matrices, with float32, float64, complex64 and complex128 data type. tau (Tensor) : Tensor of shape :math:`(*, K)`, where `K` is less than or equal to `N` indicating the reflecting coefficient in Householder transformation, which have the same type as x. Returns: Tensor, has the same shape and data type as `x`. Raises: TypeError: If `x` or `tau` are not Tensors. TypeError: If dtype of `x` and `tau` is not one of: float64, float32, complex64, complex128. ValueError: If `x` and `tau` have different batch size. ValueError: If x.shape[-2] < x.shape[-1]. ValueError: If x.shape[-1] < tau.shape[-1]. ValueError: If rank(x) - rank(tau) != 1. ValueError: If rank(x) != 2 or 3. Supported Platforms: ``Ascend`` ``CPU`` Examples: >>> x = Tensor(np.array([[-114.6, 10.9, 1.1], [-0.304, 38.07, 69.38], [-0.45, -0.17, 62.]]), mindspore.float32) >>> tau = Tensor(np.array([1.55, 1.94, 0.0]), mindspore.float32) >>> net = ops.Orgqr() >>> y = net(x, tau) >>> print(y) [[-0.54999995 -0.2128925 0.8137956 ] [ 0.47119996 -0.8752807 0.08240613] [ 0.69749993 0.42560163 0.57772595]] """ orgqr_ = Orgqr() return orgqr_(x, tau) def hypot(x1, x2): """ Computes hypotenuse of input tensors element-wise as legs of a right triangle. The shape of two inputs should be broadcastable, and data type of them should be one of: float32, float64 Args: x1 (Tensor): The first input tensor. x2 (Tensor): The second input tensor. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is one with higher precision in the two inputs. Raises: TypeError: If data type `x1` or `x2` is not float32 or float64. ValueError: If shape of two inputs are not broadcastable. Supported Platforms: ``Ascend`` ``CPU`` Examples: >>> x1 = Tensor(np.array([3., 5., 7.])) >>> x2 = Tensor(np.array([4., 12., 24.])) >>> y = ops.hypot(x1, x2) >>> print(y) [ 5. 13. 25.] """ hypot_ = Hypot() return hypot_(x1, x2) def heaviside(x, values): r""" Computes the Heaviside step function for each element in input. .. math:: \text { heaviside }(\text { x, values })=\left\{\begin{array}{ll} 0, & \text { if x }<0 \\ \text { values, } & \text { if x }==0 \\ 1, & \text { if x }>0 \end{array}\right. Args: x (Tensor): The input tensor. With real number data type. values (Tensor): The values to use where x is zero. Values can be broadcast with x. 'x' should have the same dtype with 'values'. Returns: Tensor, has the same type as 'x' and 'values'. Raises: TypeError: If `x` or `values` is not Tensor. TypeError: If data type `x` and `values` is different. ValueError: If shape of two inputs are not broadcastable. Supported Platforms: ``Ascend`` ``CPU`` Examples: >>> x = Tensor(np.array([-1.5, 0., 2.])) >>> values = Tensor(np.array([0.5])) >>> heaviside = ops.Heaviside() >>> y = heaviside(x, values) >>> print(y) [ 0. 0.5 1. ] """ heaviside_ = Heaviside() return heaviside_(x, values) def logaddexp(x1, x2): """ Computes the logarithm of the sum of exponentiations of the inputs. Calculates ``log(exp(x1) + exp(x2))``. This function is useful in statistics, where the computed probability of an event may be so small that it exceeds the range of a normal floating point number. In this case, the logarithm of the calculated probability is stored. This function allows to add probabilities stored in this way. Args: x1 (Tensor): Input Tensor. x2 (Tensor): Input Tensor. If ``x1.shape != x2.shape``, they must be broadcastable to a common shape (which becomes the shape of the output). Returns: Tensor or scalar. This is a scalar if both `x1` and `x2` are scalars. Raises: TypeError: If `x1`, `x2` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x1 = Tensor(np.array([1, 2, 3]).astype(np.float16)) >>> x2 = Tensor(np.array(2).astype(np.float16)) >>> output = ops.logaddexp(x1, x2) >>> print(output) [2.312 2.693 3.312] """ log_op = _get_cache_prim(P.Log)() exp_op = _get_cache_prim(P.Exp)() y = log_op(exp_op(x1) + exp_op(x2)) return y def logaddexp2(x1, x2): """ Computes the logarithm of the sum of exponentiations in base of 2 of the inputs. Calculates ``log2(2**x1 + 2**x2)``. This function is useful in machine learning when the computed probability of an event may be small beyond the range of normal floating point numbers. In this case, the base-2 logarithm of the calculated probability can be used instead. This function allows to add probabilities stored in this way. Args: x1 (Tensor): Input tensor. x2 (Tensor): Input tensor. If ``x1.shape != x2.shape``, they must be broadcastable to a common shape (which becomes the shape of the output). Returns: Tensor or scalar. This is a scalar if both `x1` and `x2` are scalars. Raises: TypeError: If `x1`, `x2` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x1 = Tensor(np.array([2, 4, 8]).astype(np.float16)) >>> x2 = Tensor(np.array([2]).astype(np.float16)) >>> output = ops.logaddexp2(x1, x2) >>> print(output) [3. 4.32 8.02] """ log_op = _get_cache_prim(P.Log)() pow_op = _get_cache_prim(P.Pow)() add_op = _get_cache_prim(P.Add)() if not isinstance(x1, (Tensor, Tensor_)): raise TypeError("The input x1 must be Tensor.") if not isinstance(x2, (Tensor, Tensor_)): raise TypeError("The input x2 must be Tensor.") add_exp = add_op(pow_op(2, x1), pow_op(2, x2)) tensor_2 = _make_tensor(2, add_exp.dtype) return log_op(add_exp) / log_op(tensor_2)
[docs]def std(input_x, axis=(), unbiased=True, keep_dims=False): """ Returns the standard-deviation and mean of each row of the input tensor in the dimension `axis`. If `axis` is a list of dimensions, reduce over all of them. Args: input_x (Tensor[Number]): Input tensor. axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Must be in the range [-rank(`input_x`), rank(`input_x`)). unbiased (bool): Whether to use Bessel’s correction. If true, will use the Bessel correction unbiased estimation. If false, will through the biased estimation to calculate the standard deviation. keep_dims (bool): Whether the output tensor has dim retained or not. If true, keep these reduced dimensions and the length is 1. If false, don't keep these dimensions. Returns: A tuple (output_std, output_mean) containing the standard deviation and mean. Raises: TypeError: If `input_x` is not a Tensor. TypeError: If `axis` is not one of the following: int, tuple or list. TypeError: If `keep_dims` is not a bool. ValueError: If `axis` is out of range. Supported Platforms: ``Ascend`` ``CPU`` Examples: >>> import mindspore.ops as ops >>> input_x = Tensor(np.array([[1, 2, 3], [-1, 1, 4]]).astype(np.float32)) >>> output = ops.std(input_x, 1, True, False) >>> output_std, output_mean = output[0], output[1] >>> print(output_std) [1. 2.5166116] >>> print(output_mean) [2. 1.3333334] """ reduce_std_op = ReduceStd(axis=axis, unbiased=unbiased, keep_dims=keep_dims) output = reduce_std_op(input_x) return output
def sqrt(x): """ Returns sqrt of a tensor element-wise. .. math:: out_{i} = \\sqrt{x_{i}} Args: x (Tensor): The input tensor with a dtype of Number, its rank must be in [0, 7] inclusive. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1.0, 4.0, 9.0]), mindspore.float32) >>> output = ops.sqrt(x) >>> print(output) [1. 2. 3.] """ return sqrt_(x)
[docs]def square(x): """ Returns square of a tensor element-wise. .. math:: out_{i} = (x_{i})^2 Args: x (Tensor): The input tensor with a dtype of Number, its rank must be in [0, 7] inclusive. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32) >>> output = ops.square(x) >>> print(output) [1. 4. 9.] """ return square_(x)
def outer(x1, x2): """ Return outer product of `x1` and `x2`. If `x1` is a vector of size n and `x2` is a vector of size m, then output must be a matrix of size n x m. Note: This function does not broadcast. Args: x1 (Tensor): 1-D input vector. x2 (Tensor): 1-D input vector. Outputs: out (Tensor, optional) : optional output matrix. Raises: TypeError: If `x1` is not a Tensor. TypeError: If `x2` is not a Tensor. ValueError: Expected 1-D input `x1`, but got n-D. ValueError: Expected 1-D input `x2`, but got n-D. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> import mindspore >>> import numpy as np >>> from mindspore import Tensor >>> from mindspore import ops >>> x1 = Tensor(np.array([1, 2, 3]), mindspore.int32) >>> x2 = Tensor(np.array([1, 2, 3]), mindspore.int32) >>> out = ops.outer(x1, x2) >>> print(out) [[1 2 3] [2 4 6] [3 6 9]] """ if not isinstance(x1, (Tensor, Tensor_)): raise TypeError("the input x1 must be Tensor!") if not isinstance(x2, (Tensor, Tensor_)): raise TypeError("the input x2 must be Tensor!") if len(x1.shape) != 1: raise ValueError("the input x1 must be a 1-D vector!") if len(x2.shape) != 1: raise ValueError("the input x2 must be a 1-D vector!") x1 = x1.reshape(-1, 1) mul_ops = _get_cache_prim(P.Mul)() y = mul_ops(x1, x2) return y def mv(mat, vec): """ Multiplies matrix `mat` and vector `vec`. If mat is a :math:`(N, M)` tensor, vec is a 1-D tensor of size :math:`M`, out will be 1-D of size :math:`N`. Args: mat (Tensor): Input matrix of the tensor. The shape of the tensor is :math:`(N, M)`. vec (Tensor): Input vector of the tensor. The shape of the tensor is :math:`(M,)`. Returns: Tensor, the shape of the output tensor is :math:`(N,)`. Raises: TypeError: If `mat`, `vec` is not a Tensor. ValueError: If `mat` is not a 2-D Tensor. If `vec` is not a 1-D Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> mat = Tensor(np.array([[3., 4.], [1., 6.], [1., 3.]]).astype(np.float32)) >>> vec = Tensor(np.array([1., 2.]).astype(np.float32)) >>> output = mv(mat, vec) >>> print(output) [11. 13. 7.] """ matmul_op = _get_cache_prim(P.MatMul)() reshape_op = _get_cache_prim(P.Reshape)() if not isinstance(mat, (Tensor, Tensor_)): raise TypeError("The input mat must be Tensor.") if not isinstance(vec, (Tensor, Tensor_)): raise TypeError("The input vec must be Tensor.") if len(mat.shape) != 2: raise ValueError("The input mat must be 2-D Tensor.") if len(vec.shape) != 1: raise ValueError("The input vec must be 1-D Tensor.") length_vec = get_x_shape(vec.shape) vec = reshape_op(vec, (length_vec[0], 1)) out = matmul_op(mat, vec) out = out.T out = out[0] return out def addmv(x, mat, vec, beta=1, alpha=1): """ Multiplies matrix `mat` and vector `vec`. The vector `x` is added to the final result. If mat is a :math:`(N, M)` tensor, vec is a 1-D tensor of size :math:`M`, then `x` must be broadcastable with a 1-D tensor of size :math:`N` and `out` will be 1-D tensor of size :math:`N`. The optional values `beta` and `alpha` are the matrix-vector product between `mat` and `vec` and the scale factor for the added tensor `x` respectively. If `beta` is 0, then `x` will be ignored. .. math:: output = β x + α (mat @ vec) Args: x (Tensor): Vector to be added. The shape of the tensor is :math:`(N,)`. mat (Tensor): The first tensor to be multiplied. The shape of the tensor is :math:`(N, M)`. vec (Tensor): The second tensor to be multiplied. The shape of the tensor is :math:`(M,)`. beta (scalar[int, float, bool], optional): Multiplier for `x` (β). The `beta` must be int or float or bool, Default: 1. alpha (scalar[int, float, bool], optional): Multiplier for `mat` @ `vec` (α). The `alpha` must be int or float or bool, Default: 1. Returns: Tensor, the shape of the output tensor is :math:`(N,)`, has the same dtype as `x`. Raises: TypeError: If `mat`, `vec`, `x` is not a Tensor. TypeError: If inputs `x`, `mat`, 'vec' are not the same dtype. ValueError: If `mat` is not a 2-D Tensor. If `x`, `vec` is not a 1-D Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([2., 3.]).astype(np.float32)) >>> mat = Tensor(np.array([[2., 5., 3.], [4., 2., 2.]]).astype(np.float32)) >>> vec = Tensor(np.array([3., 2., 4.]).astype(np.float32)) >>> output = addmv(x, mat, vec) >>> print(output) [30. 27.] """ dtypeop = P.DType() input_dtype = dtypeop(x) if not (isinstance(x, Tensor) and isinstance(mat, Tensor) and isinstance(vec, Tensor)): raise TypeError("For Addmv, inputs must be all tensors.") if not (input_dtype == dtypeop(mat) and input_dtype == dtypeop(vec)): raise TypeError("For Addmv, the inputs should be the same dtype.") _check_input_1d(x.shape, "x", "Addmv") _check_input_1d(vec.shape, "vec", "Addmv") _check_input_2d(mat.shape, "mat", "Addmv") _check_input_dtype("x", input_dtype, [mstype.float16, mstype.float32, mstype.float64, mstype.int16, mstype.int32, mstype.int64], "Addmv") _check_attr_dtype("alpha", alpha, [int, float, bool], "Addmv") _check_attr_dtype("beta", beta, [int, float, bool], "Addmv") if input_dtype in (mstype.int16, mstype.int32, mstype.int64): scalar_cast = P.ScalarCast() alpha = scalar_cast(alpha, mstype.int32) beta = scalar_cast(beta, mstype.int32) out = beta * x + alpha * mv(mat, vec) return out def addr(x, vec1, vec2, beta=1, alpha=1): """ Executes the outer-product of `vec1` and `vec2` and adds it to the vec1rix `x`. If `vec1` is a vector of size :vec1:`N` and `vec2` is a vector of size :vec1:`M`, then `x` must be broadcastable with a vec1rix of size :vec1:`(N, M)` and `out` will be a vec1rix of size :vec1:`(N, M)`. The optional values `beta` and `alpha` are the scale factors on the outer product between `vec1` and `vec2` and the added vec1rix `x` respectively. If `beta` is 0, then `x` will be ignored. .. vec1:: output = β x + α (vec1 ⊗ vec2) Args: x (Tensor): Vector to be added. The shape of the tensor is :vec1:`(N, M)`. vec1 (Tensor): The first tensor to be multiplied. The shape of the tensor is :vec1:`(N,)`. vec2 (Tensor): The second tensor to be multiplied. The shape of the tensor is :vec1:`(M,)`. beta (scalar[int, float, bool], optional): Multiplier for `x` (β). The `beta` must be int or float or bool, Default: 1. alpha (scalar[int, float, bool], optional): Multiplier for `vec1` @ `vec2` (α). The `alpha` must be int or float or bool, Default: 1. Outputs: Tensor, the shape of the output tensor is :vec1:`(N, M)`, has the same dtype as `x`. Raises: TypeError: If `x`, `vec1`, `vec2` is not a Tensor. TypeError: If inputs `x`, `vec1`, 'vec2' are not the same dtype. ValueError: If `x` is not a 2-D Tensor. If `vec1`, `vec2` is not a 1-D Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([[2., 2.], [3., 2.], [3., 4.]], np.float32)) >>> vec1 = Tensor(np.array([2., 3., 2.], np.float32)) >>> vec2 = Tensor(np.array([3, 4], np.float32)) >>> output = ops.addr(x, vec1, vec2) >>> print(output) [[ 8. 10.] [12. 14.] [ 9. 12.]] """ dtypeop = P.DType() input_dtype = dtypeop(x) if not (isinstance(x, Tensor) and isinstance(vec1, Tensor) and isinstance(vec2, Tensor)): raise TypeError("For Addr, inputs must be all tensors.") if not (input_dtype == dtypeop(vec1) and input_dtype == dtypeop(vec2)): raise TypeError("For Addr, the inputs should be the same dtype.") _check_input_1d(vec1.shape, "vec1", "Addr") _check_input_1d(vec2.shape, "vec2", "Addr") _check_input_2d(x.shape, "x", "Addr") _check_input_dtype("x", input_dtype, [mstype.float16, mstype.float32, mstype.float64, mstype.int16, mstype.int32, mstype.int64], "Addmv") _check_attr_dtype("alpha", alpha, [int, float, bool], "Addr") _check_attr_dtype("beta", beta, [int, float, bool], "Addr") if input_dtype in (mstype.int16, mstype.int32, mstype.int64): scalar_cast = P.ScalarCast() alpha = scalar_cast(alpha, mstype.int32) beta = scalar_cast(beta, mstype.int32) matmul_op = P.MatMul() reshape_op = P.Reshape() length_vec1 = get_x_shape(vec1.shape) vec1 = reshape_op(vec1, (length_vec1[0], 1)) length_vec2 = get_x_shape(vec2.shape) vec2 = reshape_op(vec2, (1, length_vec2[0])) out = beta * x + alpha * matmul_op(vec1, vec2) return out def lcm(x1, x2): """ Computes least common multiplier of input tensors element-wise. The shape of two inputs should be broadcastable, and data type of them should be one of: int32, int64 Inputs: - **x1** (Tensor) - The first input tensor. - **x2** (Tensor) - The second input tensor. Outputs: Tensor, the shape is the same as the one after broadcasting, and the data type is one with higher digits in the two inputs. Raises: TypeError: If data type `x1` or `x2` is not int32 or int64. ValueError: If shape of two inputs are not broadcastable. Supported Platforms: ``Ascend`` ``CPU`` Examples: >>> x1 = Tensor(np.array([7, 8, 9])) >>> x2 = Tensor(np.array([14, 6, 12])) >>> y = ops.lcm(x1, x2) >>> print(y) [14 24 36] """ lcm_ = _get_cache_prim(Lcm)() return lcm_(x1, x2)
[docs]def cdist(x, y, p=2.0): """ Computes batched the p-norm distance between each pair of the two collections of row vectors. Args: x (Tensor): Input tensor of shape :math:`(B, P, M)`. Letter :math:`B` represents 0 or positive int number. When :math:`B` is equal to 0, it means this dimension can be ignored, i.e. shape of the tensor is :math:`(P, M)`. y (Tensor): Input tensor of shape :math:`(B, R, M)`. p (float): P value for the p-norm distance to calculate between each vector pair, P ∈ [0,∞]. Default: 2.0. Returns: Tensor, has the same dtype as `x`, which shape is :math:`(B, P, R)`. Raises: TypeError: If `x` or `y` is not a Tensor. TypeError: If dtype of `x` or `y` is neither float16 nor float32. TypeError: If `p` is not a float. ValueError: If `p` is a negative float. ValueError: If dimension of `x` is not the same as `y`. ValueError: If dimension of `x` or `y` is neither 2 nor 3. Supported Platforms: ``Ascend`` ``CPU`` Examples: >>> x = Tensor(np.array([[[1.0, 1.0], [2.0, 2.0]]]).astype(np.float32)) >>> y = Tensor(np.array([[[3.0, 3.0], [3.0, 3.0]]]).astype(np.float32)) >>> output = ops.cdist(x, y, 2.0) >>> print(output) [[[2.8284273 2.8284273] [1.4142137 1.4142137]]] """ cdist_ = _get_cache_prim(P.Cdist)(p) return cdist_(x, y)
def gcd(x1, x2): """ Computes greatest common divisor of input tensors element-wise. The shape of two inputs should be broadcastable, and data type of them should be one of: int32, int64 Inputs: - **x1** (Tensor) - The first input tensor. - **x2** (Tensor) - The second input tensor. Outputs: Tensor, the shape is the same as the one after broadcasting, and the data type is one with higher digits in the two inputs. Raises: TypeError: If data type `x1` or `x2` is not int32 or int64. ValueError: If shape of two inputs are not broadcastable. Supported Platforms: ``Ascend`` ``CPU`` Examples: >>> x1 = Tensor(np.array([7, 8, 9])) >>> x2 = Tensor(np.array([14, 6, 12])) >>> y = ops.gcd(x1, x2) >>> print(y) [7 2 3] """ gcd_ = _get_cache_prim(Gcd)() return gcd_(x1, x2)
[docs]def lerp(start, end, weight): """ Does a linear interpolation of two tensors start and end based on a float or tensor weight. If `weight` is a tensor, the shapes of three inputs need to be broadcast; If `weight` is a float, the shapes of `start` and `end` need to be broadcast. .. math:: output_{i} = start_{i} + weight_{i} * (end_{i} - start_{i}) Args: start (Tensor): The tensor with the starting points. Data type must be float16 or float32. end (Tensor): The tensor with the ending points. Data type must be float16 or float32. weight (Union[float, Tensor]): The weight for the interpolation formula. Must be a float or a scalar tensor with float16 or float32 data type. Returns: Tensor, has the same type and shape as input `start`. Raises: TypeError: If `start` or `end` is not a tensor. TypeError: If `weight` is neither scalar(float) nor tensor. TypeError: If dtype of `start` or `end` is neither float16 nor float32. TypeError: If dtype of `weight` is neither float16 nor float32 when it is a tensor. TypeError: If `start` and `end` have different data types. TypeError: If `start`, `end` and `weight` have different data types when `weight` is a tensor. ValueError: If `end` could not be broadcast to a tensor with shape of `start`. ValueError: If `weight` could not be broadcast to tensors with shapes of `start` and `end` when it is a tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> start = Tensor(np.array([1., 2., 3., 4.]), mindspore.float32) >>> end = Tensor(np.array([10., 10., 10., 10.]), mindspore.float32) >>> output = ops.lerp(start, end, 0.5) >>> print(output) [5.5 6. 6.5 7. ] """ return lerp_(start, end, weight)
[docs]def bernoulli(x, p=0.5, seed=-1): r""" Randomly set the elements of output to 0 or 1 with the probability of P which follows the Bernoulli distribution. .. math:: out_{i} \sim Bernoulli(p_{i}) Args: x (Tensor): Tensor of shape :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Data type must be int8, uint8, int16, int32, int64, bool, float32 or float64. p (Union[Tensor, float], optional): The shape of p need to be broadcast. Data type must be float32 or float64. The elements of p represent the probability of setting 1 for the corresponding broadcast position of the current Tensor. The value of `p` must be in the range `[0, 1]`. Default: 0.5. seed (int, optional): The seed value for random generating. The value of `seed` must be -1 or a positive integer. Default: -1, which means using the current timestamp. Returns: output (Tensor), with the same shape and type as x. Raises: TypeError: If dtype of `x` is not one of: int8, uint8, int16, int32, int64, bool, float32, float64. TypeError: If dtype of `p` is not one of: float32, float64. TypeError: If dtype of `seed` is not int. ValueError: If `p` is not in range [0, 1]. ValueError: If `seed` is less than 0 and not -1. Supported Platforms: ``GPU`` Examples: >>> import mindspore >>> import numpy as np >>> from mindspore import Tensor >>> import mindspore.ops as ops >>> input_x = Tensor(np.array([1, 2, 3]), mindspore.int8) >>> output = ops.bernoulli(input_x, p=1.0) >>> print(output) [1 1 1] >>> input_p = Tensor(np.array([0.0, 1.0, 1.0]), mindspore.float32) >>> output = ops.bernoulli(input_x, input_p) >>> print(output) [0 1 1] """ bernoulli_ = _get_cache_prim(Bernoulli)(seed) return bernoulli_(x, p)
[docs]def bessel_i1(x): r""" Computes the Bessel i1 function of x element-wise. Args: x (Tensor): The input tensor. The data type must be float16, float32 or float64. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([-1, -0.5, 0.5, 1]), mindspore.float32) >>> output = ops.bessel_i1(x) >>> print(output) [-0.5651591 -0.25789431 0.25789431 0.5651591] """ return bessel_i1_(x)
[docs]def bessel_i1e(x): r""" Computes the Bessel i1e function of x element-wise. Args: x (Tensor): The input tensor. The data type must be float16, float32 or float64. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([-1, -0.5, 0.5, 1]), mindspore.float32) >>> output = ops.bessel_i1e(x) >>> print(output) [-0.20791042 -0.15642083 0.15642083 0.20791042] """ return bessel_i1e_(x)
[docs]def bessel_k1(x): r""" Computes the Bessel k1 function of x element-wise. Args: x (Tensor): The input tensor. The data type must be float16, float32 or float64. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.5, 1., 2., 4.]), mindspore.float32) >>> output = ops.bessel_k1(x) >>> print(output) [1.65644112 0.60190723 0.13986588 0.0124835] """ return bessel_k1_(x)
[docs]def bessel_k1e(x): r""" Computes the Bessel k1e function of x element-wise. Args: x (Tensor): The input tensor. The data type must be float16, float32 or float64. :math:`(N,*)` where :math:`*` means, any number of additional dimensions. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16, float32 or float64. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([0.5, 1., 2., 4.]), mindspore.float32) >>> output = ops.bessel_k1e(x) >>> print(output) [2.73100971 1.63615349 1.03347685 0.68157595] """ return bessel_k1e_(x)
@constexpr def _check_input_dtype(param_name, input_dtype, allow_dtypes, cls_name): validator.check_type_name(param_name, input_dtype, allow_dtypes, cls_name) def deg2rad(x): """ Calculates a new tensor with each of the elements of `x` converted from angles in degrees to radians. Args: x (Tensor[Number]): The input tensor. It must be a positive-definite matrix. With float16, float32 or float64 data type. Returns: Tensor, has the same dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` isn't float16, float32 or float64. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([[90.0, -90.0], [180.0, -180.0], [270.0, -270.0]]).astype(np.float32)) >>> output = ops.deg2Rad(x) >>> print(output) [[ 1.5707964 -1.5707964] [ 3.1415927 -3.1415927] [ 4.712389 -4.712389 ]] """ if not isinstance(x, (Tensor, Tensor_)): raise TypeError("The input x must be tensor") dtype_op = _get_cache_prim(P.DType)() x_dtype = dtype_op(x) _check_input_dtype("x", x_dtype, [mstype.float16, mstype.float32, mstype.float64], "") if x_dtype == mstype.float16: out = x * (Tensor(math.pi / 180.0).astype(mstype.float16)) else: out = x * math.pi / 180.0 return out def rad2deg(x): """ Returns a new tensor with each of the elements of `x` converted from angles in radians to degrees. Args: x (Tensor): The input tensor. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` isn't float16, float32 or float64. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> import mindspore >>> from mindspore import Tensor >>> import mindspore.ops as ops >>> x = Tensor([[6.283, -3.142],[1.570, -6.283],[3.142, -1.570]], mindspore.float32) >>> output = ops.rad2deg(x) >>> print(output) [[ 359.98935 -180.02333] [ 89.95438 -359.98935] [ 180.02333 -89.95438]] """ if not isinstance(x, (Tensor, Tensor_)): raise TypeError("The input x must be tensor") dtype_op = _get_cache_prim(P.DType)() x_dtype = dtype_op(x) _check_input_dtype("x", x_dtype, [mstype.float16, mstype.float32, mstype.float64], "") if x_dtype == mstype.float16: out = x * (Tensor(180.0 / math.pi).astype(mstype.float16)) else: out = x * 180.0 / math.pi return out def frac(x): """ Calculates the fractional part of each element in the input Inputs: - x (Tensor) - x is a tensor. Outputs: Tensor, has the same shape and type as input. Raises: TypeError: If `x` is not a Tensor. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> import mindspore >>> import numpy as np >>> from mindspore import Tensor >>> from mindspore.common import dtype as mstype >>> import mindspore.ops as ops >>> x = Tensor([2, 4.2, -2.5], mstype.float16) >>> output = frac(x) >>> print(output) [ 0. 0.1992 -0.5 ] """ frac_op = P.Mod() return frac_op(x, 1) ##################################### # Reduction Operation Functions. ##################################### @constexpr def _create_cummin_perm(axis, x_shape): """Insure axis is in [-len(x_shape),len(s_shape)-1]""" len_axis = len(x_shape) if not isinstance(axis, int): raise TypeError(f"The date type of 'axis' must be Int, but got {axis}.") if axis < -len_axis or axis > len_axis: raise ValueError(f"The value of axis must be in [{-len_axis}, {len_axis}], but got {axis}.") prem = [i for i in range(len_axis)] if axis < 0: axis = axis + len_axis prem[0], prem[axis] = axis, 0 prem = tuple(prem) return prem
[docs]def cummin(x, axis): r""" Returns a tuple (values,indices) where 'values' is the cumulative minimum value of input Tensor `x` along the dimension `axis`, and `indices` is the index location of each minimum value. .. math:: \begin{array}{ll} \\ y_{i} = min(x_{1}, x_{2}, ... , x_{i}) \end{array} Args: x (Tensor): The input Tensor, rank of `x` > 0. axis (int): The dimension to do the operation over. The value of `axis` must be in the range `[-x.ndim, x.ndim - 1]`. Returns: tuple [Tensor], tuple of 2 Tensors, containing the cumulative minimum of elements and the index, The shape of each output tensor is the same as input `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If `axis` is not an int. ValueError: If `axis` is out the range of `[-x.ndim, x.ndim - 1]`. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> from mindspore import Tensor, ops >>> import mindspore >>> a = Tensor([-0.2284, -0.6628, 0.0975, 0.2680, -1.3298, -0.4220], mindspore.float32) >>> output = ops.cummin(a, axis=0) >>> print(output[0]) [-0.2284 -0.6628 -0.6628 -0.6628 -1.3298 -1.3298] >>> print(output[1]) [0 1 1 1 4 4] """ cummin_op = _get_cache_prim(Cummin)(axis=0) if axis == 0: out1, out2 = cummin_op(x) else: transpose = _get_cache_prim(P.Transpose)() _shape_op = _get_cache_prim(P.Shape)() x_shape = _shape_op(x) prem = _create_cummin_perm(axis, x_shape) x = transpose(x, prem) out1, out2 = cummin_op(x) out1 = transpose(out1, prem) out2 = transpose(out2, prem) return [out1, out2]
[docs]def cummax(x, axis): r""" Returns a tuple (values,indices) where 'values' is the cumulative maximum value of input Tensor `x` along the dimension `axis`, and `indices` is the index location of each maximum value. .. math:: \begin{array}{ll} \\ y_{i} = max(x_{1}, x_{2}, ... , x_{i}) \end{array} Args: x (Tensor): The input Tensor, rank of `x` > 0. axis (int): The dimension to do the operation over. The value of `axis` must be in the range `[-x.ndim, x.ndim - 1]`. Returns: tuple [Tensor], tuple of 2 Tensors, containing the cumulative maximum of elements and the index, The shape of each output tensor is the same as input `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If `axis` is not an int. ValueError: If `axis` is out the range of `[-x.ndim, x.ndim - 1]`. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> import mindspore >>> import numpy as np >>> from mindspore import Tensor >>> import mindspore.ops as ops >>> x = Tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float32)) >>> output = ops.cummax(x, axis=0) >>> print(output[0]) [[ 3. 4. 6. 10.] [ 3. 6. 7. 10.] [ 4. 6. 8. 10.] [ 4. 6. 8. 10.]] >>> print(output[1]) [[0 0 0 0] [0 1 1 0] [2 1 2 0] [2 1 2 0]] """ _cummax = _get_cache_prim(ops.Cummax)(axis=axis) return _cummax(x)
[docs]def sparse_segment_mean(x, indices, segment_ids): r""" Computes a Tensor such that :math:`output_i = \frac{\sum_j x_{indices[j]}}{N}` where mean is over :math:`j` such that :math:`segment\_ids[j] == i` and :math:`N` is the total number of values summed. If the mean is empty for a given segment ID :math:`i`, :math:`output[i] = 0`. Note: - On CPU, values in `segment_ids` are always validated to be sorted, and an error is thrown for indices that are not increasing. Moreover, values in `indices` are validated to be bounded, and an error is thrown when `indices` are out of range[0, x.shape[0]). - On GPU, this does not throw an error for unsorted `segment_ids` and out-of-bound `indices`. Out-of-order `segment_ids` result in safe but unspecified behavior, while out-of-range `indices` will be ignored. Args: x (Tensor): A Tensor, and its rank must be greater than or equal to 1. indices (Tensor): A 1-D Tensor, with int32 or int64 data type. segment_ids (Tensor): A 1-D Tensor, must have the same dtype as `indices`. Values should be sorted and can be repeated. Returns: Tensor, whose dtype and rank is the same as `x`. The first dimension is equal to the value of the last element of `segment_ids` plus one, and the other dimensions are the same as those of `x`. Raises: TypeError: If `x`, `indices` or `segment_ids` is not a Tensor. TypeError: If the dtype of `x` is not one of the following dtype: float16, float32, float64. TypeError: If the dtype of `indices` and `segment_ids` are not one of the following dtype: int32, int64. TypeError: If the dtype of `indices` and `segment_ids` are not the same. ValueError: If the shape of `x`, 'indices' or `segment_ids` don't meet the parameter description. ValueError: If the size of 'indices' and `segment_ids` are not the same. Supported Platforms: ``GPU`` ``CPU`` Examples: >>> x = Tensor([[0, 1, 2], [1, 2, 3], [3, 6, 7]], dtype=mindspore.float32) >>> indices = Tensor([0, 1, 2], dtype=mindspore.int32) >>> segment_ids = Tensor([1,2,2], dtype=mindspore.int32) >>> out = ops.sparse_segment_mean(x, indices, segment_ids) >>> print(out) [[0. 0. 0.] [0. 1. 2.] [2. 4. 5.]] """ return sparse_segment_mean_(x, indices, segment_ids)
[docs]def logsumexp(x, axis, keep_dims=False): r""" Reduces a dimension of a tensor by calculating exponential for all elements in the dimension, then calculate logarithm of the sum. .. math:: logsumexp(x) = \log(\sum(e^(x-x_{max}))) + x_{max} Note: The dimension of input Tensor on Ascend should be less than or equal to 8, and the dimension of input Tensor on CPU should be less than 8. Args: x (Tensor): The input tensor. With float16 or float32 data type. axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. keep_dims (bool): If True, keep these reduced dimensions and the length is 1. If False, don't keep these dimensions. Default : False. Returns: Tensor, has the same dtype as the `x`. - If axis is (), and keep_dims is False, the output is a 0-D tensor representing the sum of all elements in the input tensor. - If axis is int, set as 2, and keep_dims is False, the shape of output is :math:`(x_1, x_3, ..., x_R)`. - If axis is tuple(int), set as (2, 3), and keep_dims is False, the shape of output is :math:`(x_1, x_4, ..., x_R)`. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32)) >>> output = ops.logsumexp(x, 1, keep_dims=True) >>> print(output.shape) (3, 1, 5, 6) """ _exp = _get_cache_prim(P.Exp)() _reduce_sum = _get_cache_prim(P.ReduceSum)(keep_dims) _log = _get_cache_prim(P.Log)() _exp = _get_cache_prim(P.Exp)() _reduce_sum = _get_cache_prim(P.ReduceSum)(keep_dims) _log = _get_cache_prim(P.Log)() x_max = x.max(axis=axis, keepdims=True) x_exp = _exp(x - x_max) x_sumexp = _reduce_sum(x_exp, axis) x_logsumexp = _log(x_sumexp) if not keep_dims: x_max = x_max.squeeze(axis=axis) return x_logsumexp + x_max
[docs]def amin(x, axis=(), keep_dims=False): r""" Reduces a dimension of a tensor by the minimum value in the dimension, by default. And also can reduce a dimension of `x` along the axis. Determine whether the dimensions of the output and input are the same by controlling `keep_dims`. Args: x (Tensor[Number]): The input tensor. The dtype of the tensor to be reduced is number. :math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8. axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Assume the rank of `x` is r, and the value range is [-r,r).. keep_dims (bool): If true, keep these reduced dimensions and the length is 1. If false, don't keep these dimensions. Default: False. Returns: Tensor, has the same data type as input tensor. - If `axis` is (), and `keep_dims` is False, the output is a 0-D tensor representing the product of all elements in the input tensor. - If `axis` is int, set as 1, and `keep_dims` is False, the shape of output is :math:`(x_0, x_2, ..., x_R)`. - If `axis` is tuple(int), set as (1, 2), and `keep_dims` is False, the shape of output is :math:`(x_0, x_3, ..., x_R)`. Raises: TypeError: If `x` is not a Tensor. TypeError: If `axis` is not one of the following: int, tuple or list. TypeError: If `keep_dims` is not a bool. ValueError: If `axis` is out of range. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32)) >>> output = ops.amin(x, 1, keep_dims=True) >>> result = output.shape >>> print(result) (3, 1, 5, 6) >>> # case 1: Reduces a dimension by the minimum value of all elements in the dimension. >>> x = Tensor(np.array([[[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]], ... [[4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]], ... [[7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]]]), mindspore.float32) >>> output = ops.amin(x) >>> print(output) 1.0 >>> print(output.shape) () >>> # case 2: Reduces a dimension along axis 0. >>> output = ops.amin(x, 0, True) >>> print(output) [[[1. 1. 1. 1. 1. 1.] [2. 2. 2. 2. 2. 2.] [3. 3. 3. 3. 3. 3.]]] >>> # case 3: Reduces a dimension along axis 1. >>> output = ops.amin(x, 1, True) >>> print(output) [[[1. 1. 1. 1. 1. 1.]] [[4. 4. 4. 4. 4. 4.]] [[7. 7. 7. 7. 7. 7.]]] >>> # case 4: Reduces a dimension along axis 2. >>> output = ops.amin(x, 2, True) >>> print(output) [[[1.] [2.] [3.]] [[4.] [5.] [6.]] [[7.] [8.] [9.]]] """ return _get_cache_prim(P.ReduceMin)(keep_dims)(x, axis)
[docs]def amax(x, axis=(), keep_dims=False): r""" Reduces a dimension of a tensor by the maximum value in this dimension, by default. And also can reduce a dimension of `x` along the axis. Determine whether the dimensions of the output and input are the same by controlling `keep_dims`. Args: x (Tensor[Number]): The input tensor. The dtype of the tensor to be reduced is number. :math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8. axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Assume the rank of `x` is r, and the value range is [-r,r). keep_dims (bool): If true, keep these reduced dimensions and the length is 1. If false, don't keep these dimensions. Default: False. Returns: Tensor, has the same data type as input tensor. - If `axis` is (), and `keep_dims` is False, the output is a 0-D tensor representing the product of all elements in the input tensor. - If `axis` is int, set as 1, and `keep_dims` is False, the shape of output is :math:`(x_0, x_2, ..., x_R)`. - If `axis` is tuple(int), set as (1, 2), and `keep_dims` is False, the shape of output is :math:`(x_0, x_3, ..., x_R)`. Raises: TypeError: If `x` is not a Tensor. TypeError: If `axis` is not one of the following: int, tuple or list. TypeError: If `keep_dims` is not a bool. ValueError: If `axis` is out of range. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32)) >>> output = ops.amax(x, 1, keep_dims=True) >>> result = output.shape >>> print(result) (3, 1, 5, 6) >>> # case 1: Reduces a dimension by the maximum value of all elements in the dimension. >>> x = Tensor(np.array([[[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]], ... [[4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]], ... [[7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]]]), mindspore.float32) >>> output = ops.amax(x) >>> print(output) 9.0 >>> print(output.shape) () >>> # case 2: Reduces a dimension along axis 0. >>> output = ops.amax(x, 0, True) >>> print(output) [[[7. 7. 7. 7. 7. 7.] [8. 8. 8. 8. 8. 8.] [9. 9. 9. 9. 9. 9.]]] >>> # case 3: Reduces a dimension along axis 1. >>> output = ops.amax(x, 1, True) >>> print(output) [[[3. 3. 3. 3. 3. 3.]] [[6. 6. 6. 6. 6. 6.]] [[9. 9. 9. 9. 9. 9.]]] >>> # case 4: Reduces a dimension along axis 2. >>> output = ops.amax(x, 2, True) >>> print(output) [[[1.] [2.] [3.]] [[4.] [5.] [6.]] [[7.] [8.] [9.]]] """ return _get_cache_prim(P.ReduceMax)(keep_dims)(x, axis)
[docs]def mean(x, axis=(), keep_dims=False): r""" Reduces a dimension of a tensor by averaging all elements in the dimension, by default. And also can reduce a dimension of `x` along the axis. Determine whether the dimensions of the output and input are the same by controlling `keep_dims`. Args: x (Tensor[Number]): The input tensor. The dtype of the tensor to be reduced is number. :math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8. axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Assume the rank of `x` is r, and the value range is [-r,r). keep_dims (bool): If true, keep these reduced dimensions and the length is 1. If false, don't keep these dimensions. Default: False. Returns: Tensor, has the same data type as input tensor. - If `axis` is (), and `keep_dims` is False, the output is a 0-D tensor representing the product of all elements in the input tensor. - If `axis` is int, set as 1, and `keep_dims` is False, the shape of output is :math:`(x_0, x_2, ..., x_R)`. - If `axis` is tuple(int), set as (1, 2), and `keep_dims` is False, the shape of output is :math:`(x_0, x_3, ..., x_R)`. Raises: TypeError: If `x` is not a Tensor. TypeError: If `axis` is not one of the following: int, tuple or list. TypeError: If `keep_dims` is not a bool. ValueError: If `axis` is out of range. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32)) >>> output = ops.mean(x, 1, keep_dims=True) >>> result = output.shape >>> print(result) (3, 1, 5, 6) >>> # case 1: Reduces a dimension by averaging all elements in the dimension. >>> x = Tensor(np.array([[[2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2]], ... [[4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]], ... [[6, 6, 6, 6, 6, 6], [8, 8, 8, 8, 8, 8], [10, 10, 10, 10, 10, 10]]]), ... mindspore.float32) >>> output = ops.mean(x) >>> print(output) 5.0 >>> print(output.shape) () >>> # case 2: Reduces a dimension along the axis 0 >>> output = ops.mean(x, 0, True) >>> print(output) [[[4. 4. 4. 4. 4. 4.] [5. 5. 5. 5. 5. 5.] [6. 6. 6. 6. 6. 6.]]] >>> # case 3: Reduces a dimension along the axis 1 >>> output = ops.mean(x, 1, True) >>> print(output) [[[2. 2. 2. 2. 2. 2.]] [[5. 5. 5. 5. 5. 5.]] [[8. 8. 8. 8. 8. 8.]]] >>> # case 4: Reduces a dimension along the axis 2 >>> output = ops.mean(x, 2, True) >>> print(output) [[[ 2.] [ 2.] [ 2.]] [[ 4.] [ 5.] [ 6.]] [[ 6.] [ 8.] [10.]]] """ return _get_cache_prim(P.ReduceMean)(keep_dims)(x, axis)
[docs]def prod(x, axis=(), keep_dims=False): r""" Reduces a dimension of a tensor by multiplying all elements in the dimension, by default. And also can reduce a dimension of `x` along the axis. Determine whether the dimensions of the output and input are the same by controlling `keep_dims`. Args: x (Tensor[Number]): The input tensor. The dtype of the tensor to be reduced is number. :math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8. axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Assume the rank of `x` is r, and the value range is [-r,r). keep_dims (bool): If true, keep these reduced dimensions and the length is 1. If false, don't keep these dimensions. Default: False. Returns: Tensor, has the same data type as input tensor. - If `axis` is (), and `keep_dims` is False, the output is a 0-D tensor representing the product of all elements in the input tensor. - If `axis` is int, set as 1, and `keep_dims` is False, the shape of output is :math:`(x_0, x_2, ..., x_R)`. - If `axis` is tuple(int), set as (1, 2), and `keep_dims` is False, the shape of output is :math:`(x_0, x_3, ..., x_R)`. Raises: TypeError: If `x` is not a Tensor. TypeError: If `axis` is not one of the following: int, tuple or list. TypeError: If `keep_dims` is not a bool. ValueError: If `axis` is out of range. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32)) >>> output = ops.prod(x, 1, keep_dims=True) >>> result = output.shape >>> print(result) (3, 1, 5, 6) >>> # case 1: Reduces a dimension by multiplying all elements in the dimension. >>> x = Tensor(np.array([[[1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]], ... [[4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6]], ... [[7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9]]]), mindspore.float32) >>> output = ops.prod(x) >>> print(output) 2.2833798e+33 >>> print(output.shape) () >>> # case 2: Reduces a dimension along axis 0. >>> output = ops.prod(x, 0, True) >>> print(output) [[[ 28. 28. 28. 28. 28. 28.] [ 80. 80. 80. 80. 80. 80.] [162. 162. 162. 162. 162. 162.]]] >>> # case 3: Reduces a dimension along axis 1. >>> output = ops.prod(x, 1, True) >>> print(output) [[[ 6. 6. 6. 6. 6. 6.]] [[120. 120. 120. 120. 120. 120.]] [[504. 504. 504. 504. 504. 504.]]] >>> # case 4: Reduces a dimension along axis 2. >>> output = ops.prod(x, 2, True) >>> print(output) [[[1.00000e+00] [6.40000e+01] [7.29000e+02]] [[4.09600e+03] [1.56250e+04] [4.66560e+04]] [[1.17649e+05] [2.62144e+05] [5.31441e+05]]] """ return _get_cache_prim(P.ReduceProd)(keep_dims)(x, axis)
[docs]def norm(input_x, axis, p=2, keep_dims=False, epsilon=1e-12): r""" Returns the matrix norm or vector norm of a given tensor. .. math:: output = sum(abs(input)**p)**(1/p) Args: input_x (Tensor): Input tensor. The dtype must be float32 or float16. axis (Union[int, list, tuple]): Specifies which dimension or dimensions of input to calculate the norm across. p (int): The order of norm. Default: 2. `p` is greater than or equal to 0. keep_dims (bool): Whether the output tensors have dim retained or not. Default: False. epsilon (float): A value added to the denominator for numerical stability. Default: 1e-12. Returns: Tensor, has the same dtype as `input`, which shape depends on the args axis. For example, if the size of input is (2, 3, 4), axis is [0, 1], Outputs' shape will be (4,). Raises: TypeError: If `input` is not a Tensor. TypeError: If dtype of `input` is not one of: float16, float32. TypeError: If `p` is not an int. TypeError: If `axis` is not an int, a tuple or a list. TypeError: If `axis` is a tuple or a list, but the element of `axis` is not an int. TypeError: If `keep_dims` is not a bool. TypeError: If `epsilon` is not a float. ValueError: If the element of `axis` is out of the range (-len(input.shape), len(input.shape)). ValueError: If the length of shape of `axis` is bigger than the length of shape of `input`. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> input_x = Tensor(np.array([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]).astype(np.float32)) >>> output = ops.norm(input_x, [0, 1], p=2) >>> print(output) [ 9.165152 10.954452] """ lp_norm_inner = _get_cache_prim(P.LpNorm)(axis, p, keep_dims, epsilon) return lp_norm_inner(input_x)
def lu_unpack(LU_data, LU_pivots, unpack_data=True, unpack_pivots=True): """ Unpack the LU_data and LU_pivots from a LU factorization of a tensor. Args: LU_data (Tensor): The packed LU factorization data. A tensor of size [*, M, N], where * is batch dimensions, with data type int8, uint8, int16, int32, int64, float16, float32, float64. The dims of LU_data must be equal to or greater than 2. LU_pivots (Tensor): The packed LU factorization pivots. A tensor of size [*, min(M, N)], where * is batch dimensions, with data type int8, uint8, int16, int32, int64. unpack_data (bool): A flag indicating if the LU_data should be unpacked. If False, then the returned L and U are None. Default: True. unpack_pivots (bool): A flag indicating if the LU_pivots should be unpacked into a permutation matrix P. If False, then the returned P is None. Default: True. Returns: pivots (Tensor) - The permutation matrix of LU factorization. The shape is `[*, M, M]`, the dtype is same as `LU_data`. L (Tensor) - The L matrix of LU factorization. The dtype is same as `LU_data`. U (Tensor) - The U matrix of LU factorization. The dtype is same as `LU_data`. Raises: TypeError: If the dtype of `LU_data` is not one of the following: int8, uint8, int16, int32, int64, float16, float32, float64. TypeError: If the dtype of `LU_pivots` is not one of the following: int8, uint8, int16, int32, int64. ValueError: If the dimension of `LU_data` is less than 2. ValueError: If the dimension of `LU_pivots` is less than 1. ValueError: If the size of the last dimension of LU_pivots is not equal to the minimum of the sizes of the last two dimensions of LU_data. ValueError: If the batch dimensions of LU_data's does not match LU_pivots's batch dimensions. ValueError: On the CPU platform, if the value of `LU_pivots` are out of range[1, LU_data.shape[-2]). RuntimeError: On the Ascend platform, if the value of `LU_pivots` are out of range[1, LU_data.shape[-2]). Supported Platforms: ``Ascend`` ``CPU`` Examples: >>> from mindspore.ops import functional as F >>> LU_data = Tensor(np.array([[[-0.3806, -0.4872, 0.5536], ... [-0.1287, 0.6508, -0.2396], ... [ 0.2583, 0.5239, 0.6902]], ... [[ 0.6706, -1.1782, 0.4574], ... [-0.6401, -0.4779, 0.6701], ... [ 0.1015, -0.5363, 0.6165]]]), mstype.float64) >>> LU_pivots = Tensor(np.array([[1, 3, 3], ... [2, 3, 3]]), mstype.int32) >>> pivots, L, U = F.lu_unpack(LU_data, LU_pivots, unpack_data, unpack_pivots) >>> print(pivots) [[[1. 0. 0.] [0. 0. 1.] [0. 1. 0.]] [[0. 0. 1.] [1. 0. 0.] [0. 1. 0.]]] >>> print(L) [[[ 1. 0. 0.] [-0.1287 1. 0.] [ 0.2583 0.5239 1.]] [[ 1.0000 0. 0.] [-0.6401 1. 0.] [ 0.1015 -0.5363 1.]]] >>> print(U) [[[-0.3806 -0.4872 0.5536] [ 0. 0.6508 -0.2396] [ 0. 0. 0.6902]] [[ 0.6706 -1.1782 0.4574] [ 0. -0.4779 0.6701] [ 0. 0. 0.6165]]] """ pivots, l, u = LuUnpack(LU_data, LU_pivots) if unpack_data: if unpack_pivots: return pivots, l, u return None, l, u if unpack_pivots: return pivots, None, None return None, None, None
[docs]def renorm(input_x, p, dim, maxnorm): """ Renormalizes the sub-tensors along dimension `dim`, and each sub-tensor's p-norm should not exceed the 'maxnorm'. The values of current sub-tensor don't need change if the p-norm of the sub-tensor is less than `maxnorm`. Otherwise the sub-tensor needs to be modified to the original value of the corresponding position divided by the p-norm of the substensor and then multiplied by `maxnorm`. Args: input_x (Tensor): A Tensor, types: float32 or float16. p (int): Power of norm calculation. dim (int): The dimension that expected to get the slice-tensor. maxnorm (float32): Max norm. Returns: Tensor, has the same dtype and shape as input_x. Raises: TypeError: If dtype of `p` is not int. TypeError: If dtype of `dim` is not int. TypeError: If dtype of `maxnorm` is not float32. ValueError: If the value of `p` less than 1. Supported Platforms: ``Ascend`` ``CPU`` ``GPU`` Examples: >>> x = Tensor(np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]), mindspore.float32) >>> y = ops.renorm(x, p=1, dim=0, maxnorm=5.) >>> print(y) [[1. 1. 1. ] [1.6666666 1.6666666 1.6666666 ] [1.6666667 1.6666667 1.6666667 ]] """ renorm_ = _get_cache_prim(Renorm)(p, dim, maxnorm) return renorm_(input_x)
@constexpr def _check_attr_dtype(param_name, input_dtype, allow_dtypes, cls_name): validator.check_value_type(param_name, input_dtype, allow_dtypes, cls_name) @constexpr def _check_positive_float(arg_value, arg_name, cls_name): validator.check_positive_float(arg_value, arg_name, cls_name) @constexpr def _check_int_range(arg_value, lower_limit, upper_limit, arg_name=None, prim_name=None): validator.check_int_range(arg_value, lower_limit, upper_limit, Rel.INC_LEFT, arg_name, prim_name)
[docs]def gumbel_softmax(logits, tau=1, hard=False, dim=-1): r""" Returns the samples from the Gumbel-Softmax distribution and optionally discretizes. If `hard = True`, the returned samples will be one-hot, otherwise it will be probability distributions that sum to 1 across `dim`. Args: logits (Tensor): Unnormalized log probabilities. The data type must be float16 or float32. tau (float): The scalar temperature, which is a positive number. Default: 1.0. hard (bool): if `True`, the returned samples will be discretized as one-hot vectors, but will be differentiated as if it is the soft sample in autograd. Default: False. dim (int): Dim for softmax to compute. Default: -1. Returns: Tensor, has the same dtype and shape as `logits`. Raises: TypeError: If `logits` is not a Tensor. TypeError: If dtype of `logits` is not one of: float16, float32. TypeError: If `tau` is not an float. TypeError: If `hard` is not a bool. TypeError: If `dim` is not a int. ValueError: If If `tau` is not positive. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32) >>> output = ops.gumbel_softmax(input_x, 1.0, True, -1) >>> print(output.shape) (2, 3) """ if not isinstance(logits, (Tensor, Tensor_)): raise TypeError("The input logits must be tensor") if logits.shape == (): raise ValueError("For gumbel_softmax, the 0-D input is not supported.") logits_dtype = _get_cache_prim(P.DType)()(logits) _check_input_dtype("logits", logits_dtype, [mstype.float16, mstype.float32], "gumbel_softmax") _check_attr_dtype("tau", tau, [float], "gumbel_softmax") _check_attr_dtype("hard", hard, [bool], "gumbel_softmax") _check_attr_dtype("dim", dim, [int], "gumbel_softmax") _check_positive_float(tau, "tau", "gumbel_softmax") if hard: _check_int_range(dim, -1, len(logits.shape), 'dim', "gumbel_softmax") else: _check_int_range(dim, -len(logits.shape), len(logits.shape), 'dim', "gumbel_softmax") log_op = _get_cache_prim(P.Log)() const_op = _get_cache_prim(P.ScalarToArray)() sample_shape = _get_cache_prim(P.Shape)()(logits) uniform = C.uniform(sample_shape, const_op(0.0), const_op(1.0)) uniform = _get_cache_prim(P.Cast)()(uniform, logits_dtype) gumbel = neg_tensor(log_op(neg_tensor(log_op(uniform)))) gumbel = (logits + gumbel) / tau y_soft = _get_cache_prim(P.Softmax)(dim)(gumbel) if hard: index = y_soft.argmax(axis=dim) y_hard = _get_cache_prim(P.OneHot)(dim)(index, sample_shape[dim], Tensor(1, logits_dtype), Tensor(0, logits_dtype)) ret = y_hard - ops.stop_gradient(y_soft) + y_soft else: ret = y_soft return ret
def stft(x, n_fft, hop_length=None, win_length=None, window=None, center=True, pad_mode="REFLECT", normalized=False, onesided=None, return_complex=None): r""" STFTs can be used as a way of quantifying the change of a nonstationary signal’s frequency and phase content over time. Ignoring the optional batch dimension, this method computes the following expression: math:`X[\omega, m]=\sum_{k=0}^{\text {win_length-1 }} \text { window }[k] \text { input }[m \times \text { hop_length }+ k] \exp \left(-j \frac{2 \pi \cdot \omega k}{\text { win_length }}\right)` where m is the index of the sliding window, and math:`\omegaω` is the frequency math:`0 \leq \omega < \text{n\_fft}0≤ω<n_fft` Args: x (Tensor): Time sequence of stft, must be either a 1-D time tensor or a 2-D tensor. n_fft (int): The size of Fourier transform. hop_length (int, optional): The distance between neighboring sliding window frames. Default: ``None`` (treated as equal to ``floor(n_fft / 4)``). win_length (int, optional): the size of window frame and STFT filter. Default: ``None`` (treated as equal to :attr:`n_fft`). window (Tensor, optional): the optional window function. Default: ``None`` (treated as window of all :math:`1` s). center (bool, optional): whether to pad :attr:`input` on both sides. Default: ``True``. pad_mode (string, optional): controls the padding method used when :attr:`center` is ``True``. Default: ``"REFLECT"``. normalized (bool, optional): controls whether to return the normalized STFT results Default: ``False``. onesided (bool, optional): controls whether to return half of results to avoid redundancy for real inputs. Default: ``True`` for real :attr:`input` and :attr:`window`, ``False`` otherwise. return_complex (bool, optional): whether to return a complex tensor, or a real tensor with an extra last dimension for the real and imaginary components. Default: ``True`` for complex :attr:`input` or :attr:`window`, ``False`` otherwise. Returns: Tensor. - **output** (Tensor) - A tensor containing the STFT result with shape described above. Raises: TypeError: If the x is not a tensor. ValueError: If x arguments have values not specified above. Examples: >>> import mindspore as ms >>> from mindspore import ops >>> import numpy as np >>> x = ms.Tensor(np.random.rand(2,7192), ms.float32) >>> output = ops.stft(n_fft=64, x=x) >>> print(output.shape) (2, 33, 450, 2) """ if hop_length is None: hop_length = int(np.floor(n_fft / 4)) if win_length is None: win_length = int(np.floor(n_fft)) if window is None: window = P.Ones()((win_length,), mstype.float32) def _is_complex(x): dtype = P.DType() return dtype(x) in [mstype.complex64, mstype.complex128] if onesided is None: onesided = (not _is_complex(x)) and (not _is_complex(window)) if return_complex is None: return_complex = _is_complex(x) or _is_complex(window) if center: _check_input_dtype("center", center, [bool], "") signal_dim = len(x.shape) pad = n_fft // 2 if signal_dim == 1: x = layer.Pad(((pad, pad),), pad_mode)(x) elif signal_dim == 2: x = layer.Pad(((0, 0), (pad, pad)), pad_mode)(x) else: raise ValueError( f"Expected a 1-D tensor or a 2-D tensor, but got {signal_dim}") stft_ = STFT(n_fft, hop_length, win_length, normalized, onesided, return_complex) return stft_(x, window) def _check_same_type(dtype1, dtype2): return dtype1 == dtype2 @constexpr def _max(*args): """Returns the maximum value.""" return max(*args) @constexpr def _min(*args): """Returns the minimum value.""" return min(*args) @constexpr def _infer_shape_rem(shape1, shape2, ndim1, ndim2, transpose_b): """Infers the shape of the last two dimensions after performing matmul.""" shape_rem = [] if ndim1 >= 2: shape_rem.append(shape1[-2]) if transpose_b: if ndim2 >= 2: shape_rem.append(shape2[-2]) else: if ndim1 >= 1: shape_rem.append(shape2[-1]) return tuple(shape_rem) @constexpr def _check_matmul_shapes(shape1, shape2, prim_name=None): """Checks shape1 and shape2 are valid to perform matmul, and returns output shape after broadcasting.""" msg_prefix = f"For '{prim_name}', the" if prim_name else "The" ndim1, ndim2 = len(shape1), len(shape2) if ndim1 < 1 or ndim2 < 1: raise ValueError(f"{msg_prefix} dimension of input operands must be at least 1, but got " f"the length of shape1: {ndim1}, the length of shape2: {ndim2}.") if ndim2 >= 2 and shape1[-1] != shape2[-2]: raise ValueError(f"{msg_prefix} shape1[-1] must be equal to shape2[-2] when the length of shape2 " f"is greater than or equal to 2, but got shape1[-1]: {shape1[-1]}, " f"shape2[-2]: {shape2[-2]}.") shape_out = deque() for items in zip_longest(reversed(shape1[:-2]), reversed(shape2[:-2]), fillvalue=1): max_size = max(items) for item in items: if item not in (1, max_size): raise ValueError(f"{msg_prefix} operands could not be broadcast together with shape1 {shape1} and " f"shape2 {shape2}.") shape_out.appendleft(max_size) return tuple(shape_out) @constexpr def _tile_size(shape, out_shape, ndim): """Returns tile_size such that shape*tile_size = out_shape""" size = [1] * ndim for idx, (i, j) in enumerate(zip(shape, out_shape)): if i != j: size[idx] = j return tuple(size) @constexpr def _check_need_broadcast(shape1, shape2): """Returns True if broadcast is necessary for batchmatmul.""" return shape1[:-2] != shape2[:-2] @constexpr def _check_input_1d(input_shape, param_name, func_name): if len(input_shape) != 1: raise ValueError(f"{func_name} {param_name} should be 1d, but got shape {input_shape}") return True @constexpr def _check_input_2d(input_shape, param_name, func_name): if len(input_shape) != 2: raise ValueError(f"{func_name} {param_name} should be 2d, but got shape {input_shape}") return True def _expand(x, ndim): """Expand x to ndim from axis, which can be 0 or -1.""" rank_op = _get_cache_prim(P.Rank)() expand_dims_op = _get_cache_prim(P.ExpandDims)() while rank_op(x) < ndim: x = expand_dims_op(x, 0) return x def _broadcast_to(x, shape_cur, shape_to, ndim_to): """Broadcasts x from shape_cur to shape_to.""" tile_op = _get_cache_prim(P.Tile)() size = _tile_size(shape_cur, shape_to, ndim_to) return tile_op(x, size)
[docs]def matmul(x1, x2): """ Returns the matrix product of two tensors. Note: Numpy arguments `out`, `casting`, `order`, `subok`, `signature`, and `extobj` are not supported. On GPU, the supported dtypes are np.float16 and np.float32. On CPU, the supported dtypes are np.float16 and np.float32. Args: x1 (Tensor): Input tensor, scalar not allowed. The last dimension of `x1` must be the same size as the second last dimension of `x2`. And the shape of x1 and x2 could be broadcast. x2 (Tensor): Input tensor, scalar not allowed. The last dimension of `x1` must be the same size as the second last dimension of `x2`. And the shape of x1 and x2 could be broadcast. Returns: Tensor or scalar, the matrix product of the inputs. This is a scalar only when both `x1`, `x2` are 1-d vectors. Raises: ValueError: If the last dimension of `x1` is not the same size as the second-to-last dimension of `x2`, or if a scalar value is passed in. ValueError: If the shape of `x1` and `x2` could not broadcast together. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> # case 1 : Reasonable application of broadcast mechanism >>> x1 = Tensor(np.arange(2*3*4).reshape(2, 3, 4), mindspore.float32) >>> x2 = Tensor(np.arange(4*5).reshape(4, 5), mindspore.float32) >>> output = ops.matmul(x1, x2) >>> print(output) [[[ 70. 76. 82. 88. 94.] [ 190. 212. 234. 256. 278.] [ 310. 348. 386. 424. 462.]] [[ 430. 484. 538. 592. 646.] [ 550. 620. 690. 760. 830.] [ 670. 756. 842. 928. 1014.]]] >>> print(output.shape) (2, 3, 5) >>> # case 2 : the rank of `x1` is 1 >>> x1 = Tensor(np.ones([1, 2]), mindspore.float32) >>> x2 = Tensor(np.ones([2,]), mindspore.float32) >>> output = ops.matmul(x1, x2) >>> print(output) [2.] >>> print(output.shape) (1,) """ dtype_op = _get_cache_prim(P.DType)() rank_op = _get_cache_prim(P.Rank)() shape_op = _get_cache_prim(P.Shape)() reshape_op = _get_cache_prim(P.Reshape)() dtype1 = dtype_op(x1) dtype2 = dtype_op(x2) if not _check_same_type(dtype1, dtype2): x1 = x1.astype(mstype.float32) x2 = x2.astype(mstype.float32) ndim1_orig, ndim2_orig = rank_op(x1), rank_op(x2) shape1_orig, shape2_orig = shape_op(x1), shape_op(x2) transpose_b = ndim2_orig == 1 shape_backbone = _check_matmul_shapes(shape1_orig, shape2_orig, 'matmul') # infers the shape of the output shape_out = shape_backbone + _infer_shape_rem(shape1_orig, shape2_orig, ndim1_orig, ndim2_orig, transpose_b) _matmul = _get_cache_prim(P.MatMul)(False, transpose_b) _batch_matmul = _get_cache_prim(P.BatchMatMul)(False, transpose_b) x1 = _expand(x1, 2) x2 = _expand(x2, 2) if rank_op(x2) == 2: if rank_op(x1) > 2: x1 = reshape_op(x1, (-1, shape1_orig[-1])) res = _matmul(x1, x2) else: # broadcasts x1.shape[:-2] with x2.shape[:-2] ndim_aligned = _max(ndim1_orig, ndim2_orig) x1 = _expand(x1, ndim_aligned) x2 = _expand(x2, ndim_aligned) shape1_aligned, shape2_aligned = shape_op(x1), shape_op(x2) x1 = _broadcast_to(x1, shape1_aligned[:-2], shape_backbone, ndim_aligned) x2 = _broadcast_to(x2, shape2_aligned[:-2], shape_backbone, ndim_aligned) res = _batch_matmul(x1, x2) return reshape_op(res, shape_out)
def bmm(input_x, mat2): """ Computes matrix multiplication between two tensors by batch. .. math:: \text{output}[..., :, :] = \text{matrix}(input_x[..., :, :]) * \text{matrix}(mat2[..., :, :]) The first input tensor must be not less than `3` and the second input must be not less than `2`. Args: input_x (Tensor): The first tensor to be multiplied. The shape of the tensor is :math:`(*B, N, C)`, where :math:`*B` represents the batch size which can be multidimensional, :math:`N` and :math:`C` are the size of the last two dimensions. mat2 (Tensor): The second tensor to be multiplied. The shape of the tensor is :math:`(*B, C, M)`. Returns: Tensor, the shape of the output tensor is :math:`(*B, N, M)`. Raises: ValueError: If length of shape of `input_x` is not equal to length of shape of `mat2` or length of shape of `input_x` is less than `3`. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> input_x = Tensor(np.ones(shape=[2, 4, 1, 3]), mindspore.float32) >>> mat2 = Tensor(np.ones(shape=[2, 4, 3, 4]), mindspore.float32) >>> output = ops.bmm(input_x, mat2) >>> print(output) [[[[3. 3. 3. 3.]] [[3. 3. 3. 3.]] [[3. 3. 3. 3.]] [[3. 3. 3. 3.]]] [[[3. 3. 3. 3.]] [[3. 3. 3. 3.]] [[3. 3. 3. 3.]] [[3. 3. 3. 3.]]]] """ if not (isinstance(input_x, Tensor) and isinstance(mat2, Tensor)): raise TypeError("For bmm op, inputs input_x and mat2 must be all tensors.") bmm_op = _get_cache_prim(P.BatchMatMul)() return bmm_op(input_x, mat2) def baddbmm(x, batch1, batch2, beta=1, alpha=1): r""" Performs a batch matrix-matrix product of matrices in batch1 and batch2. input is added to the final result. The formula is defined as follows: .. math:: \text{out}_{i} = \beta \text{input}_{i} + \alpha (\text{batch1}_{i} \mathbin{@} \text{batch2}_{i}) Args: x (Tensor): The tensor to be added. batch1 (Tensor): The first batch of matrices to be multiplied. batch2 (Tensor): The second batch of matrices to be multiplied. alpha: multiplier for `batch1 @ batch2`. beta: multiplier for input. Returns: Tensor, the output tensor. Raises: TypeError: For Baddbmm, inputs are not tensors. ValueError: If `batch1` and `batch2` are not 3-D tensors. TypeError: For inputs of type FloatTensor or DoubleTensor, \ arguments beta and alpha not be real numbers, otherwise not be integers. ValueError: For Baddbmm, attributes alpha and beta are not real numbers Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> input = Tensor(np.ones([1, 3, 3]).astype(np.float32)) >>> batch1 = Tensor(np.ones([1, 3, 4]).astype(np.float32)) >>> batch2 = Tensor(np.ones([1, 4, 3]).astype(np.float32)) >>> output = ops.baddbmm(input, batch1, batch2) >>> print(output) [[[5. 5. 5.] [5. 5. 5.] [5. 5. 5.]]] """ dtypeop = _get_cache_prim(P.DType)() bmmop = _get_cache_prim(P.BatchMatMul)(False, False) if not (isinstance(x, Tensor) and isinstance(batch1, Tensor) and isinstance(batch2, Tensor)): raise TypeError("For Baddbmm, inputs must be all tensors.") if len(batch1.shape) != 3 or len(batch2.shape) != 3: raise ValueError("For batch1 and batch2 must be 3-D tensors each containing the same number of matrices, " f"but got length of batch1:'{len(batch1.shape)}', length of batch2:'{len(batch2.shape)}'.") input_dtype = dtypeop(x) if not (input_dtype == dtypeop(batch1) and input_dtype == dtypeop(batch2)): raise TypeError("For Baddbmm, the inputs should be the same dtype.") if input_dtype in (mstype.float16, mstype.float32, mstype.float64): if not (isinstance(alpha, (int, float)) and isinstance(beta, (int, float))): raise TypeError("For attributes alpha and beta should be real numbers.") check_is_number(alpha, (int, float)) check_is_number(beta, (int, float)) else: if not (isinstance(alpha, int) and isinstance(beta, int)): raise TypeError("For inputs of type not FloatTensor or DoubleTensor, " "arguments beta and alpha must be integers.") y = beta * x + alpha * (bmmop(batch1, batch2)) return y def log2(x): r""" Returns a new tensor with the logarithm to the base 2 of the elements of input. .. math:: y_i = log_2(x_i) .. warning:: If the input value of operator Log2 is within the range (0, 0.01] or [0.95, 1.05], the output accuracy may be affacted. If the input value of operator Log2 is less than or equal to 0, it will not raise Error. .. note:: The dimension of the input Tensor on Ascend should be less than or equal to 8, and the dimension of the input Tensor on the CPU or GPU should be less than 8. Args: x (Tensor): Input Tensor of any dimension. The value must be greater than 0. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16 or float32 or float64 on CPU and GPU, if dtype of `x` is not float16 or float32 on Ascend. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([2, 4, 8]).astype(np.float16)) >>> output = ops.log2(x) >>> print(output) [1. 2. 3.] """ dtype_op = _get_cache_prim(P.DType)() x_dtype = dtype_op(x) denominator = log_(_make_tensor(2, x_dtype)) frac_log = log_(x) output = frac_log / denominator return output def arrange(x): lists = [] for i in range(0, x): lists.append(i) return lists def rot90(x, k, dims): """ Rotate a n-D tensor by 90 degrees in the plane specified by dims axis. Rotation direction is from the first towards the second axis if k > 0, and from the second towards the first for k < 0. Args: x (Tensor): Input tensor. k (int): Number of times to rotate. dims (a list or tuple): Axis to rotate. Returns: Tensor. Raises: TypeError: If `x` is not a Tensor. TypeError: If `k` is not integer. TypeError: If `dims` is not tuple of integers or list of ints. ValueError: If the length of `dims` is not `2`. ValueError: If any dims is out of range. RuntimeError: If rotation dims are not different. Supported Platforms: ``Ascend`` ``GPU`` Examples: >>> x = Tensor(np.array([[0, 1], [2, 3]])).astype(np.float32) >>> k = 1 >>> dims = [0, 1] >>> output = ops.rot90(x, k, dims) >>> print(output) [[1. 3.] [0. 2.]] """ if not isinstance(x, (Tensor, Tensor_)): raise TypeError("the input x must be Tensor!") if not isinstance(k, int): raise TypeError("the input k must be int!") if not isinstance(dims, (list, tuple)): raise TypeError("the input dims must be list or tuple!") total_dims = x.ndim total_rot_dims = len(dims) if total_rot_dims != 2: raise ValueError("total rotation dims must be 2.") if dims[0] == dims[1] or (dims[0] - dims[1]) == total_dims or (dims[1] - dims[0]) == total_dims: raise RuntimeError("rotation dims must be different.") if dims[0] >= total_dims or dims[0] < -total_dims: raise ValueError("Rotation dim0 out of range, dim0 = {}".format(dims[0])) if dims[1] >= total_dims or dims[1] < -total_dims: raise ValueError("Rotation dim1 out of range, dim1 = {}".format(dims[1])) k = (4 + (k % 4)) % 4 if k == 0: out = x return out if k == 2: op1 = P.ReverseV2(axis=[dims[0]]) output = op1(x) op2 = P.ReverseV2(axis=[dims[1]]) out = op2(output) return out axes_list = arrange(total_dims) (axes_list[dims[0]], axes_list[dims[1]]) = (axes_list[dims[1]], axes_list[dims[0]]) if k == 1: op = P.ReverseV2(axis=[dims[1]]) output = op(x) out = output.transpose(axes_list) else: output = x.transpose(axes_list) op = P.ReverseV2(axis=[dims[1]]) out = op(output) return out
[docs]def xdivy(x, y): """ Divides the first input tensor by the second input tensor element-wise. Returns zero when `x` is zero. Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, dtypes of them cannot be bool at the same time, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant. Args: x (Union[Tensor, Number, bool]): The first input is a number, or a bool, or a tensor whose data type is float16, float32, float64, complex64, complex128 or bool. y (Union[Tensor, Number, bool]): The second input is a number, or a bool when the first input is a tensor, or a tensor whose data type is float16, float32, float64, complex64, complex128 or bool. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs. Raises: TypeError: If `x` and `y` is not one of the following: Tensor, Number, bool. TypeError: If dtype of `x` and 'y' is not in [float16, float32, float64, complex64, complex128, bool]. ValueError: If `x` could not be broadcast to a tensor with shape of `y`. RuntimeError: If the data type of `x`, `y` conversion of Parameter is given but data type conversion of Parameter is not supported. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([2, 4, -1]), mindspore.float32) >>> y = Tensor(np.array([2, 2, 2]), mindspore.float32) >>> output = ops.xdivy(x, y) >>> print(output) [ 1. 2. -0.5] """ return xdivy_(x, y)
def log10(x): r""" Returns a new tensor with the logarithm to the base 10 of the elements of input. .. math:: y_i = log_{10}(x_i) .. warning:: If the input value of operator Log10 is within the range (0, 0.01] or [0.95, 1.05], the output accuracy may be affacted. If the input value of operator Log10 is less than or equal to 0, it will not raise Error. .. note:: The dimension of the input Tensor on Ascend should be less than or equal to 8, and the dimension of the input Tensor on the CPU or GPU should be less than 8. Args: x (Tensor): Input Tensor of any dimension. The value must be greater than 0. Returns: Tensor, has the same shape and dtype as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is not float16 or float32 or float64 on CPU and GPU, if dtype of `x` is not float16 or float32 on Ascend. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([2, 4, 10]).astype(np.float16)) >>> output = ops.log10(x) >>> print(output) [0.301 0.602 1. ] """ dtype_op = P.DType() x_dtype = dtype_op(x) denominator = log_(_make_tensor(10, x_dtype)) frac_log = log_(x) output = frac_log / denominator return output
[docs]def log1p(x): r""" Returns the natural logarithm of one plus the input tensor element-wise. .. math:: out_i = {log_e}(x_i + 1) Args: x (Tensor): The input tensor. With float16 or float32 data type. The value must be greater than -1. :math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8. Returns: Tensor, has the same shape as the `x`. Raises: TypeError: If `x` is not a Tensor. TypeError: If dtype of `x` is neither float16 nor float32. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) >>> output = ops.log1p(x) >>> print(output) [0.6931472 1.0986123 1.609438 ] """ _log1p = _get_cache_prim(P.Log1p)() return _log1p(x)
def kron(x, y): """ Computes the Kronecker product, denoted by ⊗, of `x` and `y`. If `x` is a :math:`(a_{0}` x :math:`a_{1}` x ... x :math:`a_{n})` tensor and `y` is a :math:`(b_{0}` x :math:`b_{1}` x ... x :math:`b_{n})` tensor, the result will be a :math:`(a_{0}*b_{0}` x :math:`a_{1}*b_{1}` x ... x :math:`a_{n}*b_{n})` tensor with the following entries: .. math:: (x ⊗ y)_{k_{0},k_{1},...k_{n}} = x_{i_{0},i_{1},...i_{n}} * y_{j_{0},j_{1},...j_{n}}, where :math:`k_{t} = i_{t} * b_{t} + j_{t}` for 0 ≤ `t` ≤ `n`. If one tensor has fewer dimensions than the other it is unsqueezed until it has the same number of dimensions. Note: Supports real-valued and complex-valued inputs. Args: x (Tensor): Input tensor. y (Tensor): Input tensor. Returns: Tensor. Raises: TypeError: If `x` is not a Tensor. TypeError: If `y` is not a Tensor. Supported Platforms: ``Ascend`` ``CPU`` ``GPU`` Examples: >>> import mindspore >>> import numpy as np >>> from mindspore import Tensor, nn >>> from mindspore import ops >>> x = Tensor(np.array([[0, 1, 2], [3, 4, 5]])).astype(np.float32) >>> y = Tensor(np.array([[-1, -2, -3], [-4, -6, -8]])).astype(np.float32) >>> output = ops.kron(x, y) >>> print(output) [[ 0. 0. 0. -1. -2. -3. -2. -4. -6.] [ 0. 0. 0. -4. -6. -8. -8. -12. -16.] [ -3. -6. -9. -4. -8. -12. -5. -10. -15.] [-12. -18. -24. -16. -24. -32. -20. -30. -40.]] """ if not isinstance(x, (Tensor, Tensor_)): raise TypeError("the input x must be Tensor!") if not isinstance(y, (Tensor, Tensor_)): raise TypeError("the input y must be Tensor!") if x is None or y is None: return None if x.ndim == 0 or y.ndim == 0: return x * y if x.ndim >= y.ndim: maxdim = x.ndim else: maxdim = y.ndim pad_x = maxdim - x.ndim pad_y = maxdim - y.ndim x_reshape = [0 for x in range(2 * maxdim)] y_reshape = [0 for x in range(2 * maxdim)] result_shape = [0 for x in range(maxdim)] for i in range(maxdim): if i >= pad_x: x_reshape[2 * i] = x.shape[i - pad_x] else: x_reshape[2 * i] = 1 x_reshape[2 * i + 1] = 1 y_reshape[2 * i] = 1 if i >= pad_y: y_reshape[2 * i + 1] = y.shape[i - pad_y] else: y_reshape[2 * i + 1] = 1 result_shape[i] = x_reshape[2 * i] * y_reshape[2 * i + 1] x = x.reshape(x_reshape) y = y.reshape(y_reshape) result = (x * y).reshape(result_shape) return result def all(x, axis=(), keep_dims=False): r""" Reduces a dimension of a tensor by the "logicalAND" of all elements in the dimension, by default. And also can reduce a dimension of `x` along the axis. Determine whether the dimensions of the output and input are the same by controlling `keep_dims`. Args: x (Tensor[bool]): The input tensor. The dtype of the tensor to be reduced is bool. :math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8. axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Must be in the range [-rank(x), rank(x)). keep_dims (bool): If true, keep these reduced dimensions and the length is 1. If false, don't keep these dimensions. Default : False. Returns: Tensor, the dtype is bool. - If axis is (), and keep_dims is False, the output is a 0-D tensor representing the "logical and" of all elements in the input tensor. - If axis is int, set as 2, and keep_dims is False, the shape of output is :math:`(x_1, x_3, ..., x_R)`. - If axis is tuple(int), set as (2, 3), and keep_dims is False, the shape of output is :math:`(x_1, x_4, ..., x_R)`. Raises: TypeError: If `keep_dims` is not a bool. TypeError: If `x` is not a Tensor. TypeError: If `axis` is not one of the following: int, tuple or list. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([[True, False], [True, True]])) >>> # case 1: Reduces a dimension by the "logicalAND" of all elements in the dimension. >>> output = ops.all(x, keep_dims=True) >>> print(output) [[False]] >>> print(output.shape) (1, 1) >>> # case 2: Reduces a dimension along axis 0. >>> output = ops.all(x, axis=0) >>> print(output) [True False] >>> # case 3: Reduces a dimension along axis 1. >>> output = ops.all(x, axis=1) >>> print(output) [False True] """ return _get_cache_prim(P.ReduceAll)(keep_dims)(x, axis) def any(x, axis=(), keep_dims=False): r""" Reduces a dimension of a tensor by the "logical OR" of all elements in the dimension, by default. And also can reduce a dimension of `x` along the axis. Determine whether the dimensions of the output and input are the same by controlling `keep_dims`. Args: x (Tensor[bool]): The input tensor. The dtype of the tensor to be reduced is bool. :math:`(N,*)` where :math:`*` means, any number of additional dimensions, its rank should be less than 8. axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Default: (), reduce all dimensions. Only constant value is allowed. Must be in the range [-rank(x), rank(x)). keep_dims (bool): If true, keep these reduced dimensions and the length is 1. If false, don't keep these dimensions. Default : False. Returns: Tensor, the dtype is bool. - If axis is (), and keep_dims is False, the output is a 0-D tensor representing the "logical or" of all elements in the input tensor. - If axis is int, set as 2, and keep_dims is False, the shape of output is :math:`(x_1, x_3, ..., x_R)`. - If axis is tuple(int), set as (2, 3), and keep_dims is False, the shape of output is :math:`(x_1, x_4, ..., x_R)`. Raises: TypeError: If `keep_dims` is not a bool. TypeError: If `x` is not a Tensor. TypeError: If `axis` is not one of the following: int, tuple or list. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([[True, False], [True, True]])) >>> # case 1: Reduces a dimension by the "logical OR" of all elements in the dimension. >>> output = ops.any(x, keep_dims=True) >>> print(output) [[ True]] >>> print(output.shape) (1, 1) >>> # case 2: Reduces a dimension along axis 0. >>> output = ops.any(x, axis=0) >>> print(output) [True True] >>> # case 3: Reduces a dimension along axis 1. >>> output = ops.any(x, axis=1) >>> print(output) [True True] """ return _get_cache_prim(P.ReduceAny)(keep_dims)(x, axis) def remainder(x, y): r""" Computes the remainder of dividing the first input tensor by the second input tensor element-wise. Inputs of `x` and `y` comply with the implicit type conversion rules to make the data types consistent. The inputs must be two tensors or one tensor and one scalar. When the inputs are two tensors, both dtypes cannot be bool, and the shapes of them could be broadcast. When the inputs are one tensor and one scalar, the scalar could only be a constant. .. math:: out_{i} = input_{i} \text{ % } other_{i} .. warning:: - The input data does not support 0. - When the elements of input exceed 2048, the accuracy of operator cannot guarantee the requirement of double thousandths in the mini form. - Due to different architectures, the calculation results of this operator on NPU and CPU may be inconsistent. - If shape is expressed as (D1,D2... ,Dn), then D1\*D2... \*DN<=1000000,n<=8. Args: x (Union[Tensor, numbers.Number, bool]): The first input is a number, a bool or a tensor whose data type is number. y (Union[Tensor, numbers.Number, bool]): When the first input is a tensor, The second input could be a number, a bool or a tensor whose data type is number. Returns: Tensor, the shape is the same as the one after broadcasting, and the data type is the one with higher precision or higher digits among the two inputs. Raises: TypeError: If neither `x` nor `y` is one of the following: Tensor, number, bool. ValueError: If the shape `x` and `y` cannot be broadcasted to each other. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([-4.0, 5.0, 6.0]).astype(np.float16)) >>> y = Tensor(np.array([3.0, 2.0, 3.0]).astype(np.float16)) >>> output = ops.remainder(x, y) >>> print(output) [2. 1. 0.] """ out = x - tensor_floordiv(x, y) * y return out def accumulate_n(x): r""" Computes accumulation of all input tensors element-wise. AccumulateNV2 is similar to AddN, but there is a significant difference among them: AccumulateNV2 will not wait for all of its inputs to be ready before summing. That is to say, AccumulateNV2 is able to save memory when inputs are ready at different time since the minimum temporary storage is proportional to the output size rather than the input size. Args: x (Union(tuple[Tensor], list[Tensor])): The input tuple or list is made up of multiple tensors whose dtype is number to be added together. Each element of tuple or list should have the same shape. Returns: Tensor, has the same shape and dtype as each entry of the `x`. Raises: TypeError: If `x` is neither tuple nor list. ValueError: If there is an input element with a different shape. Supported Platforms: ``Ascend`` Examples: >>> x = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> y = Tensor(np.array([4, 5, 6]), mindspore.float32) >>> output = ops.accumulate_n([x, y, x, y]) >>> print(output) [10. 14. 18.] """ accumulate_ = _get_cache_prim(P.AccumulateNV2)() return accumulate_(x)
[docs]def iou(anchor_boxes, gt_boxes, mode='iou'): r""" Calculates intersection over union for boxes. Computes the intersection over union (IOU) or the intersection over foreground (IOF) based on the ground-truth and predicted regions. .. math:: \text{IOU} = \frac{\text{Area of Overlap}}{\text{Area of Union}} \text{IOF} = \frac{\text{Area of Overlap}}{\text{Area of Ground Truth}} .. warning:: In Ascend, only computation of float16 data is supported. To avoid overflow, the input length and width are scaled by 0.2 internally. Args: anchor_boxes (Tensor): Anchor boxes, tensor of shape (N, 4). "N" indicates the number of anchor boxes, and the value "4" refers to "x0", "y0", "x1", and "y1". Data type must be float16 or float32. gt_boxes (Tensor): Ground truth boxes, tensor of shape (M, 4). "M" indicates the number of ground truth boxes, and the value "4" refers to "x0", "y0", "x1", and "y1". Data type must be float16 or float32. mode (string): The mode is used to specify the calculation method, now supporting 'iou' (intersection over union) or 'iof' (intersection over foreground) mode. Default: 'iou'. Returns: Tensor, the 'iou' values, tensor of shape (M, N), with the same data type as `anchor_boxes`. Raises: KeyError: When `mode` is not 'iou' or 'iof'. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> anchor_boxes = Tensor(np.random.randint(1.0, 5.0, [3, 4]), mindspore.float16) >>> gt_boxes = Tensor(np.random.randint(1.0, 5.0, [3, 4]), mindspore.float16) >>> mode = 'iou' >>> output = ops.iou(anchor_boxes, gt_boxes, mode) >>> print(output.shape) (3, 3) """ return _get_cache_prim(P.IOU)(mode)(anchor_boxes, gt_boxes)
__all__ = [ 'addn', 'absolute', 'abs', 'tensor_add', 'add', 'addcdiv', 'addcmul', 'argmin', 'neg_tensor', 'neg', 'tensor_lt', 'less', 'logaddexp2', 'tensor_le', 'le', 'lerp', 'norm', 'tensor_gt', 'logaddexp', 'mv', 'addmv', 'outer', 'gt', 'tensor_ge', 'ge', 'addr', 'tensor_sub', 'sub', 'tensor_mul', 'mul', 'tensor_div', 'div', 'tensor_floordiv', 'floor_div', 'floordiv', 'xdivy', 'tensor_pow', 'pow', 'pows', 'renorm', 'tensor_mod', 'floor_mod', 'floormod', 'tensor_exp', 'exp', 'tensor_expm1', 'expm1', 'equal', 'not_equal', 'ne', 'inplace_update', 'inplace_add', 'inplace_sub', 'isfinite', 'isnan', 'isclose', 'isreal', 'log', 'log_matrix_determinant', 'matrix_determinant', 'linspace', 'matrix_solve', 'std', 'same_type_shape', 'maximum', 'minimum', 'median', 'floor', 'logical_not', 'logical_or', 'logical_and', 'logit', 'logsumexp', 'ldexp', 'sqrt', 'square', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'sinh', 'cosh', 'tanh', 'asinh', 'acosh', 'atanh', 'atan2', 'round', 'bitwise_and', 'bitwise_or', 'bitwise_xor', 'inv', 'invert', 'erf', 'erfc', 'cdist', 'ceil', 'bernoulli', 'bessel_j0', 'bessel_j1', 'bessel_i0', 'bessel_i0e', 'bessel_k0', 'bessel_k0e', 'bessel_y0', 'bessel_y1', 'bessel_i1', 'bessel_i1e', 'bessel_k1', 'bessel_k1e', 'exp2', 'deg2rad', 'stft', 'rad2deg', 'truncate_div', 'truncate_mod', 'trunc', 'gumbel_softmax', 'matmul', 'baddbmm', 'cummin', 'cummax', 'amin', 'amax', 'mean', 'prod', 'all', 'any', 'sparse_segment_mean', 'log2', 'xlogy', 'log10', 'log1p', 'approximate_equal', 'frac', 'kron', 'rot90', 'remainder', 'accumulate_n', 'iou', 'bmm' ] __all__.sort()