tinyms.primitives¶
Primitives module. Operators can be used in the construct function of Layer.
Examples
>>> import tinyms as ts
>>> from tinyms.primitives import tensor_add
>>>
>>> x = ts.ones([2, 3])
>>> y = ts.ones([2, 3])
>>> print(tensor_add(x, y))
[[2. 2. 2.]
[2. 2. 2.]]
-
tinyms.primitives.
add_flags
(fn=None, **flags)[source]¶ A decorator that adds a flag to the function.
Note
Only supports bool value.
- Parameters:
fn (Function) – Function or cell to add flag. Default: None.
flags (dict) – Flags use kwargs. Default: None.
- Returns:
Function, the function with added flags.
Examples
>>> net = Net(); >>> net = add_flags(net, predit=True) >>> print(hasattr(net, '_func_graph_flags')) True
-
class
tinyms.primitives.
Map
(ops=None, reverse=False)[source]¶ Map will apply the set operation on input sequences.
Apply the operations to every element of the sequence.
- Parameters:
ops (Union[MultitypeFuncGraph, None]) – ops is the operation to apply. If ops is None, the operations should be put in the first input of the instance. Default: None
reverse (bool) – The optimizer needs to be inverted in some scenarios to improve parallel performance, general users please ignore. Reverse is the flag to decide if apply the operation reversely. Only supported in graph mode. Default is False.
- Inputs:
args (Tuple[sequence]) - If ops is not None, all the inputs should be the same length sequences, and each row of the sequences. e.g. If the length of args is 2, and for i in length of each sequence (args[0][i], args[1][i]) will be the input of the operation.
If ops is None, the first input is the operation, and the other is inputs.
- Outputs:
Sequence, the sequence of output after applying the function. e.g. operation(args[0][i], args[1][i]).
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> from mindspore import dtype as mstype >>> from mindspore import Tensor, ops >>> from mindspore.ops import MultitypeFuncGraph, Map >>> tensor_list = (Tensor(1, mstype.float32), Tensor(2, mstype.float32), Tensor(3, mstype.float32)) >>> # square all the tensor in the list >>> >>> square = MultitypeFuncGraph('square') >>> @square.register("Tensor") ... def square_tensor(x): ... return ops.square(x) >>> >>> common_map = Map() >>> output = common_map(square, tensor_list) >>> print(output) (Tensor(shape=[], dtype=Float32, value= 1), Tensor(shape=[], dtype=Float32, value= 4), Tensor(shape=[], dtype=Float32, value= 9)) >>> square_map = Map(square, False) >>> output = square_map(tensor_list) >>> print(output) (Tensor(shape=[], dtype=Float32, value= 1), Tensor(shape=[], dtype=Float32, value= 4), Tensor(shape=[], dtype=Float32, value= 9))
-
class
tinyms.primitives.
MultitypeFuncGraph
(name, read_value=False, doc_url='')[source]¶ MultitypeFuncGraph is a class used to generate overloaded functions, considering different types as inputs. Initialize an MultitypeFuncGraph object with name, and use register with input types as the decorator for the function to be registered. And the object can be called with different types of inputs, and work with HyperMap and Map.
- Parameters:
name (str) – Operator name.
read_value (bool, optional) – If the registered function do not need to set value on Parameter, and all inputs will pass by value, set read_value to True. Default: False.
doc_url (str, optional) – The official document link corresponding to the registered function. Default:””.
- Raises:
ValueError – If failed to find a matching function for the given arguments.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> # `add` is a metagraph object which will add two objects according to >>> # input type using ".register" decorator. >>> from mindspore import Tensor >>> from mindspore import ops >>> from mindspore import dtype as mstype >>> import mindspore.ops as ops >>> >>> tensor_add = ops.Add() >>> add = ops.MultitypeFuncGraph('add') >>> @add.register("Number", "Number") ... def add_scala(x, y): ... return x + y >>> @add.register("Tensor", "Tensor") ... def add_tensor(x, y): ... return tensor_add(x, y) >>> output = add(1, 2) >>> print(output) 3 >>> output = add(Tensor([0.1, 0.6, 1.2], dtype=mstype.float32), Tensor([0.1, 0.6, 1.2], dtype=mstype.float32)) >>> print(output) [0.2 1.2 2.4]
-
class
tinyms.primitives.
GradOperation
(get_all=False, get_by_list=False, sens_param=False)[source]¶ A higher-order function which is used to generate the gradient function for the input function.
The gradient function generated by GradOperation higher-order function can be customized by construction arguments.
For example, given an input function net = Net() that takes x and y as inputs, and has a parameter z, see Net in Examples.
Used to get the derivative of the input:
Returns gradients with respect to the first input (see GradNetWrtX in Examples).
Construct a GradOperation higher-order function with default arguments: grad_op = GradOperation().
Call it with input function as argument to get the gradient function: gradient_function = grad_op(net).
Call the gradient function with input function’s inputs to get the gradients with respect to the first input: grad_op(net)(x, y).
Returns gradients with respect to all inputs (see GradNetWrtXY in Examples).
Construct a GradOperation higher-order function with get_all=True which indicates getting gradients with respect to all inputs, they are x and y in example function Net(): grad_op = GradOperation(get_all=True).
Call it with input function as argument to get the gradient function: gradient_function = grad_op(net).
Call the gradient function with input function’s inputs to get the gradients with respect to all inputs: gradient_function(x, y).
Used to get the derivative of the parameters:
Returns gradients with respect to given parameters (see GradNetWithWrtParams in Examples).
Construct a GradOperation higher-order function with get_by_list=True: grad_op = GradOperation(get_by_list=True).
Construct a ParameterTuple that will be passed to the input function when constructing GradOperation higher-order function, it will be used as a parameter filter that determine which gradient to return: params = ParameterTuple(net.trainable_params()).
Call it with input function and params as arguments to get the gradient function: gradient_function = grad_op(net, params).
Call the gradient function with input function’s inputs to get the gradients with respect to given parameters: gradient_function(x, y).
Used to get the derivative of the inputs and parameters at the same time: Returns gradients with respect to all inputs and given parameters in the format of ((dx, dy), (dz)) (see GradNetWrtInputsAndParams in Examples).
Construct a GradOperation higher-order function with get_all=True and get_by_list=True: grad_op = GradOperation(get_all=True, get_by_list=True).
Construct a ParameterTuple that will be passed along input function when constructing GradOperation higher-order function: params = ParameterTuple(net.trainable_params()).
Call it with input function and params as arguments to get the gradient function: gradient_function = grad_op(net, params).
Call the gradient function with input function’s inputs to get the gradients with respect to all inputs and given parameters: gradient_function(x, y).
We can configure the sensitivity(gradient with respect to output) by setting sens_param as True and passing an extra sensitivity input to the gradient function, the sensitivity input should has the same shape and type with input function’s output(see GradNetWrtXYWithSensParam in Examples).
Construct a GradOperation higher-order function with get_all=True and sens_param=True: grad_op = GradOperation(get_all=True, sens_param=True).
Define grad_wrt_output as sens_param which works as the gradient with respect to output: grad_wrt_output = Tensor(np.ones([2, 2]).astype(np.float32)).
Call it with input function as argument to get the gradient function: gradient_function = grad_op(net).
Call the gradient function with input function’s inputs and sens_param to get the gradients with respect to all inputs: gradient_function(x, y, grad_wrt_output).
Note
For above gradient functions, the returned gradient result may vary for grad result element number:
Return a single value if only one result.
Return a tuple for multiple results.
Return an empty tuple for no result.
- Parameters:
get_all (bool) – If True, get all the gradients with respect to inputs. Default: False.
get_by_list (bool) – If True, get all the gradients with respect to Parameter free variables. If get_all and get_by_list are both False, get the gradient with respect to first input. If get_all and get_by_list are both True, get the gradients with respect to inputs and Parameter free variables at the same time in the form of (“gradients with respect to inputs”, “gradients with respect to parameter free variables”). Default: False.
sens_param (bool) – Whether to append sensitivity (gradient with respect to output) as input. If sens_param is False, a ‘ones_like(outputs)’ sensitivity will be attached automatically. Default: False. If the sensor_param is True, a sensitivity (gradient with respect to output) needs to be transferred through the location parameter or key-value pair parameter. If the value is transferred through the key-value pair parameter, the key must be sens.
- Returns:
The higher-order function which takes a function as argument and returns gradient function for it.
- Raises:
TypeError – If get_all, get_by_list or sens_param is not a bool.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.matmul = ops.MatMul() ... self.z = Parameter(Tensor(np.array([1.0], np.float32)), name='z') ... def construct(self, x, y): ... x = x * self.z ... out = self.matmul(x, y) ... return out ... >>> class GradNetWrtX(nn.Cell): ... def __init__(self, net): ... super(GradNetWrtX, self).__init__() ... self.net = net ... self.grad_op = ops.GradOperation() ... def construct(self, x, y): ... gradient_function = self.grad_op(self.net) ... return gradient_function(x, y) ... >>> x = Tensor([[0.5, 0.6, 0.4], [1.2, 1.3, 1.1]], dtype=mstype.float32) >>> y = Tensor([[0.01, 0.3, 1.1], [0.1, 0.2, 1.3], [2.1, 1.2, 3.3]], dtype=mstype.float32) >>> output = GradNetWrtX(Net())(x, y) >>> print(output) [[1.4100001 1.5999999 6.6 ] [1.4100001 1.5999999 6.6 ]] >>> >>> class GradNetWrtXY(nn.Cell): ... def __init__(self, net): ... super(GradNetWrtXY, self).__init__() ... self.net = net ... self.grad_op = ops.GradOperation(get_all=True) ... def construct(self, x, y): ... gradient_function = self.grad_op(self.net) ... return gradient_function(x, y) >>> >>> x = Tensor([[0.8, 0.6, 0.2], [1.8, 1.3, 1.1]], dtype=mstype.float32) >>> y = Tensor([[0.1, 3.3, 1.1], [1.1, 0.2, 1.4], [1.1, 2.2, 0.3]], dtype=mstype.float32) >>> output = GradNetWrtXY(Net())(x, y) >>> print(output) (Tensor(shape=[2, 3], dtype=Float32, value= [[ 4.50000000e+00, 2.70000005e+00, 3.60000014e+00], [ 4.50000000e+00, 2.70000005e+00, 3.60000014e+00]]), Tensor(shape=[3, 3], dtype=Float32, value= [[ 2.59999990e+00, 2.59999990e+00, 2.59999990e+00], [ 1.89999998e+00, 1.89999998e+00, 1.89999998e+00], [ 1.30000007e+00, 1.30000007e+00, 1.30000007e+00]])) >>> >>> class GradNetWrtXYWithSensParam(nn.Cell): ... def __init__(self, net): ... super(GradNetWrtXYWithSensParam, self).__init__() ... self.net = net ... self.grad_op = ops.GradOperation(get_all=True, sens_param=True) ... self.grad_wrt_output = Tensor([[0.1, 0.6, 0.2], [0.8, 1.3, 1.1]], dtype=mstype.float32) ... def construct(self, x, y): ... gradient_function = self.grad_op(self.net) ... return gradient_function(x, y, self.grad_wrt_output) >>> >>> x = Tensor([[0.8, 0.6, 0.2], [1.8, 1.3, 1.1]], dtype=mstype.float32) >>> y = Tensor([[0.11, 3.3, 1.1], [1.1, 0.2, 1.4], [1.1, 2.2, 0.3]], dtype=mstype.float32) >>> output = GradNetWrtXYWithSensParam(Net())(x, y) >>> print(output) (Tensor(shape=[2, 3], dtype=Float32, value= [[ 2.21099997e+00, 5.09999990e-01, 1.49000001e+00], [ 5.58800030e+00, 2.68000007e+00, 4.07000017e+00]]), Tensor(shape=[3, 3], dtype=Float32, value= [[ 1.51999998e+00, 2.81999993e+00, 2.14000010e+00], [ 1.09999990e+00, 2.04999995e+00, 1.54999995e+00], [ 9.00000036e-01, 1.54999995e+00, 1.25000000e+00]])) >>> >>> class GradNetWithWrtParams(nn.Cell): ... def __init__(self, net): ... super(GradNetWithWrtParams, self).__init__() ... self.net = net ... self.params = ParameterTuple(net.trainable_params()) ... self.grad_op = ops.GradOperation(get_by_list=True) ... def construct(self, x, y): ... gradient_function = self.grad_op(self.net, self.params) ... return gradient_function(x, y) >>> >>> x = Tensor([[0.8, 0.6, 0.2], [1.8, 1.3, 1.1]], dtype=mstype.float32) >>> y = Tensor([[0.11, 3.3, 1.1], [1.1, 0.2, 1.4], [1.1, 2.2, 0.3]], dtype=mstype.float32) >>> output = GradNetWithWrtParams(Net())(x, y) >>> print(output) (Tensor(shape=[1], dtype=Float32, value= [ 2.15359993e+01]),) >>> >>> class GradNetWrtInputsAndParams(nn.Cell): ... def __init__(self, net): ... super(GradNetWrtInputsAndParams, self).__init__() ... self.net = net ... self.params = ParameterTuple(net.trainable_params()) ... self.grad_op = ops.GradOperation(get_all=True, get_by_list=True) ... def construct(self, x, y): ... gradient_function = self.grad_op(self.net, self.params) ... return gradient_function(x, y) >>> >>> x = Tensor([[0.1, 0.6, 1.2], [0.5, 1.3, 0.1]], dtype=mstype.float32) >>> y = Tensor([[0.12, 2.3, 1.1], [1.3, 0.2, 2.4], [0.1, 2.2, 0.3]], dtype=mstype.float32) >>> output = GradNetWrtInputsAndParams(Net())(x, y) >>> print(output) ((Tensor(shape=[2, 3], dtype=Float32, value= [[ 3.51999998e+00, 3.90000010e+00, 2.59999990e+00], [ 3.51999998e+00, 3.90000010e+00, 2.59999990e+00]]), Tensor(shape=[3, 3], dtype=Float32, value= [[ 6.00000024e-01, 6.00000024e-01, 6.00000024e-01], [ 1.89999998e+00, 1.89999998e+00, 1.89999998e+00], [ 1.30000007e+00, 1.30000007e+00, 1.30000007e+00]])), (Tensor(shape=[1], dtype=Float32, value= [ 1.29020004e+01]),))
-
class
tinyms.primitives.
HyperMap
(ops=None, reverse=False)[source]¶ Hypermap will apply the set operation to input sequences.
Apply the operations to every element of the sequence or nested sequence. Different from mindspore.ops.Map, the HyperMap supports to apply on nested structure.
- Parameters:
ops (Union[MultitypeFuncGraph, None]) – ops is the operation to apply. If ops is None, the operations should be put in the first input of the instance. Default is None.
reverse (bool) – The optimizer needs to be inverted in some scenarios to improve parallel performance, general users please ignore. reverse is the flag to decide if apply the operation reversely. Only supported in graph mode. Default is False.
- Inputs:
args (Tuple[sequence]) -
If ops is not None, all the inputs should be sequences with the same length. And each row of the sequences will be the inputs of the operation.
If ops is None, the first input is the operation, and the others are inputs.
Note
Except for the operation input, the number of inputs should be equal to the number of inputs to ops.
- Outputs:
Sequence or nested sequence, the sequence of output after applying the function. e.g. operation(args[0][i], args[1][i]).
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> from mindspore import Tensor, ops >>> from mindspore import dtype as mstype >>> nest_tensor_list = ((Tensor(1, mstype.float32), Tensor(2, mstype.float32)), ... (Tensor(3, mstype.float32), Tensor(4, mstype.float32))) >>> # square all the tensor in the nested list >>> >>> square = ops.MultitypeFuncGraph('square') >>> @square.register("Tensor") ... def square_tensor(x): ... return ops.square(x) >>> >>> common_map = ops.HyperMap() >>> output = common_map(square, nest_tensor_list) >>> print(output) ((Tensor(shape=[], dtype=Float32, value= 1), Tensor(shape=[], dtype=Float32, value= 4)), (Tensor(shape=[], dtype=Float32, value= 9), Tensor(shape=[], dtype=Float32, value= 16))) >>> square_map = ops.HyperMap(square, False) >>> output = square_map(nest_tensor_list) >>> print(output) ((Tensor(shape=[], dtype=Float32, value= 1), Tensor(shape=[], dtype=Float32, value= 4)), (Tensor(shape=[], dtype=Float32, value= 9), Tensor(shape=[], dtype=Float32, value= 16)))
-
tinyms.primitives.
zeros_like
(input, *, dtype=None)[source]¶ Creates a tensor filled with 0, with the same size as x, and the given dtype.
If dtype = None, the tensor will have the same dtype as input input.
- Parameters:
input (Tensor) – Tensor of any dimension.
- Keyword Arguments:
dtype (
mindspore.dtype
, optional) – The specified dtype of the output tensor. If dtype is None, the dtype of the input tensor will be used. Default: None.- Returns:
Tensor, filled with 0.
- Raises:
TypeError – If dtype is not a MindSpore dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.arange(4).reshape(2, 2)) >>> output = ops.zeros_like(x, dtype=mindspore.float32) >>> print(output) [[0. 0.] [0. 0.]]
-
tinyms.primitives.
ones_like
(input, *, dtype=None)[source]¶ Returns a Tensor with a value of 1 and its shape is the same as the input.
- Parameters:
input (Tensor) – Tensor of any dimension.
- Keyword Arguments:
dtype (
mindspore.dtype
, optional) – The specified dtype of the output tensor. If dtype is None, the dtype of the input tensor will be used. Default: None.- Returns:
Tensor, has the same shape as input but filled with ones.
- Raises:
TypeError – If input is not a Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32)) >>> output = ops.ones_like(x) >>> print(output) [[1 1] [1 1]]
-
tinyms.primitives.
normal
(shape, mean, stddev, seed=None)[source]¶ Generates random numbers according to the Normal (or Gaussian) random number distribution.
- Parameters:
shape (tuple) – The shape of random tensor to be generated. The format is \((N,*)\) where \(*\) means, any number of additional dimensions.
mean (Union[Tensor, int, float]) – The mean μ distribution parameter, which specifies the location of the peak, with data type in [int8, int16, int32, int64, float16, float32].
stddev (Union[Tensor, int, float]) – The deviation σ distribution parameter. It should be greater than 0, with data type in [int8, int16, int32, int64, float16, float32].
seed (int) – Seed is used as entropy source for the Random number engines to generate pseudo-random numbers. The value must be non-negative. Default: None, which will be treated as 0.
- Returns:
Tensor. The shape should be equal to the broadcasted shape between the input shape and shapes of mean and stddev. The dtype is float32.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> import numpy as np >>> from mindspore import Tensor, ops >>> shape = (3, 1, 2) >>> mean = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32) >>> stddev = Tensor(1.0, mindspore.float32) >>> output = ops.normal(shape, mean, stddev, seed=5) >>> result = output.shape >>> print(result) (3, 2, 2) >>> shape = (3, 1, 3) >>> mean = Tensor(np.array([[3, 4, 3], [3, 5, 6]]), mindspore.float32) >>> stddev = Tensor(1.0, mindspore.float32) >>> output = ops.normal(shape, mean, stddev, seed=5) >>> result = output.shape >>> print(result) (3, 2, 3) >>> shape = (3, 1, 3) >>> mean = Tensor(np.array([[1, 2, 3], [3, 4, 3], [3, 5, 6]]), mindspore.float32) >>> stddev = Tensor(1.0, mindspore.float32) >>> output = ops.normal(shape, mean, stddev, seed=5) >>> result = output.shape >>> print(result) (3, 3, 3)
-
tinyms.primitives.
laplace
(shape, mean, lambda_param, seed=None)[source]¶ Generates random numbers according to the Laplace random number distribution. It is defined as:
\[\text{f}(x;μ,λ) = \frac{1}{2λ}\exp(-\frac{|x-μ|}{λ}),\]- Parameters:
shape (tuple) – The shape of random tensor to be generated. The format is \((N,*)\) where \(*\) means, any number of additional dimensions.
mean (Tensor) – The mean μ distribution parameter, which specifies the location of the peak. With float32 data type.
lambda_param (Tensor) – The parameter used for controlling the variance of this random distribution. The variance of Laplace distribution is equal to twice the square of lambda_param. With float32 data type.
seed (int, optional) – Seed is used as entropy source for Random number engines generating pseudo-random numbers. Default: None, which will be treated as 0.
- Returns:
Tensor. The shape should be the broadcasted shape of input shape and shapes of mean and lambda_param. The dtype is float32.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> from mindspore import Tensor >>> from mindspore import ops as ops >>> shape = (2, 3) >>> mean = Tensor(1.0, mindspore.float32) >>> lambda_param = Tensor(1.0, mindspore.float32) >>> output = ops.laplace(shape, mean, lambda_param, seed=5) >>> print(output.shape) (2, 3)
-
tinyms.primitives.
uniform
(shape, minval, maxval, seed=None, dtype=mindspore.float32)[source]¶ Generates random numbers according to the Uniform random number distribution.
Note
The number in tensor minval should be strictly less than maxval at any position after broadcasting.
- Parameters:
shape (Union[tuple, Tensor]) – The shape of random tensor to be generated.
minval (Tensor) – The distribution parameter a. It defines the minimum possible generated value, with int32 or float32 data type. If dtype is int32, only one number is allowed.
maxval (Tensor) – The distribution parameter b. It defines the maximum possible generated value, with int32 or float32 data type. If dtype is int32, only one number is allowed.
seed (int) – Seed is used as entropy source for the random number engines to generate pseudo-random numbers, must be non-negative. Default: None, which will be treated as 0.
dtype (mindspore.dtype) – Type of the Uniform distribution. If it is int32, it generates numbers from discrete uniform distribution; if it is float32, it generates numbers from continuous uniform distribution. It only supports these two data types. Default: mindspore.float32.
- Returns:
Tensor. The shape should be equal to the broadcasted shape between the input shape and shapes of minval and maxval. The dtype is designated as the input dtype.
- Raises:
- Supported Platforms:
GPU
CPU
Examples
>>> from mindspore import Tensor, ops >>> import mindspore >>> import numpy as np >>> # For discrete uniform distribution, only one number is allowed for both minval and maxval: >>> shape = (4, 2) >>> minval = Tensor(1, mindspore.int32) >>> maxval = Tensor(2, mindspore.int32) >>> output = ops.uniform(shape, minval, maxval, seed=5, dtype=mindspore.int32) >>> >>> # For continuous uniform distribution, minval and maxval can be multi-dimentional: >>> shape = (3, 1, 2) >>> minval = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32) >>> maxval = Tensor([8.0, 10.0], mindspore.float32) >>> output = ops.uniform(shape, minval, maxval, seed=5) >>> result = output.shape >>> print(result) (3, 2, 2)
-
tinyms.primitives.
gamma
(shape, alpha, beta, seed=None)[source]¶ Generates random numbers according to the Gamma random number distribution.
- Parameters:
shape (tuple) – The shape of random tensor to be generated.
alpha (Tensor) – The \(\alpha\) distribution parameter. It should be greater than 0 with float32 data type.
beta (Tensor) – The \(\beta\) distribution parameter. It should be greater than 0 with float32 data type.
seed (int) – Seed is used as entropy source for the random number engines to generate pseudo-random numbers, must be non-negative. Default: None, which will be treated as 0.
- Returns:
Tensor. The shape should be equal to the broadcasted shape between the input shape and shapes of alpha and beta. The dtype is float32.
- Raises:
- Supported Platforms:
Ascend
Examples
>>> import mindspore >>> import numpy as np >>> from mindspore import Tensor, ops >>> # case 1: alpha_shape is (2, 2) >>> shape = (3, 1, 2) >>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32) >>> beta = Tensor(np.array([1.0]), mindspore.float32) >>> output = ops.gamma(shape, alpha, beta, seed=5) >>> result = output.shape >>> print(result) (3, 2, 2) >>> # case 2: alpha_shape is (2, 3), so shape is (3, 1, 3) >>> shape = (3, 1, 3) >>> alpha = Tensor(np.array([[1, 3, 4], [2, 5, 6]]), mindspore.float32) >>> beta = Tensor(np.array([1.0]), mindspore.float32) >>> output = ops.gamma(shape, alpha, beta, seed=5) >>> result = output.shape >>> print(result) (3, 2, 3) >>> # case 3: beta_shape is (1, 2), the output is different. >>> shape = (3, 1, 2) >>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32) >>> beta = Tensor(np.array([1.0, 2]), mindspore.float32) >>> output = ops.gamma(shape, alpha, beta, seed=5) >>> result = output.shape >>> print(output) [[[ 2.2132034 5.8855834]] [ 3.3981476 7.5805717] [[ 3.3981476 7.5805717]] [ 3.7190282 19.941492] [[ 2.9512358 2.5969937]] [ 3.786061 5.160872 ]]] >>> # case 4: beta_shape is (2, 1), the output is different. >>> shape = (3, 1, 2) >>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mindspore.float32) >>> beta = Tensor(np.array([[1.0], [2.0]]), mindspore.float32) >>> output = ops.gamma(shape, alpha, beta, seed=5) >>> result = output.shape >>> print(output) [[[ 5.6085486 7.8280783]] [ 15.97684 16.116285] [[ 1.8347423 1.713663]] [ 3.2434065 15.667398] [[ 4.2922077 7.3365674]] [ 5.3876944 13.159832 ]]]
-
tinyms.primitives.
poisson
(shape, mean, seed=None)[source]¶ The ops.poisson is deprecated, please use
mindspore.ops.random_poisson
Generates random numbers according to the Poisson random number distribution.\[\text{P}(i|μ) = \frac{\exp(-μ)μ^{i}}{i!}\]- Parameters:
shape (tuple) – The shape of random tensor to be generated. The format is \((N,*)\) where \(*\) means, any number of additional dimensions.
mean (Tensor) – The mean μ distribution parameter. It should be greater than 0 with float32 data type.
seed (int) – Seed is used as entropy source for the random number engines to generate pseudo-random numbers and must be non-negative. Default: None, which will be treated as 0.
- Returns:
Tensor. The shape should be equal to the broadcasted shape between the input “shape” and shapes of mean. The dtype is float32.
- Raises:
- Supported Platforms:
deprecated
Examples
>>> from mindspore import Tensor, ops >>> import mindspore >>> # case 1: It can be broadcast. >>> shape = (4, 1) >>> mean = Tensor(np.array([5.0, 10.0]), mindspore.float32) >>> output = ops.poisson(shape, mean, seed=5) >>> result = output.shape >>> print(result) (4, 2) >>> # case 2: It can not be broadcast. It is recommended to use the same shape. >>> shape = (2, 2) >>> mean = Tensor(np.array([[5.0, 10.0], [5.0, 1.0]]), mindspore.float32) >>> output = ops.poisson(shape, mean, seed=5) >>> result = output.shape >>> print(result) (2, 2)
-
tinyms.primitives.
multinomial
(input, num_samples, replacement=True, seed=None)[source]¶ Returns a tensor sampled from the multinomial probability distribution located in the corresponding row of the input tensor.
Note
The rows of input do not need to sum to one (in which case we use the values as weights), but must be non-negative, finite and have a non-zero sum.
- Parameters:
input (Tensor) – The input tensor containing probabilities, must be 1 or 2 dimensions, with float32 data type.
num_samples (int) – Number of samples to draw.
replacement (bool, optional) – Whether to draw with replacement or not, default: True.
seed (int, optional) – Seed is used as entropy source for the random number engines to generate pseudo-random numbers, must be non-negative. Default: None.
- Returns:
Tensor, has the same rows with input. The number of sampled indices of each row is num_samples. The dtype is float32.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> from mindspore import Tensor, ops >>> from mindspore import dtype as mstype >>> # case 1: The output is random, and the length of the output is the same as num_sample. >>> input = Tensor([0, 9, 4, 0], mindspore.float32) >>> output = ops.multinomial(input, 2) >>> # print(output) >>> # [1 2] or [2 1] >>> # the case where the result is [2 1] in multiple times. >>> # This is because the value corresponding to the index 1 is larger than the value of the index 2. >>> print(len(output)) 2 >>> # case 2: The output is random, and the length of the output is the same as num_sample. >>> # replacement is False(Default). >>> # If the extracted value is 0, the index value of 1 will be returned. >>> input = Tensor([0, 9, 4, 0], mstype.float32) >>> output = ops.multinomial(input, 4) >>> print(output) [1 1 2 1] >>> # case 3: The output is random, num_sample == x_length = 4, and replacement is True, >>> # Can extract the same elements。 >>> input = Tensor([0, 9, 4, 0], mstype.float32) >>> output = ops.multinomial(input, 4, True) >>> print(output) [1 1 2 2]
-
tinyms.primitives.
count_nonzero
(x, axis=(), keep_dims=False, dtype=mindspore.int32)[source]¶ Count number of nonzero elements across axis of input tensor.
- Parameters:
x (Tensor) – Input data is used to count non-zero numbers. With shape \((N,*)\) where \(*\) means, any number of additional dimensions.
axis (Union[int, tuple(int), list(int)], optional) – The dimensions to reduce. Default: (), reduce all dimensions.
keep_dims (bool, optional) – Whether to maintain dimensions specified by axis. If true, keep these reduced dimensions and the length is 1. If false, don’t keep these dimensions. Default: False.
dtype (Union[Number, mindspore.bool_], optional) – The data type of the output tensor. Default: mindspore.int32.
- Returns:
Tensor, number of nonzero element across axis specified by axis. The data type is specified by dtype.
- Raises:
TypeError – If axis is not int, tuple or list.
ValueError – If any value in axis is not in range [-x.ndim, x.ndim).
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> from mindspore import Tensor, ops >>> import numpy as np >>> # case 1: each value specified. >>> x = Tensor(np.array([[0, 1, 0], [1, 1, 0]]).astype(np.float32)) >>> nonzero_num = ops.count_nonzero(x=x, axis=[0, 1], keep_dims=True, dtype=mindspore.int32) >>> print(nonzero_num) [[3]] >>> # case 2: all value is default. >>> nonzero_num = ops.count_nonzero(x=x) >>> print(nonzero_num) 3 >>> # case 3: axis value was specified 0. >>> nonzero_num = ops.count_nonzero(x=x, axis=[0,]) >>> print(nonzero_num) [1 2 0] >>> # case 4: axis value was specified 1. >>> nonzero_num = ops.count_nonzero(x=x, axis=[1,]) >>> print(nonzero_num) [1 2] >>> # case 5: keep_dims value was specified. >>> nonzero_num = ops.count_nonzero(x=x, keep_dims=True) >>> print(nonzero_num) [[3]] >>> # case 6: keep_dims and axis value was specified. >>> nonzero_num = ops.count_nonzero(x=x, axis=[0,], keep_dims=True) >>> print(nonzero_num) [[1 2 0]]
-
tinyms.primitives.
cummin
(input, axis)[source]¶ Returns a tuple (values,indices) where ‘values’ is the cumulative minimum value of input Tensor input along the dimension axis, and indices is the index location of each minimum value.
\[\begin{split}\begin{array}{ll} \\ y_{i} = min(x_{1}, x_{2}, ... , x_{i}) \end{array}\end{split}\]- Parameters:
- 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 input.
- Raises:
TypeError – If input is not a Tensor.
TypeError – If axis is not an int.
ValueError – If axis is out the range of [-input.ndim, input.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]
-
tinyms.primitives.
tensor_dot
(x1, x2, axes)[source]¶ Computation of Tensor contraction on arbitrary axes between tensors a and b.
Contraction allows for the summation of products of elements of a and b on specified axes. The same number of axes must be specified for both x1 and x2, and values must be within range of number of dims of both a and b.
Selected dims in both inputs must also match.
axes = 0 leads to outer product. axes = 1 leads to normal matrix multiplication when inputs both 2D. axes = 1 is the same as axes = ((1,),(0,)) where both a and b are 2D. axes = 2 is the same as axes = ((1,2),(0,1)) where both a and b are 3D.
- Parameters:
x1 (Tensor) – First tensor in tensor_dot with datatype float16 or float32
x2 (Tensor) – Second tensor in tensor_dot with datatype float16 or float32
axes (Union[int, tuple(int), tuple(tuple(int)), list(list(int))]) – Single value or tuple/list of length 2 with dimensions specified for a and b each. If single value N passed, automatically picks up last N dims from a input shape and first N dims from b input shape in order as axes for each respectively.
- Returns:
Tensor, the shape of the output tensor is \((N + M)\). Where \(N\) and \(M\) are the free axes not contracted in both inputs
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> from mindspore import Tensor, ops >>> import mindspore >>> import numpy as np >>> input_x1 = Tensor(np.ones(shape=[1, 2, 3]), mindspore.float32) >>> input_x2 = Tensor(np.ones(shape=[3, 1, 2]), mindspore.float32) >>> output = ops.tensor_dot(input_x1, input_x2, ((0,1),(1,2))) >>> print(output) [[2. 2. 2] [2. 2. 2] [2. 2. 2]]
-
tinyms.primitives.
dot
(input, other)[source]¶ Computation a dot product between samples in two tensors.
- Parameters:
- Returns:
Tensor, dot product of input and other.
- Raises:
TypeError – If type of input and other are not the same.
TypeError – If dtype of input or other is not float16 or float32.
ValueError – If rank of input or other less than 2.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> import mindspore >>> from mindspore import Tensor, ops >>> input = Tensor(np.ones(shape=[2, 3]), mindspore.float32) >>> other = Tensor(np.ones(shape=[1, 3, 2]), mindspore.float32) >>> output = ops.dot(input, other) >>> print(output) [[[3. 3.]] [[3. 3.]]] >>> print(output.shape) (2, 1, 2) >>> input = Tensor(np.ones(shape=[1, 2, 3]), mindspore.float32) >>> other = Tensor(np.ones(shape=[1, 3, 2]), mindspore.float32) >>> output = ops.dot(input, other) >>> print(output) [[[[3. 3.]] [[3. 3.]]]] >>> print(output.shape) (1, 2, 1, 2) >>> input = Tensor(np.ones(shape=[1, 2, 3]), mindspore.float32) >>> other = Tensor(np.ones(shape=[2, 3, 2]), mindspore.float32) >>> output = ops.dot(input, other) >>> print(output) [[[[3. 3.] [3. 3.]] [[3. 3.] [3. 3.]]]] >>> print(output.shape) (1, 2, 2, 2) >>> input = Tensor(np.ones(shape=[3, 2, 3]), mindspore.float32) >>> other = Tensor(np.ones(shape=[2, 1, 3, 2]), mindspore.float32) >>> output = ops.dot(input, other) >>> 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.]]]]] >>> print(output.shape) (3, 2, 2, 1, 2)
-
tinyms.primitives.
batch_dot
(x1, x2, axes=None)[source]¶ Computation of batch dot product between samples in two tensors containing batch dims.
\[output = x1[batch, :] * x2[batch, :]\]- Parameters:
x1 (Tensor) – First tensor in Batch Dot op with datatype float32 and the rank of x1 must be greater than or equal to 2.
x2 (Tensor) – Second tensor in Batch Dot op with datatype float32. The datatype of x2 should be same as x1 and the rank of x2 must be greater than or equal to 2.
axes (Union[int, tuple(int), list(int)]) – Single value or tuple/list of length 2 with dimensions specified for a and b each. If single value N passed, automatically picks up last N dims from a input shape and last N dimensions from b input shape in order as axes for each respectively. Default: None.
- Returns:
Tensor, batch dot product of x1 and x2. For example, the Shape of output for input x1 shapes (batch, d1, axes, d2) and x2 shapes (batch, d3, axes, d4) is (batch, d1, d2, d3, d4), where d1 and d2 means any number.
- Raises:
TypeError – If type of x1 and x2 are not the same.
TypeError – If dtype of x1 or x2 is not float32.
ValueError – If rank of x1 or x2 less than 2.
ValueError – If batch dim used in axes.
ValueError – If len(axes) less than 2.
ValueError – If axes is not one of those: None, int, (int, int).
ValueError – If axes reversed from negative int is too low for dimensions of input arrays.
ValueError – If axes value is too high for dimensions of input arrays.
ValueError – If batch size of x1 and x2 are not the same.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> from mindspore import Tensor, ops >>> import numpy as np >>> x1 = Tensor(np.ones(shape=[2, 2, 3]), mindspore.float32) >>> x2 = Tensor(np.ones(shape=[2, 3, 2]), mindspore.float32) >>> axes = (-1, -2) >>> output = ops.batch_dot(x1, x2, axes) >>> print(output) [[[3. 3.] [3. 3.]] [[3. 3.] [3. 3.]]] >>> x1 = Tensor(np.ones(shape=[2, 2]), mindspore.float32) >>> x2 = Tensor(np.ones(shape=[2, 3, 2]), mindspore.float32) >>> axes = (1, 2) >>> output = ops.batch_dot(x1, x2, axes) >>> print(output) [[2. 2. 2.] [2. 2. 2.]] >>> print(output.shape) (2, 3) >>> x1 = Tensor(np.ones(shape=[6, 2, 3, 4]), mindspore.float32) >>> x2 = Tensor(np.ones(shape=[6, 5, 4, 8]), mindspore.float32) >>> output = ops.batch_dot(x1, x2) >>> print(output.shape) (6, 2, 3, 5, 8) >>> x1 = Tensor(np.ones(shape=[2, 2, 4]), mindspore.float32) >>> x2 = Tensor(np.ones(shape=[2, 5, 4, 5]), mindspore.float32) >>> output = ops.batch_dot(x1, x2) >>> print(output.shape) (2, 2, 5, 5)
-
tinyms.primitives.
repeat_elements
(x, rep, axis=0)[source]¶ Repeat elements of a tensor along an axis, like np.repeat .
- Parameters:
- Returns:
One tensor with values repeated along the specified axis. If x has shape \((s1, s2, ..., sn)\) and axis is i, the output will have shape \((s1, s2, ..., si * rep, ..., sn)\). The output type will be the same as the type of x.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> # case 1 : repeat on axis 0 >>> x = Tensor(np.array([[0, 1, 2], [3, 4, 5]]), mindspore.int32) >>> output = ops.repeat_elements(x, rep = 2, axis = 0) >>> print(output) [[0 1 2] [0 1 2] [3 4 5] [3 4 5]] >>> # case 2 : repeat on axis 1 >>> x = Tensor(np.array([[0, 1, 2], [3, 4, 5]]), mindspore.int32) >>> output = ops.repeat_elements(x, rep = 2, axis = 1) >>> print(output) [[0 0 1 1 2 2] [3 3 4 4 5 5]]
-
tinyms.primitives.
repeat_interleave
(input, repeats, axis=None)[source]¶ Repeat elements of a tensor along an axis, like numpy.repeat.
- Parameters:
input (Tensor) – The tensor to repeat values for. Must be of type: float16, float32, int8, uint8, int16, int32, or int64.
repeats (int) – The number of times to repeat, must be positive.
axis (int, optional) – The axis along which to repeat, default: None. if dims is None, the input Tensor will be flattened and the output will alse be flattened.
- Returns:
One tensor with values repeated along the specified axis. If input has shape \((s1, s2, ..., sn)\) and axis is i, the output will have shape \((s1, s2, ..., si * repeats, ..., sn)\). The output type will be the same as the type of input.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input = Tensor(np.array([[0, 1, 2], [3, 4, 5]]), mindspore.int32) >>> output = ops.repeat_interleave(input, repeats=2, axis=0) >>> print(output) [[0 1 2] [0 1 2] [3 4 5] [3 4 5]]
-
tinyms.primitives.
sequence_mask
(lengths, maxlen=None)[source]¶ Returns a mask tensor representing the first N positions of each cell.
If lengths has shape \((d_1, d_2, ..., d_n)\), then the resulting tensor mask has type and shape \((d_1, d_2, ..., d_n, maxlen)\), with mask \([i_1, i_2, ..., i_n, j] = (j < lengths[i_1, i_2, ..., i_n])\).
- Parameters:
lengths (Tensor) – Tensor to calculate the mask for. All values in this tensor should be less than or equal to maxlen. Values greater than maxlen will be treated as maxlen.
maxlen (int) – size of the last dimension of returned tensor. Must be positive and same type as elements in lengths. Default is None.
- Returns:
One mask tensor of shape lengths.shape + (maxlen,) .
- Raises:
- Supported Platforms:
GPU
CPU
Examples
>>> # case 1: When maxlen is assigned >>> x = Tensor(np.array([1, 2, 3, 4])) >>> output = ops.sequence_mask(x, 5) >>> print(output) [[ True False False False False] [ True True False False False] [ True True True False False] [ True True True True False]] >>> # case 2: When there is 0 in x >>> x = Tensor(np.array([[1, 3], [2, 0]])) >>> output = ops.sequence_mask(x, 5) >>> print(output) [[[ True False False False False] [ True True True False False]] [[ True True False False False] [False False False False False]]] >>> # case 3: when the maxlen is not assigned >>> x = Tensor(np.array([[1, 3], [2, 4]])) >>> output = ops.sequence_mask(x) >>> print(output) [[[ True False False False ] [ True True True False ]] [[ True True False False ] [ True True True True ]]]
-
tinyms.primitives.
matmul
(input, other)[source]¶ 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.
- Parameters:
input (Tensor) – Input tensor, scalar not allowed. The last dimension of input must be the same size as the second last dimension of other. And the shape of input and other could be broadcast.
other (Tensor) – Input tensor, scalar not allowed. The last dimension of input must be the same size as the second last dimension of other. And the shape of input and other could be broadcast.
- Returns:
Tensor or scalar, the matrix product of the inputs. This is a scalar only when both input, other are 1-d vectors.
- Raises:
ValueError – If the last dimension of input is not the same size as the second-to-last dimension of other, or if a scalar value is passed in.
ValueError – If the shape of input and other could not broadcast together.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> # case 1 : Reasonable application of broadcast mechanism >>> input = Tensor(np.arange(2*3*4).reshape(2, 3, 4), mindspore.float32) >>> other = Tensor(np.arange(4*5).reshape(4, 5), mindspore.float32) >>> output = ops.matmul(input, other) >>> 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 `input` is 1 >>> input = Tensor(np.ones([1, 2]), mindspore.float32) >>> other = Tensor(np.ones([2,]), mindspore.float32) >>> output = ops.matmul(input, other) >>> print(output) [2.] >>> print(output.shape) (1,)
-
tinyms.primitives.
mm
(input, mat2)[source]¶ Returns the matrix product of two arrays. If input is a \((n \times m)\) Tensor, mat2 is a \((m \times p)\) Tensor, out will be a \((n \times p)\) Tensor.
Note
This function cannot support broadcasting. Refer to
mindspore.ops.matmul()
instead if you need a broadcastable function.- Parameters:
- Returns:
Tensor or scalar, the matrix product of the inputs.
- Raises:
ValueError – If the last dimension of input is not the same size as the second-to-last dimension of mat2.
ValueError – If input or mat2 is not a matrix.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> import mindspore.ops as ops >>> import numpy as np >>> x1 = ms.Tensor(np.random.rand(2, 3)) >>> x2 = ms.Tensor(np.random.rand(3, 4)) >>> out = ops.mm(x1, x2) >>> print(out.shape) (2, 4)
-
class
tinyms.primitives.
ACos
[source]¶ Computes arccosine of input tensors element-wise.
Refer to
mindspore.ops.acos()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> acos = ops.ACos() >>> x = Tensor(np.array([0.74, 0.04, 0.30, 0.56]), mindspore.float32) >>> output = acos(x) >>> print(output) [0.737726 1.5307857 1.2661036 0.9764105]
-
class
tinyms.primitives.
Abs
[source]¶ Returns absolute value of a tensor element-wise.
Refer to
mindspore.ops.abs()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([-1.0, 1.0, 0.0]), mindspore.float32) >>> abs = ops.Abs() >>> output = abs(x) >>> print(output) [1. 1. 0.]
-
class
tinyms.primitives.
AccumulateNV2
[source]¶ Computes accumulation of all input tensors element-wise.
Refer to
mindspore.ops.accumulate_n()
for more details.- Supported Platforms:
Ascend
GPU
Examples
>>> class NetAccumulateNV2(nn.Cell): ... def __init__(self): ... super(NetAccumulateNV2, self).__init__() ... self.accumulateNV2 = ops.AccumulateNV2() ... ... def construct(self, *z): ... return self.accumulateNV2(z) ... >>> net = NetAccumulateNV2() >>> x = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> y = Tensor(np.array([4, 5, 6]), mindspore.float32) >>> output = net(x, y, x, y) >>> print(output) [10. 14. 18.]
-
class
tinyms.primitives.
Acosh
[source]¶ Computes inverse hyperbolic cosine of the inputs element-wise.
Refer to
mindspore.ops.acosh()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> import mindspore.ops as ops >>> from mindspore import Tensor, dtype >>> acosh = ops.Acosh() >>> x = Tensor(np.array([1.0, 1.5, 3.0, 100.0]), dtype.float32) >>> output = acosh(x) >>> print(output) [0. 0.9624237 1.7627472 5.298292 ]
-
class
tinyms.primitives.
Adam
(use_locking=False, use_nesterov=False)[source]¶ Updates gradients by the Adaptive Moment Estimation (Adam) algorithm.
The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.
For more details, please refer to
mindspore.nn.Adam
.The updating formulas are as follows,
\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ l = \alpha * \frac{\sqrt{1-\beta_2^t}}{1-\beta_1^t} \\ w = w - l * \frac{m}{\sqrt{v} + \epsilon} \end{array}\end{split}\]\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(l\) represents scaling factor lr, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(beta_1^t(\beta_1^{t})\) and \(beta_2^t(\beta_2^{t})\) represent beta1_power and beta2_power, \(\alpha\) represents learning_rate, \(w\) represents var, \(\epsilon\) represents epsilon.
- Parameters:
use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.
use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. If true, update the gradients using NAG. If false, update the gradients without using NAG. Default: False.
- Inputs:
var (Parameter) - Weights to be updated. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions. The data type can be float16 or float32.
m (Parameter) - The 1st moment vector in the updating formula, the shape should be the same as var.
v (Parameter) - the 2nd moment vector in the updating formula, the shape should be the same as var.
beta1_power (float) - \(beta_1^t(\beta_1^{t})\) in the updating formula.
beta2_power (float) - \(beta_2^t(\beta_2^{t})\) in the updating formula.
lr (float) - \(l\) in the updating formula. The paper suggested value is \(10^{-8}\).
beta1 (float) - The exponential decay rate for the 1st moment estimations. The paper suggested value is \(0.9\).
beta2 (float) - The exponential decay rate for the 2nd moment estimations. The paper suggested value is \(0.999\).
epsilon (float) - Term added to the denominator to improve numerical stability.
gradient (Tensor) - Gradient, has the same shape and data type as var.
- Outputs:
Tuple of 3 Tensor, the updated parameters.
var (Tensor) - The same shape and data type as Inputs var.
m (Tensor) - The same shape and data type as Inputs m.
v (Tensor) - The same shape and data type as Inputs v.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_adam = ops.Adam() ... self.var = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="var") ... self.m = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="m") ... self.v = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="v") ... def construct(self, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad): ... out = self.apply_adam(self.var, self.m, self.v, beta1_power, beta2_power, lr, beta1, beta2, ... epsilon, grad) ... return out ... >>> net = Net() >>> gradient = Tensor(np.ones([2, 2]).astype(np.float32)) >>> output = net(0.9, 0.999, 0.001, 0.9, 0.999, 1e-8, gradient) >>> print(net.var.asnumpy()) [[0.9996838 0.9996838] [0.9996838 0.9996838]]
-
class
tinyms.primitives.
AdamNoUpdateParam
(use_locking=False, use_nesterov=False)[source]¶ Updates gradients by the Adaptive Moment Estimation (Adam) algorithm. This operator do not update the parameter, but calculate the value that should be added to the parameter instead.
The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.
The updating formulas are as follows,
\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ l = \alpha * \frac{\sqrt{1-\beta_2^t}}{1-\beta_1^t} \\ \Delta{w} = - l * \frac{m}{\sqrt{v} + \epsilon} \end{array}\end{split}\]\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(l\) represents scaling factor lr, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(beta_1^t(\beta_1^{t})\) and \(beta_2^t(\beta_2^{t})\) represent beta1_power and beta2_power, \(\alpha\) represents learning_rate, \(w\) represents the parameter to be updated, \(\epsilon\) represents epsilon.
- Parameters:
use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.
use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. If true, update the gradients using NAG. If false, update the gradients without using NAG. Default: False.
- Inputs:
m (Tensor) - The 1st moment vector in the updating formula. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions. The data type must be float32.
v (Tensor) - the 2nd moment vector in the updating formula. The shape must be the same as m. The data type must be float32.
beta1_power (Tensor) - \(beta_1^t(\beta_1^{t})\) in the updating formula. The shape is \((1, )\) and the data type must be float32.
beta2_power (Tensor) - \(beta_2^t(\beta_2^{t})\) in the updating formula. The shape is \((1, )\) and the data type must be float32.
lr (Tensor) - \(l\) in the updating formula. The shape is \((1, )\) and the data type must be float32.
beta1 (Tensor) - The exponential decay rate for the 1st moment estimations. The shape is \((1, )\) and the data type must be float32.
beta2 (Tensor) - The exponential decay rate for the 2nd moment estimations. The shape is \((1, )\) and the data type must be float32.
epsilon (Tensor) - Term added to the denominator to improve numerical stability. The shape is \((1, )\) and the data type must be float32.
gradient (Tensor) - Gradient, the shape must be the same as m, the data type must be float32.
- Outputs:
Tensor, whose shape and data type are the same with Inputs gradient, is a value that should be added to the parameter to be updated.
- Raises:
- Supported Platforms:
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.adam = ops.AdamNoUpdateParam() ... self.m = Parameter(Tensor(np.array([[0.1, 0.1, 0.1], [0.2, 0.2, 0.2]]).astype(np.float32)), ... name="m") ... self.v = Parameter(Tensor(np.array([[0.1, 0.1, 0.1], [0.2, 0.2, 0.2]]).astype(np.float32)), ... name="v") ... def construct(self, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad): ... out = self.adam(self.m, self.v, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad) ... return out >>> net = Net() >>> beta1_power = Tensor(0.9, ms.float32) >>> beta2_power = Tensor(0.999, ms.float32) >>> lr = Tensor(0.001, ms.float32) >>> beta1 = Tensor(0.9, ms.float32) >>> beta2 = Tensor(0.999, ms.float32) >>> epsilon = Tensor(1e-8, ms.float32) >>> gradient = Tensor(np.array([[0.1, 0.1, 0.1], [0.1, 0.1, 0.1]]).astype(np.float32)) >>> result = net(beta1_power, beta2_power, lr, beta1, beta2, epsilon, gradient) >>> print(result) [[-0.00010004 -0.00010004 -0.00010004] [-0.00013441 -0.00013441 -0.00013441]]
-
class
tinyms.primitives.
AdamWeightDecay
(use_locking=False)[source]¶ Updates gradients by the Adaptive Moment Estimation algorithm with weight decay (AdamWeightDecay).
The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization. The AdamWeightDecay variant was proposed in Decoupled Weight Decay Regularization.
The updating formulas are as follows,
\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ update = \frac{m}{\sqrt{v} + \epsilon} \\ update = \begin{cases} update + weight\_decay * w & \text{ if } weight\_decay > 0 \\ update & \text{ otherwise } \end{cases} \\ w = w - lr * update \end{array}\end{split}\]\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(\beta_1, \beta_2\) represent beta1 and beta2, \(lr\) represents learning_rate, \(w\) represents var, \(decay\) represents weight_decay, \(\epsilon\) represents epsilon.
- Parameters:
use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.
- Inputs:
var (Parameter) - Weights to be updated. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions. The data type can be float16 or float32.
m (Parameter) - The 1st moment vector in the updating formula, it should have the the shape as var. The data type can be float16 or float32.
v (Parameter) - The 2nd moment vector in the updating formula, it should have the same shape and dtype as m.
lr (float) - \(lr\) in the updating formula. The paper suggested value is \(10^{-8}\), the data type should be float32.
beta1 (float) - The exponential decay rate for the 1st moment estimations, the data type should be float32. The paper suggested value is \(0.9\)
beta2 (float) - The exponential decay rate for the 2nd moment estimations, the data type should be float32. The paper suggested value is \(0.999\)
epsilon (float) - Term added to the denominator to improve numerical stability, the data type should be float32.
decay (float) - The weight decay value, must be a scalar tensor with float32 data type. Default: 0.0.
gradient (Tensor) - Gradient, has the same shape and data type as var.
- Outputs:
Tuple of 3 Tensor, the updated parameters.
var (Tensor) - The same shape and data type as var.
m (Tensor) - The same shape and data type as m.
v (Tensor) - The same shape and data type as v.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> import mindspore.nn as nn >>> from mindspore import Tensor, Parameter, ops >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.adam_weight_decay = ops.AdamWeightDecay() ... self.var = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="var") ... self.m = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="m") ... self.v = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="v") ... def construct(self, lr, beta1, beta2, epsilon, decay, grad): ... out = self.adam_weight_decay(self.var, self.m, self.v, lr, beta1, beta2, ... epsilon, decay, grad) ... return out >>> net = Net() >>> gradient = Tensor(np.ones([2, 2]).astype(np.float32)) >>> output = net(0.001, 0.9, 0.999, 1e-8, 0.0, gradient) >>> print(net.var.asnumpy()) [[0.999 0.999] [0.999 0.999]]
-
class
tinyms.primitives.
AdaptiveAvgPool2D
(output_size)[source]¶ AdaptiveAvgPool2D operation.
Refer to
mindspore.ops.adaptive_avg_pool2d()
for more details.- Supported Platforms:
GPU
Examples
>>> # case 1: output_size=(None, 2) >>> input_x = Tensor(np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], ... [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], ... [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]), mindspore.float32) >>> adaptive_avg_pool_2d = ops.AdaptiveAvgPool2D((None, 2)) >>> output = adaptive_avg_pool_2d(input_x) >>> print(output) [[[1.5 2.5] [4.5 5.5] [7.5 8.5]] [[1.5 2.5] [4.5 5.5] [7.5 8.5]] [[1.5 2.5] [4.5 5.5] [7.5 8.5]]] >>> # case 2: output_size=2 >>> adaptive_avg_pool_2d = ops.AdaptiveAvgPool2D(2) >>> output = adaptive_avg_pool_2d(input_x) >>> print(output) [[[3. 4.] [6. 7.]] [[3. 4.] [6. 7.]] [[3. 4.] [6. 7.]]] >>> # case 3: output_size=(1, 2) >>> adaptive_avg_pool_2d = ops.AdaptiveAvgPool2D((1, 2)) >>> output = adaptive_avg_pool_2d(input_x) >>> print(output) [[[4.5 5.5]] [[4.5 5.5]] [[4.5 5.5]]]
-
class
tinyms.primitives.
AdaptiveAvgPool3D
(output_size)[source]¶ AdaptiveAvgPool3D operation.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.adaptive_avg_pool3d()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> import numpy as np >>> from mindspore import nn, Tensor >>> from mindspore.ops import AdaptiveAvgPool3D >>> class AdaptiveAvgPool3DNet(nn.Cell): ... def __init__(self, output_size): ... super(AdaptiveAvgPool3DNet, self).__init__() ... self.output_size_ = output_size ... self.adaptive_avg_pool_3d = AdaptiveAvgPool3D(self.output_size_) ... def construct(self, x_): ... return self.adaptive_avg_pool_3d(x_) ... >>> output_size=(1,1,1) >>> input_x_val = np.zeros((1,1,2,2,2)) >>> input_x_val[:,:,0,:,:] += 1 >>> input_x = Tensor(input_x_val, mindspore.float32) >>> adaptive_avg_pool_3d = AdaptiveAvgPool3DNet(output_size) >>> output = adaptive_avg_pool_3d(input_x) >>> print(output) [[[[[0.5]]]]]
-
class
tinyms.primitives.
AdaptiveMaxPool2D
(output_size)[source]¶ Performs 2D adaptive max pooling on a multi-plane input signal.
Refer to
mindspore.ops.adaptive_max_pool2d()
for more details.- Parameters:
output_size (Union[int, tuple]) – The target output size. ouput_size can be a tuple \((H, W)\), or an int H for \((H, H)\). \(H\) and \(W\) can be int or None. If it is None, it means the output size is the same as the input size.
- Inputs:
input_x (Tensor) - The input of AdaptiveMaxPool2D, which is a 3D or 4D tensor, with float16, float32 or float64 data type.
- Outputs:
Tensor, with the same type as the input_x.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> # case 1: output_size=(None, 2) >>> input_x = Tensor(np.array([[[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], ... [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], ... [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]]), mindspore.float32) >>> adaptive_max_pool_2d = ops.AdaptiveMaxPool2D((None, 2)) >>> output = adaptive_max_pool_2d(input_x) >>> print(output[0]) [[[[2. 3.] [5. 6.] [8. 9.]] [[2. 3.] [5. 6.] [8. 9.]] [[2. 3.] [5. 6.] [8. 9.]]]] >>> # case 2: output_size=2 >>> adaptive_max_pool_2d = ops.AdaptiveMaxPool2D(2) >>> output = adaptive_max_pool_2d(input_x) >>> print(output[0]) [[[[5. 6.] [8. 9.]] [[5. 6.] [8. 9.]] [[5. 6.] [8. 9.]]]] >>> # case 3: output_size=(1, 2) >>> adaptive_max_pool_2d = ops.AdaptiveMaxPool2D((1, 2)) >>> output = adaptive_max_pool_2d(input_x) >>> print(output[0]) [[[[8. 9.]] [[8. 9.]] [[8. 9.]]]]
-
class
tinyms.primitives.
AdaptiveMaxPool3D
[source]¶ Performs 3D adaptive max pooling on a multi-plane input signal.
Refer to
mindspore.ops.adaptive_max_pool3d()
for more details.- Inputs:
x (Tensor) - Tensor, with shape \((C, D, H, W)\) or \((N, C, D, H, W)\).
output_size (Union[int, tuple]) - The specified output size, which is an integer that represents depth, height and width, or a tuple of three int numbers that represent depth, height and width respectively. The value must be a positive integer. If it is None, the output size and input size of the corresponding dimension are the same.
- Outputs:
y (Tensor) - Tensor, with the same number of dims and data type as the input.
argmax (Tensor) - Tensor, the indices of max value, which has the same shape as the y and it’s data type is int32.
- Supported Platforms:
GPU
CPU
Examples
>>> class AdaptiveMaxPool3DNet(nn.Cell): ... def __init__(self): ... super(AdaptiveMaxPool3DNet, self).__init__() ... self.adaptive_max_pool_3d = ops.AdaptiveMaxPool3D() ... def construct(self, x_, output_size_): ... return self.adaptive_max_pool_3d(x_, output_size_) >>> x = np.arange(0,36).reshape((1, 3, 3, 4)).astype(np.float32) >>> output_size = np.array([1, 1, 2], dtype=np.int32) >>> net = AdaptiveMaxPool3DNet() >>> output = net(Tensor(x), Tensor(output_size)) >>> print(output[0].asnumpy()) [[[[33. 35.]]]] >>> print(output[1].asnumpy()) [[[[33 35]]]]
-
class
tinyms.primitives.
Add
[source]¶ Adds two input tensors element-wise.
Refer to
mindspore.ops.add()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> # case 1: x and y are both Tensor. >>> add = ops.Add() >>> x = Tensor(np.array([1, 2, 3]).astype(np.float32)) >>> y = Tensor(np.array([4, 5, 6]).astype(np.float32)) >>> output = add(x, y) >>> print(output) [5. 7. 9.] >>> # case 2: x is a scalar and y is a Tensor >>> add = ops.Add() >>> x = Tensor(1, mindspore.int32) >>> y = Tensor(np.array([4, 5, 6]).astype(np.float32)) >>> output = 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
-
class
tinyms.primitives.
AddN
[source]¶ Computes addition of all input tensors element-wise.
Refer to
mindspore.ops.addn()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class NetAddN(nn.Cell): ... def __init__(self): ... super(NetAddN, self).__init__() ... self.addN = ops.AddN() ... ... def construct(self, *z): ... return self.addN(z) ... >>> net = NetAddN() >>> x = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> y = Tensor(np.array([4, 5, 6]), mindspore.float32) >>> output = net(x, y, x, y) >>> print(output) [10. 14. 18.]
-
class
tinyms.primitives.
Addcdiv
[source]¶ Performs the element-wise division of tensor x1 by tensor x2, multiply the result by the scalar value and add it to input_data.
\[y[i] = input\_data[i] + value[i] * (x1[i] / x2[i])\]- Inputs:
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.
- Outputs:
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 x1, x2, value, input_data are not the same.
ValueError – If x1 could not be broadcast to x2.
ValueError – If value could not be broadcast to x1/x2.
ValueError – If input_data could not be broadcast to 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) >>> addcdiv = ops.Addcdiv() >>> y = addcdiv(input_data, x1, x2, value) >>> print(y) [1.25 1.6666667 2.5 5. ]
-
class
tinyms.primitives.
Addcmul
[source]¶ Performs the element-wise product of tensor x1 and tensor x2, multiply the result by the scalar value and add it to input_data.
\[output[i] = input\_data[i] + value[i] * (x1[i] * x2[i])\]- Inputs:
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.
- Outputs:
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 x1, x2, value, input_data are not the same.
ValueError – If x1 could not be broadcast to x2.
ValueError – If value could not be broadcast to x1 * x2.
ValueError – If input_data could not be broadcast to value*(x1*x2).
- Supported Platforms:
Ascend
GPU
CPU
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) >>> addcmul = ops.Addcmul() >>> y = addcmul(input_data, x1, x2, value) >>> print(y) [[ 2. 3. 4.] [ 3. 5. 7.] [ 4. 7. 10.]]
-
class
tinyms.primitives.
AdjustHue
[source]¶ Adjust hue of RGB images.
Note
A convenience method that transform an RGB image to float representation. The image is adjusted by transforming the image to HSV and shifting the intensities in the hue channel, then transform back to original data mode. It is recommended to minimize the number of redundant transformations when several adjustments are chained.
- Inputs:
image (Tensor): RGB image or images, a Tensor has at least 3-D. The last dimension is interpreted as channels whose size must be three. the dtype is float16 or float32.
delta (Tensor): How much to add to the hue channel, the dtype is float32. Must be 0-D.
- Outputs:
Adjusted image(s), same shape and dtype as image.
- Raises:
TypeError – If neither image nor delta is a tensor.
TypeError – If the dtype of image is neither float16 nor float32.
TypeError – If the dtype of delta not float32.
ValueError – If the dimension of image is less than 3.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class AdjustHue(nn.Cell): ... def __init__(self): ... super(AdjustHue, self).__init__() ... self.adjustHue = ops.AdjustHue() ... def construct(self, image, delta): ... return self.adjustHue(image, delta) ... >>> image = np.array([[[1, 2, 3], [4, 5, 6]], ... [[7, 8, 9], [10, 11, 12]], ... [[13, 14, 15], [16, 17, 18]]]).astype(np.float32) >>> delta = 0.2 >>> adjust_hue = AdjustHue() >>> output = adjust_hue(Tensor(image), Tensor(delta)) >>> print("output", output) output [[[ 2.3999996 1. 3. ] [ 5.3999996 4. 6. ]] [[ 8.4 7. 9. ] [11.4 10. 12. ]] [[14.4 13. 15. ] [17.4 16. 18. ]]]
-
class
tinyms.primitives.
AdjustSaturation
[source]¶ Adjust saturation of RGB images.
Note
This is a convenience method that converts RGB images to float representation, converts them to HSV, adds an offset to the saturation channel, converts back to RGB and then back to the original data type. If several adjustments are chained it is advisable to minimize the number of redundant conversions.
- Inputs:
image (Tensor) - Images to adjust. Must be one of the following types: float16, float32. At least 3-D. The last dimension is interpreted as channels, and must be three.
scale (Tensor) - A scale factor determines the amount of saturation adjustment to apply to the image. A value greater than 1.0 increases the saturation, while a value less than 1.0 decreases the saturation. A value of 1.0 leaves the saturation unchanged. Must be 0-D Tensor of type float32.
- Outputs:
Adjusted image(s), same shape and dtype as image.
- Raises:
TypeError – If any iput is not Tensor.
TypeError – If the type of image is not one of the following dtype: float16, float32.
TypeError – If the type of scale is not float32.
ValueError – If the dimension of the ‘image’ is less than 3.
ValueError – If the last dimension of the ‘image’ is not 3.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor([[[1.0, 2.0, 3.0], ... [4.0, 5.0, 6.0]], ... [[7.0, 8.0, 9.0], ... [10.0, 11.0, 12.0]]]) >>> scale = Tensor(float(0.5)) >>> adjustsaturation = ops.AdjustSaturation() >>> output = adjustsaturation(x, scale) >>> print(output) [[[ 2. 2.4999998 3. ] [ 5. 5.5 6. ]] [[ 8. 8.5 9. ] [11. 11.5 12. ]]]
-
class
tinyms.primitives.
AffineGrid
(align_corners=False)[source]¶ Creates a 2D or 3D flow field (sampling grid) based on a batch of affine matrices theta.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.affine_grid()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> affinegrid = ops.AffineGrid(align_corners=False) >>> theta = Tensor([[[0.8, 0.5, 0],[-0.5, 0.8, 0]]], mindspore.float32) >>> out_size = (1, 3, 2, 3) >>> output = affinegrid(theta, out_size) >>> print(output) [[[[-0.78333336 -0.06666666] [-0.25 -0.4 ] [ 0.28333336 -0.73333335]] [[-0.28333336 0.73333335] [ 0.25 0.4 ] [ 0.78333336 0.06666666]]]]
-
class
tinyms.primitives.
AllGather
(group='hccl_world_group')[source]¶ Gathers tensors from the specified communication group.
Note
The tensors must have the same shape and format in all processes of the collection.
Currently only supports GRAPH_MODE and it should be called in Cell.
- Parameters:
group (str) – The communication group to work on. Default: “GlobalComm.WORLD_COMM_GROUP”.
- Inputs:
input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).
- Outputs:
Tensor. If the number of devices in the group is N, then the shape of output is \((N, x_1, x_2, ..., x_R)\).
- Raises:
TypeError – If group is not a str.
ValueError – If the local rank id of the calling process in the group is larger than the group’s rank size.
- Supported Platforms:
Ascend
GPU
Examples
Note
Before running the following examples, you need to configure the communication environment variables.
For the Ascend devices, users need to prepare the rank table, set rank_id and device_id. Please see the Ascend tutorial for more details.
For the GPU devices, users need to prepare the host file and mpi, please see the GPU tutorial .
This example should be run with 2 devices.
>>> import numpy as np >>> import mindspore as ms >>> import mindspore.ops as ops >>> import mindspore.nn as nn >>> from mindspore.communication import init >>> from mindspore import Tensor >>> >>> ms.set_context(mode=ms.GRAPH_MODE) >>> init() >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.allgather = ops.AllGather() ... ... def construct(self, x): ... return self.allgather(x) ... >>> input_x = Tensor(np.ones([2, 8]).astype(np.float32)) >>> net = Net() >>> output = net(input_x) >>> print(output) [[1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1.]]
-
class
tinyms.primitives.
AllReduce
(op='sum', group='hccl_world_group')[source]¶ Reduces the tensor data across all devices in such a way that all devices will get the same final result.
Note
The tensors must have the same shape and format in all processes of the collection.
- Parameters:
- Inputs:
input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).
- Outputs:
Tensor, has the same shape of the input, i.e., \((x_1, x_2, ..., x_R)\). The contents depend on the specified operation.
- Raises:
TypeError – If any of op and group is not a str, or fusion is not an integer, or the input’s dtype is bool.
- Supported Platforms:
Ascend
GPU
CPU
Examples
Note
Before running the following examples, you need to configure the communication environment variables.
For the Ascend devices, users need to prepare the rank table, set rank_id and device_id. Please see the Ascend tutorial for more details.
For the GPU devices, users need to prepare the host file and mpi, please see the GPU tutorial .
This example should be run with multiple devices.
>>> import numpy as np >>> from mindspore.communication import init >>> from mindspore import Tensor >>> from mindspore.ops import ReduceOp >>> import mindspore.nn as nn >>> import mindspore.ops as ops >>> >>> init() >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.allreduce_sum = ops.AllReduce(ReduceOp.SUM) ... ... def construct(self, x): ... return self.allreduce_sum(x) ... >>> input_ = Tensor(np.ones([2, 8]).astype(np.float32)) >>> net = Net() >>> output = net(input_) >>> print(output) [[2. 2. 2. 2. 2. 2. 2. 2.] [2. 2. 2. 2. 2. 2. 2. 2.]]
-
class
tinyms.primitives.
AlltoAll
(split_count, split_dim, concat_dim, group='hccl_world_group')[source]¶ AlltoAll is a collective operation.
AlltoAll sends data from the all processes to the all processes in the specified group. It has two phases:
The scatter phase: On each process, the operand is split into split_count number of blocks along the split_dimensions, and the blocks are scattered to all processes, e.g., the ith block is send to the ith process.
The gather phase: Each process concatenates the received blocks along the concat_dimension.
Note
This operator requires a full-mesh network topology, each device has the same vlan id, and the ip & mask are in the same subnet, please check the details .
- Parameters:
split_count (int) – On each process, divide blocks into split_count number.
split_dim (int) – On each process, split blocks along the split_dim.
concat_dim (int) – On each process, gather the received blocks along the concat_dimension.
group (str) – The communication group to work on. Default: “GlobalComm.WORLD_COMM_GROUP”.
- Inputs:
input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).
- Outputs:
Tensor. If the shape of input tensor is \((x_1, x_2, ..., x_R)\), then the shape of output tensor is \((y_1, y_2, ..., y_R)\), where:
\(y_{split\_dim} = x_{split\_dim} / split\_count\)
\(y_{concat\_dim} = x_{concat\_dim} * split\_count\)
\(y_{other} = x_{other}\).
- Raises:
TypeError – If group is not a string.
- Supported Platforms:
Ascend
Examples
Note
Before running the following examples, you need to configure the communication environment variables.
For the Ascend devices, users need to prepare the rank table, set rank_id and device_id. Please see the Ascend tutorial for more details.
For the GPU devices, users need to prepare the host file and mpi, please see the GPU tutorial .
This example should be run with 8 devices.
>>> import os >>> import mindspore as ms >>> from mindspore import Tensor >>> from mindspore.communication import init >>> import mindspore.nn as nn >>> import mindspore.ops as ops >>> import numpy as np >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.alltoall = ops.AlltoAll(split_count = 8, split_dim = -2, concat_dim = -1) ... ... def construct(self, x): ... out = self.alltoall(x) ... return out ... >>> ms.set_context(mode=ms.GRAPH_MODE, device_target='Ascend') >>> init() >>> net = Net() >>> rank_id = int(os.getenv("RANK_ID")) >>> input_x = Tensor(np.ones([1, 1, 8, 1]) * rank_id, dtype = ms.float32) >>> output = net(input_x) >>> print(output) [[[[0. 1. 2. 3. 4. 5. 6. 7.]]]]
-
class
tinyms.primitives.
Angle
[source]¶ Returns the element-wise argument of a complex tensor.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.angle()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input = Tensor([-1.5 + 7.8j, 3 + 5.75j], mindspore.complex64) >>> angle = ops.Angle() >>> output = angle(input) >>> print(output) [1.7607845 1.0899091]
-
class
tinyms.primitives.
ApplyAdaMax
[source]¶ Updates relevant entries according to the adamax scheme.
The updating formulas are as follows,
\[\begin{split}\begin{array}{ll} \\ m_{t+1} = \beta_1 * m_{t} + (1 - \beta_1) * g \\ v_{t+1} = \max(\beta_2 * v_{t}, \left| g \right|) \\ var = var - \frac{l}{1 - \beta_1^{t+1}} * \frac{m_{t+1}}{v_{t+1} + \epsilon} \end{array}\end{split}\]\(t\) represents updating step while \(m\) represents the 1st moment vector, \(m_{t}\) is the last moment of \(m_{t+1}\), \(v\) represents the 2nd moment vector, \(v_{t}\) is the last moment of \(v_{t+1}\), \(l\) represents scaling factor lr, \(g\) represents grad, \(\beta_1, \beta_2\) represent beta1 and beta2, \(\beta_1^{t+1}\) represents beta1_power, \(var\) represents the variable to be updated, \(\epsilon\) represents epsilon.
Inputs of var, m, v and grad 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.
- Inputs:
var (Parameter) - Variable to be updated. With float32 or float16 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
m (Parameter) - The 1st moment vector in the updating formula, has the same shape and type as var. With float32 or float16 data type.
v (Parameter) - The 2nd moment vector in the updating formula. Mean square gradients with the same shape and type as var. With float32 or float16 data type.
beta1_power (Union[Number, Tensor]) - \(beta_1^t\) in the updating formula, must be a scalar. With float32 or float16 data type.
lr (Union[Number, Tensor]) - Learning rate, \(l\) in the updating formula, must be a scalar. With float32 or float16 data type.
beta1 (Union[Number, Tensor]) - The exponential decay rate for the 1st moment estimations, must be a scalar. With float32 or float16 data type.
beta2 (Union[Number, Tensor]) - The exponential decay rate for the 2nd moment estimations, must be a scalar. With float32 or float16 data type.
epsilon (Union[Number, Tensor]) - A small value added for numerical stability, must be a scalar. With float32 or float16 data type.
grad (Tensor) - A tensor for gradient, has the same shape and type as var. With float32 or float16 data type.
- Outputs:
Tuple of 3 Tensor, the updated parameters.
var (Tensor) - The same shape and data type as var.
m (Tensor) - The same shape and data type as m.
v (Tensor) - The same shape and data type as v.
- Raises:
TypeError – If dtype of var, m, v, beta_power, lr, beta1, beta2, epsilon or grad is neither float16 nor float32.
TypeError – If beta_power, lr, beta1, beta2 or epsilon is neither a Number nor a Tensor.
TypeError – If grad is not a Tensor.
RuntimeError – If the data type of var, m, v and grad conversion of Parameter is not supported.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_ada_max = ops.ApplyAdaMax() ... self.var = Parameter(Tensor(np.array([[0.6, 0.4], ... [0.1, 0.5]]).astype(np.float32)), name="var") ... self.m = Parameter(Tensor(np.array([[0.6, 0.5], ... [0.2, 0.6]]).astype(np.float32)), name="m") ... self.v = Parameter(Tensor(np.array([[0.9, 0.1], ... [0.7, 0.8]]).astype(np.float32)), name="v") ... def construct(self, beta1_power, lr, beta1, beta2, epsilon, grad): ... out = self.apply_ada_max(self.var, self.m, self.v, beta1_power, lr, beta1, beta2, epsilon, grad) ... return out ... >>> net = Net() >>> beta1_power =Tensor(0.9, mindspore.float32) >>> lr = Tensor(0.001, mindspore.float32) >>> beta1 = Tensor(0.9, mindspore.float32) >>> beta2 = Tensor(0.99, mindspore.float32) >>> epsilon = Tensor(1e-10, mindspore.float32) >>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32)) >>> output = net(beta1_power, lr, beta1, beta2, epsilon, grad) >>> print(output) (Tensor(shape=[2, 2], dtype=Float32, value= [[ 5.93602717e-01, 3.92571449e-01], [ 9.72582996e-02, 4.92249995e-01]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 5.69999993e-01, 5.19999981e-01], [ 1.89999998e-01, 6.20000005e-01]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 8.90999973e-01, 6.99999988e-01], [ 6.93000019e-01, 8.00000012e-01]]))
-
class
tinyms.primitives.
ApplyAdadelta
[source]¶ Updates relevant entries according to the adadelta scheme.
The Adadelta algorithm is proposed in ADADELTA: AN ADAPTIVE LEARNING RATE METHOD.
\[\begin{split}\begin{array}{ll} \\ \text{accum} = \rho * \text{accum} + (1 - \rho) * \text{grad}^2 \\ \text{update} = \sqrt{\text{accum_update} + \epsilon} * \frac{\text{grad}}{\sqrt{\text{accum} + \epsilon}} \\ \text{accum_update} = \rho * \text{accum_update} + (1 - \rho) * \text{update}^2 \\ \text{var} = \text{var} - \text{lr} * \text{update} \end{array}\end{split}\]where \(\rho\) represents rho, \(\epsilon\) represents epsilon.
Inputs of var, accum, accum_update and grad 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.
- Inputs:
var (Parameter) - Weights to be updated. With float32 or float16 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
accum (Parameter) - Accumulation to be updated, has the same shape and data type as var.
accum_update (Parameter) - Accum_update to be updated, has the same shape and data type as var.
lr (Union[Number, Tensor]) - Learning rate, must be a scalar. With float32 or float16 data type.
rho (Union[Number, Tensor]) - Decay rate, must be a scalar. With float32 or float16 data type.
epsilon (Union[Number, Tensor]) - A small value added for numerical stability, must be a scalar. With float32 or float16 data type.
grad (Tensor) - Gradients, has the same shape and data type as var.
- Outputs:
Tuple of 3 Tensor, the updated parameters.
var (Tensor) - The same shape and data type as var.
accum (Tensor) - The same shape and data type as accum.
accum_update (Tensor) - The same shape and data type as accum_update.
- Raises:
TypeError – If dtype of var, accum, accum_update, lr, rho, epsilon or grad is neither float16 nor float32.
TypeError – If accum_update, lr, rho or epsilon is neither a Number nor a Tensor.
RuntimeError – If the data type of var, accum, accum_update and grad conversion of Parameter is not supported.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> import mindspore >>> from mindspore import nn, Tensor, ops, Parameter >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_adadelta = ops.ApplyAdadelta() ... self.var = Parameter(Tensor(np.array([[0.6, 0.4], ... [0.1, 0.5]]).astype(np.float32)), name="var") ... self.accum = Parameter(Tensor(np.array([[0.6, 0.5], ... [0.2, 0.6]]).astype(np.float32)), name="accum") ... self.accum_update = Parameter(Tensor(np.array([[0.9, 0.1], ... [0.7, 0.8]]).astype(np.float32)), ... name="accum_update") ... def construct(self, lr, rho, epsilon, grad): ... out = self.apply_adadelta(self.var, self.accum, self.accum_update, lr, rho, epsilon, grad) ... return out ... >>> net = Net() >>> lr = Tensor(0.001, mindspore.float32) >>> rho = Tensor(0.0, mindspore.float32) >>> epsilon = Tensor(1e-6, mindspore.float32) >>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32)) >>> output = net(lr, rho, epsilon, grad) >>> print(output) (Tensor(shape=[2, 2], dtype=Float32, value= [[ 5.99051356e-01, 3.99683774e-01], [ 9.91633832e-02, 4.99105573e-01]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 9.00000036e-02, 4.89999980e-01], [ 1.00000007e-02, 6.40000045e-01]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 8.99990857e-01, 1.00000791e-01], [ 6.99930906e-01, 7.99999774e-01]]))
-
class
tinyms.primitives.
ApplyAdagrad
(update_slots=True)[source]¶ Updates relevant entries according to the adagrad scheme. The Adagrad algorithm was proposed in Adaptive Subgradient Methods for Online Learning and Stochastic Optimization. This module can adaptively assign different learning rates for each parameter in view of the uneven number of samples for different parameters.
\[\begin{split}\begin{array}{ll} \\ accum += grad * grad \\ var -= lr * grad * \frac{1}{\sqrt{accum}} \end{array}\end{split}\]Inputs of var, accum and grad 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.
- Parameters:
update_slots (bool) – If True, accum will be updated. Default: True.
- Inputs:
var (Parameter) - Variable to be updated. With float or complex data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
accum (Parameter) - Accumulation to be updated. The shape and data type must be the same as var.
lr (Union[Number, Tensor]) - The learning rate value, must be a scalar. With float or complex data type.
grad (Tensor) - A tensor for gradient. The shape and data type must be the same as var.
- Outputs:
Tuple of 2 Tensors, the updated parameters.
var (Tensor) - The same shape and data type as var.
accum (Tensor) - The same shape and data type as accum.
- Raises:
TypeError – If dtype of var, accum, lr or grad is neither float nor complex.
TypeError – If lr is neither a Number nor a Tensor.
RuntimeError – If the data type of var, accum and grad conversion of Parameter is not supported.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_adagrad = ops.ApplyAdagrad() ... self.var = Parameter(Tensor(np.array([[0.6, 0.4], ... [0.1, 0.5]]).astype(np.float32)), name="var") ... self.accum = Parameter(Tensor(np.array([[0.6, 0.5], ... [0.2, 0.6]]).astype(np.float32)), name="accum") ... def construct(self, lr, grad): ... out = self.apply_adagrad(self.var, self.accum, lr, grad) ... return out ... >>> net = Net() >>> lr = Tensor(0.001, mindspore.float32) >>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32)) >>> output = net(lr, grad) >>> print(output) (Tensor(shape=[2, 2], dtype=Float32, value= [[ 5.99638879e-01, 3.99296492e-01], [ 9.97817814e-02, 4.99281585e-01]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 6.90000057e-01, 9.90000010e-01], [ 2.10000008e-01, 1.24000001e+00]]))
-
class
tinyms.primitives.
ApplyAdagradDA
(use_locking=False)[source]¶ Update var according to the proximal adagrad scheme. The Adagrad algorithm was proposed in Adaptive Subgradient Methods for Online Learning and Stochastic Optimization.
\[\begin{split}\begin{array}{ll} \\ grad\_accum += grad \\ grad\_squared\_accum += grad * grad \\ tmp\_val= \begin{cases} sign(grad\_accum) * max\left \{|grad\_accum|-l1*global\_step, 0\right \} & \text{ if } l1>0 \\ grad\_accum & \text{ otherwise } \\ \end{cases} \\ x\_value = -1 * lr * tmp\_val \\ y\_value = l2 * global\_step * lr + \sqrt{grad\_squared\_accum} \\ var = \frac{ x\_value }{ y\_value } \end{array}\end{split}\]Inputs of var, gradient_accumulator, gradient_squared_accumulator and grad 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.
- Parameters:
use_locking (bool) – If True, updating of the var and accum tensors will be protected by a lock. Otherwise the behavior is undefined, but may exhibit less contention. Default: False.
- Inputs:
var (Parameter) - Variable to be updated. The data type must be float16 or float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
gradient_accumulator (Parameter) - The dict of mutable tensor \(grad\_accum\). Must have the same shape and dtype as var.
gradient_squared_accumulator (Parameter) - The dict of mutable tensor \(grad\_squared\_accum\). Must have the same shape and dtype as var.
grad (Tensor) - A tensor for gradient. Must have the same shape and dtype as var.
lr ([Number, Tensor]) - Scaling factor. Must be a scalar. With float32 or float16 data type.
l1 ([Number, Tensor]) - L1 regularization. Must be a scalar. With float32 or float16 data type.
l2 ([Number, Tensor]) - L2 regularization. Must be a scalar. With float32 or float16 data type.
global_step ([Number, Tensor]) - Training step number. Must be a scalar. With int32 or int64 data type.
- Outputs:
Tuple of 3 Tensors, the updated parameters.
var (Tensor) - The same shape and data type as var.
gradient_accumulator (Tensor) - The same shape and data type as gradient_accumulator.
gradient_squared_accumulator (Tensor) - The same shape and data type as gradient_squared_accumulator.
- Raises:
TypeError – If var, gradient_accumulator or gradient_squared_accumulator is not a Parameter.
TypeError – If grad is not a Tensor.
TypeError – If lr, l1, l2 or global_step is neither a Number nor a Tensor.
TypeError – If use_locking is not a bool.
TypeError – If dtype of var, gradient_accumulator, gradient_squared_accumulator, grad, lr, l1 or l2 is neither float16 nor float32.
TypeError – If dtype of gradient_accumulator, gradient_squared_accumulator or grad is not same as var.
TypeError – If dtype of global_step is not int32 nor int64.
ValueError – If the shape size of lr, l1, l2 and global_step is not 0.
RuntimeError – If the data type of var, gradient_accumulator, gradient_squared_accumulator and grad conversion of Parameter is not supported.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class ApplyAdagradDANet(nn.Cell): ... def __init__(self, use_locking=False): ... super(ApplyAdagradDANet, self).__init__() ... self.apply_adagrad_d_a = ops.ApplyAdagradDA(use_locking) ... self.var = Parameter(Tensor(np.array([[0.6, 0.4], [0.1, 0.5]]).astype(np.float32)), name="var") ... self.gradient_accumulator = Parameter(Tensor(np.array([[0.1, 0.3], ... [0.1, 0.5]]).astype(np.float32)), ... name="gradient_accumulator") ... self.gradient_squared_accumulator = Parameter(Tensor(np.array([[0.2, 0.1], ... [0.1, 0.2]]).astype(np.float32)), ... name="gradient_squared_accumulator") ... self.gradient_accumulator = Parameter(Tensor(np.array([[0.1, 0.3], ... [0.1, 0.5]]).astype(np.float32)), ... name="gradient_accumulator") ... def construct(self, grad, lr, l1, l2, global_step): ... out = self.apply_adagrad_d_a(self.var, self.gradient_accumulator, ... self.gradient_squared_accumulator, grad, lr, l1, l2, global_step) ... return out ... >>> net = ApplyAdagradDANet() >>> grad = Tensor(np.array([[0.3, 0.4], [0.1, 0.2]]).astype(np.float32)) >>> lr = Tensor(0.001, mstype.float32) >>> l1 = Tensor(0.001, mstype.float32) >>> l2 = Tensor(0.001, mstype.float32) >>> global_step = Tensor(2, mstype.int32) >>> output = net(grad, lr, l1, l2, global_step) >>> print(output) (Tensor(shape=[2, 2], dtype=Float32, value= [[-7.39064650e-04, -1.36888528e-03], [-5.96988888e-04, -1.42478070e-03]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 4.00000006e-01, 7.00000048e-01], [ 2.00000003e-01, 6.99999988e-01]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 2.90000021e-01, 2.60000020e-01], [ 1.09999999e-01, 2.40000010e-01]]))
-
class
tinyms.primitives.
ApplyAdagradV2
(epsilon, update_slots=True)[source]¶ Updates relevant entries according to the adagradv2 scheme.
\[\begin{split}\begin{array}{ll} \\ accum += grad * grad \\ var -= lr * grad * \frac{1}{\sqrt{accum} + \epsilon} \end{array}\end{split}\]where \(\epsilon\) represents epsilon.
Inputs of var, accum and grad 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.
Note
The difference is that ApplyAdagradV2 has one more small constant value \(\epsilon\) than ApplyAdagrad.
- Parameters:
- Inputs:
var (Parameter) - Variable to be updated. With float16 or float32 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
accum (Parameter) - Accumulation to be updated. The shape and data type must be the same as var.
lr (Union[Number, Tensor]) - The learning rate value, must be a float number or a scalar tensor with float16 or float32 data type.
grad (Tensor) - A tensor for gradient. The shape and data type must be the same as var.
- Outputs:
Tuple of 2 Tensors, the updated parameters.
var (Tensor) - The same shape and data type as var.
accum (Tensor) - The same shape and data type as accum.
- Raises:
TypeError – If dtype of var, accum, lr or grad is neither float16 nor float32.
TypeError – If lr is neither a Number nor a Tensor.
RuntimeError – If the data type of var, accum and grad conversion of Parameter is not supported.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_adagrad_v2 = ops.ApplyAdagradV2(epsilon=1e-6) ... self.var = Parameter(Tensor(np.array([[0.6, 0.4], ... [0.1, 0.5]]).astype(np.float32)), name="var") ... self.accum = Parameter(Tensor(np.array([[0.6, 0.5], ... [0.2, 0.6]]).astype(np.float32)), name="accum") ... def construct(self, lr, grad): ... out = self.apply_adagrad_v2(self.var, self.accum, lr, grad) ... return out ... >>> net = Net() >>> lr = Tensor(0.001, mindspore.float32) >>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32)) >>> output = net(lr, grad) >>> print(output) (Tensor(shape=[2, 2], dtype=Float32, value= [[ 5.99638879e-01, 3.99296492e-01], [ 9.97817814e-02, 4.99281585e-01]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 6.90000057e-01, 9.90000010e-01], [ 2.10000008e-01, 1.24000001e+00]]))
-
class
tinyms.primitives.
ApplyAdamWithAmsgrad
(beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False)[source]¶ Update var according to the Adam algorithm.
\[\begin{split}\begin{array}{l1} \\ lr_t:=learning\_rate*\sqrt{1-\beta_2^t}/(1-\beta_1^t) \\ m_t:=\beta_1*m_{t-1}+(1-\beta_1)*g \\ v_t:=\beta_2*v_{t-1}+(1-\beta_2)*g*g \\ \hat v_t:=max(\hat v_{t-1}, v_t) \\ var:=var-lr_t*m_t/(\sqrt{\hat v_t}+\epsilon) \\ \end{array}\end{split}\]- Parameters:
beta1 (float) – A Tensor. Must have the same type as beta1_power. Momentum factor. Must be a scalar.
beta2 (float) – A Tensor. Must have the same type as beta1_power. Momentum factor. Must be a scalar.
epsilon (float) – A Tensor. Must have the same type as beta1_power. Ridge term. Must be a scalar.
use_locking (bool) – use_locking: If True , updating of the var, m, and v tensors will be protected by a lock; Otherwise the behavior is undefined, but may exhibit less contention. Default: False.
- Inputs:
var (Parameter) - Variable to be updated. The data type can be float16 or float32.
m (Parameter) - The 1st moment vector in the updating formula, the shape and data type value should be the same as var.
v (Parameter) - the 2nd moment vector in the updating formula, the shape and data type value should be the same as var.
vhat (Parameter) - \(\hat v_t\) in the updating formula, the shape and data type value should be the same as var.
beta1_power (Union[float, Tensor]) - \(beta_1^t(\beta_1^{t})\) in the updating formula, a scalar tensor with float16 or float32 data type.
beta2_power (Union[float, Tensor]) - \(beta_2^t(\beta_2^{t})\) in the updating formula, a scalar tensor with float16 or float32 data type.
lr (Union[float, Tensor]) - Scaling factor, a scalar tensor with float16 or float32 data type.
grad (Tensor) - The gradient, has the same shape and data type as var.
- Outputs:
Tuple of 4 Tensors, the updated parameters.
var (Tensor) - The same shape and data type as var.
m (Tensor) - The same shape and data type as m.
v (Tensor) - The same shape and data type as v.
vhat (Tensor) - The same shape and data type as vhat.
- Raises:
TypeError – If var, m, v, vhat is not a Parameter.
TypeError – If beta1_power, beta2_power, lr is neither a Number nor a Tensor.
TypeError – If grad is not a Tensor.
TypeError – If dtype of var, m, v, vhat, beta1_power, beta2_power, lr, grad, momentum is not float32 or float16.
ValueError – If m or v or vhat or grad doesn’t have the same shape of var.
ValueError – If the shape of beta1_power, beta2_power, lr is not 0.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class ApplyAdamWithAmsgradNet(nn.Cell): ... def __init__(self, beta1=0.9, beta2=0.999, epsilon=1e-8, use_locking=False): ... super(ApplyAdamWithAmsgradNet, self).__init__() ... self.apply_adam_with_amsgrad = P.ApplyAdamWithAmsgrad(beta1, beta2, epsilon, use_locking) ... self.var = Parameter(Tensor(np.array([[0.2, 0.2], [0.2, 0.2]]).astype(np.float32)), name="var") ... self.m = Parameter(Tensor(np.array([[0.1, 0.2], [0.4, 0.3]]).astype(np.float32)), name="m") ... self.v = Parameter(Tensor(np.array([[0.2, 0.1], [0.3, 0.4]]).astype(np.float32)), name="v") ... self.vhat = Parameter(Tensor(np.array([[0.1, 0.2], [0.6, 0.2]]).astype(np.float32)), name="vhat") ... def construct(self, beta1_power, beta2_power, lr, grad): ... out = self.apply_adam_with_amsgrad(self.var, self.m, self.v, self.vhat, ... beta1_power, beta2_power, lr, grad) ... return out >>> net = ApplyAdamWithAmsgradNet() >>> grad = Tensor(np.array([[0.4, 0.2], [0.2, 0.3]]).astype(np.float32)) >>> output = net(Tensor(0.9, mstype.float32), Tensor(0.999, mstype.float32), Tensor(0.01, mstype.float32), grad) >>> print(net.var.asnumpy()) [[0.19908068 0.1985858 ] [0.19844866 0.19849943]]
-
class
tinyms.primitives.
ApplyAddSign
[source]¶ Updates relevant entries according to the AddSign algorithm.
\[\begin{split}\begin{array}{ll} \\ m_{t+1} = \beta * m_{t} + (1 - \beta) * g \\ \text{update} = (\alpha + \text{sign_decay} * sign(g) * sign(m)) * g \\ var = var - lr_{t+1} * \text{update} \end{array}\end{split}\]\(t\) represents updating step while \(m\) represents the 1st moment vector, \(m_{t}\) is the last moment of \(m_{t+1}\), \(lr\) represents scaling factor lr, \(g\) represents grad, \(\alpha\) represents alpha, \(\beta\) represents beta.
Inputs of var, accum and grad 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. The data type of inputs must be float16 or float32 on Ascend and float16, float32 or float64 on CPU and GPU.
- Inputs:
var (Parameter) - Variable tensor to be updated. With float16, float32 or float64 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
m (Parameter) - Variable tensor to be updated, has the same shape and data type as var.
lr (Union[Number, Tensor]) - The learning rate value, must be a scalar. With float16, float32 or float64 data type.
alpha (Union[Number, Tensor]) - Must be a scalar. With float16, float32 or float64 data type.
sign_decay (Union[Number, Tensor]) - Must be a scalar. With float16, float32 or float64 data type.
beta (Union[Number, Tensor]) - The exponential decay rate, must be a scalar. With float16, float32 or float64 data type.
grad (Tensor) - A tensor of the same shape and data type as var, for the gradient.
- Outputs:
Tuple of 2 Tensors, the updated parameters.
var (Tensor) - The same shape and data type as var.
m (Tensor) - The same shape and data type as m.
- Raises:
TypeError – If dtype of var, lr, alpha, sign_decay or beta is not float16, float32 or float64.
TypeError – If lr, alpha or sign_decay is neither a Number nor a Tensor.
TypeError – If grad is not a Tensor.
RuntimeError – If the data type of var, accum and grad conversion of Parameter is not supported.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_add_sign = ops.ApplyAddSign() ... self.var = Parameter(Tensor(np.array([[0.6, 0.4], ... [0.1, 0.5]]).astype(np.float32)), name="var") ... self.m = Parameter(Tensor(np.array([[0.6, 0.5], ... [0.2, 0.6]]).astype(np.float32)), name="m") ... self.lr = 0.001 ... self.alpha = 1.0 ... self.sign_decay = 0.99 ... self.beta = 0.9 ... def construct(self, grad): ... out = self.apply_add_sign(self.var, self.m, self.lr, self.alpha, self.sign_decay, self.beta, grad) ... return out ... >>> net = Net() >>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32)) >>> output = net(grad) >>> print(output) (Tensor(shape=[2, 2], dtype=Float32, value= [[ 5.99403024e-01, 3.98607016e-01], [ 9.98010039e-02, 4.98407990e-01]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 5.70000052e-01, 5.19999981e-01], [ 1.89999998e-01, 6.20000064e-01]]))
-
class
tinyms.primitives.
ApplyCenteredRMSProp
(use_locking=False)[source]¶ Optimizer that implements the centered RMSProp algorithm. Please refer to the usage in source code of
mindspore.nn.RMSProp
.The updating formulas of ApplyCenteredRMSProp algorithm are as follows,
\[\begin{split}\begin{array}{ll} \\ g_{t+1} = \rho g_{t} + (1 - \rho)\nabla Q_{i}(w) \\ s_{t+1} = \rho s_{t} + (1 - \rho)(\nabla Q_{i}(w))^2 \\ m_{t+1} = \beta m_{t} + \frac{\eta} {\sqrt{s_{t+1} - g_{t+1}^2 + \epsilon}} \nabla Q_{i}(w) \\ w = w - m_{t+1} \end{array}\end{split}\]where \(w\) represents var, which will be updated. \(g_{t+1}\) represents mean_gradient, \(g_{t}\) is the last moment of \(g_{t+1}\). \(s_{t+1}\) represents mean_square, \(s_{t}\) is the last moment of \(s_{t+1}\), \(m_{t+1}\) represents moment, \(m_{t}\) is the last moment of \(m_{t+1}\). \(\rho\) represents decay. \(\beta\) is the momentum term, represents momentum. \(\epsilon\) is a smoothing term to avoid division by zero, represents epsilon. \(\eta\) represents learning_rate. \(\nabla Q_{i}(w)\) represents grad.
Note
The difference between ApplyCenteredRMSProp and ApplyRMSProp is that the former uses the centered RMSProp algorithm, and the centered RRMSProp algorithm uses an estimate of the centered second moment(i.e., the variance) for normalization, as opposed to regular RMSProp, which uses the (uncertained) second moment. This often helps with training, but is slightly more expensive in terms of computation and memory.
Warning
In dense implementation of this algorithm, mean_gradient, mean_square, and moment will update even if the grad is zero. But in this sparse implementation, mean_gradient, mean_square, and moment will not update in iterations during which the grad is zero.
- Parameters:
use_locking (bool) – Whether to enable a lock to protect the variable and accumulation tensors from being updated. Default: False.
- Inputs:
var (Parameter) - Weights to be updated.
mean_gradient (Tensor) - Mean gradients, must be the same type as var.
mean_square (Tensor) - Mean square gradients, must be the same type as var.
moment (Tensor) - Delta of var, must be the same type as var.
grad (Tensor) - Gradient, must be the same type as var.
learning_rate (Union[Number, Tensor]) - Learning rate. Must be a float number or a scalar tensor with float16 or float32 data type.
decay (float) - Decay rate.
momentum (float) - Momentum.
epsilon (float) - Ridge term.
- Outputs:
Tensor, parameters to be updated.
- Raises:
TypeError – If use_locking is not a bool.
TypeError – If var, mean_gradient, mean_square, moment or grad is not a Tensor.
TypeError – If learing_rate is neither a Number nor a Tensor.
TypeError – If dtype of learing_rate is neither float16 nor float32.
TypeError – If decay, momentum or epsilon is not a float.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_centerd_rms_prop = ops.ApplyCenteredRMSProp() ... self.var = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="var") ... ... def construct(self, mean_grad, mean_square, moment, grad, decay, momentum, epsilon, lr): ... out = self.apply_centerd_rms_prop(self.var, mean_grad, mean_square, moment, grad, ... lr, decay, momentum, epsilon) ... return out ... >>> net = Net() >>> mean_grad = Tensor(np.ones([2, 2]).astype(np.float32)) >>> mean_square = Tensor(np.ones([2, 2]).astype(np.float32)) >>> moment = Tensor(np.ones([2, 2]).astype(np.float32)) >>> grad = Tensor(np.ones([2, 2]).astype(np.float32)) >>> output = net(mean_grad, mean_square, moment, grad, 0.0, 1e-10, 0.001, 0.01) >>> print(net.var.asnumpy()) [[0.68377227 0.68377227] [0.68377227 0.68377227]]
-
class
tinyms.primitives.
ApplyFtrl
(use_locking=False)[source]¶ Updates relevant entries according to the FTRL scheme.
For more details, please refer to
mindspore.nn.FTRL
.Note
Currently, only positive numbers are supported on the Ascend platform, and the calculation results for other scenarios are not defined.
- Parameters:
use_locking (bool) – Use locks for updating operation if true . Default: False.
- Inputs:
var (Parameter) - The variable to be updated. The data type must be float16 or float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
accum (Parameter) - The accumulation to be updated, must be same shape and data type as var.
linear (Parameter) - The linear coefficient to be updated, must be same shape and data type as var.
grad (Tensor) - Gradient. The data type must be float16 or float32.
lr (Union[Number, Tensor]) - The learning rate value, must be positive. Default: 0.001. It must be a float number or a scalar tensor with float16 or float32 data type.
l1 (Union[Number, Tensor]) - l1 regularization strength, must be greater than or equal to zero. Default: 0.0. It must be a float number or a scalar tensor with float16 or float32 data type.
l2 (Union[Number, Tensor]) - l2 regularization strength, must be greater than or equal to zero. Default: 0.0. It must be a float number or a scalar tensor with float16 or float32 data type.
lr_power (Union[Number, Tensor]) - Learning rate power controls how the learning rate decreases during training, must be less than or equal to zero. Use fixed learning rate if lr_power is zero. Default: -0.5. It must be a float number or a scalar tensor with float16 or float32 data type.
- Outputs:
var (Tensor) - Represents the updated var. As the input parameters has been updated in-place, this value is always zero when the platform is GPU.
- Raises:
TypeError – If use_locking is not a bool.
TypeError – If dtype of var, grad, lr, l1, l2 or lr_power is neither float16 nor float32.
TypeError – If lr, l1, l2 or lr_power is neither a Number nor a Tensor.
TypeError – If grad is not a Tensor.
RuntimeError – If the parameter types of var, accum and linear are inconsistent.
RuntimeError – If the parameter types of grad, lr, l1, l2, lr_power are inconsistent with var and the precision is greater than var.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class ApplyFtrlNet(nn.Cell): ... def __init__(self): ... super(ApplyFtrlNet, self).__init__() ... self.apply_ftrl = ops.ApplyFtrl() ... self.lr = 0.001 ... self.l1 = 0.0 ... self.l2 = 0.0 ... self.lr_power = -0.5 ... self.var = Parameter(Tensor(np.array([[0.6, 0.4], ... [0.1, 0.5]]).astype(np.float32)), name="var") ... self.accum = Parameter(Tensor(np.array([[0.6, 0.5], ... [0.2, 0.6]]).astype(np.float32)), name="accum") ... self.linear = Parameter(Tensor(np.array([[0.9, 0.1], ... [0.7, 0.8]]).astype(np.float32)), name="linear") ... ... def construct(self, grad): ... out = self.apply_ftrl(self.var, self.accum, self.linear, grad, self.lr, self.l1, self.l2, ... self.lr_power) ... return out ... >>> net = ApplyFtrlNet() >>> input_x = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32)) >>> output = net(input_x) >>> print(net.var.asnumpy()) [[ 0.0390525 0.11492836] [ 0.00066425 0.15075898]]
-
class
tinyms.primitives.
ApplyGradientDescent
[source]¶ Updates var by subtracting alpha * delta from it.
\[var = var - \alpha * \delta\]where \(\alpha\) represents alpha, \(\delta\) represents delta.
Inputs of var and delta 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.
- Inputs:
var (Parameter) - Variable tensor to be updated. With float32 or float16 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
alpha (Union[Number, Tensor]) - Scaling factor, must be a scalar. With float32 or float16 data type.
delta (Tensor) - A tensor for the change, has the same shape and data type as var.
- Outputs:
Tensor, represents the updated var.
- Raises:
TypeError – If dtype of var or alpha is neither float16 nor float32.
TypeError – If delta is not a Tensor.
TypeError – If alpha is neither a Number nor a Tensor.
RuntimeError – If the data type of var and delta conversion of Parameter is not supported.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_gradient_descent = ops.ApplyGradientDescent() ... self.var = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="var") ... self.alpha = 0.001 ... def construct(self, delta): ... out = self.apply_gradient_descent(self.var, self.alpha, delta) ... return out ... >>> net = Net() >>> delta = Tensor(np.array([[0.1, 0.1], [0.1, 0.1]]).astype(np.float32)) >>> output = net(delta) >>> print(output) [[0.9999 0.9999] [0.9999 0.9999]]
-
class
tinyms.primitives.
ApplyKerasMomentum
(use_locking=False, use_nesterov=False)[source]¶ Update var according to the momentum scheme.
\[\begin{split}\begin{array}{ll} \\ accum = accum * momentum - grad * lr \\ var = \begin{cases} var + accum * momentum - grad * lr, &\text{if use_nesterov} \\ var + accum, &\text{else} \end{cases} \end{array}\end{split}\]Refer to the paper On the importance of initialization and momentum in deep learning for more details.
Inputs of var, accum and grad 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 relatively highest priority data type. RuntimeError exception will be thrown when the data type conversion of Parameter is required.
- Parameters:
use_locking (bool) – If True, updating of the var and accum tensors will be protected by a lock; Otherwise the behavior is undefined, but may exhibit less contention. Default: False.
use_nesterov (bool) – If True, the tensor passed to compute grad will be var + momentum * accum, so in the end, the var you get is actually var + momentum * accum. Default: False.
- Inputs:
var (Parameter) - Variable to be updated. With float16 or float32 data type.
accum (Parameter) - Must have the same shape and type as var. With float16 or float32 data type.
lr (Union[Number, Tensor]) - Scaling factor. Must be a scalar. With float16 or float32 data type.
grad (Tensor) - The gradient. Must have the same shape and type as var. With float16 or float32 data type.
momentum (Union[Number, Tensor]) - Momentum. Must be a scalar. With float16 or float32 data type.
- Outputs:
Tuple of 2 Tensors, the updated parameters.
var (Tensor) - The same shape and data type as var.
accum (Tensor) - The same shape and data type as accum.
- Raises:
TypeError – If the use_locking or use_nesterov is not a bool.
TypeError – If var or accum is not a Parameter.
TypeError – If lr is neither a Number nor a Tensor.
TypeError – If grad is not a Tensor.
TypeError – If momentum is neither a Number nor a Tensor.
TypeError – If dtype of var, accum, lr, grad, momentum is neither float16 nor float32.
ValueError – If accum or grad doesn’t have the same shape as var.
ValueError – If the shape size of lr, momentum is not 0.
- Supported Platforms:
Ascend
Examples
>>> class ApplyKerasMomentumNet(nn.Cell): ... def __init__(self, use_locking=False, use_nesterov=False): ... super(ApplyKerasMomentumNet, self).__init__() ... self.apply_keras_momentum = P.ApplyKerasMomentum(use_locking, use_nesterov) ... self.var = Parameter(Tensor(np.array([[0.2, 0.3], [0.1, 0.4]]).astype(np.float32)), name="var") ... self.accum = Parameter(Tensor(np.array([[0.2, 0.3], [0.1, 0.4]]).astype(np.float32)), name="accum") ... def construct(self, lr, grad, momentum): ... out = self.apply_keras_momentum(self.var, self.accum, lr, grad, momentum) ... return out ... >>> net = ApplyKerasMomentumNet() >>> lr = Tensor(0.001, mstype.float32) >>> grad = Tensor(np.array([[0.3, 0.2], [0.4, 0.1]]).astype(np.float32)) >>> momentum = Tensor(0.99, mstype.float32) >>> output = net(lr, grad, momentum) >>> print(output) (Tensor(shape=[2, 2], dtype=Float32, value= [[ 3.97700012e-01, 5.96800029e-01], [ 1.98599994e-01, 7.95899987e-01]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 1.97699994e-01, 2.96800017e-01], [ 9.86000001e-02, 3.95900011e-01]]))
-
class
tinyms.primitives.
ApplyMomentum
(use_nesterov=False, use_locking=False, gradient_scale=1.0)[source]¶ Optimizer that implements the Momentum algorithm.
Refer to the paper On the importance of initialization and momentum in deep learning for more details.
Inputs of variable, accumulation and gradient 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.
Refer to
mindspore.nn.Momentum
for more details about the formula and usage.- Parameters:
- Inputs:
variable (Parameter) - Weights to be updated. Data type must be float64, int64, float, float16, int16, int32, int8, uint16, uint32, uint64, uint8, complex64, complex128.
accumulation (Parameter) - Accumulated gradient value by moment weight, has the same data type with variable.
learning_rate (Union[Number, Tensor]) - The learning rate value, must be a float64, int64, float, float16, int16, int32, int8, uint16, uint32, uint64, uint8, complex64, complex128 number or a scalar tensor with float64, int64, float, float16, int16, int32, int8, uint16, uint32, uint64, uint8, complex64, complex128 data type.
gradient (Tensor) - Gradient, has the same data type as variable.
momentum (Union[Number, Tensor]) - Momentum, must be a float64, int64, float, float16, int16, int32, int8, uint16, uint32, uint64, uint8, complex64, complex128 number or a scalar tensor with float64, int64, float, float16, int16, int32, int8, uint16, uint32, uint64, uint8, complex64, complex128 data type.
- Outputs:
Tensor, parameters to be updated.
- Raises:
TypeError – If the use_locking or use_nesterov is not a bool or gradient_scale is not a float.
RuntimeError – If the data type of var, accum and grad conversion of Parameter is not supported.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_momentum = ops.ApplyMomentum() ... self.variable = Parameter(Tensor(np.array([[0.6, 0.4], ... [0.1, 0.5]]).astype(np.float32)), name="variable") ... self.accumulate = Parameter(Tensor(np.array([[0.6, 0.5], ... [0.2, 0.6]]).astype(np.float32)), name="accumulate") ... def construct(self, lr, grad, moment): ... out = self.apply_momentum(self.variable, self.accumulate, lr, grad, moment) ... return out >>> net = Net() >>> lr = Tensor(0.1, mindspore.float32) >>> moment = Tensor(0.9, mindspore.float32) >>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32)) >>> output = net(lr, grad, moment) >>> print(output) [[0.51600003 0.285 ] [0.072 0.366 ]]
-
class
tinyms.primitives.
ApplyPowerSign
[source]¶ Updates relevant entries according to the AddSign algorithm.
The AddSign algorithm was proposed in Neural Optimizer Search with Reinforcement Learning.
\[\begin{split}\begin{array}{ll} \\ m_{t+1} = \beta * m_{t} + (1 - \beta) * g \\ \text{update} = \exp(\text{logbase} * \text{sign_decay} * sign(g) * sign(m)) * g \\ var = var - lr_{t+1} * \text{update} \end{array}\end{split}\]\(t\) represents updating step while \(m\) represents the 1st moment vector, \(m_{t}\) is the last moment of \(m_{t+1}\), \(lr\) represents scaling factor lr, \(g\) represents grad, \(\beta\) represents beta.
All of inputs comply with the implicit type conversion rules to make the data types consistent. If lr, logbase, sign_decay or beta is a number, the number is automatically converted to Tensor, and the data type is consistent with the Tensor data type involved in the operation. If inputs are tensors and have different data types, the lower priority data type will be converted to the relatively highest priority data type.
Note
On Ascend, input data type of float64 is currently not supported.
- Inputs:
var (Parameter) - Variable tensor to be updated. With float64, float32 or float16 data type. If data type of var is float16, all inputs must have the same data type as var. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
m (Parameter) - Variable tensor to be updated, has the same shape and data type as var.
lr (Union[Number, Tensor]) - The learning rate value, should be a scalar or Tensor with float64, float32 or float16 data type.
logbase (Union[Number, Tensor]) - Should be a scalar or Tensor with float64, float32 or float16 data type.
sign_decay (Union[Number, Tensor]) - Should be a scalar or Tensor with float64, float32 or float16 data type.
beta (Union[Number, Tensor]) - The exponential decay rate, should be a scalar or Tensor with float64, float32 or float16 data type.
grad (Tensor) - A tensor of the same shape and data type as var, for the gradient.
- Outputs:
Tuple of 2 Tensors, the updated parameters.
var (Tensor) - The same shape and data type as var.
m (Tensor) - The same shape and data type as m.
- Raises:
TypeError – If dtype of var, lr, logbase, sign_decay, beta or grad is not one of float16,
float32 or float64. –
TypeError – If lr, logbase, sign_decay or beta is neither a Number nor a Tensor.
TypeError – If grad is not a Tensor.
RuntimeError – If the data type of lr, logbase, sign_decay and grad conversion of Parameter is not supported.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_power_sign = ops.ApplyPowerSign() ... self.var = Parameter(Tensor(np.array([[0.6, 0.4], ... [0.1, 0.5]]).astype(np.float32)), name="var") ... self.m = Parameter(Tensor(np.array([[0.6, 0.5], ... [0.2, 0.6]]).astype(np.float32)), name="m") ... self.lr = 0.001 ... self.logbase = np.e ... self.sign_decay = 0.99 ... self.beta = 0.9 ... def construct(self, grad): ... out = self.apply_power_sign(self.var, self.m, self.lr, self.logbase, ... self.sign_decay, self.beta, grad) ... return out ... >>> net = Net() >>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32)) >>> output = net(grad) >>> print(output) (Tensor(shape=[2, 2], dtype=Float32, value= [[ 5.95575690e-01, 3.89676481e-01], [ 9.85252112e-02, 4.88201708e-01]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 5.70000052e-01, 5.19999981e-01], [ 1.89999998e-01, 6.20000064e-01]]))
-
class
tinyms.primitives.
ApplyProximalAdagrad
(use_locking=False)[source]¶ Updates relevant entries according to the proximal adagrad algorithm. The proximal adagrad algorithm was proposed in Efficient Learning using Forward-Backward Splitting.
\[\begin{split}\begin{array}{ll} \\ accum += grad * grad \\ \text{prox_v} = var - lr * grad * \frac{1}{\sqrt{accum}} \\ var = \frac{sign(\text{prox_v})}{1 + lr * l2} * \max(\left| \text{prox_v} \right| - lr * l1, 0) \end{array}\end{split}\]Inputs of var, accum and grad 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.
- Parameters:
use_locking (bool) – If true, the var and accumulation tensors will be protected from being updated. Default: False.
- Inputs:
var (Parameter) - Variable to be updated. The data type must be float16 or float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
accum (Parameter) - Accumulation to be updated, must have the same shape and dtype as var.
lr (Union[Number, Tensor]) - The learning rate value, must be a scalar. The data type must be float16 or float32.
l1 (Union[Number, Tensor]) - l1 regularization strength, must be a scalar. The data type must be float16 or float32.
l2 (Union[Number, Tensor]) - l2 regularization strength, must be a scalar. The data type must be float16 or float32.
grad (Tensor) - Gradient with the same shape and dtype as var.
- Outputs:
Tuple of 2 Tensors, the updated parameters.
var (Tensor) - The same shape and data type as var.
accum (Tensor) - The same shape and data type as accum.
- Raises:
TypeError – If use_blocking is not a bool.
TypeError – If dtype of var, lr, l1 or l2 is neither float16 nor float32.
TypeError – If lr, l1 or l2 is neither a Number nor a Tensor.
TypeError – If grad is not a Tensor.
RuntimeError – If the data type of var, accum and grad conversion of Parameter is not supported.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_proximal_adagrad = ops.ApplyProximalAdagrad() ... self.var = Parameter(Tensor(np.array([[0.6, 0.4], ... [0.1, 0.5]]).astype(np.float32)), name="var") ... self.accum = Parameter(Tensor(np.array([[0.6, 0.5], ... [0.2, 0.6]]).astype(np.float32)), name="accum") ... self.lr = 0.01 ... self.l1 = 0.0 ... self.l2 = 0.0 ... def construct(self, grad): ... out = self.apply_proximal_adagrad(self.var, self.accum, self.lr, self.l1, self.l2, grad) ... return out ... >>> net = Net() >>> grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32)) >>> output = net(grad) >>> print(output) (Tensor(shape=[2, 2], dtype=Float32, value= [[ 5.96388459e-01, 3.92964751e-01], [ 9.78178233e-02, 4.92815793e-01]]), Tensor(shape=[2, 2], dtype=Float32, value= [[ 6.90000057e-01, 9.90000010e-01], [ 2.10000008e-01, 1.24000001e+00]]))
-
class
tinyms.primitives.
ApplyProximalGradientDescent
[source]¶ Updates relevant entries according to the FOBOS(Forward Backward Splitting) algorithm. Refer to the paper Efficient Learning using Forward-Backward Splitting for more details.
\[\begin{split}\begin{array}{ll} \\ \text{prox_v} = var - \alpha * \delta \\ var = \frac{sign(\text{prox_v})}{1 + \alpha * l2} * \max(\left| \text{prox_v} \right| - \alpha * l1, 0) \end{array}\end{split}\]where \(\alpha\) represents alpha, \(\delta\) represents delta.
Inputs of var and delta 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.
- Inputs:
var (Parameter) - Variable tensor to be updated. With float32 or float16 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
alpha (Union[Number, Tensor]) - Scaling factor, must be a scalar. With float32 or float16 data type.
l1 (Union[Number, Tensor]) - l1 regularization strength, must be a scalar. With float32 or float16 data type.
l2 (Union[Number, Tensor]) - l2 regularization strength, must be a scalar. With float32 or float16 data type.
delta (Tensor) - A tensor for the change.
- Outputs:
Tensor, represents the updated var.
- Raises:
TypeError – If dtype of var, alpha, l1 or l2 is neither float16 nor float32.
TypeError – If alpha, l1 or l2 is neither a Number nor a Tensor.
TypeError – If delta is not a Tensor.
RuntimeError – If the data type of var, and delta conversion of Parameter is not supported.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_proximal_gradient_descent = ops.ApplyProximalGradientDescent() ... self.var = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="var") ... self.alpha = 0.001 ... self.l1 = 0.1 ... self.l2 = 0.1 ... def construct(self, delta): ... out = self.apply_proximal_gradient_descent(self.var, self.alpha, self.l1, self.l2, delta) ... return out ... >>> net = Net() >>> delta = Tensor(np.array([[0.1, 0.1], [0.1, 0.1]]).astype(np.float32)) >>> output = net(delta) >>> print(output) [[0.99969995 0.99969995] [0.99969995 0.99969995]]
-
class
tinyms.primitives.
ApplyRMSProp
(use_locking=False)[source]¶ Optimizer that implements the Root Mean Square prop(RMSProp) algorithm. Please refer to the usage in source code of
mindspore.nn.RMSProp
.The updating formulas of ApplyRMSProp algorithm are as follows,
\[\begin{split}\begin{array}{ll} \\ s_{t+1} = \rho s_{t} + (1 - \rho)(\nabla Q_{i}(w))^2 \\ m_{t+1} = \beta m_{t} + \frac{\eta} {\sqrt{s_{t+1} + \epsilon}} \nabla Q_{i}(w) \\ w = w - m_{t+1} \end{array}\end{split}\]where \(w\) represents var, which will be updated. \(s_{t+1}\) represents mean_square, \(s_{t}\) is the last moment of \(s_{t+1}\), \(m_{t+1}\) represents moment, \(m_{t}\) is the last moment of \(m_{t+1}\). \(\rho\) represents decay. \(\beta\) is the momentum term, represents momentum. \(\epsilon\) is a smoothing term to avoid division by zero, represents epsilon. \(\eta\) represents learning_rate. \(\nabla Q_{i}(w)\) represents grad.
Warning
Note that in dense implementation of this algorithm, “mean_square” and “moment” will update even if “grad” is 0, but in this sparse implementation, “mean_square” and “moment” will not update in iterations during which “grad” is 0.
- Parameters:
use_locking (bool) – Whether to enable a lock to protect the variable and accumulation tensors from being updated. Default: False.
- Inputs:
var (Parameter) - Weights to be updated.
mean_square (Tensor) - Mean square gradients, must be the same type as var.
moment (Tensor) - Delta of var, must be the same type as var.
learning_rate (Union[Number, Tensor]) - Learning rate. Must be a float number or a scalar tensor with float16 or float32 data type.
grad (Tensor) - Gradient, must be the same type as var.
decay (float) - Decay rate. Only constant value is allowed.
momentum (float) - Momentum. Only constant value is allowed.
epsilon (float) - Ridge term. Only constant value is allowed.
- Outputs:
Tensor, parameters to be updated.
- Raises:
TypeError – If use_locking is not a bool.
TypeError – If var, mean_square, moment or decay is not a Tensor.
TypeError – If learning_rate is neither a Number nor a Tensor.
TypeError – If dtype of decay, momentum or epsilon is not float.
TypeError – If dtype of learning_rate is neither float16 nor float32.
ValueError – If decay, momentum or epsilon is not a constant value.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.apply_rms_prop = ops.ApplyRMSProp() ... self.var = Parameter(Tensor(np.ones([2, 2]).astype(np.float32)), name="var") ... ... def construct(self, mean_square, moment, grad, decay, momentum, epsilon, lr): ... out = self.apply_rms_prop(self.var, mean_square, moment, lr, grad, decay, momentum, epsilon) ... return out ... >>> net = Net() >>> mean_square = Tensor(np.ones([2, 2]).astype(np.float32)) >>> moment = Tensor(np.ones([2, 2]).astype(np.float32)) >>> grad = Tensor(np.ones([2, 2]).astype(np.float32)) >>> output = net(mean_square, moment, grad, 0.0, 1e-10, 0.001, 0.01) >>> print(net.var.asnumpy()) [[0.990005 0.990005] [0.990005 0.990005]]
-
class
tinyms.primitives.
ApproximateEqual
(tolerance=1e-05)[source]¶ Returns True if abs(x-y) is smaller than tolerance element-wise, otherwise False.
\[\begin{split}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}\end{split}\]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.
- Parameters:
tolerance (float) – The maximum deviation that two elements can be considered equal. Default: 1e-05.
- Inputs:
x (Tensor) - A tensor. Must be one of the following types: float32, float16. \((N,*)\) where \(*\) 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.
- Outputs:
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
>>> x = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> y = Tensor(np.array([2, 3, 6]), mindspore.float32) >>> approximate_equal = ops.ApproximateEqual(2.) >>> output = approximate_equal(x, y) >>> print(output) [ True True False]
-
class
tinyms.primitives.
ArgMaxWithValue
(axis=0, keep_dims=False)[source]¶ Calculates the maximum value along with the given axis for the input tensor, and returns the maximum values and indices.
Note
In auto_parallel and semi_auto_parallel mode, the first output index can not be used.
Warning
If there are multiple maximum values, the index of the first maximum value is used.
The value range of “axis” is [-dims, dims - 1]. “dims” is the dimension length of “x”.
Also see: func: mindspore.ops.max.
- Parameters:
- Inputs:
x (Tensor) - The input tensor, can be any dimension. Set the shape of input tensor as \((x_1, x_2, ..., x_N)\).
- Outputs:
tuple (Tensor), tuple of 2 tensors, containing the corresponding index and the maximum value of the input tensor.
index (Tensor) - The index for the maximum value of the input tensor, with dtype int32. If keep_dims is true, the shape of output tensors is \((x_1, x_2, ..., x_{axis-1}, 1, x_{axis+1}, ..., x_N)\). Otherwise, the shape is \((x_1, x_2, ..., x_{axis-1}, x_{axis+1}, ..., x_N)\) .
values (Tensor) - The maximum value of input tensor, with the same shape as index, and same dtype as x.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.array([0.0, 0.4, 0.6, 0.7, 0.1]), mindspore.float32) >>> index, output = ops.ArgMaxWithValue()(input_x) >>> print(index, output) 3 0.7 >>> index, output = ops.ArgMaxWithValue(keep_dims=True)(input_x) >>> print(index, output) [3] [0.7]
-
class
tinyms.primitives.
ArgMinWithValue
(axis=0, keep_dims=False)[source]¶ Calculates the minimum value along with the given axis for the input tensor, and returns the minimum values and indices.
Note
In auto_parallel and semi_auto_parallel mode, the first output index can not be used.
Warning
If there are multiple minimum values, the index of the first minimum value is used.
The value range of “axis” is [-dims, dims - 1]. “dims” is the dimension length of “x”.
Also see: func: mindspore.ops.min.
- Parameters:
- Inputs:
x (Tensor) - The input tensor, can be any dimension. Set the shape of input tensor as \((x_1, x_2, ..., x_N)\) .Complex tensor is not supported.
- Outputs:
tuple (Tensor), tuple of 2 tensors, containing the corresponding index and the minimum value of the input tensor.
index (Tensor) - The index for the minimum value of the input tensor, with dtype int32. If keep_dims is true, the shape of output tensors is \((x_1, x_2, ..., x_{axis-1}, 1, x_{axis+1}, ..., x_N)\). Otherwise, the shape is \((x_1, x_2, ..., x_{axis-1}, x_{axis+1}, ..., x_N)\) .
values (Tensor) - The minimum value of input tensor, with the same shape as index, and same dtype as x.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([0.0, 0.4, 0.6, 0.7, 0.1]), mindspore.float32) >>> index, output = ops.ArgMinWithValue()(x) >>> print(index, output) 0 0.0 >>> index, output = ops.ArgMinWithValue(keep_dims=True)(x) >>> print(index, output) [0] [0.0]
-
class
tinyms.primitives.
Argmax
(axis=-1, output_type=mindspore.int32)[source]¶ Returns the indices of the maximum value of a tensor across the axis.
Refer to
mindspore.ops.argmax()
for more details.- Parameters:
axis (int) – Axis where the Argmax operation applies to. Default: -1.
output_type (
mindspore.dtype
) – An optional data type of mindspore.dtype.int32. Default: mindspore.dtype.int32.
- Inputs:
input_x (Tensor) - Input tensor. \((N,*)\) where \(*\) means, any number of additional dimensions. Support data type list as follows:
Ascend: Float16, Float32.
GPU: Float16, Float32.
CPU: Float16, Float32, Float64.
- Outputs:
Tensor, indices of the max value of input tensor across the axis.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.array([[1, 20, 5], [67, 8, 9], [130, 24, 15]]).astype(np.float32)) >>> output = ops.Argmax(output_type=mindspore.int32)(input_x) >>> print(output) [1 0 0]
-
class
tinyms.primitives.
Argmin
(axis=-1, output_type=mindspore.int32)[source]¶ Returns the indices of the minimum value of a tensor across the axis.
If the shape of input tensor is \((x_1, ..., x_N)\), the shape of the output tensor is \((x_1, ..., x_{axis-1}, x_{axis+1}, ..., x_N)\).
- Parameters:
axis (int) – Axis where the Argmin operation applies to. Default: -1.
output_type (
mindspore.dtype
) – An optional data type of mindspore.dtype.int32 and mindspore.dtype.int64. Default: mindspore.dtype.int32.
- Inputs:
input_x (Tensor) - Input tensor. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
Ascend: Float16, Float32, Float64, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64.
- Outputs:
Tensor, whose dtype is determined by output_type.
- 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
-
class
tinyms.primitives.
Asin
[source]¶ Computes arcsine of input tensors element-wise.
Refer to
mindspore.ops.asin()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> asin = ops.Asin() >>> x = Tensor(np.array([0.74, 0.04, 0.30, 0.56]), mindspore.float32) >>> output = asin(x) >>> print(output) [0.8330704 0.04001067 0.30469266 0.5943858 ]
-
class
tinyms.primitives.
Asinh
[source]¶ Computes inverse hyperbolic sine of the input element-wise.
Refer to
mindspore.ops.asinh()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> asinh = ops.Asinh() >>> x = Tensor(np.array([-5.0, 1.5, 3.0, 100.0]), mindspore.float32) >>> output = asinh(x) >>> print(output) [-2.3124382 1.1947632 1.8184465 5.298342 ]
-
class
tinyms.primitives.
Assert
(summarize=3)[source]¶ Asserts whether the given condition is True. If input condition is identified to be false, print a list of the tensor in data.
- Parameters:
summarize (int, optional) – The number of entries to be printed in each tensor while the given condition is identified to be False. Default: 3.
- Inputs:
condition (Union[Tensor[bool], bool]) - The condition to be identified.
input_data (Union[tuple[Tensor], list[Tensor]]) - The tensors to be printed out when the condition is false.
- Raises:
- Supported Platforms:
GPU
CPU
Examples
>>> a = Tensor(np.array([-1, 0, 1, 2, 3]).astype(np.int32)) >>> b = Tensor(np.array([1, 2, 3, 4, 5]).astype(np.float32)) >>> assert1 = ops.Assert(3) >>> assert1(False, [a, b]) For 'Assert' condition is false. input data: [-1 0 1] input data: [1 2 3] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "mindspore/ops/primitive.py", line 294, in __call__ return _run_op(self, self.name, args) File "mindspore/common/api.py", line 99, in wrapper results = fn(*arg, **kwargs) File "mindspore/ops/primitive.py", line 743, in _run_op output = real_run_op(obj, op_name, args) RuntimeError: assert failed
-
class
tinyms.primitives.
Assign
[source]¶ Assigns Parameter with a value.
Refer to
mindspore.ops.assign()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> value = Tensor([2.0], mindspore.float32) >>> variable = mindspore.Parameter(Tensor([1.0], mindspore.float32), name="variable") >>> assign = ops.Assign() >>> x = assign(variable, value) >>> print(variable.asnumpy()) [2.]
-
class
tinyms.primitives.
AssignAdd
[source]¶ Updates a Parameter by adding a value to it.
Refer to
mindspore.ops.assign_add()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.AssignAdd = ops.AssignAdd() ... self.variable = mindspore.Parameter(initializer(1, [1], mindspore.int64), name="global_step") ... ... def construct(self, x): ... self.AssignAdd(self.variable, x) ... return self.variable ... >>> net = Net() >>> value = Tensor(np.ones([1]).astype(np.int64)*100) >>> output = net(value) >>> print(net.variable.asnumpy()) [101]
-
class
tinyms.primitives.
AssignSub
[source]¶ Updates a Parameter by subtracting a value from it.
Refer to
mindspore.ops.assign_sub()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.AssignSub = ops.AssignSub() ... self.variable = mindspore.Parameter(initializer(1, [1], mindspore.int32), name="global_step") ... ... def construct(self, x): ... self.AssignSub(self.variable, x) ... return self.variable ... >>> net = Net() >>> value = Tensor(np.ones([1]).astype(np.int32)*100) >>> output = net(value) >>> print(net.variable.asnumpy()) [-99]
-
class
tinyms.primitives.
Atan
[source]¶ Computes the trigonometric inverse tangent of the input element-wise.
Refer to
mindspore.ops.atan()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([1.0, 0.0]), mindspore.float32) >>> atan = ops.Atan() >>> output = atan(x) >>> print(output) [0.7853982 0. ]
-
class
tinyms.primitives.
Atan2
[source]¶ Returns arctangent of x/y element-wise.
Refer to
mindspore.ops.atan2()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([0, 1]), mindspore.float32) >>> y = Tensor(np.array([1, 1]), mindspore.float32) >>> atan2 = ops.Atan2() >>> output = atan2(x, y) >>> print(output) [0. 0.7853982]
-
class
tinyms.primitives.
Atanh
[source]¶ Computes inverse hyperbolic tangent of the input element-wise.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.atanh()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([0, -0.5]), mindspore.float32) >>> atanh = ops.Atanh() >>> output = atanh(x) >>> print(output) [ 0. -0.54930615]
-
class
tinyms.primitives.
AvgPool
(kernel_size=1, strides=1, pad_mode='valid', data_format='NCHW')[source]¶ Average pooling operation.
Refer to
mindspore.ops.avg_pool2d()
for more details.- Parameters:
kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the average value, is an int number that represents height and width of the kernel, or a tuple of two int numbers that represent height and width respectively. Default: 1.
strides (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 1.
pad_mode (str) –
The optional value for pad mode, is ‘same’ or ‘valid’. Default: ‘valid’.
same: The height and width of the output are the same as the input divided by ‘strides’ and rounded up.
valid: Returns the output of the valid calculation without filling. Redundant pixels that do not satisfy the calculation will be discarded.
data_format (str) – The format of input and output data. It should be ‘NHWC’ or ‘NCHW’. Default: ‘NCHW’.
- Inputs:
x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).
- Outputs:
Tensor, with shape \((N, C_{out}, H_{out}, W_{out})\).
- Raises:
TypeError – If kernel_size or strides is neither int nor tuple.
ValueError – If kernel_size or strides is less than 1.
ValueError – If pad_mode is neither ‘valid’ nor ‘same’ with not case sensitive.
ValueError – If data_format is neither ‘NCHW’ nor ‘NHWC’.
ValueError – If length of shape of x is not equal to 4.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.avgpool_op = ops.AvgPool(pad_mode="VALID", kernel_size=2, strides=1) ... ... def construct(self, x): ... result = self.avgpool_op(x) ... return result ... >>> x = Tensor(np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4), mindspore.float32) >>> net = Net() >>> output = net(x) >>> print(output) [[[[ 2.5 3.5 4.5] [ 6.5 7.5 8.5]] [[14.5 15.5 16.5] [18.5 19.5 20.5]] [[26.5 27.5 28.5] [30.5 31.5 32.5]]]]
-
class
tinyms.primitives.
AvgPool3D
(kernel_size=1, strides=1, pad_mode='valid', pad=0, ceil_mode=False, count_include_pad=True, divisor_override=0, data_format='NCDHW')[source]¶ 3D Average pooling operation.
Typically the input is of shape \((N, C, D_{in}, H_{in}, W_{in})\), AvgPool3D outputs regional average in the \((D_{in}, H_{in}, W_{in})\)-dimension. Given kernel size \(ks = (d_{ker}, h_{ker}, w_{ker})\) and stride \(s = (s_0, s_1, s_2)\), the operation is as follows.
Warning
“kernel_size” is in the range [1, 255]. “strides” is in the range [1, 63].
\[\text{output}(N_i, C_j, d, h, w) = \frac{1}{d_{ker} * h_{ker} * w_{ker}} \sum_{l=0}^{d_{ker}-1} \sum_{m=0}^{h_{ker}-1} \sum_{n=0}^{w_{ker}-1} \text{input}(N_i, C_j, s_0 \times d + l, s_1 \times h + m, s_2 \times w + n)\]- Parameters:
kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the average value, is an int number that represents depth, height and width are both kernel_size, or a tuple of three int numbers that represent depth, height and width respectively. Default: 1.
strides (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the depth, height and width of movement are both strides, or a tuple of three int numbers that represent depth, height and width of movement respectively. Default: 1.
pad_mode (str) –
The optional value for pad mode, is “same”, “valid”, “pad”. Default: “valid”.
same: Adopts the way of completion. The depth, height and width of the output will be the same as the input. The total number of padding will be calculated in depth, horizontal and vertical directions and evenly distributed to head and tail, top and bottom, left and right if possible. Otherwise, the last extra padding will be done from the tail, bottom and the right side. If this mode is set, pad must be 0.
valid: Adopts the way of discarding. The possible largest depth, height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, pad must be 0.
pad: Implicit paddings on both sides of the input in depth, height, width. The number of pad will be padded to the input Tensor borders. pad must be greater than or equal to 0.
pad (Union(int, tuple[int], list[int])) – The pad value to be filled. Default: 0. If pad is an integer, the paddings of head, tail, top, bottom, left and right are the same, equal to pad. If pad is a tuple of six integers, the padding of head, tail, top, bottom, left and right equal to pad[0], pad[1], pad[2], pad[3], pad[4] and pad[5] correspondingly.
ceil_mode (bool) – If True, ceil instead of floor to compute the output shape. Default: False.
count_include_pad (bool) – If True, averaging calculation will include the zero-padding. Default: True.
divisor_override (int) – If specified, it will be used as divisor in the averaging calculation, otherwise kernel_size will be used. Default: 0.
data_format (str) – The optional value for data format. Currently only support ‘NCDHW’. Default: ‘NCDHW’.
- Inputs:
x (Tensor) - Tensor of shape \((N, C, D_{in}, H_{in}, W_{in})\). Currently support float16 and float32 data type.
- Outputs:
Tensor, with shape \((N, C, D_{out}, H_{out}, W_{out})\). Has the same data type with x.
- Raises:
TypeError – If kernel_size, strides or pad is neither an int not a tuple.
TypeError – If ceil_mode or count_include_pad is not a bool.
TypeError – If pad_mode or data_format is not a string.
TypeError – If divisor_override is not an int.
ValueError – If numbers in kernel_size or strides are not positive.
ValueError – If kernel_size or strides is a tuple whose length is not equal to 3.
ValueError – If pad_mode is not one of ‘same’, ‘valid’ or ‘pad’.
ValueError – If pad is a tuple whose length is not equal to 6.
ValueError – If element of pad is less than 0.
ValueError – If pad_mode is not equal to ‘pad’ and pad is not equal to 0 or (0, 0, 0, 0, 0, 0).
ValueError – If data_format is not ‘NCDHW’.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.arange(1 * 2 * 2 * 2 * 3).reshape((1, 2, 2, 2, 3)), mindspore.float16) >>> avg_pool3d = ops.AvgPool3D(kernel_size=2, strides=1, pad_mode="valid") >>> output = avg_pool3d(x) >>> print(output) [[[[[ 5. 6.]]] [[[17. 18.]]]]]
-
class
tinyms.primitives.
BCEWithLogitsLoss
(reduction='mean')[source]¶ Adds sigmoid activation function to input logits, and uses the given logits to compute binary cross entropy between the logits and the label.
Sets input logits as \(X\), input label as \(Y\), input weight as \(W\), output as \(L\). Then,
\[\begin{split}\begin{array}{ll} \\ p_{ij} = sigmoid(X_{ij}) = \frac{1}{1 + e^{-X_{ij}}} \\ L_{ij} = -[Y_{ij}log(p_{ij}) + (1 - Y_{ij})log(1 - p_{ij})] \end{array}\end{split}\]\(i\) indicates the \(i^{th}\) sample, \(j\) indicates the category. Then,
\[\begin{split}\ell(x, y) = \begin{cases} L, & \text{if reduction} = \text{'none';}\\ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]\(\ell\) indicates the method of calculating the loss. There are three methods: the first method is to provide the loss value directly, the second method is to calculate the average value of all losses, and the third method is to calculate the sum of all losses.
This operator will multiply the output by the corresponding weight. The tensor weight assigns different weights to each piece of data in the batch, and the tensor pos_weight adds corresponding weights to the positive examples of each category.
In addition, it can trade off recall and precision by adding weights to positive examples. In the case of multi-label classification the loss can be described as:
\[\begin{split}\begin{array}{ll} \\ p_{ij,c} = sigmoid(X_{ij,c}) = \frac{1}{1 + e^{-X_{ij,c}}} \\ L_{ij,c} = -[P_{c}Y_{ij,c} * log(p_{ij,c}) + (1 - Y_{ij,c})log(1 - p_{ij,c})] \end{array}\end{split}\]where c is the class number (c>1 for multi-label binary classification, c=1 for single-label binary classification), n is the number of the sample in the batch and \(P_c\) is the weight of the positive answer for the class c. \(P_c>1\) increases the recall, \(P_c<1\) increases the precision.
- Parameters:
reduction (str) – Type of reduction to be applied to loss. The optional values are ‘mean’, ‘sum’, and ‘none’, not case sensitive. If ‘none’, do not perform reduction. Default: ‘mean’.
- Inputs:
logits (Tensor) - Input logits. Data type must be float16 or float32. Tensor of shape \((N, *)\) where \(*\) means, any number of additional dimensions.
label (Tensor) - Ground truth label, has the same shape as logits. Data type must be float16 or float32.
weight (Tensor) - A rescaling weight applied to the loss of each batch element. It can be broadcast to a tensor with shape of logits. Data type must be float16 or float32.
pos_weight (Tensor) - A weight of positive examples. Must be a vector with length equal to the number of classes. It can be broadcast to a tensor with shape of logits. Data type must be float16 or float32.
- Outputs:
Tensor or Scalar, if reduction is ‘none’, it’s a tensor with the same shape and type as input logits. Otherwise, the output is a scalar.
- Raises:
TypeError – If any input is not Tensor.
TypeError – If data type of any input is neither float16 nor float32.
TypeError – If data type of reduction is not string.
ValueError – If weight or pos_weight can not be broadcast to a tensor with shape of logits.
ValueError – If reduction is not one of ‘none’, ‘mean’ or ‘sum’.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> logits = Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]), mindspore.float32) >>> label = Tensor(np.array([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]), mindspore.float32) >>> weight = Tensor(np.array([1.0, 1.0, 1.0]), mindspore.float32) >>> pos_weight = Tensor(np.array([1.0, 1.0, 1.0]), mindspore.float32) >>> loss = ops.BCEWithLogitsLoss() >>> output = loss(logits, label, weight, pos_weight) >>> print(output) 0.3463612
-
class
tinyms.primitives.
BNTrainingReduce
(data_format='NCHW')[source]¶ The BNTrainingReduce interface is deprecated, please use the
mindspore.ops.BatchNorm
instead.- Supported Platforms:
Deprecated
-
class
tinyms.primitives.
BNTrainingUpdate
(isRef=True, epsilon=1e-05, factor=0.1, data_format='NCHW')[source]¶ The BNTrainingUpdate interface is deprecated, please use the
mindspore.ops.BatchNorm
instead.- Supported Platforms:
Deprecated
-
class
tinyms.primitives.
BartlettWindow
(periodic=True, dtype=mindspore.float32)[source]¶ Bartlett window function.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.bartlett_window()
for more details.- Parameters:
periodic (bool, optional) – If True, returns a window to be used as periodic function. If False, return a symmetric window. Default: True.
dtype (mindspore.dtype, optional) – The desired datatype of returned tensor. Only float16, float32 and float64 are allowed. Default: mstype.float32.
- Inputs:
window_length (Tensor) - The size of returned window, with data type int32, int64. The input data should be an integer with a value of [0, 1000000].
- Outputs:
A 1-D tensor of size window_length containing the window. Its datatype is set by the attr dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> window_length = Tensor(5, mstype.int32) >>> bartlett_window = ops.BartlettWindow(periodic=True, dtype=mstype.float32) >>> output = bartlett_window(window_length) >>> print(output) [0. 0.4 0.8 0.8 0.4]
-
class
tinyms.primitives.
BasicLSTMCell
(keep_prob=1.0, forget_bias=1.0, state_is_tuple=True, activation='tanh')[source]¶ It’s similar to operator
mindspore.ops.DynamicRNN
. BasicLSTMCell will be deprecated in the future. Please use DynamicRNN instead.- Supported Platforms:
Deprecated
-
class
tinyms.primitives.
BatchMatMul
(transpose_a=False, transpose_b=False)[source]¶ Computes matrix multiplication between two tensors by batch.
\[\text{output}[..., :, :] = \text{matrix}(x[..., :, :]) * \text{matrix}(y[..., :, :])\]The first input tensor must be not less than 3 and the second input must be not less than 2.
- Parameters:
- Inputs:
x (Tensor) - The first tensor to be multiplied. The shape of the tensor is \((*B, N, C)\), where \(*B\) represents the batch size which can be multidimensional, \(N\) and \(C\) are the size of the last two dimensions. If transpose_a is True, its shape must be \((*B, C, N)\).
y (Tensor) - The second tensor to be multiplied. The shape of the tensor is \((*B, C, M)\). If transpose_b is True, its shape must be \((*B, M, C)\).
- Outputs:
Tensor, the shape of the output tensor is \((*B, N, M)\).
- Raises:
TypeError – If transpose_a or transpose_b is not a bool.
ValueError – If length of shape of x is not equal to length of shape of y or length of shape of x is less than 3.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.ones(shape=[2, 4, 1, 3]), mindspore.float32) >>> y = Tensor(np.ones(shape=[2, 4, 3, 4]), mindspore.float32) >>> batmatmul = ops.BatchMatMul() >>> output = batmatmul(x, y) >>> print(output.shape) (2, 4, 1, 4) >>> x = Tensor(np.ones(shape=[2, 4, 3, 1]), mindspore.float32) >>> y = Tensor(np.ones(shape=[2, 4, 3, 4]), mindspore.float32) >>> batmatmul = ops.BatchMatMul(transpose_a=True) >>> output = batmatmul(x, y) >>> print(output.shape) (2, 4, 1, 4)
-
class
tinyms.primitives.
BatchNorm
(is_training=False, epsilon=1e-05, momentum=0.1, data_format='NCHW')[source]¶ Batch Normalization for input data and updated parameters.
Batch Normalization is widely used in convolutional neural networks. This operation applies Batch Normalization over inputs to avoid internal covariate shift as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. It rescales and recenters the features using a mini-batch of data and the learned parameters can be described in the following formula,
\[y = \frac{x - mean}{\sqrt{variance + \epsilon}} * \gamma + \beta\]where \(\gamma\) is scale, \(\beta\) is bias, \(\epsilon\) is epsilon, \(mean\) is the mean of \(x\), \(variance\) is the variance of \(x\).
Warning
If the operation is used for inference, and outputs “reserve_space_1” and “reserve_space_2” are available, then “reserve_space_1” has the same value as “mean” and “reserve_space_2” has the same value as “variance”.
For Ascend 310, the result accuracy fails to reach 1‰ due to the square root instruction.
- Parameters:
is_training (bool) – If is_training is True, mean and variance are computed during training. If is_training is False, they’re loaded from checkpoint during inference. Default: False.
epsilon (float) – A small value added for numerical stability. Default: 1e-5.
momentum (float) – The hyper parameter to compute moving average for running_mean and running_var (e.g. \(new\_running\_mean = (1 - momentum) * running\_mean + momentum * current\_mean\)). Momentum value must be [0, 1]. Default: 0.1.
data_format (str) – The optional value for data format, is ‘NHWC’ or ‘NCHW’, and the ‘NHWC’ format is only supported in GPU target. Default: “NCHW”.
- Inputs:
If is_training is False, inputs are Tensors.
input_x (Tensor) - Tensor of shape \((N, C)\), with float16 or float32 data type.
scale (Tensor) - Tensor of shape \((C,)\), with float16 or float32 data type.
bias (Tensor) - Tensor of shape \((C,)\), has the same data type with scale.
mean (Tensor) - Tensor of shape \((C,)\), has the same data type with scale.
variance (Tensor) - Tensor of shape \((C,)\), has the same data type with scale.
If is_training is True, scale, bias, mean and variance are Parameters.
input_x (Tensor) - Tensor of shape \((N, C)\), with float16 or float32 data type.
scale (Parameter) - Parameter of shape \((C,)\), with float16 or float32 data type.
bias (Parameter) - Parameter of shape \((C,)\), has the same data type with scale.
mean (Parameter) - Parameter of shape \((C,)\), has the same data type with scale.
variance (Parameter) - Parameter of shape \((C,)\), has the same data type with scale.
- Outputs:
Tuple of 5 Tensors, the normalized inputs and the updated parameters.
output_x (Tensor) - The same type and shape as the input_x. The shape is \((N, C)\).
batch_mean (Tensor) - Tensor of shape \((C,)\).
batch_variance (Tensor) - Tensor of shape \((C,)\).
reserve_space_1 (Tensor) - Tensor of shape \((C,)\).
reserve_space_2 (Tensor) - Tensor of shape \((C,)\).
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.ones([2, 2]), mindspore.float32) >>> scale = Tensor(np.ones([2]), mindspore.float32) >>> bias = Tensor(np.ones([2]), mindspore.float32) >>> mean = Tensor(np.ones([2]), mindspore.float32) >>> variance = Tensor(np.ones([2]), mindspore.float32) >>> batch_norm = ops.BatchNorm() >>> output = batch_norm(input_x, scale, bias, mean, variance) >>> print(output[0]) [[1. 1.] [1. 1.]]
-
class
tinyms.primitives.
BatchToSpace
(block_size, crops)[source]¶ Divides batch dimension with blocks and interleaves these blocks back into spatial dimensions.
This operation will divide batch dimension N into blocks with block_size, the output tensor’s N dimension is the corresponding number of blocks after division. The output tensor’s H, W dimension is product of original H, W dimension and block_size with given amount to crop from dimension, respectively.
- Parameters:
block_size (int) – The block size of division, has the value not less than 2.
crops (Union[list(int), tuple(int)]) – The crop value for H and W dimension, containing 2 subtraction lists. Each list contains 2 integers. All values must be not less than 0. crops[i] specifies the crop values for the spatial dimension i, which corresponds to the input dimension i+2. It is required that \(input\_shape[i+2]*block\_size > crops[i][0]+crops[i][1]\) .
- Inputs:
input_x (Tensor) - The input tensor. It must be a 4-D tensor, dimension 0 must be divisible by product of block_shape. The data type is float16 or float32.
- Outputs:
Tensor, the output tensor with the same type as input. Assume input shape is \((n, c, h, w)\) with block_size and crops. The output shape will be \((n', c', h', w')\), where
\(n' = n//(block\_size*block\_size)\)
\(c' = c\)
\(h' = h*block\_size-crops[0][0]-crops[0][1]\)
\(w' = w*block\_size-crops[1][0]-crops[1][1]\)
- Raises:
TypeError – If block_size or element of crops is not an int.
TypeError – If crops is neither list nor tuple.
ValueError – If block_size is less than 2.
- Supported Platforms:
Ascend
GPU
Examples
>>> block_size = 2 >>> crops = [[0, 0], [0, 0]] >>> batch_to_space = ops.BatchToSpace(block_size, crops) >>> input_x = Tensor(np.array([[[[1]]], [[[2]]], [[[3]]], [[[4]]]]), mindspore.float32) >>> output = batch_to_space(input_x) >>> print(output) [[[[1. 2.] [3. 4.]]]]
-
class
tinyms.primitives.
BatchToSpaceND
(block_shape, crops)[source]¶ ops.BatchToSpaceND is deprecated from version 2.0 and will be removed in a future version, use ops.batch_to_space_nd instead.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> block_size = 2 >>> crops = [[0, 0], [0, 0]] >>> batch_to_space = ops.BatchToSpaceND(block_size, crops) >>> input_x = Tensor(np.array([[[[1]]], [[[2]]], [[[3]]], [[[4]]]]), mindspore.float32) >>> output = batch_to_space(input_x) >>> print(output) [[[[1. 2.] [3. 4.]]]]
-
class
tinyms.primitives.
BatchToSpaceNDV2
[source]¶ Divides batch dimension with blocks and interleaves these blocks back into spatial dimensions.
Refer to
mindspore.ops.batch_to_space_nd()
for more details.- Supported Platforms:
Ascend
-
class
tinyms.primitives.
Bernoulli
(seed=-1, offset=0)[source]¶ Randomly set the elements of output to 0 or 1 with the probability of P which follows the Bernoulli distribution.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.bernoulli()
for more details.- Supported Platforms:
GPU
CPU
Examples
>>> input_x = Tensor([0.1, 0.2, 0.3], mindspore.float32) >>> bernoulli = ops.Bernoulli() >>> output = bernoulli(input_x, Tensor([1.0])) >>> print(output) [1. 1. 1.] >>> input_p = Tensor([0.0, 1.0, 1.0], mindspore.float32) >>> output = bernoulli(input_x, input_p) >>> print(output) [0. 1. 1.]
-
class
tinyms.primitives.
BesselI0
[source]¶ Computes BesselI0 of input element-wise.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.bessel_i0()
for more details.- Supported Platforms:
GPU
CPU
Examples
>>> bessel_i0 = ops.BesselI0() >>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) >>> output = bessel_i0(x) >>> print(output) [1.0144521 1.1797839 1.0241698 1.0020262]
-
class
tinyms.primitives.
BesselI0e
[source]¶ Computes BesselI0e of input element-wise.
The formula is defined as:
\[BesselI0e(x) = \exp(|x|) * bessel\_i0(x)\]where bessel_i0 is Bessel function of the first kind with 0 order.
- Inputs:
x (Tensor) - The input tensor. Data type must be float16, float32 or float64.
- Outputs:
Tensor, has the same shape as x.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> bessel_i0e = ops.BesselI0e() >>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) >>> output = bessel_i0e(x) >>> print(output) [0.7979961 0.5144438 0.75117415 0.9157829 ]
-
class
tinyms.primitives.
BesselI1
[source]¶ Computes BesselI1 of input element-wise.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.bessel_i1()
for more details.- Supported Platforms:
GPU
CPU
Examples
>>> bessel_i1 = ops.BesselI1() >>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) >>> output = bessel_i1(x) >>> print(output) [0.1208661 0.45177728 0.1568694 0.04504559]
-
class
tinyms.primitives.
BesselI1e
[source]¶ Computes BesselI1e of input element-wise.
The formula is defined as:
\[BesselI1e(x) = \exp(|x|) * bessel\_i1(x)\]where bessel_i1 is Bessel function of the first kind with 1 order.
- Inputs:
x (Tensor) - The input tensor. Data type must be float16 or float32, float64.
- Outputs:
Tensor, has the same shape as x.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> bessel_i1e = ops.BesselI1e() >>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) >>> output = bessel_i1e(x) >>> print(output) [0.09507662 0.19699717 0.11505538 0.04116856]
-
class
tinyms.primitives.
BesselJ0
[source]¶ Computes BesselJ0 of input element-wise.
Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
x (Tensor) - The input tensor. Data type must be float16, float32 or float64.
- Outputs:
Tensor, has the same shape as x.
- Raises:
TypeError – If x is not a Tensor of float16, float32 or float64.
- Supported Platforms:
GPU
CPU
Examples
>>> bessel_j0 = ops.BesselJ0() >>> x = Tensor(np.array([0.5, 1., 2., 4.]), mindspore.float32) >>> output = bessel_j0(x) >>> print(output) [0.93846981 0.76519769 0.22389078 -0.39714981]
-
class
tinyms.primitives.
BesselJ1
[source]¶ Computes BesselJ1 of input element-wise.
Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
x (Tensor) - The input tensor. Data type must be float16, float32 or float64.
- Outputs:
Tensor, has the same shape as x.
- Raises:
TypeError – If x is not a Tensor of float16, float32 or float64.
- Supported Platforms:
GPU
CPU
Examples
>>> bessel_j1 = ops.BesselJ1() >>> x = Tensor(np.array([0.5, 1., 2., 4.]), mindspore.float32) >>> output = bessel_j1(x) >>> print(output) [0.24226846, 0.44005059, 0.57672481, -0.06604333]
-
class
tinyms.primitives.
BesselK0
[source]¶ Computes BesselK0 of input element-wise.
Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
x (Tensor) - The input tensor. Data type must be float16, float32, float64.
- Outputs:
Tensor, has the same shape as x.
- Raises:
TypeError – If x is not a Tensor of float16, float32, float64.
- Supported Platforms:
GPU
CPU
Examples
>>> bessel_k0 = ops.BesselK0() >>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) >>> output = bessel_k0(x) >>> print(output) [1.579826 0.5402144 1.3424659 2.5310173]
-
class
tinyms.primitives.
BesselK0e
[source]¶ Computes BesselK0e of input element-wise.
Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
x (Tensor) - The input tensor. Data type must be float16, float32, float64.
- Outputs:
Tensor, has the same shape as x.
- Raises:
TypeError – If x is not a Tensor of float16, float32, float64.
- Supported Platforms:
GPU
CPU
Examples
>>> bessel_k0e = ops.BesselK0e() >>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) >>> output = bessel_k0e(x) >>> print(output) [2.0083523 1.2388839 1.8303517 2.769374 ]
-
class
tinyms.primitives.
BesselK1
[source]¶ Computes BesselK1 of input element-wise.
Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
x (Tensor) - The input tensor. Data type must be float16, float32, float64.
- Outputs:
Tensor, has the same shape as x.
- Raises:
TypeError – If x is not a Tensor of float16, float32, float64.
- Supported Platforms:
GPU
CPU
Examples
>>> bessel_k1 = ops.BesselK1() >>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) >>> output = bessel_k1(x) >>> print(output) [3.9190812 0.8143549 2.9440577 10.974864]
-
class
tinyms.primitives.
BesselK1e
[source]¶ Computes BesselK1e of input element-wise.
Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
x (Tensor) - The input tensor. Data type must be float16, float32, float64.
- Outputs:
Tensor, has the same shape as x.
- Raises:
TypeError – If x is not a Tensor of float16, float32, float64.
- Supported Platforms:
GPU
CPU
Examples
>>> bessel_k1e = ops.BesselK1e() >>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) >>> output = bessel_k1e(x) >>> print(output) [ 4.9821286 1.8675754 4.0140023 12.008413 ]
-
class
tinyms.primitives.
BesselY0
[source]¶ Computes BesselY0 of input element-wise.
Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
x (Tensor) - The input tensor. Data type must be float16, float32 or float64.
- Outputs:
Tensor, has the same shape as x.
- Raises:
TypeError – If x is not a Tensor of float16, float32, float64.
- Supported Platforms:
GPU
CPU
Examples
>>> bessel_y0 = ops.BesselY0() >>> x = Tensor(np.array([0.5, 1., 2., 4.]), mindspore.float32) >>> output = bessel_y0(x) >>> print(output) [-0.44451873 0.08825696 0.51037567 -0.01694074]
-
class
tinyms.primitives.
BesselY1
[source]¶ Computes BesselY1 of input element-wise.
Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
x (Tensor) - The input tensor. Data type must be float16, float32 or float64.
- Outputs:
Tensor, has the same shape as x.
- Raises:
TypeError – If x is not a Tensor of float16, float32, float64.
- Supported Platforms:
GPU
CPU
Examples
>>> bessel_y1 = ops.BesselY1() >>> x = Tensor(np.array([0.5, 1., 2., 4.]), mindspore.float32) >>> output = bessel_y1(x) >>> print(output) [-1.47147239 -0.78121282 -0.10703243 0.39792571]
-
class
tinyms.primitives.
Betainc
[source]¶ Calculates the regularized incomplete beta function \(I_{x}(a, b)\). It is defined as the ratio of the incomplete beta function to the complete beta function:
\[I_{x}(a, b)=\frac{B(x ; a, b)}{B(a, b)}\]where
\[B(x ; a, b)=\int_{0}^{x} t^{a-1}(1-t)^{b-1} dt\]is the incomplete beta function and
\[B(a, b) = \int_0^1 t^{a-1} (1-t)^{b-1} dt\]is the complete beta function.
- Inputs:
a (Tensor) - Peak location of beta distribution. A Tensor of types: float32, float64.
b (Tensor) - Spread of the beta distribution. A Tensor, must have the same dtype and shape as a .
x (Tensor) - Upper limit of integration of the incomplete beta function. A Tensor, must have the same dtype and shape as a .
- Outputs:
A Tensor, has the same dtype and shape as a .
- Raises:
TypeError – If dtype of a is not float32 nor float64.
TypeError – If either dtype of b and x is not the same as the a.
ValueError – If either shape of b and x is not the same as the a.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> a = Tensor(np.array([0.3, 0.1, 0.4]), mindspore.float32) >>> b = Tensor(np.array([0.4, 0.5, 0.9]), mindspore.float32) >>> x = Tensor(np.array([0.2, 0.6, 0.5]), mindspore.float32) >>> betainc = ops.Betainc() >>> print(betainc(a, b, x)) [0.41462693 0.8706035 0.7298298 ]
-
class
tinyms.primitives.
BiasAdd
(data_format='NCHW')[source]¶ Returns the sum of the input Tensor and the bias Tensor. Before adding, the bias Tensor will be broadcasted to be consistent with the shape of the input Tensor.
- Parameters:
data_format (str) – The format of input and output data. It should be ‘NHWC’, ‘NCHW’ or ‘NCDHW’. Default is ‘NCHW’.
- Inputs:
input_x (Tensor) - The input tensor. The shape can be 2-5 dimensions.
bias (Tensor) - The bias tensor, with shape \((C)\). C must be the same as channel dimension C of input_x.
- Outputs:
Tensor, with the same shape and data type as input_x.
- Raises:
TypeError – If data_format is not a str.
ValueError – If value of data_format is not in the range of [‘NHWC’,’NCHW’,’NCDHW’].
TypeError – If input_x or bias is not a Tensor.
TypeError – If dtype of input_x or bias is neither float16 nor float32.
TypeError – If dtype of input_x or bias is inconsistent.
TypeError – If dimension of input_x is not in the range [2, 5].
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.arange(6).reshape((2, 3)), mindspore.float32) >>> bias = Tensor(np.random.random(3).reshape((3,)), mindspore.float32) >>> bias_add = ops.BiasAdd() >>> output = bias_add(input_x, bias) >>> print(output.shape) (2, 3)
-
class
tinyms.primitives.
BinaryCrossEntropy
(reduction='mean')[source]¶ Computes the binary cross entropy between the logits and the labels.
Sets logits as \(x\), labels as \(y\), output as \(\ell(x, y)\). Let,
\[L = \{l_1,\dots,l_N\}^\top, \quad l_n = - w_n \left[ y_n \cdot \log x_n + (1 - y_n) \cdot \log (1 - x_n) \right]\]In which, \(L\) indicates the loss of all batch_sizes, \(l\) indicates the loss of one batch_size, and n indicates one batch_size in the 1-N range, \(w_n\) indicates the weight of \(n\)-th batch of binary cross entropy. Then,
\[\begin{split}\ell(x, y) = \begin{cases} L, & \text{if reduction} = \text{'none';}\\ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]Warning
The value of \(x\) must range from 0 to 1.
- Parameters:
reduction (str) – Specifies the reduction to be applied to the output. Its value must be one of ‘none’, ‘mean’ or ‘sum’. Default: ‘mean’.
- Inputs:
logits (Tensor) - The predictive value whose data type must be float16 or float32, The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
labels (Tensor) - The target value which has the same shape and data type as logits.
weight (Tensor, optional) - A rescaling weight applied to the loss of each batch element. And it must have the same shape and data type as logits. Default: None.
- Outputs:
Tensor or Scalar. Returns Tensor that has the same dtype and shape as logits if reduction is ‘none’. Otherwise, returns a scalar Tensor.
- Raises:
TypeError – If dtype of logits, labels or weight (if given) is neither float16 nor float32.
ValueError – If reduction is not one of ‘none’, ‘mean’ or ‘sum’.
ValueError – If shape of labels is not the same as logits or weight (if given).
TypeError – If logits, labels or weight is not a Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.binary_cross_entropy = ops.BinaryCrossEntropy() ... def construct(self, logits, labels, weight): ... result = self.binary_cross_entropy(logits, labels, weight) ... return result ... >>> net = Net() >>> logits = Tensor(np.array([0.2, 0.7, 0.1]), mindspore.float32) >>> labels = Tensor(np.array([0., 1., 0.]), mindspore.float32) >>> weight = Tensor(np.array([1, 2, 2]), mindspore.float32) >>> output = net(logits, labels, weight) >>> print(output) 0.38240486
-
class
tinyms.primitives.
Bincount
[source]¶ Counts the number of occurrences of each value in an integer array.
Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
array (Tensor) - A Tensor of type int32, whose value can not be less than zero.
size (Tensor) - A non-negative Tensor of type int32.
weights (Tensor) - A Tensor with the same shape as array, or a length-0 Tensor, in which case it acts as all weights equal to 1. Must be one of the following types: int32, int64, float32, float64.
- Outputs:
A Tensor. Has the same type as weights.
- Raises:
TypeError – If dtype of array is not int32.
TypeError – If dtype of size is not int32.
ValueError – If size is negative.
ValueError – If weights are empty.
ValueError – If size of weights is not zero and the shape of weights is different with the shape of array.
TypeError – If dtype of weights is not in int32,int64,float32,float64
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> array = Tensor(np.array([1, 2, 2, 3, 3, 3, 4, 4, 4, 4]), mindspore.int32) >>> size = Tensor(5, mindspore.int32) >>> weights = Tensor(np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), mindspore.float32) >>> bincount = ops.Bincount() >>> bins = bincount(array, size, weights) >>> print(bins) [0. 1. 2. 3. 4.]
-
class
tinyms.primitives.
BitwiseAnd
[source]¶ Returns bitwise and of two tensors element-wise.
Refer to
mindspore.ops.bitwise_and()
for more details.- 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) >>> bitwise_and = ops.BitwiseAnd() >>> output = bitwise_and(x, y) >>> print(output) [ 0 0 1 -1 1 0 1]
-
class
tinyms.primitives.
BitwiseOr
[source]¶ Returns bitwise or of two tensors element-wise.
Refer to
mindspore.ops.bitwise_or()
for more details.- 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) >>> bitwise_or = ops.BitwiseOr() >>> output = bitwise_or(x, y) >>> print(output) [ 0 1 1 -1 -1 3 3]
-
class
tinyms.primitives.
BitwiseXor
[source]¶ Returns bitwise xor of two tensors element-wise.
Refer to
mindspore.ops.bitwise_xor()
for more details.- 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) >>> bitwise_xor = ops.BitwiseXor() >>> output = bitwise_xor(x, y) >>> print(output) [ 0 1 0 0 -2 3 2]
-
class
tinyms.primitives.
BlackmanWindow
(periodic=True, dtype=mindspore.float32)[source]¶ Blackman window function.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.blackman_window()
for more details.- Parameters:
periodic (bool, optional) – If True, returns a window to be used as periodic function. If False, return a symmetric window. Default: True.
dtype (mindspore.dtype, optional) – the desired data type of returned tensor. Only float16, float32 and float64 is allowed. Default: mstype.float32.
- Inputs:
window_length (Tensor) - the size of returned window, with data type int32, int64. The input data should be an integer with a value of [0, 1000000].
- Outputs:
A 1-D tensor of size window_length containing the window. Its datatype is set by the attr dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> window_length = Tensor(10, mindspore.int32) >>> blackman_window = ops.BlackmanWindow(periodic = True, dtype = mindspore.float32) >>> output = blackman_window(window_length) >>> print(output) [-2.9802322e-08 4.0212840e-02 2.0077014e-01 5.0978714e-01 8.4922993e-01 1.0000000e+00 8.4922981e-01 5.0978690e-01 2.0077008e-01 4.0212870e-02]
-
class
tinyms.primitives.
BoundingBoxDecode
(max_shape, means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0), wh_ratio_clip=0.016)[source]¶ Decodes bounding boxes locations.
The function of the operator is to calculate the offset, and this operator converts the offset into a Bbox, which is used to mark the target in the subsequent images, etc.
- Parameters:
max_shape (tuple) – The max size limit for decoding box calculation.
means (tuple) – The means of deltas calculation. Default: (0.0, 0.0, 0.0, 0.0).
stds (tuple) – The standard deviations of deltas calculation. Default: (1.0, 1.0, 1.0, 1.0).
wh_ratio_clip (float) – The limit of width and height ratio for decoding box calculation. Default: 0.016.
- Inputs:
anchor_box (Tensor) - Anchor boxes. The shape of anchor_box must be \((n, 4)\).
deltas (Tensor) - Delta of boxes. Which has the same shape with anchor_box.
- Outputs:
Tensor, decoded boxes. It has the same data type and shape as anchor_box.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> anchor_box = Tensor([[4, 1, 2, 1], [2, 2, 2, 3]], mindspore.float32) >>> deltas = Tensor([[3, 1, 2, 2], [1, 2, 1, 4]], mindspore.float32) >>> boundingbox_decode = ops.BoundingBoxDecode(means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0), ... max_shape=(768, 1280), wh_ratio_clip=0.016) >>> output = boundingbox_decode(anchor_box, deltas) >>> print(output) [[ 4.1953125 0. 0. 5.1953125] [ 2.140625 0. 3.859375 60.59375 ]]
-
class
tinyms.primitives.
BoundingBoxEncode
(means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0))[source]¶ Encodes bounding boxes locations.
This operator will calculate the offset between the predicted bounding boxes and the real bounding boxes, and this offset will be used as a variable for the loss.
- Parameters:
- Inputs:
anchor_box (Tensor) - Anchor boxes. The shape of anchor_box must be \((n, 4)\).
groundtruth_box (Tensor) - Ground truth boxes. Which has the same shape with anchor_box.
- Outputs:
Tensor, encoded bounding boxes. It has the same data type and shape as input anchor_box.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> anchor_box = Tensor([[2, 2, 2, 3], [2, 2, 2, 3]], mindspore.float32) >>> groundtruth_box = Tensor([[1, 2, 1, 4], [1, 2, 1, 4]], mindspore.float32) >>> boundingbox_encode = ops.BoundingBoxEncode(means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0)) >>> output = boundingbox_encode(anchor_box, groundtruth_box) >>> print(output) [[ -1. 0.25 0. 0.40551758] [ -1. 0.25 0. 0.40551758]]
-
class
tinyms.primitives.
Broadcast
(root_rank, group='hccl_world_group')[source]¶ Broadcasts the tensor to the whole group.
Note
The tensors must have the same shape and format in all processes of the collection.
- Parameters:
- Inputs:
input_x (tuple[Tensor]) - The shape of tensor is \((x_1, x_2, ..., x_R)\).
- Outputs:
tuple[Tensor], Tensor has the same shape of the input, i.e., \((x_1, x_2, ..., x_R)\). The contents depend on the data of the root_rank device.
- Raises:
TypeError – If root_rank is not an integer or group is not a string.
- Supported Platforms:
Ascend
GPU
Examples
Note
Before running the following examples, you need to configure the communication environment variables.
For the Ascend devices, users need to prepare the rank table, set rank_id and device_id. Please see the Ascend tutorial for more details.
For the GPU devices, users need to prepare the host file and mpi, please see the GPU tutorial .
This example should be run with multiple devices.
>>> import mindspore as ms >>> from mindspore import Tensor >>> from mindspore.communication import init >>> import mindspore.nn as nn >>> import mindspore.ops as ops >>> import numpy as np >>> >>> ms.set_context(mode=ms.GRAPH_MODE) >>> init() >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.broadcast = ops.Broadcast(1) ... ... def construct(self, x): ... return self.broadcast((x,)) ... >>> input_x = Tensor(np.ones([2, 4]).astype(np.int32)) >>> net = Net() >>> output = net(input_x) >>> print(output) (Tensor(shape[2,4], dtype=Int32, value= [[1, 1, 1, 1], [1, 1, 1, 1]]),)
-
class
tinyms.primitives.
BroadcastTo
(shape)[source]¶ Broadcasts input tensor to a given shape.
Refer to
mindspore.ops.broadcast_to()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> shape = (2, 3) >>> x = Tensor(np.array([1, 2, 3]).astype(np.float32)) >>> output = ops.BroadcastTo(shape=shape)(x) >>> print(output) [[1. 2. 3.] [1. 2. 3.]] >>> >>> shape = (-1, 2) >>> x = Tensor(np.array([[1], [2]]).astype(np.float32)) >>> output = ops.BroadcastTo(shape=shape)(x) >>> print(output) [[1. 1.] [2. 2.]]
-
class
tinyms.primitives.
Bucketize
(boundaries)[source]¶ Bucketizes input based on boundaries.
- Parameters:
boundaries (list[float]) – A sorted list of floats gives the boundary of the buckets, and no default value.
- Inputs:
input (Tensor) - A tensor containing the search value(s).
- Outputs:
Tensor, with the same shape as the input, and data type is int32.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Bucketize(nn.Cell): ... def __init__(self, boundaries): ... super().__init__() ... self.bucketize = ops.Bucketize(boundaries=boundaries) ... def construct(self, input): ... return self.bucketize(input) >>> input = Tensor(np.array([[3, 6, 9], [3, 6, 9]]).astype(np.int32)) >>> boundaries = list(np.array([1., 3., 5., 7., 9.])) >>> net = Bucketize(boundaries) >>> output = net(input) >>> print(output) [[2 3 5] [2 3 5]]
-
class
tinyms.primitives.
BufferAppend
(capacity, buffer_shape, buffer_dtype)[source]¶ In reinforcement learning, the experience data is collected in each step. We use BufferAppend to push data to the bottom of buffer under the First-In-First-Out rule.
Warning
This is an experimental API that is subject to change or deletion.
- Parameters:
- Inputs:
data (tuple(Parameter(Tensor))) - The tuple(Tensor) represents replaybuffer, each tensor is described by the buffer_shape and buffer_type.
exp (tuple(Parameter(Tensor))) - The tuple(Tensor) represents one list of experience data, each tensor is described by the buffer_shape and buffer_type.
count (Parameter) - The count means the real available size of the buffer, data type: int32.
head (Parameter) - The position of the first data in buffer, data type: int32.
- Outputs:
None.
- Raises:
ValueError – If count and head is not an integer.
ValueError – If capacity is not a positive integer.
ValueError – If length of data is not equal to length of exp.
ValueError – If dim of data is equal to dim of exp, but data[1:] is not equal to the shape in exp.
ValueError – If the shape of data[1:] is not equal to the shape in exp.
TypeError – If the type in exp is not the same with data.
- Supported Platforms:
GPU
CPU
Examples
>>> capacity = 100 >>> count = Parameter(Tensor(5, ms.int32), name="count") >>> head = Parameter(Tensor(0, ms.int32), name="head") >>> shapes = [(4,), (2,), (1,), (4,)] >>> types = [ms.float32, ms.int32, ms.int32, ms.float32] >>> buffer = [Parameter(Tensor(np.arange(100 * 4).reshape(100, 4).astype(np.float32)), name="states"), ... Parameter(Tensor(np.arange(100 * 2).reshape(100, 2).astype(np.int32)), name="action"), ... Parameter(Tensor(np.ones((100, 1)).astype(np.int32)), name="reward"), ... Parameter(Tensor(np.arange(100 * 4).reshape(100, 4).astype(np.float32)), name="state_")] >>> exp = [Tensor(np.array([2, 2, 2, 2]), ms.float32), Tensor(np.array([0, 0]), ms.int32), ... Tensor(np.array([0]), ms.int32), Tensor(np.array([3, 3, 3, 3]), ms.float32)] >>> batch_exp = [Tensor(np.array([[2, 2, 2, 2], [2, 2, 2, 2]]), ms.float32), ... Tensor(np.array([[0, 0], [0, 0]]), ms.int32), ... Tensor(np.array([[0], [0]]), ms.int32), ... Tensor(np.array([[3, 3, 3, 3], [3, 3, 3, 3]]), ms.float32)] >>> buffer_append = ops.BufferAppend(capacity, shapes, types) >>> buffer_append(buffer, exp, count, head) >>> buffer_append(buffer, batch_exp, count, head)
-
class
tinyms.primitives.
BufferGetItem
(capacity, buffer_shape, buffer_dtype)[source]¶ Get the data from buffer in the position of input index.
Warning
This is an experimental API that is subject to change or deletion.
- Parameters:
- Inputs:
data (tuple(Parameter(Tensor))) - The tuple(Tensor) represents replaybuffer, each tensor is described by the buffer_shape and buffer_type.
count (Parameter) - The count means the real available size of the buffer, data type: int32.
head (Parameter) - The position of the first data in buffer, data type: int32.
index (int64) - The position of the data in buffer.
- Outputs:
tuple(Tensor). The shape is buffer_shape. The dtype is buffer_dtype.
- Raises:
ValueError – If count and head is not an integer.
ValueError – If capacity is not a positive integer.
TypeError – If buffer_shape is not a tuple.
- Supported Platforms:
GPU
CPU
Examples
>>> capacity = 100 >>> index = 3 >>> count = Parameter(Tensor(5, ms.int32), name="count") >>> head = Parameter(Tensor(0, ms.int32), name="head") >>> shapes = [(4,), (2,), (1,), (4,)] >>> types = [ms.float32, ms.int32, ms.int32, ms.float32] >>> buffer = [Parameter(Tensor(np.arange(100 * 4).reshape(100, 4).astype(np.float32)), name="states"), ... Parameter(Tensor(np.arange(100 * 2).reshape(100, 2).astype(np.int32)), name="action"), ... Parameter(Tensor(np.ones((100, 1)).astype(np.int32)), name="reward"), ... Parameter(Tensor(np.arange(100 * 4).reshape(100, 4).astype(np.float32)), name="state_")] >>> buffer_get = ops.BufferGetItem(capacity, shapes, types) >>> output = buffer_get(buffer, count, head, index) >>> print(output) (Tensor(shape=[4], dtype=Float32, value= [ 1.20000000e+01, 1.30000000e+01, 1.40000000e+01, 1.50000000e+01]), Tensor(shape=[2], dtype=Int32, value= [6, 7]), Tensor(shape=[1], dtype=Int32, value= [1]), Tensor(shape=[4], dtype=Float32, value= [ 1.20000000e+01, 1.30000000e+01, 1.40000000e+01, 1.50000000e+01]))
-
class
tinyms.primitives.
BufferSample
(capacity, batch_size, buffer_shape, buffer_dtype, seed=0, unique=False)[source]¶ In reinforcement learning, the data is sampled from the replaybuffer randomly.
Returns the tuple tensor with the given shape, decided by the given batchsize.
Warning
This is an experimental API that is subject to change or deletion.
- Parameters:
capacity (int64) – Capacity of the buffer, must be non-negative.
batch_size (int64) – The size of the sampled data, lessequal to capacity.
buffer_shape (tuple(shape)) – The shape of an buffer.
seed (int64) – Random seed for sample. Default: 0. If use the default seed, it will generate a ramdom
in kernel. Set a number other than 0 to keep a specific seed. Default (one) –
unique (bool) – Whether the sampled data is strictly unique. Setting it to False has a better performance. Default: False
- Inputs:
data (tuple(Parameter(Tensor))) - The tuple(Tensor) represents replaybuffer, each tensor is described by the buffer_shape and buffer_type.
count (Parameter) - The count means the real available size of the buffer, data type: int32.
head (Parameter) - The position of the first data in buffer, data type: int32.
- Outputs:
tuple(Tensor). The shape is batch_size * buffer_shape. The dtype is buffer_dtype.
- Raises:
TypeError – If buffer_shape is not a tuple.
ValueError – If batch_size is larger than capacity.
ValueError – If capacity is not a positive integer.
- Supported Platforms:
GPU
CPU
Examples
>>> capacity = 100 >>> batch_size = 5 >>> count = Parameter(Tensor(5, ms.int32), name="count") >>> head = Parameter(Tensor(0, ms.int32), name="head") >>> shapes = [(4,), (2,), (1,), (4,)] >>> types = [ms.float32, ms.int32, ms.int32, ms.float32] >>> buffer = [Parameter(Tensor(np.arange(100 * 4).reshape(100, 4).astype(np.float32)), name="states"), ... Parameter(Tensor(np.arange(100 * 2).reshape(100, 2).astype(np.int32)), name="action"), ... Parameter(Tensor(np.ones((100, 1)).astype(np.int32)), name="reward"), ... Parameter(Tensor(np.arange(100 * 4).reshape(100, 4).astype(np.float32)), name="state_")] >>> buffer_sample = ops.BufferSample(capacity, batch_size, shapes, types) >>> output = buffer_sample(buffer, count, head) >>> print(output) (Tensor(shape=[5, 4], dtype=Float32, value= [[ 0.00000000e+00, 1.00000000e+00, 2.00000000e+00, 3.00000000e+00], [ 8.00000000e+00, 9.00000000e+00, 1.00000000e+01, 1.10000000e+01], [ 1.60000000e+01, 1.70000000e+01, 1.80000000e+01, 1.90000000e+01], [ 1.20000000e+01, 1.30000000e+01, 1.40000000e+01, 1.50000000e+01], [ 3.20000000e+01, 3.30000000e+01, 3.40000000e+01, 3.50000000e+01]]), Tensor(shape=[5, 2], dtype=Int32, value= [[ 0, 1], [ 4, 5], [ 8, 9], [ 6, 7], [16, 17]]), Tensor(shape=[5, 1], dtype=Int32, value= [[1], [1], [1], [1], [1]]), Tensor(shape=[5, 4], dtype=Float32, value= [[ 0.00000000e+00, 1.00000000e+00, 2.00000000e+00, 3.00000000e+00], [ 8.00000000e+00, 9.00000000e+00, 1.00000000e+01, 1.10000000e+01], [ 1.60000000e+01, 1.70000000e+01, 1.80000000e+01, 1.90000000e+01], [ 1.20000000e+01, 1.30000000e+01, 1.40000000e+01, 1.50000000e+01], [ 3.20000000e+01, 3.30000000e+01, 3.40000000e+01, 3.50000000e+01]]))
-
class
tinyms.primitives.
CTCGreedyDecoder
(merge_repeated=True)[source]¶ Performs greedy decoding on the logits given in inputs.
Refer to
mindspore.ops.ctc_greedy_decoder()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> inputs = Tensor(np.array([[[0.6, 0.4, 0.2], [0.8, 0.6, 0.3]], ... [[0.0, 0.6, 0.0], [0.5, 0.4, 0.5]]]), mindspore.float32) >>> sequence_length = Tensor(np.array([2, 2]), mindspore.int32) >>> decoded_indices, decoded_values, decoded_shape, log_probability = ops.CTCGreedyDecoder()(inputs, ... sequence_length) >>> print(decoded_indices) [[0 0] [0 1] [1 0]] >>> print(decoded_values) [0 1 0] >>> print(decoded_shape) [2 2] >>> print(log_probability) [[-1.2] [-1.3]]
-
class
tinyms.primitives.
CTCLoss
(preprocess_collapse_repeated=False, ctc_merge_repeated=True, ignore_longer_outputs_than_inputs=False)[source]¶ Calculates the CTC (Connectionist Temporal Classification) loss and the gradient.
The bottom layer of this interface calls the implementation of the third-party baidu-research::warp-ctc. The CTC algorithm is proposed in Connectionist Temporal Classification: Labeling Unsegmented Sequence Data with Recurrent Neural Networks.
CTCLoss calculates loss between a continuous time series and a target sequence. CTCLoss sums over the probability of input to target, producing a loss value which is differentiable with respect to each input node. The alignment of input to target is assumed to be “many-to-one”, such that the length of target series must be less than or equal to the length of input.
- Parameters:
preprocess_collapse_repeated (bool) – If true, repeated labels will be collapsed prior to the CTC calculation. Default: False.
ctc_merge_repeated (bool) – If false, during CTC calculation, repeated non-blank labels will not be merged and these labels will be interpreted as individual ones. This is a simplified version of CTC. Default: True.
ignore_longer_outputs_than_inputs (bool) – If true, sequences with longer outputs than inputs will be ignored. Default: False.
- Inputs:
x (Tensor) - The input Tensor must be a 3-D tensor whose shape is \((max\_time, batch\_size, num\_classes)\). num_classes must be num_labels + 1 classes, num_labels indicates the number of actual labels. Blank labels are reserved. Default blank label is num_classes - 1. Data type must be float16, float32 or float64.
labels_indices (Tensor) - The indices of labels. labels_indices[i, :] = [b, t] means labels_values[i] stores the id for (batch b, time t). The type must be int64 and rank must be 2.
labels_values (Tensor) - A 1-D input tensor. The values are associated with the given batch and time. The type must be int32. labels_values[i] must be in the range of [0, num_classes).
sequence_length (Tensor) - A tensor containing sequence lengths with the shape of \((batch\_size, )\). The type must be int32. Each value in the tensor must not be greater than max_time.
- Outputs:
loss (Tensor) - A tensor containing log-probabilities, the shape is \((batch\_size, )\). The tensor has the same data type as x.
gradient (Tensor) - The gradient of loss, has the same shape and data type as x.
- Raises:
TypeError – If preprocess_collapse_repeated, ctc_merge_repeated or ignore_longer_outputs_than_inputs is not a bool.
TypeError – If x, labels_indices, labels_values or sequence_length is not a Tensor.
ValueError – If rank of labels_indices is not equal to 2.
TypeError – If dtype of x is not one of the following: float16, float32 nor float64.
TypeError – If dtype of labels_indices is not int64.
TypeError – If dtype of labels_values or sequence_length is not int32.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([[[0.3, 0.6, 0.6], ... [0.4, 0.3, 0.9]], ... ... [[0.9, 0.4, 0.2], ... [0.9, 0.9, 0.1]]]).astype(np.float32)) >>> labels_indices = Tensor(np.array([[0, 0], [1, 0]]), mindspore.int64) >>> labels_values = Tensor(np.array([2, 2]), mindspore.int32) >>> sequence_length = Tensor(np.array([2, 2]), mindspore.int32) >>> ctc_loss = ops.CTCLoss() >>> loss, gradient = ctc_loss(x, labels_indices, labels_values, sequence_length) >>> print(loss) [ 0.79628 0.5995158 ] >>> print(gradient) [[[ 0.27029088 0.36485454 -0.6351454 ] [ 0.28140804 0.25462854 -0.5360366 ]] [[ 0.47548494 0.2883962 0.04510255 ] [ 0.4082751 0.4082751 0.02843709 ]]]
-
class
tinyms.primitives.
CTCLossV2
(blank=0, reduction='none', zero_infinity=False)[source]¶ Calculates the CTC (Connectionist Temporal Classification) loss and the gradient.
The CTC algorithm is proposed in Connectionist Temporal Classification: Labeling Unsegmented Sequence Data with Recurrent Neural Networks.
Warning
This is an experimental API that is subject to change or deletion.
- Parameters:
blank (int, optional) – The blank label. Default: 0.
reduction (str, optional) – Apply specific reduction method to the output. Currently only support ‘none’, not case sensitive. Default: “none”.
zero_infinity (bool, optional) – If loss is infinite, this parameter determines whether to set that loss and its correlated gradient to zero. Default: False.
- Inputs:
log_probs (Tensor) - A tensor of shape \((T, C, N)\), where \(T\) is input length, \(N\) is batch size and \(C\) is number of classes (including blank).
targets (Tensor) - A tensor of shape \((N, S)\), where \(S\) is max target length, means the target sequences.
input_lengths (Union(Tuple, Tensor)) - A tuple or Tensor of shape \((N)\). It means the lengths of the input.
target_lengths (Union(Tuple, Tensor)) - A tuple or Tensor of shape \((N)\). It means the lengths of the target.
- Outputs:
neg_log_likelihood (Tensor) - A loss value which is differentiable with respect to each input node.
log_alpha (Tensor) - The probability of possible trace of input to target.
- Raises:
TypeError – If zero_infinity is not a bool.
TypeError – If reduction is not string.
TypeError – If the dtype of log_probs is not float or double.
TypeError – If the dtype of targets, input_lengths or target_lengths is not int32 or int64.
ValueError – If the rank of log_probs is not 3.
ValueError – If the rank of targets is not 2.
ValueError – If the shape of input_lengths does not match batch_size \(N\).
ValueError – If the shape of target_lengths does not match batch_size \(N\).
TypeError – If the types of targets, input_lengths or target_lengths are different.
ValueError – If the value of blank is not in range [0, num_labels|C).
RuntimeError – If any value of input_lengths is larger than (num_labels|C).
RuntimeError – If any target_lengths[i] is not in range [0, input_length[i]].
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> log_probs = Tensor(np.array([[[0.3, 0.6, 0.6]], ... [[0.9, 0.4, 0.2]]]).astype(np.float32)) >>> targets = Tensor(np.array([[0, 1]]), mstype.int32) >>> input_lengths = Tensor(np.array([2]), mstype.int32) >>> target_lengths = Tensor(np.array([1]), mstype.int32) >>> CTCLossV2 = ops.CTCLossV2(blank=0, reduction='none', zero_infinity=False) >>> neg_log_hood, log_alpha = CTCLossV2( ... log_probs, targets, input_lengths, target_lengths) >>> print(neg_log_hood) [-2.2986124] >>> print(log_alpha) [[[0.3 0.3 -inf -inf -inf] [1.2 1.8931472 1.2 -inf -inf]]]
-
class
tinyms.primitives.
Cast
[source]¶ Returns a tensor with the new specified data type.
- Inputs:
input_x (Union[Tensor, Number]) - The shape of tensor is \((x_1, x_2, ..., x_R)\). The tensor to be cast.
type (dtype.Number) - The valid data type of the output tensor. Only constant value is allowed.
- Outputs:
Tensor, the shape of tensor is the same as input_x, \((x_1, x_2, ..., x_R)\).
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_np = np.random.randn(2, 3, 4, 5).astype(np.float32) >>> input_x = Tensor(input_np) >>> type_dst = mindspore.int32 >>> cast = ops.Cast() >>> output = cast(input_x, type_dst) >>> print(output.dtype) Int32 >>> print(output.shape) (2, 3, 4, 5)
-
class
tinyms.primitives.
Cauchy
(size, median=0.0, sigma=1.0)[source]¶ Create a tensor of shape size with random numbers drawn from Cauchy distribution. It is defined as follows:
\[f(x)= \frac{1}{\pi} \frac{\sigma}{(x-median)^2 +\sigma^2}\]- Parameters:
- Outputs:
Tensor with cauchy distribution data. Tensor shape is size, and data type is float32.
- Raises:
TypeError – If sigma is not a float.
TypeError – If median is not a float.
TypeError – If size is not a list.
ValueError – If size list is empty.
ValueError – If data of size is not a positive integer.
- Supported Platforms:
Ascend
CPU
Examples
>>> size = [1] >>> net = ops.Cauchy(size) >>> y = net() >>> print(y) [0.03128606]
-
class
tinyms.primitives.
Cdist
(p=2.0)[source]¶ Computes batched the p-norm distance between each pair of the two collections of row vectors.
Refer to
mindspore.ops.cdist()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> import mindspore >>> from mindspore import Tensor, ops >>> input_x = Tensor(np.array([[[1.0, 1.0], [2.0, 2.0]]]).astype(np.float32)) >>> input_y = Tensor(np.array([[[3.0, 3.0], [3.0, 3.0]]]).astype(np.float32)) >>> op = ops.Cdist(p=2.0) >>> output = op(input_x, input_y) >>> print(output) [[[2.8284273 2.8284273] [1.4142137 1.4142137]]]
-
class
tinyms.primitives.
CeLU
(alpha=1.0)[source]¶ Computes CeLU (Continuously differentiable exponential linear units) of input tensors element-wise.
Refer to
mindspore.ops.celu()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.array([-2.0, -1.0, 1.0, 2.0]), mindspore.float32) >>> celu = ops.CeLU(alpha=1.0) >>> output = celu(input_x) >>> print(output) [-0.86466473 -0.63212055 1. 2. ]
-
class
tinyms.primitives.
Ceil
[source]¶ Rounds a tensor up to the closest integer element-wise.
Refer to
mindspore.ops.ceil()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([1.1, 2.5, -1.5]), mindspore.float32) >>> ceil_op = ops.Ceil() >>> output = ceil_op(x) >>> print(output) [ 2. 3. -1.]
-
class
tinyms.primitives.
ChannelShuffle
(group)[source]¶ Divide the channels in a tensor of shape (, C, H, W) into g groups and rearrange them as (, C/g, g, H*W), while keeping the original tensor shapes.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.channel_shuffle()
for more detail.- Supported Platforms:
Ascend
CPU
Examples
>>> group = 2 >>> x = Tensor(np.arange(1 * 4 * 2 * 2).reshape(1, 4, 2, 2).astype(np.int16)) >>> channel_shuffle_func = ops.ChannelShuffle(group) >>> y = channel_shuffle_func(x) >>> print(y) [[[[ 0 1] [ 2 3]] [[ 8 9] [10 11]] [[ 4 5] [ 6 7]] [[12 13] [14 15]]]]
-
class
tinyms.primitives.
CheckNumerics
[source]¶ Checks a tensor for NaN and Inf values. A runtime error is raised if input has NaN or Inf values.
- Inputs:
x (Tensor) - Input Tensor of any dimension. The data type is float16, float32 or float64.
- Outputs:
Tensor, has the same shape and data type as x if x has no NaN or Inf values.
- Raises:
TypeError – If x data type is not float16, float32, float64.
RuntimeError – If x has NaN or Inf values.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([[1, 3], [2, 4]], dtype=np.float32)) >>> checknumerics = ops.CheckNumerics() >>> output = checknumerics(x) >>> print(output) [[1. 3.] [2. 4.]]
-
class
tinyms.primitives.
CheckValid
[source]¶ Checks bounding box.
Checks whether the bounding boxes specified by bboxes is valid. Returns True if the box is within borders specified by img_metas, False if not.
- Inputs:
bboxes (Tensor) - Bounding boxes tensor with shape \((N, 4)\). \(N\) indicates the number of bounding boxes, the value “4” indicates “x0”, “x1”, “y0”, and “y1”. Data type must be float16 or float32.
img_metas (Tensor) - Raw image size information with the format of \((height, width, ratio)\), specifying the valid boundary \((height * ratio, width * ratio)\). Data type must be float16 or float32.
- Outputs:
Tensor, with shape of \((N,)\) and dtype of bool, specifying whether the bounding boxes is in the image. “True” indicates valid, while “False” indicates invalid.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> import mindspore.nn as nn >>> import numpy as np >>> from mindspore import Tensor, ops >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.check_valid = ops.CheckValid() ... def construct(self, x, y): ... valid_result = self.check_valid(x, y) ... return valid_result ... >>> bboxes = Tensor(np.linspace(0, 6, 12).reshape(3, 4), mindspore.float32) >>> img_metas = Tensor(np.array([2, 1, 3]), mindspore.float32) >>> net = Net() >>> output = net(bboxes, img_metas) >>> print(output) [ True False False]
-
class
tinyms.primitives.
Cholesky
(upper=False)[source]¶ Performs the Cholesky decomposition on a single or a batch of symmetric positive-definite matrices.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.cholesky()
for more details.- Supported Platforms:
GPU
CPU
Examples
>>> input_x = Tensor(np.array([[1.0, 1.0], [1.0, 2.0]]), mindspore.float32) >>> cholesky = ops.Cholesky(upper=False) >>> output = cholesky(input_x) >>> print(output) [[1. 0.] [1. 1.]]
-
class
tinyms.primitives.
CholeskyInverse
(upper=False)[source]¶ Returns the inverse of the positive definite matrix using cholesky matrix factorization given its Cholesky factor.
Refer to
mindspore.ops.cholesky_inverse()
for more details.- Supported Platforms:
Ascend
CPU
Examples
>>> x = Tensor(np.array([[2,0,0], [4,1,0], [-1,1,2]]), mindspore.float32) >>> net = ops.CholeskyInverse() >>> y = net(x) >>> print(y) [[ 5.8125 -2.625 0.625 ] [-2.625 1.25 -0.25 ] [ 0.625 -0.25 0.25 ]]
-
class
tinyms.primitives.
CholeskySolve
(upper=False)[source]¶ Computes the solution of a set of linear equations with a positive definite matrix, according to its Cholesky decomposition factor u , and outputs the result as c.
If upper is set to True, u is upper triangular and c is returned such that:
\[c = (u^{T}u)^{{-1}}b\]If upper is set to False, u is lower triangular and c is returned such that:
\[c = (uu^{T})^{{-1}}b\]- Parameters:
upper (bool, optional) – A flag indicates whether to treat the Cholesky factor as an upper or a lower triangular matrix. Default: False.
- Inputs:
x1 (Tensor) - Tensor of shape \((*, N, M)\), indicating 2D or 3D matrices, with float32 or float64 data type.
x2 (Tensor) - Tensor of shape \((*, N, N)\), indicating 2D or 3D square matrices composed of upper or lower triangular Cholesky factor, with float32 or float64 data type. x1 and x2 must have the same type.
- Outputs:
Tensor, has the same shape and data type as x1.
- Raises:
TypeError – If upper is not a bool.
TypeError – If dtype of x1 and x2 is not one of: float64, float32.
TypeError – If x1 is not a Tensor.
TypeError – If x2 is not a Tensor.
ValueError – If x1 and x2 have different batch size.
ValueError – If x1 and x2 have different row numbers.
ValueError – If x1 is not 2D or 3D matrices.
ValueError – If x2 is not 2D or 3D square matrices.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x1 = Tensor(np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), mindspore.float32) >>> x2 = Tensor(np.array([[2, 0, 0], [4, 1, 0], [-1, 1, 2]]), mindspore.float32) >>> net = ops.CholeskySolve() >>> y = net(x1, x2) >>> print(y) [[ 5.8125 -2.625 0.625 ] [-2.625 1.25 -0.25 ] [ 0.625 -0.25 0.25 ]]
-
class
tinyms.primitives.
Coalesce
[source]¶ Returns the coalesced sparse tensor of the input.
- Inputs:
x_indices (Tensor) - A 2-D Tensor, represents the indices of the nonzero elements of the sparse tensor. Supported data type is int64. Its elements should be non-negative. The shape is \((y, x)\).
x_values (Tensor) - A 1-D Tensor, represents the values corresponding to the indices in x_indices. Supported data types are float16 and float32. The shape is \((x,)\).
x_shape (Tensor) - A 1-D Tensor, specifies the shape of the sparse tensor. Supported data type is int64. The shape is \((y,)\).
- Outputs:
y_indices (Tensor) - A 2-D Tensor, represents the indices of the nonzero elements of the sparse tensor. Data type is int64. It’s elements are non-negative. The shape is \((y, z)\). z represents the number of different indices in x_indices.
y_values (Tensor) - A 1-D Tensor, represents the values corresponding to the indices in y_indices. Data type is the same as x_values’s. The shape is \((z,)\).
y_shape (Tensor) - A 1-D Tensor, specifies the shape of the sparse tensor. Data type is int64. The shape is \((y,)\).
- Raises:
TypeError – If the data type of x_values is neither float32 nor float16.
TypeError – If any of the data types of x_indices and x_shape is not int64.
ValueError – If any of x_values and x_shape is not a 1-D tensor.
ValueError – If x_indices is not a 2-D tensor.
ValueError – If sizes of second dimension of x_indices and first dimension of x_values are not the same.
ValueError – If sizes of first dimension of x_indices and first dimension of x_shape are not the same.
ValueError – If any of the values of elements of x_indices is negative.
ValueError – If any of the values of elements of x_indices exceed the limit set by x_shape.
- Supported Platforms:
GPU
CPU
Examples
>>> x_indices = Tensor([[0, 0, 1], [1, 1, 2]], dtype=mstype.int64) >>> x_values = Tensor([1, 5, 4], dtype=mstype.float32) >>> x_shape = Tensor([3, 3], dtype=mstype.int64) >>> coalesce = ops.Coalesce() >>> y_indices, y_values, y_shape = coalesce(x_indices, x_values, x_shape) >>> print(y_indices) [[0 1] [1 2]] >>> print(y_values) [6. 4.] >>> print(y_shape) [3 3]
-
class
tinyms.primitives.
Col2Im
(kernel_size, dilation=1, padding=0, stride=1)[source]¶ Combines an array of sliding local blocks into a large containing tensor. It is usually used to reconstruct an image from a set of image patches(or sliding local blocks).
Consider a batched
input
tensor containing sliding local blocks, e.g., patches of images, of shape \((N, C, \prod(\text{kernel_size}), L)\), where \(N\) is batch dimension, \(C\) is channel dimension, \(\prod(\text{kernel_size})\) is the block size, and \(L\) is the total number of blocks. This operation combines these local blocks into the largeoutput
tensor of shape \((N, C, \text{output_size}[0], \text{output_size}[1], \dots)\) by summing the overlapping values.\[L = \prod_d \left\lfloor\frac{\text{output_size}[d] + 2 \times \text{padding}[d] % - \text{dilation}[d] \times (\text{kernel_size}[d] - 1) - 1}{\text{stride}[d]} + 1\right\rfloor\]where \(d\) is over all spatial dimensions. The padding, stride and dilation arguments specify how the sliding blocks are retrieved.
Warning
This is an experimental API that is subject to change or deletion.
- Parameters:
kernel_size (Union[int, tuple[int], list[int]]) – The size of the kernel, should be two positive int for height and width. If type is int, it means that height equal with width. Must be specified.
dilation (Union[int, tuple[int], list[int]], optional) – The size of the dilation, should be two positive int for height and width. If type is int, it means that height equal with width. Default: 1.
padding (Union[int, tuple[int], list[int]], optional) – The size of the padding, should be two int for height and width. If type is int, it means that height equal with width. Default: 0.
stride (Union[int, tuple[int], list[int]], optional) – The size of the stride, should be two positive int for height and width. If type is int, it means that height equal with width. Default: 1.
- Inputs:
x (Tensor) - 4D tensor with data type float16 or float32.
output_size (Tensor) - 1D tensor with 2 elements of data type int32.
- Outputs:
Tensor, a 4-D Tensor with same type of input x.
- Raises:
TypeError – If dtype of kernel_size , dilation , padding or stride is not in Union[int, tuple[int], list[int]].
ValueError – If values in kernel_size , dilation , padding or stride are not greater than zero or any one of them has more than 2 elements.
ValueError – If x.shape[2] != kernel_size[0] * kernel_size[1].
ValueError – If x.shape[3] does not match the calculated number of sliding blocks.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor, ops >>> from mindspore import dtype as mstype >>> x = Tensor(input_data=np.random.rand(16, 16, 4, 25), dtype=mstype.float32) >>> output_size = Tensor(input_data=[8, 8], dtype=mstype.int32) >>> col2im = ops.Col2Im(kernel_size=[2, 2], dilation=[2, 2], padding=[2, 2], stride=[2, 2]) >>> y = col2im(x, output_size) >>> print(y.shape) (16, 16, 8, 8)
-
class
tinyms.primitives.
CombinedNonMaxSuppression
(pad_per_class=False, clip_boxes=True)[source]¶ Applies a greedy approach to select a subset of bounding boxes from a list of candidates using NonMaxSuppression, where the boxes are sorted in descending order of their confidence score.
- Parameters:
clip_boxes (bool, optional) –
Determines whether to apply bounding box normalization to ensure the coordinates are within [0, 1] range. Default: True.
If True, clip the boxes that fall outside this range.
If False, return the box coordinates as they are without any modifications.
pad_per_class (bool, optional) –
Determines whether the output of the non-maximum suppression (NMS) algorithm should be padded or clipped to meet the maximum size constraints. Default: False.
If False, the output is clipped to the maximum size of max_total_size.
If True, the output is padded up to max_size_per_class * num_classes and clipped if it exceeds max_total_size.
- Inputs:
boxes (Tensor) - A float32 Tensor with shape \((batch_size, num_boxes, q, 4)\) representing the bounding box coordinates. q indicates mapping relationship between boxes and classes. If q is 1, all classes use the same bounding box. If q is equal to the number of classes, class-specific boxes are applied.
scores (Tensor) - A 3-D Tensor of float32 type with the shape \((batch_size, num_boxes, num_classes)\). It contains a score value for each box, with each row of boxes represented by a single score.
max_output_size_per_class (Tensor) - The maximum number of boxes that can be selected for each class by the non-maximum suppression algorithm, represented by a scalar Tensor of type int32.
max_total_size (Tensor) - A scalar Tensor of type int32 that represents the maximum number of boxes that are kept for all classes.
iou_threshold (Tensor) - A scalar Tensor of float32 type that represents the threshold for determining if the IOU overlap between boxes is too high. iou_threshold must be equal or greater than 0 and be equal or smaller than 1.
score_threshold (Tensor) - A scalar Tensor of type float32 that represents the threshold for determining when to remove boxes based on their scores.
- Outputs:
nmsed_boxes - A Tensor of float32 with shape of (batch_size, num_detection, 4), which contains the non-max suppressed boxes.
nmsed_scores - A Tensor of float32 with shape of (batch_size, num_detection), which contains score of boxes.
nmsed_classes - A Tensor of float32 with shape of (batch_size, num_detection), which contains classes of boxes.
valid_detections A Tensor of int32 with shape of (batch_size,), which indicates the number of valid detections of each batch.
- Raises:
TypeError – If the dtype of boxes, scores , iou_threshold , score threshold are not float32.
TypeError – If the dtype of max_output_size_per_class and max_total_size are not int32.
ValueError – If boxes is not 4D.
ValueError – If max_output_size_per_class, max_total_size, iou_threshold and score threshold are not 0D.
ValueError – If scores is not 3D.
ValueError – If shape[0] or shape[1] of boxes is not same with that of the scores.
ValueError – If shape[2] of boxes is not same with shape[2] of scores or 1
ValueError – If max_total_size < 0.
ValueError – If max_output_size_per_class < 0.
ValueError – If iou_threshold not in [0,1].
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> boxes = Tensor(np.array([[[[200, 100, 150, 100]], ... [[220, 120, 150, 100]], ... [[190, 110, 150, 100]], ... [[210, 112, 150, 100]]]])).astype('float32') >>> scores = Tensor(np.array([[[0.2000, 0.7000, 0.1000], [0.1000, 0.8000, 0.1000], [0.3000, 0.6000, 0.1000], ... [0.0500, 0.9000, 0.0500]]])).astype('float32') >>> max_output_size_per_class = Tensor(4, mstype.int32) >>> max_total_size = Tensor(1, mstype.int32) >>> iou_threshold = Tensor(0, mstype.float32) >>> score_threshold = Tensor(0, mstype.float32) >>> net = ops.CombinedNonMaxSuppression() >>> out = net(boxes, scores, max_output_size_per_class, max_total_size, iou_threshold, score_threshold) >>> print(out) (Tensor(shape=[1, 1, 4], dtype=Float32, value= [[[1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00]]]), Tensor(shape=[1, 1], dtype=Float32, value= [[ 8.99999976e-01]]), Tensor(shape=[1, 1], dtype=Float32, value= [[ 1.00000000e+00]]), Tensor(shape=[1], dtype=Int32, value= [1]))
-
class
tinyms.primitives.
CompareAndBitpack
[source]¶ Compare values of x to threshold and pack resulting bits into a uint8.
Each comparison returns a boolean true (if x_value > threshold) or and false otherwise.
Given an x shaped \((s_0, s_1, ..., s_n)\), the output is a uint8 Tensor shaped \((s_0, s_1, ..., s_n / 8)\).
- Inputs:
x (Tensor) - Input tensor. Values to compare against threshold and bitpack. The data type must be bool, float16, float32, float64, int8, int16, int32, int64. Note: Currently, the innermost dimension of the tensor must be divisible by 8.
threshold (Tensor) - A 0D Tensor, whose data type is same as x.
- Outputs:
Tensor, has the uint8 type.
- Raises:
TypeError – If x or threshold is not a Tensor.
TypeError – If the dtype of ‘x’ is not one of: bool, float16, float32, float64, int8, int16, int32, int64.
TypeError – If threshold’s type is not as same ‘x’.
ValueError – If threshold is not a 0D Tensor.
ValueError – If x is a 0D Tensor.
ValueError – If the innermost dimension of x’s shape is not disvisible by 8.
- Supported Platforms:
Ascend
CPU
Examples
>>> x = Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8]), mindspore.float32) >>> threshold = Tensor(6, mindspore.float32) >>> net = ops.CompareAndBitpack() >>> output = net(x, threshold) >>> print(output) [3]
-
class
tinyms.primitives.
Complex
[source]¶ Returns a complex Tensor from the real part and the imag part.
Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
real (Tensor) - The real input tensor. types: float32, float64.
imag (Tensor) - The imag input tensor. types: float32, float64.
- Outputs:
Tensor, has the complex type.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> real = Tensor(np.array([1]), mindspore.float32) >>> imag = Tensor(np.array([2]), mindspore.float32) >>> complex = ops.Complex() >>> output = complex(real, imag) >>> print(output) [1.+2.j]
-
class
tinyms.primitives.
ComplexAbs
[source]¶ Returns a Tensor that contains the magnitudes of the input.
The complex numbers in input must be of the form \(a + bj\), where \(a\) is the real part and \(b\) is the imaginary part.
\[y = \sqrt{a^2+b^2}\]Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
x (Tensor) - A Tensor, types: complex64, complex128.
- Outputs:
Tensor, has the same shape as x. If the type of x is complex64, the type of output is float32. If the type of x is complex128, the type of output is float64.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.asarray(np.complex(3+4j)), mindspore.complex64) >>> complex_abs = ops.ComplexAbs() >>> output = complex_abs(x) >>> print(output) 5.0
-
class
tinyms.primitives.
ComputeAccidentalHits
(num_true=1)[source]¶ Compute accidental hits of sampled classes which match target classes.
When a target class matches the sample class, we call it “accidental hit”. The result of calculating accidental hits contain three parts (index, id, weight), where index represents the row number in true_classes, and id represents the position in sampled_candidates, the weight is FLOAT_MAX. FLOAT_MAX indicates the max value in the type of Float
- Parameters:
num_true (int) – The number of target classes per training example. Default: 1.
- Inputs:
true_classes (Tensor) - The target classes. With data type of int32 or int64 and shape \((batch\_size, num\_true)\).
sampled_candidates (Tensor) - The Candidate sampling results of operators, types of training samples, with data type of int32 or int64 and shape \((num\_sampled, )\).
- Outputs:
Tuple of 3 Tensors.
indices (Tensor) - A Tensor with shape \((num\_accidental\_hits, )\), with the same type as true_classes.
ids (Tensor) - A Tensor with shape \((num\_accidental\_hits, )\), with the same type as true_classes.
weights (Tensor) - A Tensor with shape \((num\_accidental\_hits, )\), with the type float32.
- Raises:
- Supported Platforms:
Ascend
Examples
>>> true_classes = np.array([[1, 2], [0, 4], [3, 3]]) >>> sampled_candidates = np.array([0, 1, 2, 3, 4]) >>> sampler = ops.ComputeAccidentalHits(2) >>> indices, ids, weights = sampler(Tensor(true_classes), Tensor(sampled_candidates)) >>> print(indices, ids, weights) [0 0 1 1 2 2] [1 2 0 4 3 3] [-3.4028235e+38 -3.4028235e+38 -3.4028235e+38 -3.4028235e+38 -3.4028235e+38 -3.4028235e+38]
-
class
tinyms.primitives.
Concat
(axis=0)[source]¶ Connect tensor in the specified axis.
Refer to
mindspore.ops.concat()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x1 = Tensor(np.array([[0, 1], [2, 1]]).astype(np.float32)) >>> input_x2 = Tensor(np.array([[0, 1], [2, 1]]).astype(np.float32)) >>> op = ops.Concat() >>> output = op((input_x1, input_x2)) >>> print(output) [[0. 1.] [2. 1.] [0. 1.] [2. 1.]] >>> op = ops.Concat(1) >>> output = op((input_x1, input_x2)) >>> print(output) [[0. 1. 0. 1.] [2. 1. 2. 1.]]
-
class
tinyms.primitives.
ConfusionMatrix
(num_classes, dtype='int32')[source]¶ Calculates the confusion matrix from labels and predictions.
- Parameters:
- Inputs:
labels (Tensor) - real labels, tensor of 1-D. the dtype must be non-negative Integer.
predictions (Tensor) - the labels from prediction, tensor of 1-D. the shape same as labels and the dtype must be non-negative Integer.
weights (Tensor) - tensor of 1-D. the shape same as predictions.
- Outputs:
Tensor, the confusion matrix, with shape (num_classes, num_classes).
- Raises:
Examples
>>> confusion_matrix = ops.ConfusionMatrix(4) >>> labels = Tensor([0, 1, 1, 3], mindspore.int32) >>> predictions = Tensor([1, 2, 1, 3], mindspore.int32) >>> output = confusion_matrix(labels, predictions) >>> print(output) [[0 1 0 0] [0 1 1 0] [0 0 0 0] [0 0 0 1]]
-
class
tinyms.primitives.
Conj
[source]¶ Returns a tensor of complex numbers that are the complex conjugate of each element in input. The complex numbers in input must be of the form a + bj, where a is the real part and b is the imaginary part.
The complex conjugate returned by this operation is of the form a - bj.
If input is real, it is returned unchanged.
- Inputs:
input (Tensor) - The input tensor to compute to. Must have numeric type.
- Outputs:
Tensor, has the same dtype as the input.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.asarray(np.complex(1.3+0.4j)), mindspore.complex64) >>> conj = ops.Conj() >>> output = conj(x) >>> print(output) (1.3-0.4j)
-
class
tinyms.primitives.
ConjugateTranspose
[source]¶ Calculate the conjugate matrix of input x which has been transposed according to input perm.
\[y[i,j,k,...,s,t,u] == conj(x[perm[i], perm[j], perm[k],...,perm[s], perm[t], perm[u]])\]- Inputs:
x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\).
perm (tuple[int]) - The permutation to be converted. The elements in perm are composed of the indexes of each dimension of x. The length of perm and the shape of x must be the same. Only constant value is allowed. Must be in the range [0, rank(x)).
- Outputs:
Tensor, the type of output tensor is the same as x and the shape of output tensor is decided by the shape of x and the value of Conj(perm):
\[y.shape[i] = x.shape[perm[i]]\]where i is in range [0, rank(x) - 1].
- Raises:
TypeError – If perm is not a tuple.
ValueError – If length of shape of x is not equal to length of shape of perm.
ValueError – If the same element exists in perm.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([[1 + 1j,2 + 2j], [3 + 3j, 4 + 4j]]), mindspore.complex64) >>> perm = (1, 0) >>> conjugate_transpose = ops.ConjugateTranspose() >>> output = conjugate_transpose(x, perm) >>> print(output) [[1.-1.j 3.-3.j] [2.-2.j 4.-4.j]]
-
class
tinyms.primitives.
Conv2D
(out_channel, kernel_size, mode=1, pad_mode='valid', pad=0, stride=1, dilation=1, group=1, data_format='NCHW')[source]¶ 2D convolution layer.
Applies a 2D convolution over an input tensor which is typically of shape \((N, C_{in}, H_{in}, W_{in})\), where \(N\) is batch size, \(C\) is channel number, \(H\) is height, \(W\) is width, \(X_i\) is the \(i^{th}\) input value and \(b_i\) indicates the deviation value of the \(i^{th}\) input value. For each batch of shape \((C_{in}, H_{in}, W_{in})\), the formula is defined as:
\[out_j = \sum_{i=0}^{C_{in} - 1} ccor(W_{ij}, X_i) + b_j,\]where \(ccor\) is the cross correlation operator, \(C_{in}\) is the input channel number, \(j\) ranges from \(0\) to \(C_{out} - 1\), \(W_{ij}\) corresponds to the \(i\)-th channel of the \(j\)-th filter and \(out_{j}\) corresponds to the \(j\)-th channel of the output. \(W_{ij}\) is a slice of kernel and it has shape \((\text{kernel_size[0]}, \text{kernel_size[1]})\), where \(\text{kernel_size[0]}\) and \(\text{kernel_size[1]}\) are the height and width of the convolution kernel. The full kernel has shape \((C_{out}, C_{in} / \text{group}, \text{kernel_size[0]}, \text{kernel_size[1]})\), where group is the group number to split the input in the channel dimension.
If the ‘pad_mode’ is set to be “pad”, the output height and width will be \(\left \lfloor{1 + \frac{H_{in} + \text{padding[0]} + \text{padding[1]} - \text{kernel_size[0]} - (\text{kernel_size[0]} - 1) \times (\text{dilation[0]} - 1) }{\text{stride[0]}}} \right \rfloor\) and \(\left \lfloor{1 + \frac{W_{in} + \text{padding[2]} + \text{padding[3]} - \text{kernel_size[1]} - (\text{kernel_size[1]} - 1) \times (\text{dilation[1]} - 1) }{\text{stride[1]}}} \right \rfloor\) respectively. Where \(dilation\) is Spacing between kernel elements, \(stride\) is The step length of each step, \(padding\) is zero-padding added to both sides of the input.
The first introduction can be found in paper Gradient Based Learning Applied to Document Recognition.
Note
On Ascend platform, \(group = 1\) must be satisfied.
- Parameters:
out_channel (int) – The number of output channel \(C_{out}\).
kernel_size (Union[int, tuple[int]]) – The data type is int or a tuple of 2 integers. Specifies the height and width of the 2D convolution window. Single int means the value is for both the height and the width of the kernel. A tuple of 2 ints means the first value is for the height and the other is for the width of the kernel.
mode (int) – Modes for different convolutions. The value is currently not used. Default: 1.
pad_mode (str) –
Specifies padding mode. The optional values are “same”, “valid” and “pad”. Default: “valid”.
same: Adopts the way of completion. The height and width of the output will be equal to the input x divided by stride. The padding will be evenly calculated in top and bottom, left and right possiblily. Otherwise, the last extra padding will be calculated from the bottom and the right side. If this mode is set, pad must be 0.
valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, pad must be 0.
pad: Implicit paddings on both sides of the input x. The number of pad will be padded to the input Tensor borders. pad must be greater than or equal to 0.
pad (Union(int, tuple[int])) – Implicit paddings on both sides of the input x. If pad is one integer, the paddings of top, bottom, left and right are the same, equal to pad. If pad is a tuple with four integers, the paddings of top, bottom, left and right will be equal to pad[0], pad[1], pad[2], and pad[3] accordingly. Default: 0.
stride (Union(int, tuple[int])) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default: 1.
dilation (Union(int, tuple[int])) – The data type is int or a tuple of 2 integers. Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater than or equal to 1 and bounded by the height and width of the input x. Default: 1.
group (int) – Splits input into groups. Default: 1.
data_format (str) – The optional value for data format, is ‘NHWC’ or ‘NCHW’. Default: “NCHW”.
- Inputs:
x (Tensor) - Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).
weight (Tensor) - Set size of kernel is \((\text{kernel_size[0]}, \text{kernel_size[1]})\), then the shape is \((C_{out}, C_{in}, \text{kernel_size[0]}, \text{kernel_size[1]})\).
- Outputs:
Tensor, the value that applied 2D convolution. The shape is \((N, C_{out}, H_{out}, W_{out})\).
- Raises:
TypeError – If kernel_size, stride, pad or dilation is neither an int nor a tuple.
TypeError – If out_channel or group is not an int.
ValueError – If kernel_size, stride or dilation is less than 1.
ValueError – If pad_mode is not one of ‘same’, ‘valid’ or ‘pad’.
ValueError – If pad is a tuple whose length is not equal to 4.
ValueError – If pad_mode it not equal to ‘pad’ and pad is not equal to (0, 0, 0, 0).
ValueError – If data_format is neither ‘NCHW’ nor ‘NHWC’.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.ones([10, 32, 32, 32]), mindspore.float32) >>> weight = Tensor(np.ones([32, 32, 3, 3]), mindspore.float32) >>> conv2d = ops.Conv2D(out_channel=32, kernel_size=3) >>> output = conv2d(x, weight) >>> print(output.shape) (10, 32, 30, 30)
-
class
tinyms.primitives.
Conv2DBackpropInput
(out_channel, kernel_size, pad_mode='valid', pad=0, pad_list=None, mode=1, stride=1, dilation=1, group=1, data_format='NCHW')[source]¶ The Conv2DBackpropInput interface is deprecated, please refer to
mindspore.ops.Conv2DTranspose
if you want to do unsampling.- Supported Platforms:
Deprecated
-
class
tinyms.primitives.
Conv2DTranspose
(out_channel, kernel_size, pad_mode='valid', pad=0, pad_list=None, mode=1, stride=1, dilation=1, group=1, data_format='NCHW')[source]¶ Calculates a 2D transposed convolution, which can be regarded as Conv2d for the gradient of the input, also called deconvolution, although it is not an actual deconvolution. Because it cannot restore the original input data completely, but it can restore the shape of the original input.
- Parameters:
out_channel (int) – The dimensionality of the output space.
kernel_size (Union[int, tuple[int]]) – The size of the convolution window.
pad_mode (str) – Modes to fill padding. It could be “valid”, “same”, or “pad”. Default: “valid”. Please refer to
mindspore.nn.Conv2dTranspose
for more specifications about pad_mode.pad (Union[int, tuple[int]]) – The pad value to be filled. Default: 0. If pad is an integer, the paddings of top, bottom, left and right are the same, equal to pad. If pad is a tuple of four integers, the padding of top, bottom, left and right equal to pad[0], pad[1], pad[2], and pad[3] correspondingly.
pad_list (Union[str, None]) – The pad list like (top, bottom, left, right). Default: None.
mode (int) – Modes for different convolutions. The value is currently not used. Default: 1.
stride (Union[int, tuple[int]]) – The stride to be applied to the convolution filter. Default: 1.
dilation (Union[int, tuple[int]]) – Specifies the dilation rate to be used for the dilated convolution. Default: 1.
group (int) – Splits input into groups. Default: 1.
data_format (str) – The format of input and output data. It should be ‘NHWC’ or ‘NCHW’. Default is ‘NCHW’.
- Inputs:
dout (Tensor) - the gradients with respect to the output of the convolution. The shape conforms to the default data_format \((N, C_{out}, H_{out}, W_{out})\).
weight (Tensor) - Set size of kernel is \((K_1, K_2)\), then the shape is \((C_{out}, C_{in}, K_1, K_2)\).
input_size (Tensor) - A tuple describes the shape of the input which conforms to the format \((N, C_{in}, H_{in}, W_{in})\).
- Outputs:
Tensor, the gradients with respect to the input of convolution. It has the same shape as the input.
- Raises:
TypeError – If kernel_size, stride, pad or dilation is neither an int nor a tuple.
TypeError – If out_channel or group is not an int.
ValueError – If kernel_size, stride or dilation is less than 1.
ValueError – If pad_mode is not one of ‘same’, ‘valid’ or ‘pad’.
ValueError – If padding is a tuple whose length is not equal to 4.
ValueError – If pad_mode it not equal to ‘pad’ and pad is not equal to (0, 0, 0, 0).
ValueError – If data_format is neither ‘NCHW’ nor ‘NHWC’.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> dout = Tensor(np.ones([10, 32, 30, 30]), mindspore.float32) >>> weight = Tensor(np.ones([32, 32, 3, 3]), mindspore.float32) >>> x = Tensor(np.ones([10, 32, 32, 32])) >>> conv2d_transpose_input = ops.Conv2DTranspose(out_channel=32, kernel_size=3) >>> output = conv2d_transpose_input(dout, weight, ops.shape(x)) >>> print(output.shape) (10, 32, 32, 32)
-
class
tinyms.primitives.
Conv3D
(out_channel, kernel_size, mode=1, pad_mode='valid', pad=0, stride=1, dilation=1, group=1, data_format='NCDHW')[source]¶ 3D convolution layer.
Applies a 3D convolution over an input tensor which is typically of shape \((N, C_{in}, D_{in}, H_{in}, W_{in})\) and output shape \((N, C_{out}, D_{out}, H_{out}, W_{out})\), where \(N\) is batch size, \(C\) is channel number, \(D\) is depth, \(H, W\) is feature height and width respectively. the output value of a layer is calculated as:
\[\operatorname{out}\left(N_{i}, C_{\text {out}_j}\right)=\operatorname{bias}\left(C_{\text {out}_j}\right)+ \sum_{k=0}^{C_{in}-1} ccor(\text {weight}\left(C_{\text {out}_j}, k\right), \operatorname{input}\left(N_{i}, k\right))\]where \(k\) is kernel, \(ccor\) is the cross-correlation , \(C_{in}\) is the channel number of the input, \(out_{j}\) corresponds to the jth channel of the output and \(j\) is in the range of \([0, C_{out}-1]\). \(\text{weight}(C_{\text{out}_j}, k)\) is a convolution kernel slice with shape \((\text{kernel_size[0]}, \text{kernel_size[1]}, \text{kernel_size[2]})\), where \(\text{kernel_size[0]}\), \(\text{kernel_size[1]}\) and \(\text{kernel_size[2]}\) are the depth, height and width of the convolution kernel respectively. \(\text{bias}\) is the bias parameter and \(\text{X}\) is the input tensor. The shape of full convolution kernel is \((C_{out}, C_{in} / \text{groups}, \text{kernel_size[0]}, \text{kernel_size[1]}, \text{kernel_size[2]})\), where groups is the number of groups to split input in the channel dimension.
For more details, please refer to the paper Gradient Based Learning Applied to Document Recognition .
If the ‘pad_mode’ is set to be “valid”, the output depth, height and width will be \(\left \lfloor{1 + \frac{D_{in} + 2 \times \text{padding} - \text{ks_d} - (\text{ks_d} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor\) and \(\left \lfloor{1 + \frac{H_{in} + 2 \times \text{padding} - \text{ks_h} - (\text{ks_h} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor\) and \(\left \lfloor{1 + \frac{W_{in} + 2 \times \text{padding} - \text{ks_w} - (\text{ks_w} - 1) \times (\text{dilation} - 1) }{\text{stride}}} \right \rfloor\) respectively. Where \(dilation\) is Spacing between kernel elements, \(stride\) is The step length of each step, \(padding\) is zero-padding added to both sides of the input.
- Parameters:
out_channel (int) – The number of output channel \(C_{out}\).
kernel_size (Union[int, tuple[int]]) – Specifies the depth, height and width of the 3D convolution window. It can be a single int or a tuple of 3 integers. Single int means the value is for the depth, height and width of the kernel. A tuple of 3 ints corresponds to the depth, height and width of the kernel respectively.
mode (int) – Modes for different convolutions. It is currently not used. Default: 1.
stride (Union[int, tuple[int]], optional) – The distance of kernel moving, it can be an int number that represents the depth, height and width of movement or a tuple of three int numbers that represent depth, height and width movement respectively. Default: 1.
pad_mode (str, optional) –
Specifies padding mode. The optional values are “same”, “valid” and “pad”. Default: “valid”.
same: Adopts the way of completion. The depth, height and width of the output will be equal to the input x divided by stride. The padding will be evenly calculated in head and tail, top and bottom, left and right directions possiblily. Otherwise, the last extra padding will be calculated from the tail, bottom and the right side. If this mode is set, pad must be 0.
valid: Adopts the way of discarding. The possible largest depth, height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, pad must be 0.
pad: Implicit paddings on both sides of the input in depth, height and width. The number of pad will be padded to the input Tensor borders. pad must be greater than or equal to 0.
pad (Union(int, tuple[int])) – The pad value to be filled. Default: 0. If pad is an integer, the paddings of head, tail, top, bottom, left and right are the same, equal to pad. If pad is a tuple of six integers, the padding of head, tail, top, bottom, left and right equal to pad[0], pad[1], pad[2], pad[3], pad[4] and pad[5] correspondingly.
dilation (Union[int, tuple[int]], optional) – The data type is int or a tuple of 3 integers \((dilation_d, dilation_h, dilation_w)\). Currently, dilation on depth only supports the case of 1 on Ascend backend. Specifies the dilation rate to use for dilated convolution. If set \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. The value ranges for the depth, height, and width dimensions are [1, D], [1, H], and [1, W], respectively. Default: 1.
group (int, optional) – The number of groups into which the filter is divided. in_channels and out_channels must be divisible by group. Default: 1.
data_format (str) – The optional value for data format. Currently only support “NCDHW”.
- Inputs:
x (Tensor) - Tensor of shape \((N, C_{in}, D_{in}, H_{in}, W_{in})\). Currently input data type only support float16 and float32.
weight (Tensor) - Set size of kernel is \((k_d, K_h, K_w)\), then the shape is \((C_{out}, C_{in}/groups, k_d, K_h, K_w)\). Currently weight data type only support float16 and float32.
bias (Tensor) - Tensor of shape \(C_{in}\). Currently, only support none.
- Outputs:
Tensor, the value that applied 3D convolution. The shape is \((N, C_{out}, D_{out}, H_{out}, W_{out})\).
- Raises:
TypeError – If out_channel or group is not an int.
TypeError – If kernel_size, stride, pad or dilation is neither an int nor a tuple.
ValueError – If out_channel, kernel_size, stride or dilation is less than 1.
ValueError – If pad is less than 0.
ValueError – If pad_mode is not one of ‘same’, ‘valid’ or ‘pad’.
ValueError – If pad is a tuple whose length is not equal to 6.
ValueError – If pad_mode is not equal to ‘pad’ and pad is not equal to (0, 0, 0, 0, 0, 0).
ValueError – If data_format is not ‘NCDHW’.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.ones([16, 3, 10, 32, 32]), mindspore.float16) >>> weight = Tensor(np.ones([32, 3, 4, 3, 3]), mindspore.float16) >>> conv3d = ops.Conv3D(out_channel=32, kernel_size=(4, 3, 3)) >>> output = conv3d(x, weight) >>> print(output.shape) (16, 32, 7, 30, 30)
-
class
tinyms.primitives.
Conv3DTranspose
(in_channel, out_channel, kernel_size, mode=1, pad_mode='valid', pad=0, stride=1, dilation=1, group=1, output_padding=0, data_format='NCDHW')[source]¶ Computes a 3D transposed convolution, which is also known as a deconvolution (although it is not an actual deconvolution).
Input is typically of shape \((N, C, D, H, W)\), where \(N\) is batch size, \(C\) is channel number, \(D\) is depth, \(H\) is height, \(W\) is width.
If the ‘pad_mode’ is set to be “pad”, the depth, height and width of output are defined as:
\[ \begin{align}\begin{aligned}D_{out} = (D_{in} - 1) \times \text{stride}[0] - 2 \times \text{pad}[0] + \text{dilation}[0] \times (\text{kernel_size}[0] - 1) + \text{output_padding}[0] + 1\\H_{out} = (H_{in} - 1) \times \text{stride}[1] - 2 \times \text{pad}[1] + \text{dilation}[1] \times (\text{kernel_size}[1] - 1) + \text{output_padding}[1] + 1\\W_{out} = (W_{in} - 1) \times \text{stride}[2] - 2 \times \text{pad}[2] + \text{dilation}[2] \times (\text{kernel_size}[2] - 1) + \text{output_padding}[2] + 1\end{aligned}\end{align} \]- Parameters:
in_channel (int) – The channel of the input x.
out_channel (int) – The channel of the weight x.
kernel_size (Union[int, tuple[int]]) – The data type is int or a tuple of 3 integers. Specifies the depth, height and width of the 3D convolution window. Single int means the value is for the depth, height and width of the kernel. A tuple of 3 ints means the first value is for the depth, the second value is for the height and the other is for the width of the kernel.
mode (int) – Modes for different convolutions. Default is 1. It is currently not used.
pad_mode (str) –
Specifies padding mode. The optional values are “same”, “valid”, “pad”. Default: “valid”.
same: Adopts the way of completion. The depth, height and width of the output will be equal to the input x divided by stride. The padding will be evenly calculated in head and tail, top and bottom, left and right directions possiblily. Otherwise, the last extra padding will be calculated from the tail, bottom and the right side. If this mode is set, pad must be 0.
valid: Adopts the way of discarding. The possible largest depth, height and width of output will be returned without padding. Extra pixels will be discarded. If this mode is set, pad and output_padding must be 0.
pad: Implicit paddings on both sides of the input in depth, height and width. The number of pad will be padded to the input Tensor borders. pad must be greater than or equal to 0.
pad (Union(int, tuple[int])) – The pad value to be filled. Default: 0. If pad is an integer, the paddings of head, tail, top, bottom, left and right are the same, equal to pad. If pad is a tuple of six integers, the padding of head, tail, top, bottom, left and right equal to pad[0], pad[1], pad[2], pad[3], pad[4] and pad[5] correspondingly.
stride (Union(int, tuple[int])) – The distance of kernel moving, an int number that represents the depth, height and width of movement are both strides, or a tuple of three int numbers that represent depth, height and width of movement respectively. Default: 1.
dilation (Union(int, tuple[int])) – Specifies the space to use between kernel elements. Default: 1.
group (int) – Splits input into groups. Default: 1. Only 1 is currently supported.
output_padding (Union(int, tuple[int])) – Add extra size to each dimension of the output. Default: 0.
data_format (str) – The optional value for data format. Currently only ‘NCDHW’ is supported.
- Inputs:
dout (Tensor) - The gradients with respect to the output of the convolution. The shape conforms to the default. data_format \((N, C_{in}, D_{out}, H_{out}, W_{out})\). Currently dout data type only supports float16 and float32.
weight (Tensor) - Set size of kernel is \((K_d, K_h, K_w)\), then the shape is \((C_{in}, C_{out}//group, K_d, K_h, K_w)\). Where \(group\) is the Args parameter, \(//\) is the symbol for integer division. Currently weight data type only supports float16 and float32.
bias (Tensor) - Tensor of shape \(C_{out}\). Currently, only support none. Default: None.
- Outputs:
Tensor, the gradients with respect to the input of convolution 3D. Tensor of shape \((N, C_{out}//group, D_{out}, H_{out}, W_{out})\), where \(group\) is the Args parameter.
- Raises:
TypeError – If in_channel, out_channel or group is not an int.
TypeError – If kernel_size, stride, pad , dilation or output_padding is neither an int not a tuple.
ValueError – If in_channel, out_channel, kernel_size, stride or dilation is less than 1.
ValueError – If pad is less than 0.
ValueError – If pad_mode is not one of ‘same’, ‘valid’ nor ‘pad’.
ValueError – If pad is a tuple whose length is not equal to 6.
ValueError – If pad_mode is not equal to ‘pad’ and pad is not equal to (0, 0, 0, 0, 0, 0).
ValueError – If data_format is not ‘NCDHW’.
TypeError – If data type of dout and weight is not float16.
ValueError – If bias is not none. The rank of dout and weight is not 5.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> dout = Tensor(np.ones([32, 16, 10, 32, 32]), mindspore.float16) >>> weight = Tensor(np.ones([16, 3, 4, 6, 2]), mindspore.float16) >>> conv3d_transpose = ops.Conv3DTranspose(in_channel=16, out_channel=3, kernel_size=(4, 6, 2)) >>> output = conv3d_transpose(dout, weight) >>> print(output.shape) (32, 3, 13, 37, 33)
-
class
tinyms.primitives.
Cos
[source]¶ Computes cosine of input element-wise.
Refer to
mindspore.ops.cos()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> cos = ops.Cos() >>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) >>> output = cos(x) >>> print(output) [0.971338 0.6748758 0.95233357 0.9959527]
-
class
tinyms.primitives.
Cosh
[source]¶ Computes hyperbolic cosine of input element-wise.
Refer to
mindspore.ops.cosh()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> cosh = ops.Cosh() >>> x = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) >>> output = cosh(x) >>> print(output) [1.0289385 1.364684 1.048436 1.0040528]
-
class
tinyms.primitives.
CountNonZero
(dims=None)[source]¶ Calculates the total number of non-zero entries in the input tensor along the specified dimensions.
Refer to
mindspore.ops.count_nonzero()
for more details.- Supported Platforms:
Ascend
CPU
Examples
>>> x = Tensor([[0, 0, 1], [1, 1, 2], [0, 0, 1]], dtype=mindspore.int64) >>> countnonzero = ops.CountNonZero(dims=[1]) >>> y = countnonzero(x) >>> print(y) [1 3 1]
-
class
tinyms.primitives.
CropAndResize
(method='bilinear', extrapolation_value=0.0)[source]¶ Extracts crops from the input image tensor and resizes them.
Note
In case that the output shape depends on crop_size, the crop_size must be constant. For now, the backward of the operator only support bilinear method, for other methods, will return 0.
- Parameters:
method (str, optional) – An optional string that specifies the sampling method for resizing. It can be “bilinear”, “nearest” or “bilinear_v2”. The option “bilinear” stands for standard bilinear interpolation algorithm, while “bilinear_v2” may result in better result in some cases. Default: “bilinear”
extrapolation_value (float, optional) – An optional float value used extrapolation, if applicable. Default: 0.0.
- Inputs:
x (Tensor) - The input image must be a 4-D tensor of shape \((batch, image\_height, image\_width, depth)\). Types allowed: int8, int16, int32, int64, float16, float32, float64, uint8, uint16.
boxes (Tensor) - A 2-D tensor of shape \((num\_boxes, 4)\). The i-th row of the tensor specifies the coordinates of a box in the box_ind[i] image and is specified in normalized coordinates [y1, x1, y2, x2]. A normalized coordinate value of y is mapped to the image coordinate at y * (image_height - 1), so as the [0, 1] interval of normalized image height is mapped to [0, image_height - 1] in image height coordinates. We do allow y1 > y2, in which case the sampled crop is an up-down flipped version of the original image. The width dimension is treated similarly. Normalized coordinates outside the [0, 1] range are allowed, in which case we use extrapolation_value to extrapolate the input image values. Types allowed: float32.
box_index (Tensor) - A 1-D tensor of shape \((num\_boxes)\) with int32 values in [0, batch). The value of box_index[i] specifies the image that the i-th box refers to. Types allowed: int32.
crop_size (Tuple[int]) - A tuple of two int32 elements: (crop_height, crop_width). Only constant value is allowed. All cropped image patches are resized to this size. The aspect ratio of the image content is not preserved. Both crop_height and crop_width need to be positive.
- Outputs:
A 4-D tensor of shape \((num\_boxes, crop\_height, crop\_width, depth)\) with type: float32.
- Raises:
TypeError – If x or boxes or box_index is not a Tensor.
TypeError – If crop_size is not a Tuple with two int32 elements.
TypeError – If dtype of boxes is not float or that of box_index is not int.
TypeError – If method is not a str.
TypeError – If extrapolation_value is not a float.
ValueError – If the shape rank of x is not 4.
ValueError – If the shape rank of boxes is not 2.
ValueError – If the second dim of boxes is not 4.
ValueError – If the shape rank of box_index is not 1.
ValueError – If the first dim of box_index is not equal to that of boxes.
ValueError – If existing element in box_index is out of range [0, batch).
ValueError – If the data of crop_size is not positive.
ValueError – If method is not one of ‘bilinear’, ‘nearest’, ‘bilinear_v2’.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class CropAndResizeNet(nn.Cell): ... def __init__(self, crop_size): ... super(CropAndResizeNet, self).__init__() ... self.crop_and_resize = ops.CropAndResize() ... self.crop_size = crop_size ... ... def construct(self, x, boxes, box_index): ... return self.crop_and_resize(x, boxes, box_index, self.crop_size) ... >>> BATCH_SIZE = 1 >>> NUM_BOXES = 5 >>> IMAGE_HEIGHT = 256 >>> IMAGE_WIDTH = 256 >>> CHANNELS = 3 >>> image = np.random.normal(size=[BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS]).astype(np.float32) >>> boxes = np.random.uniform(size=[NUM_BOXES, 4]).astype(np.float32) >>> box_index = np.random.uniform(size=[NUM_BOXES], low=0, high=BATCH_SIZE).astype(np.int32) >>> crop_size = (24, 24) >>> crop_and_resize = CropAndResizeNet(crop_size=crop_size) >>> output = crop_and_resize(Tensor(image), Tensor(boxes), Tensor(box_index)) >>> print(output.shape) (5, 24, 24, 3)
-
class
tinyms.primitives.
Cross
(dim=-65530)[source]¶ Returns the cross product of vectors in dimension dim of x1 and x2.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.cross()
for more details.- Parameters:
dim (int) – Spefcified dim along which to cumpute cross product with. Default: -65530.
- Inputs:
x1 (Tensor) - Input Tensor.
x2 (Tensor) - Another input Tensor, must have the same shape and the same type as x1, and the size of their dim dimension should be 3.
- Outputs:
Tensor, has the same shape and type as inputs.
- Supported Platforms:
Ascend
CPU
Examples
>>> import mindspore >>> import numpy as np >>> from mindspore import Tensor >>> from mindspore.common import dtype as mstype >>> import mindspore.ops as ops >>> cross = ops.Cross(dim = 0) >>> x1 = Tensor([1, 2, 3], mstype.int8) >>> x2 = Tensor([1, 2, 3], mstype.int8) >>> output = cross(x1, x2) >>> print(output) [0 0 0]
-
class
tinyms.primitives.
CumProd
(exclusive=False, reverse=False)[source]¶ Computes the cumulative product of the tensor x along axis. For example, if input is a vector of size N, the result will also be a vector of size N, with elements.
\[y_i = x_1 * x_2 * x_3 * ... * x_i\]- Parameters:
- Inputs:
x (Tensor[Number]) - The input tensor. \((N,*)\) where \(*\) means, any number of additional dimensions, its rank should be less than 8.
axis (int) - The dimensions to compute the cumulative product. Only constant value is allowed.
- Outputs:
Tensor, has the same shape and dtype as the x.
- Raises:
TypeError – If exclusive or reverse is not a bool.
TypeError – If axis is not an int.
ValueError – If axis is None.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> a, b, c, = 1, 2, 3 >>> x = Tensor(np.array([a, b, c]).astype(np.float32)) >>> op0 = ops.CumProd() >>> output0 = op0(x, 0) # output=[a, a * b, a * b * c] >>> op1 = ops.CumProd(exclusive=True) >>> output1 = op1(x, 0) # output=[1, a, a * b] >>> op2 = ops.CumProd(reverse=True) >>> output2 = op2(x, 0) # output=[a * b * c, b * c, c] >>> op3 = ops.CumProd(exclusive=True, reverse=True) >>> output3 = op3(x, 0) # output=[b * c, c, 1] >>> print(output0) [1. 2. 6.] >>> print(output1) [1. 1. 2.] >>> print(output2) [6. 6. 3.] >>> print(output3) [6. 3. 1.] >>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [5, 3, 5]]).astype(np.float32)) >>> output4 = op0(x, 0) >>> output5 = op0(x, 1) >>> print(output4) [[ 1. 2. 3.] [ 4. 10. 18.] [20. 30. 90.]] >>> print(output5) [[ 1. 2. 6.] [ 4. 20. 120.] [ 5. 15. 75.]]
-
class
tinyms.primitives.
CumSum
(exclusive=False, reverse=False)[source]¶ Computes the cumulative sum of input tensor along axis.
\[y_i = x_1 + x_2 + x_3 + ... + x_i\]- Parameters:
- Inputs:
input (Tensor) - The input tensor to accumulate.
axis (int) - The axis to accumulate the tensor’s value. Only constant value is allowed. Must be in the range [-rank(input), rank(input)).
- Outputs:
Tensor, the shape of the output tensor is consistent with the input tensor’s.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float32)) >>> cumsum = ops.CumSum() >>> # case 1: along the axis 0 >>> y = cumsum(x, 0) >>> print(y) [[ 3. 4. 6. 10.] [ 4. 10. 13. 19.] [ 8. 13. 21. 26.] [ 9. 16. 28. 35.]] >>> # case 2: along the axis 1 >>> y = cumsum(x, 1) >>> print(y) [[ 3. 7. 13. 23.] [ 1. 7. 14. 23.] [ 4. 7. 15. 22.] [ 1. 4. 11. 20.]] >>> # Next demonstrate exclusive and reverse, along axis 1 >>> # case 3: exclusive = True >>> cumsum = ops.CumSum(exclusive=True) >>> y = cumsum(x, 1) >>> print(y) [[ 0. 3. 7. 13.] [ 0. 1. 7. 14.] [ 0. 4. 7. 15.] [ 0. 1. 4. 11.]] >>> # case 4: reverse = True >>> cumsum = ops.CumSum(reverse=True) >>> y = cumsum(x, 1) >>> print(y) [[23. 20. 16. 10.] [23. 22. 16. 9.] [22. 18. 15. 7.] [20. 19. 16. 9.]]
-
class
tinyms.primitives.
Cummax
(axis)[source]¶ Returns the cumulative maximum of elements and the index.
Refer to
mindspore.ops.cummax()
for more details.- Supported Platforms:
GPU
CPU
Examples
>>> import mindspore >>> import numpy as np >>> from mindspore import Tensor >>> import mindspore.ops as ops >>> cummax = ops.Cummax(axis=0) >>> x = Tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float32)) >>> output = cummax(x) >>> 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]]
-
class
tinyms.primitives.
Cummin
(axis)[source]¶ Returns the cumulative minimum of elements and the index.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.cummin()
for more detail.- 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) >>> func = ops.Cummin(axis=0) >>> output = func(a) >>> print(output[0]) [-0.2284 -0.6628 -0.6628 -0.6628 -1.3298 -1.3298] >>> print(output[1]) [0 1 1 1 4 4]
-
class
tinyms.primitives.
CumulativeLogsumexp
(exclusive=False, reverse=False)[source]¶ Compute the cumulative log-sum-exp of the input tensor x along axis . For example, with all parameters at default values, if the input x is a tensor [a, b, c], the output will be [a, log(exp(a) + exp(b)), log(exp(a) + exp(b) + exp(c))].
- Parameters:
exclusive (bool, optional) – If true, the last element will be skipped during the calculation and thus an exclusive cumulative log-sum-exp will be performed. For example, this operation will output [-inf, a, log(exp(a) * exp(b))] with tensor [a, b, c] as the input. Note that the minimal value -inf, for performance reasons, is representable by the floating point type. Default: False.
reverse (bool, optional) – If true, the function accumulation values will be calculated after the elements of x on axis are flipped, and the calculation result will be flipped afterwards. For example, this operation will output [log(exp(c) + exp(b) + exp(a)), log(exp(c) + exp(b)), c] with tensor [a, b, c] as the input. Default: False.
- Inputs:
x (Tensor) - The input tensor. Must be one of the following types: float16, float32, float64. The dimension of x must greater than 0.
axis (Tensor) - A 0-D tensor describing the dimension to compute the cumulative product. Must be one of the following types: int64, int32, int16. Must be in the range [-rank(x), rank(x)). Default: 0.
- Outputs:
Tensor, has the same dtype and shape as the x.
- Raises:
TypeError – If x or axis not a Tensor.
TypeError – If dtype of x is not in [float16, float32, float64].
TypeError – If dtype of axis is not in [int16, int32, int64].
TypeError – If exclusive or reverse is not a bool.
ValueError – If the dimension of x is not greater than 0.
RuntimeError – If axis is out of range [-rank(x), rank(x)).
- Supported Platforms:
Ascend
CPU
GPU
Examples
>>> x = Tensor(np.array([1.0, 2.0, 3.0]).astype(np.float32)) >>> op = ops.CumulativeLogsumexp(exclusive=False, reverse=False) >>> output = op(x, Tensor(0)) >>> print(output) [1. 2.3132617 3.407606 ] >>> x = Tensor(np.array([1.0, 2.0, 3.0]).astype(np.float32)) >>> op = ops.CumulativeLogsumexp(exclusive=True, reverse=False) >>> output = op(x, Tensor(0)) >>> print(output) [-3.4028235e+38 1.0000000e+00 2.3132617e+00] >>> x = Tensor(np.array([1.0, 2.0, 3.0]).astype(np.float32)) >>> op = ops.CumulativeLogsumexp(exclusive=False, reverse=True) >>> output = op(x, Tensor(0)) >>> print(output) [3.407606 3.3132617 3. ] >>> x = Tensor(np.array([1.0, 2.0, 3.0]).astype(np.float32)) >>> op = ops.CumulativeLogsumexp(exclusive=True, reverse=True) >>> output = op(x, Tensor(0)) >>> print(output) [ 3.3132617e+00 3.0000000e+00 -3.4028235e+38]
-
class
tinyms.primitives.
Custom
(func, out_shape=None, out_dtype=None, func_type='hybrid', bprop=None, reg_info=None)[source]¶ Custom primitive is used for user defined operators and is to enhance the expressive ability of built-in primitives. You can construct a Custom object with a predefined function, which describes the computation logic of a user defined operator. You can also construct another Custom object with another predefined function if needed. Then these Custom objects can be directly used in neural networks. Detailed description and introduction of user-defined operators, including correct writing of parameters, please refer to Custom Operators Tutorial .
Warning
This is an experimental API that is subject to change.
Note
The supported platforms are determined by the input func_type. The supported platforms are as follows:
“hybrid”: supports [“Ascend”, “GPU”, “CPU”].
“akg”: supports [“Ascend”, “GPU”, “CPU”].
“tbe”: supports [“Ascend”].
“aot”: supports [“GPU”, “CPU”].
“pyfunc”: supports [“CPU”].
“julia”: supports [“CPU”].
“aicpu”: supports [“Ascend”].
- Parameters:
func (Union[function, str]) –
function: If func is of function type, then func should be a Python function which describes the computation logic of a user defined operator. The function can be one of the following:
A AKG operator implementation function, which can use ir builder/tvm compute/hybrid grammar.
A TBE operator implementation function.
A pure python function
An kernel decorated function written by the Hybrid DSL.
str: If func is of str type, then str should be a path of file along with a function name. This could be used when func_type is “aot” or “julia”.
for “aot”:
Currently “aot” supports GPU/CPU(linux only) platform. “aot” means ahead of time, in which case Custom directly launches user defined “xxx.so” file as an operator. Users need to compile a handwriting “xxx.cu”/”xxx.cc” file into “xxx.so” ahead of time, and offer the path of the file along with a function name.
”xxx.so” file generation:
1) GPU Platform: Given user defined “xxx.cu” file (ex. “{path}/add.cu”), use nvcc command to compile it.(ex. “nvcc –shared -Xcompiler -fPIC -o add.so add.cu”)
2) CPU Platform: Given user defined “xxx.cc” file (ex. “{path}/add.cc”), use g++/gcc command to compile it.(ex. “g++ –shared -fPIC -o add.so add.cc”)
Define a “xxx.cc”/”xxx.cu” file:
”aot” is a cross-platform identity. The functions defined in “xxx.cc” or “xxx.cu” share the same args. Typically, the function should be as:
int func(int nparam, void **params, int *ndims, int64_t **shapes, const char **dtypes, void *stream, void *extra)
Parameters:
nparam(int): total number of inputs plus outputs; suppose the operator has 2 inputs and 3 outputs, then nparam=5
params(void **): a pointer to the array of inputs and outputs’ pointer; the pointer type of inputs and outputs is void * ; suppose the operator has 2 inputs and 3 outputs, then the first input’s pointer is params[0] and the second output’s pointer is params[3]
ndims(int *): a pointer to the array of inputs and outputs’ dimension num; suppose params[i] is a 1024x1024 tensor and params[j] is a 77x83x4 tensor, then ndims[i]=2, ndims[j]=3.
shapes(int64_t **): a pointer to the array of inputs and outputs’ shapes(int64_t *); the ith input’s jth dimension’s size is shapes[i][j](0<=j<ndims[i]); suppose params[i] is a 2x3 tensor and params[j] is a 3x3x4 tensor, then shapes[i][0]=2, shapes[j][2]=4.
dtypes(const char **): a pointer to the array of inputs and outputs’ types(const char *); (ex. “float32”, “float16”, “float”, “float64”, “int”, “int8”, “int16”, “int32”, “int64”, “uint”, “uint8”, “uint16”, “uint32”, “uint64”, “bool”)
stream(void *): stream pointer, only used in cuda file
extra(void *): used for further extension
Return Value(int):
0: MindSpore will continue to run if this aot kernel is successfully executed
others: MindSpore will raise exception and exit
Examples: see details in tests/st/ops/graph_kernel/custom/aot_test_files/
Use it in Custom:
Custom(func="{dir_path}/{file_name}:{func_name}",...) (ex. Custom(func="./reorganize.so:CustomReorganize", out_shape=[1], out_dtype=mstype.float32, "aot"))
for “julia”:
Currently “julia” supports CPU(linux only) platform. For julia use JIT compiler, and julia support c api to call julia code. The Custom can directly launches user defined “xxx.jl” file as an operator. Users need to write a “xxx.jl” file which include modules and functions, and offer the path of the file along with a module name and function name.
Examples: see details in tests/st/ops/graph_kernel/custom/julia_test_files/
Use it in Custom:
Custom(func="{dir_path}/{file_name}:{module_name}:{func_name}",...) (ex. Custom(func="./add.jl:Add:add", out_shape=[1], out_dtype=mstype.float32, "julia"))
out_shape (Union[function, list, tuple]) –
The output shape infer function or the value of output shape of func. Default: None.
If func has single output, then the value of output shape is a list or tuple of int.
If func has multiple outputs, then the value of output shape is a tuple, each item represents the shape of each output.
The input can be None only when the func_type input is “hybrid”. In this case, the automatic infer shape mechanic will be enabled.
out_dtype (Union[function,
mindspore.dtype
, tuple[mindspore.dtype
]]) –The output data type infer function or the value of output data type of func. Default: None.
If func has single output, then the value of output shape is a mindspore.dtype.
If func has multiple outputs, then the value of output shape is a tuple of mindspore.dtype, each item represents the data type of each output.
The input can be None only when the func_type input is “hybrid”. In this case, the automatic infer value mechanic will be enabled.
func_type (str) –
The implementation type of func, should be one of
[“hybrid”, “akg”, “tbe”, “aot”, “pyfunc”, “julia”, “aicpu”].
Each func_type only supports specific platforms(targets). Default: “hybrid”. The supported platforms of func_type:
”hybrid”: supports [“Ascend”, “GPU”, “CPU”].
”akg”: supports [“Ascend”, “GPU”, “CPU”].
”tbe”: supports [“Ascend”].
”aot”: supports [“GPU”, “CPU”].
”pyfunc”: supports [“CPU”].
”julia”: supports [“CPU”].
”aicpu”: supports [“Ascend”].
bprop (function) – The back propagation function of func. Default: None.
reg_info (Union[str, dict, list, tuple]) –
Represents the registration information(reg info) of func with json format of type str or dict. The reg info specifies supported data types and formats of inputs and outputs, attributes and target of func. Default: None.
If reg info is a list or tuple, then each item should be with json format of type str or dict, which represents the registration information of func in a specific target. You need to invoke CustomRegOp or the subclass of RegOp to generate the reg info for func. Then you can invoke custom_info_register to bind the reg info to func or just pass the reg info to reg_info parameter. The reg_info parameter takes higher priority than custom_info_register and the reg info in a specific target will be registered only once.
If reg info is not set, then we will infer the data types and formats from the inputs of Custom operator.
Please note that, if func_type is “tbe” or the func only supports some specified data types and formats, or it has attribute inputs, then you should set the reg info for func.
- Inputs:
input (Union(tuple, list)) - The input tuple or list is made up of multiple tensors, and attributes value(optional).
- Outputs:
Tensor or tuple[Tensor], execution results.
- Raises:
TypeError – If the type of func is invalid or the type of register information for func is invalid.
ValueError – If func_type is invalid.
ValueError – If the register information is invalid, including the target is not supported, the input numbers or the attributes of func differs in different targets.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor, ops >>> from mindspore.ops import CustomRegOp, custom_info_register, DataType, kernel >>> from mindspore import dtype as mstype >>> from mindspore.nn import Cell >>> input_x = Tensor(np.ones([16, 16]).astype(np.float32)) >>> input_y = Tensor(np.ones([16, 16]).astype(np.float32)) >>> >>> # Example, func_type = "hybrid" >>> # This is the default func_type in Custom, >>> # and both out_shape and out_dtype can be None(default value). >>> # In this case, the input func must be a function written in the Hybrid DSL >>> # and decorated by @kernel. >>> @kernel ... def add_script(a, b): ... c = output_tensor(a.shape, a.dtype) ... for i0 in range(a.shape[0]): ... for i1 in range(a.shape[1]): ... c[i0, i1] = a[i0, i1] + b[i0, i1] ... return c >>> >>> test_op_hybrid = ops.Custom(add_script) >>> output = test_op_hybrid(input_x, input_y) >>> # the result will be a 16 * 16 tensor with all elements 2 >>> print(output.shape) (16, 16) >>> # Example, func_type = "tbe" >>> square_with_bias_op_info = CustomRegOp() \ ... .fusion_type("OPAQUE") \ ... .attr("bias", "required", "float") \ ... .input(0, "x") \ ... .output(0, "y") \ ... .dtype_format(DataType.F32_Default, DataType.F32_Default) \ ... .dtype_format(DataType.F16_Default, DataType.F16_Default) \ ... .target("Ascend") \ ... .get_op_info() >>> >>> @custom_info_register(square_with_bias_op_info) ... def square_with_bias(input_x, output_y, bias=0.0, kernel_name="square_with_bias"): ... import te.lang.cce ... from te import tvm ... from topi.cce import util ... ... shape = input_x.get("shape") ... dtype = input_x.get("dtype").lower() ... ... shape = util.shape_refine(shape) ... data = tvm.placeholder(shape, name="data", dtype=dtype) ... ... with tvm.target.cce(): ... res0 = te.lang.cce.vmul(data, data) ... res = te.lang.cce.vadds(res0, bias) ... sch = te.lang.cce.auto_schedule(res) ... ... config = {"print_ir": False, ... "name": kernel_name, ... "tensor_list": [data, res]} ... ... te.lang.cce.cce_build_code(sch, config) >>> >>> def test_tbe(): ... square_with_bias = ops.Custom(square_with_bias, out_shape=lambda x, _: x, \ ... out_dtype=lambda x, _: x, func_type="tbe") ... res = self.square_with_bias(input_x, 1.0) ... return res >>> >>> # Example, func_type = "aicpu" >>> resize_bilinear_op_info = CustomRegOp("ResizeBilinear") \ ... .fusion_type("OPAQUE") \ ... .input(0, "input", "required") \ ... .output(1, "output", "required") \ ... .attr("align_corners", "required", "bool") \ ... .attr("cust_aicpu", "optional", "str", "aicpu_kernels") \ ... .dtype_format(DataType.F32_Default, DataType.F32_Default) \ ... .dtype_format(DataType.F16_Default, DataType.F32_Default) \ ... .target("Ascend") \ ... .get_op_info() >>> >>> @custom_info_register(resize_bilinear_op_info) ... def resize_bilinear_aicpu(): ... return >>> >>> def test_aicpu(x): ... resize_bilinear_op = ops.Custom(resize_bilinear_aicpu, out_shape=[1, 1, 9, 9], \ ... out_dtype=mstype.float32, func_type="aicpu") ... res = resize_bilinear_op(x, True, "aicpu_kernels") ... return res >>> >>> # Example, func_type = "aot" >>> def test_aot(x, y, out_shapes, out_types): ... program = ops.Custom("./reorganize.so:CustomReorganize", out_shapes, out_types, "aot") ... out = program(x, y) ... return out >>> >>> # Example, func_type = "pyfunc" >>> def func_multi_output(x1, x2): ... return (x1 + x2), (x1 - x2) >>> >>> test_pyfunc = ops.Custom(func_multi_output, lambda x, _: (x, x), lambda x, _: (x, x), "pyfunc") >>> output = test_pyfunc(input_x, input_y) >>> >>> # Example, func_type = "julia" >>> # julia code: >>> # add.jl >>> # module Add >>> # function add(x, y, z) >>> # z .= x + y >>> # return z >>> # end >>> # end >>> def test_julia(x, y, out_shapes, out_types): ... program = ops.Custom("./add.jl:Add:add", out_shapes, out_types, "julia") ... out = program(x, y) ... return out
-
class
tinyms.primitives.
DType
[source]¶ Returns the data type of the input tensor as mindspore.dtype.
- Inputs:
input_x (Tensor) - Input Tensor.
- Outputs:
mindspore.dtype, the data type of a tensor.
- Raises:
TypeError – If input_x is not a Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_tensor = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32) >>> output = ops.DType()(input_tensor) >>> print(output) Float32
-
class
tinyms.primitives.
DataFormatDimMap
(src_format='NHWC', dst_format='NCHW')[source]¶ Returns the dimension index in the destination data format given in the source data format.
- Parameters:
- Inputs:
input_x (Tensor) - A Tensor, each element is used as a dimension index of the source data format. The suggested values are in the range [-4, 4). Only supports int32.
- Outputs:
Tensor, Return the dimension index in the given target data format, has the same data type and shape as the input_x.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor([0, 1, 2, 3], mindspore.int32) >>> dfdm = ops.DataFormatDimMap() >>> output = dfdm(input_x) >>> print(output) [0 3 1 2]
-
class
tinyms.primitives.
DataFormatVecPermute
(src_format='NHWC', dst_format='NCHW')[source]¶ Converts the input tensor from the src_format to the dst_format by permuting its dimensions.
- Parameters:
- Inputs:
input_x (Tensor) - A Tensor of shape \((4, )\) or \((4, 2)\) in source data format. Supports int32 and int64 datatype.
- Outputs:
Tensor, has the same data type and shape as the input_x.
- Raises:
TypeError – If input_x is not a Tensor.
TypeError – If dtype of input_x is neither int32 nor int64.
ValueError – If src_format or dst_format is not a str in [‘NHWC’, ‘NCHW’].
ValueError – If input_x shape is not \((4, )\) or \((4, 2)\).
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self, src_format="NHWC", dst_format="NCHW"): ... super().__init__() ... self.op = ops.DataFormatVecPermute(src_format, dst_format) ... def construct(self, x): ... return self.op(x) ... >>> net = Net() >>> x = Tensor(np.array([1, 2, 3, 4]).astype(np.int32)) >>> output = net(x) >>> print(output) [1 4 2 3]
-
class
tinyms.primitives.
DeformableOffsets
(strides, pads, ksize, dilations=(1, 1, 1, 1), data_format='NCHW', deformable_groups=1, modulated=True)[source]¶ Computes the deformed convolution output with the expected input.
Refer to
mindspore.ops.deformable_conv2d()
for more details.- Supported Platforms:
Ascend
GPU
CPU
-
class
tinyms.primitives.
Depend
[source]¶ Depend is used for processing dependency operations.
In most scenarios, if operators have IO side effects or memory side effects, they will be executed according to the user’s semantics. In some scenarios, if the two operators A and B have no order dependency, and A must be executed before B, we recommend using Depend to specify their execution order. The usage method is as follows:
a = A(x) ---> a = A(x) b = B(y) ---> y = Depend(y, a) ---> b = B(y)
- Inputs:
value (Tensor) - the real value to return for depend operator.
expr (Expression) - the expression to execute with no outputs.
- Outputs:
Tensor, the value passed by last operator.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> import mindspore >>> import mindspore.nn as nn >>> import mindspore.ops as ops >>> from mindspore import Tensor >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.softmax = ops.Softmax() ... self.depend = ops.Depend() ... ... def construct(self, x, y): ... mul = x * y ... y = self.depend(y, mul) ... ret = self.softmax(y) ... return ret ... >>> x = Tensor(np.ones([4, 5]), dtype=mindspore.float32) >>> y = Tensor(np.ones([4, 5]), dtype=mindspore.float32) >>> net = Net() >>> output = net(x, y) >>> print(output) [[0.2 0.2 0.2 0.2 0.2] [0.2 0.2 0.2 0.2 0.2] [0.2 0.2 0.2 0.2 0.2] [0.2 0.2 0.2 0.2 0.2]]
-
class
tinyms.primitives.
DepthToSpace
(block_size)[source]¶ Rearrange blocks of depth data into spatial dimensions.
This is the reverse operation of SpaceToDepth.
The depth of output tensor is \(input\_depth / (block\_size * block\_size)\).
The output tensor’s height dimension is \(height * block\_size\).
The output tensor’s weight dimension is \(weight * block\_size\).
The input tensor’s depth must be divisible by block_size * block_size. The data format is “NCHW”.
- Parameters:
block_size (int) – The block size used to divide depth data. It must be >= 2.
- Inputs:
x (Tensor) - The target tensor. It must be a 4-D tensor with shape \((N, C_{in}, H_{in}, W_{in})\). The data type is Number.
- Outputs:
Tensor of shape \((N, C_{in} / \text{block_size} ^ 2, H_{in} * \text{block_size}, W_{in} * \text{block_size})\).
- Raises:
TypeError – If block_size is not an int.
ValueError – If block_size is less than 2.
ValueError – If length of shape of x is not equal to 4.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.random.rand(1, 12, 1, 1), mindspore.float32) >>> block_size = 2 >>> depth_to_space = ops.DepthToSpace(block_size) >>> output = depth_to_space(x) >>> print(output.shape) (1, 3, 2, 2)
-
class
tinyms.primitives.
DepthwiseConv2dNative
(channel_multiplier, kernel_size, mode=3, pad_mode='valid', pad=0, stride=1, dilation=1, group=1)[source]¶ DepthwiseConv2dNative will be deprecated in the future. Please use
mindspore.nn.Conv2d
instead.- Supported Platforms:
Deprecated
-
class
tinyms.primitives.
Diag
[source]¶ Constructs a diagonal tensor with a given diagonal values.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.diag()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor([1, 2, 3, 4]).astype('int32') >>> diag = ops.Diag() >>> output = diag(input_x) >>> print(output) [[1 0 0 0] [0 2 0 0] [0 0 3 0] [0 0 0 4]]
-
class
tinyms.primitives.
DiagPart
[source]¶ Extracts the diagonal elements from the given Tensor.
If the input_x is a Tensor of shape \([D_1,..., D_k, D_1,..., D_k]\), then the output will be a Tensor of rank k of shape \([D_1,..., D_k]\) where: \(output[i_1,..., i_k] = input_x[i_1,..., i_k, i_1,..., i_k]\).
- Inputs:
input_x (Tensor) - The rank of input tensor is 2k(k > 0).
- Outputs:
Tensor, the extracted diagonal has the same dtype as the input_x.
- Raises:
TypeError – If input_x is not a Tensor.
ValueError – If rank of input_x is not even or zero.
ValueError – If input_shape[i] is not equal to input_shape[i + len(input_shape)/2].
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor([[1, 0, 0, 0], ... [0, 2, 0, 0], ... [0, 0, 3, 0], ... [0, 0, 0, 4]]) >>> diag_part = ops.DiagPart() >>> output = diag_part(input_x) >>> print(output) [1 2 3 4]
-
class
tinyms.primitives.
Digamma
[source]¶ Computes the grad of the lgamma function on input.
\[P(x) = grad(ln(gamma(x)))\]Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
x (Tensor) - The input tensor. With type of float16 or float32 or float64.
- Outputs:
Tensor, has the same dtype as x.
- Raises:
- Supported Platforms:
GPU
CPU
Examples
>>> x = Tensor(np.array([1.5, 0.5, 9]).astype(np.float16)) >>> digamma = ops.Digamma() >>> output = digamma(x) >>> print(output) [ 0.0365 -1.964 2.14 ]
-
class
tinyms.primitives.
Dilation2D
(stride, dilation, pad_mode='SAME', data_format='NCHW')[source]¶ Computes the grayscale dilation of 4-D input and 3-D filters tensors.
Applies a 2D dilation over an input tensor which is typically of shape \((N, C_{in}, H_{in}, W_{in})\), where \(N\) is batch size, \(H\) is height, \(W\) is width, \(C\) is channel number. Given kernel size \(ks = (h_{ker}, w_{ker})\), stride \(s = (s_0, s_1)\) and dilation \(d = (d_0, d_1)\), the operation is as follows:
\[\text{output}(N_i, C_j, h, w) = \max_{m=0, \ldots, h_{ker}-1} \max_{n=0, \ldots, w_{ker}-1} \text{input}(N_i, C_j, s_0 \times h + d_0 \times m, s_1 \times w + d_1 \times n) + \text{filter}(C_j, m, n)\]Warning
This is an experimental API that is subjected to change or deletion.
Note
If the input data type is float32, this operator is still executed in float16 mode.
- Parameters:
stride (Union(int, tuple[int])) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively, or a tuple of four int numbers when data_format is ‘NCHW’ represents [1, 1, stride_height, stride_width].
dilation (Union(int, tuple[int])) – The data type is int or a tuple of 2 integers or a tuple of 4 integers. Specifies the dilation rate to use for dilated convolution. If set to be \(k > 1\), there will be \(k - 1\) pixels skipped for each sampling location. Its value must be greater or equal to 1 and bounded by the height and width of the input x.
pad_mode (str, optional) –
Specifies padding mode. The optional values are “same”, “valid”. Default: “same”. Both upper and lower case are supported.
same: Adopts the way of completion. The height and width of the output will be the same as the input x.
valid: Adopts the way of discarding. The possible largest height and width of output will be returned without padding. Extra pixels will be discarded.
data_format (str, optional) – The value for data format, only ‘NCHW’ is supported at present. Default: “NCHW”.
- Inputs:
x (Tensor) - Input data. A 4-D Tensor, its shape must be \((N, C_{in}, H_{in}, W_{in})\).
filter (Tensor) - A three dimension tensor with the same type as input. The shape must be \((C_{in}, H_{filter}, W_{filter})\).
- Outputs:
Tensor, the value that applied 2D dilation. The shape is \((N, C_{out}, H_{out}, W_{out})\) which is not necessarily the same as the input x, the type is the same as the input x.
- Raises:
TypeError – If type of x or filter is not the type in [uint8, uint16, uint32, uint64, int8, int16, int32, int64, float16, float32, float64].
TypeError – If stride or dilation is not an int number or a tuple of two or four int numbers.
ValueError – If the length of stride or dilation is neither two nor four when they are tuple.
ValueError – If stride or dilation shape is not (1, 1, height, width) when it is a tuple of four int numbers.
ValueError – If stride is not in the range of [1, 255].
ValueError – If dilation is less than 1.
ValueError – If pad_mode is not a str of ‘same’, ‘valid’, ‘SAME’ or ‘VALID’.
ValueError – If data_format is not the str of ‘NCHW’.
- Supported Platforms:
GPU
CPU
Examples
>>> x = Tensor(np.ones([10, 5, 32, 32]), mindspore.float16) >>> filter = Tensor(np.ones([5, 3, 3]), mindspore.float16) >>> dilation2d = ops.Dilation2D(stride=1, dilation=1, pad_mode='VALID') >>> output = dilation2d(x, filter) >>> print(output.shape) (10, 5, 30, 30)
-
class
tinyms.primitives.
Div
[source]¶ Computes the quotient of dividing the first input tensor by the second input tensor element-wise.
\[out_{i} = \frac{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.
- Inputs:
x (Union[Tensor, number.Number, bool]) - The first input is a number.Number or a bool or a tensor whose data type is number or bool_.
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_.
- Outputs:
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:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> # case 1 :has same data type and shape of the two inputs >>> x = Tensor(np.array([-4.0, 5.0, 6.0]), mindspore.float32) >>> y = Tensor(np.array([3.0, 2.0, 3.0]), mindspore.float32) >>> div = ops.Div() >>> output = div(x, y) >>> print(output) [-1.3333334 2.5 2. ] >>> # case 2 : different data type and shape of the two inputs >>> x = Tensor(np.array([-4.0, 5.0, 6.0]), mindspore.float32) >>> y = Tensor(2, mindspore.int32) >>> output = div(x, y) >>> print(output) [-2. 2.5 3.] >>> print(output.dtype) Float32
-
class
tinyms.primitives.
DivNoNan
[source]¶ Operates a safe division between x1 and x2 element-wise. Returns 0 if element of x2 is zero.
Inputs of x1 and x2 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.
\[\begin{split}output_{i} = \begin{cases} 0, & \text{ if } x2_{i} = 0\\ x1_{i} / x2_{i}, & \text{ if } x2_{i} \ne 0 \end{cases}\end{split}\]- Inputs:
x1 (Union[Tensor, number.Number, bool]) - The first input is a number.Number or a bool or a tensor whose data type is number or bool_ <https://www.mindspore.cn/docs/en/r2.0/api_python/mindspore.html#mindspore.dtype> _.
x2 (Union[Tensor, number.Number, bool]) - The second input is a number.Number or a bool when the first input is a bool 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_.
- Outputs:
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 x1 and x2 is not a number.Number or a bool or a Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x1 = Tensor(np.array([-1.0, 0., 1.0, 5.0, 6.0]), mindspore.float32) >>> x2 = Tensor(np.array([0., 0., 0., 2.0, 3.0]), mindspore.float32) >>> div_no_nan = ops.DivNoNan() >>> output = div_no_nan(x1, x2) >>> print(output) [0. 0. 0. 2.5 2. ]
-
class
tinyms.primitives.
Dropout
(keep_prob=0.5, Seed0=0, Seed1=0)[source]¶ During training, randomly zeroes some of the elements of the input tensor with probability 1-keep_prob from a Bernoulli distribution. It plays the role of reducing neuron correlation and avoid overfitting.
Refer to
mindspore.ops.dropout()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> dropout = ops.Dropout(keep_prob=0.5) >>> x = Tensor(np.ones([1, 2, 3, 4, 5]), mindspore.float32) >>> output, mask = dropout(x) >>> print(output.shape, mask.shape, mask.dtype) (1, 2, 3, 4, 5) (16,) UInt8
-
class
tinyms.primitives.
Dropout2D
(keep_prob=0.5)[source]¶ During training, randomly zeroes some channels of the input tensor with probability 1-keep_prob from a Bernoulli distribution(For a 4-dimensional tensor with a shape of NCHW, the channel feature map refers to a 2-dimensional feature map with the shape of HW).
Dropout2D can improve the independence between channel feature maps.
Note
The keep probability \(keep\_prob\) is equal to \(1 - p\) in
mindspore.ops.dropout2d()
.- Parameters:
keep_prob (float, optional) – The keep probability of a channel, between 0 and 1, e.g. keep_prob = 0.8, means dropping out 20% of channels. Default: 0.5.
- Inputs:
x (Tensor) - A 4-D tensor with shape \((N, C, H, W)\), where N is the batch size, C is the number of channels, H is the feature height, and W is the feature width. The data type should be int8, int16, int32, int64, float16 or float32.
- Outputs:
output (Tensor) - With the same shape and data type as x.
mask (Tensor) - With the same shape as x and the data type is bool.
- Raises:
TypeError – If x is not a Tensor.
TypeError – If dtype of x is not int8, int16, int32, int64, float16, float32 or float64.
TypeError – If the data type of keep_prob is not float.
ValueError – If keep_prob is out of the range [0.0, 1.0].
ValueError – If x shape is not 4D.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> dropout = ops.Dropout2D(keep_prob=0.5) >>> x = Tensor(np.ones([2, 1, 2, 3]), mindspore.float32) >>> output, mask = dropout(x) >>> print(output.shape) (2, 1, 2, 3)
-
class
tinyms.primitives.
Dropout3D
(keep_prob=0.5)[source]¶ During training, randomly zeroes some channels of the input tensor with probability 1-keep_prob from a Bernoulli distribution(For a 5-dimensional tensor with a shape of NCDHW, the channel feature map refers to a 3-dimensional feature map with a shape of DHW).
Note
The keep probability \(keep\_prob\) is equal to \(1 - p\) in
mindspore.ops.dropout3d()
.Dropout3D can improve the independence between channel feature maps.
- Parameters:
keep_prob (float) – The keep probability of a channel, between 0 and 1, e.g. keep_prob = 0.8, means dropping out 20% of channels. Default: 0.5.
- Inputs:
x (Tensor) - A 5-D tensor with shape \((N, C, D, H, W)\), where N is the batch size, C is the number of channels, D is the feature depth, H is the feature height, and W is the feature width. The data type should be int8, int16, int32, int64, float16 or float32.
- Outputs:
output (Tensor) - With the same shape and data type as x.
mask (Tensor) - With the same shape as x and the data type is bool.
- Raises:
TypeError – If the data type of keep_prob is not float.
ValueError – If keep_prob is out of the range [0.0, 1.0]; or if the dim of input is not 5-D.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> dropout = ops.Dropout3D(keep_prob=0.5) >>> x = Tensor(np.ones([2, 1, 2, 1, 2]), mindspore.float32) >>> output, mask = dropout(x) >>> print(output.shape) (2, 1, 2, 1, 2)
-
class
tinyms.primitives.
DropoutDoMask
[source]¶ The DropoutDoMask interface is deprecated, please use the
mindspore.ops.Dropout
instead.- Supported Platforms:
Deprecated
-
class
tinyms.primitives.
DropoutGenMask
(Seed0=0, Seed1=0)[source]¶ The DropoutGenMask interface is deprecated, please use the
mindspore.ops.Dropout
instead.- Supported Platforms:
Deprecated
-
class
tinyms.primitives.
DynamicGRUV2
(direction='UNIDIRECTIONAL', cell_depth=1, keep_prob=1.0, cell_clip=-1.0, num_proj=0, time_major=True, activation='tanh', gate_order='rzh', reset_after=True, is_training=True)[source]¶ Applies a single-layer gated recurrent unit (GRU) to an input sequence.
\[\begin{split}\begin{array}{ll} r_{t+1} = \sigma(W_{ir} x_{t+1} + b_{ir} + W_{hr} h_{(t)} + b_{hr}) \\ z_{t+1} = \sigma(W_{iz} x_{t+1} + b_{iz} + W_{hz} h_{(t)} + b_{hz}) \\ n_{t+1} = \tanh(W_{in} x_{t+1} + b_{in} + r_{t+1} * (W_{hn} h_{(t)}+ b_{hn})) \\ h_{t+1} = (1 - z_{t+1}) * n_{t+1} + z_{t+1} * h_{(t)} \end{array}\end{split}\]where \(h_{t+1}\) is the hidden state at time t+1, \(x_{t+1}\) is the input at time t+1, \(h_{t}\) is the hidden state of the layer at time t or the initial hidden state at time 0. \(r_{t+1}\), \(z_{t+1}\), \(n_{t+1}\) are the reset, update, and new gates, respectively. \(W\), \(b\) are the weight parameter and the deviation parameter respectively. \(\sigma\) is the sigmoid function, and \(*\) is the Hadamard product.
- Parameters:
direction (str) – A string identifying the direction in the operator. Default: ‘UNIDIRECTIONAL’. Only ‘UNIDIRECTIONAL’ is currently supported.
cell_depth (int) – An integer identifying the cell depth in the operator. Default: 1.
keep_prob (float) – A float identifying the keep prob in the operator. Default: 1.0.
cell_clip (float) – A float identifying the cell clip in the operator. Default: -1.0.
num_proj (int) – An integer identifying the number projection in the operator. Default: 0.
time_major (bool) – A bool identifying the time major in the operator. Default: True.
activation (str) – A string identifying the type of activation function in the operator. Default: ‘tanh’. Only ‘tanh’ is currently supported.
gate_order (str) – A string identifying the gate order in weight and bias. Default: ‘rzh’. ‘zrh’ is another option. Here, ‘rzh’ means the gate order is: reset gate, update gate, hidden gate. ‘zrh’ means the gate order is: update gate, reset gate, hidden gate.
reset_after (bool) – A bool identifying whether to apply reset gate after matrix multiplication. Default: True.
is_training (bool) – A bool identifying is training in the operator. Default: True.
- Inputs:
x (Tensor) - Current words. Tensor of shape \((\text{num_step}, \text{batch_size}, \text{input_size})\). The data type must be float16.
weight_input (Tensor) - Input-hidden weight \(W_{\{ir,iz,in\}}\). Tensor of shape \((\text{input_size}, 3 \times \text{hidden_size})\). The data type must be float16.
weight_hidden (Tensor) - Hidden-hidden weight \(W_{\{hr,hz,hn\}}\). Tensor of shape \((\text{hidden_size}, 3 \times \text{hidden_size})\). The data type must be float16.
bias_input (Tensor) - Input-hidden bias \(b_{\{ir,iz,in\}}\). Tensor of shape \((3 \times \text{hidden_size})\), or None. Has the same data type with input init_h.
bias_hidden (Tensor) - Hidden-hidden bias \(b_{\{hr,hz,hn\}}\). Tensor of shape \((3 \times \text{hidden_size})\), or None. Has the same data type with input init_h.
seq_length (Tensor) - The length of each batch. Tensor of shape \((\text{batch_size})\). Only None is currently supported.
init_h (Tensor) - Hidden state of initial time. Tensor of shape \((\text{batch_size}, \text{hidden_size})\). The data type must be float16 or float32.
- Outputs:
y (Tensor) - A Tensor of shape:
y_shape = \((num\_step, batch\_size, min(hidden\_size, num\_proj))\): If num_proj > 0,
y_shape = \((num\_step, batch\_size, hidden\_size)\): If num_proj = 0.
Has the same data type with input bias_type.
output_h (Tensor) - A Tensor of shape \((\text{num_step}, \text{batch_size}, \text{hidden_size})\). Has the same data type with input bias_type.
update (Tensor) - A Tensor of shape \((\text{num_step}, \text{batch_size}, \text{hidden_size})\). Has the same data type with input bias_type.
reset (Tensor) - A Tensor of shape \((\text{num_step}, \text{batch_size}, \text{hidden_size})\). Has the same data type with input bias_type.
new (Tensor) - A Tensor of shape \((\text{num_step}, \text{batch_size}, \text{hidden_size})\). Has the same data type with input bias_type.
hidden_new (Tensor) - A Tensor of shape \((\text{num_step}, \text{batch_size}, \text{hidden_size})\). Has the same data type with input bias_type.
A note about the bias_type:
If bias_input and bias_hidden both are None, bias_type is the data type of init_h.
If bias_input is not None, bias_type is the data type of bias_input.
If bias_input is None and bias_hidden is not None, bias_type is the data type of bias_hidden.
- Raises:
TypeError – If direction, activation or gate_order is not a str.
TypeError – If cell_depth or num_proj is not an int.
TypeError – If keep_prob or cell_clip is not a float.
TypeError – If time_major, reset_after or is_training is not a bool.
TypeError – If x, weight_input, weight_hidden, bias_input, bias_hidden, seq_length or ini_h is not a Tensor.
TypeError – If dtype of x, weight_input or weight_hidden is not float16.
TypeError – If dtype of init_h is neither float16 nor float32.
- Supported Platforms:
Ascend
Examples
>>> x = Tensor(np.random.rand(2, 8, 64).astype(np.float16)) >>> weight_i = Tensor(np.random.rand(64, 48).astype(np.float16)) >>> weight_h = Tensor(np.random.rand(16, 48).astype(np.float16)) >>> bias_i = Tensor(np.random.rand(48).astype(np.float16)) >>> bias_h = Tensor(np.random.rand(48).astype(np.float16)) >>> init_h = Tensor(np.random.rand(8, 16).astype(np.float16)) >>> dynamic_gru_v2 = ops.DynamicGRUV2() >>> output = dynamic_gru_v2(x, weight_i, weight_h, bias_i, bias_h, None, init_h) >>> print(output[0].shape) (2, 8, 16)
-
class
tinyms.primitives.
DynamicRNN
(cell_type='LSTM', direction='UNIDIRECTIONAL', cell_depth=1, use_peephole=False, keep_prob=1.0, cell_clip=-1.0, num_proj=0, time_major=True, activation='tanh', forget_bias=0.0, is_training=True)[source]¶ Applies a recurrent neural network to the input. Only long short-term memory (LSTM) is supported currently.
\[\begin{split}\begin{array}{ll} \\ i_{t+1} = \sigma(W_{ix} x_{t+1} + b_{ix} + W_{ih} h_{(t)} + b_{ih}) \\ f_{t+1} = \sigma(W_{fx} x_{t+1} + b_{fx} + W_{fh} h_{(t)} + b_{fh}) \\ \tilde{c}_{t+1} = \tanh(W_{cx} x_{t+1} + b_{cx} + W_{ch} h_{(t)} + b_{ch}) \\ o_{t+1} = \sigma(W_{ox} x_{t+1} + b_{ox} + W_{oh} h_{(t)} + b_{oh}) \\ c_{t+1} = f_{t+1} * c_{(t)} + i_t * \tilde{c}_{t+1} \\ h_{t+1} = o_{t+1} * \tanh(c_{t+1}) \\ \end{array}\end{split}\]\(h_{t+1}\) is the hidden state at time t+1. \(x_{t+1}\) is the input at time t+1. \(h_{t}\) is the hidden state of the layer at time t or the initial hidden state at time 0. \(\sigma\) is the sigmoid function, and \(*\) is the Hadamard product. \(W, b\) are learnable weights between the output and the input in the formula. For instance, \(W_{ix}, b_{ix}\) are the weight and bias used to transform from input \(x\) to \(i\).
- Parameters:
cell_type (str) – A string identifying the cell type in the operator. Default: ‘LSTM’. Only ‘LSTM’ is currently supported.
direction (str) – A string identifying the direction in the operator. Default: ‘UNIDIRECTIONAL’. Only ‘UNIDIRECTIONAL’ is currently supported.
cell_depth (int) – An integer identifying the cell depth in the operator. Default: 1.
use_peephole (bool) – A bool identifying if use peephole in the operator. Default: False.
keep_prob (float) – A float identifying the keep prob in the operator. Default: 1.0.
cell_clip (float) – A float identifying the cell clip in the operator. Default: -1.0.
num_proj (int) – An integer identifying the number projection in the operator. Default: 0.
time_major (bool) – A bool specify the data format of x. If it is set to True, the format is \((num\_step, batch\_size, input\_size)\), if it is set to False, the format is \((batch\_size, num\_step, input\_size)\). Default: True. Only supports True at present.
activation (str) – A string identifying the type of activation function in the operator. Default: ‘tanh’. Only ‘tanh’ is currently supported.
forget_bias (float) – A float identifying the forget bias in the operator. Default: 0.0.
is_training (bool) – A bool identifying is training in the operator. Default: True.
- Inputs:
x (Tensor) - Current words. Tensor of shape \((num\_step, batch\_size, input\_size)\). The data type must be float16.
w (Tensor) - Weight. Tensor of shape \((input\_size + hidden\_size, 4 * hidden\_size)\). The data type must be float16.
b (Tensor) - Bias. Tensor of shape \((4 * hidden\_size)\). The data type must be float16 or float32.
seq_length (Tensor) - The length of each batch. Tensor of shape \((batch\_size, )\). Only None is currently supported.
init_h (Tensor) - Hidden state of initial time. Tensor of shape \((1, batch\_size, hidden\_size)\). The data type must be float16.
init_c (Tensor) - Cell state of initial time. Tensor of shape \((1, batch\_size, hidden\_size)\). The data type must be float16.
- Outputs:
y (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.
output_h (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). With data type of float16.
output_c (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.
i (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.
j (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.
f (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.
o (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.
tanhct (Tensor) - A Tensor of shape \((num\_step, batch\_size, hidden\_size)\). Has the same type with input b.
- Raises:
TypeError – If cell_type, direction or activation is not a str.
TypeError – If cell_depth or num_proj is not an int.
TypeError – If keep_prob, cell_clip or forget_bias is not a float.
TypeError – If use_peehpole, time_major or is_training is not a bool.
TypeError – If x, w, b, seq_length, init_h or init_c is not a Tensor.
TypeError – If dtype of x, w, init_h or init_c is not float16.
TypeError – If dtype of b is neither float16 nor float32.
- Supported Platforms:
Ascend
Examples
>>> x = Tensor(np.random.rand(2, 16, 64).astype(np.float16)) >>> w = Tensor(np.random.rand(96, 128).astype(np.float16)) >>> b = Tensor(np.random.rand(128).astype(np.float16)) >>> init_h = Tensor(np.random.rand(1, 16, 32).astype(np.float16)) >>> init_c = Tensor(np.random.rand(1, 16, 32).astype(np.float16)) >>> dynamic_rnn = ops.DynamicRNN() >>> output = dynamic_rnn(x, w, b, None, init_h, init_c) >>> print(output[0].shape) (2, 16, 32)
-
class
tinyms.primitives.
DynamicShape
(dtype=9)[source]¶ Same as operator TensorShape. DynamicShape will be deprecated in the future. Please use TensorShape instead.
- Supported Platforms:
Deprecated
-
class
tinyms.primitives.
EditDistance
(normalize=True)[source]¶ Computes the Levenshtein Edit Distance. It is used to measure the similarity of two sequences. The inputs are variable-length sequences provided by SparseTensors (hypothesis_indices, hypothesis_values, hypothesis_shape) and (truth_indices, truth_values, truth_shape).
\[\begin{split}\operatorname{lev}_{a, b}(i, j)=\left\{\begin{array}{ll} \max (i, j) \qquad \qquad \qquad \qquad \qquad \quad \ \text { if } \min (i, j)=0 \\ \min \left\{\begin{array}{ll} \operatorname{lev}_{a, b}(i-1, j)+1 & \\ \operatorname{lev}_{a, b}(i, j-1)+1 & \text { otherwise. } \\ \operatorname{lev}_{a, b}(i-1, j-1)+1_{\left(a_{i} \neq b_{j}\right)} \end{array}\right. & \end{array}\right.\end{split}\]Where the \(a\) indicates the hypothesis and the \(b\) indicates the truth. For ease of understanding, i and j here in may be considered as lengths of a and b.
Warning
Unorded truth_indices or hypothesis_indices might lead to expected result, so it is suggested to make sure truth_indices and hypothesis_indices are both in ascending order before calling this API.
- Parameters:
normalize (bool) – If true, edit distances are normalized by length of truth. Default: True.
- Inputs:
hypothesis_indices (Tensor) - The indices of the hypothesis list SparseTensor. With int64 data type. The shape of tensor is \((N, R)\).
hypothesis_values (Tensor) - The values of the hypothesis list SparseTensor. Must be 1-D vector with length of N.
hypothesis_shape (Tensor) - The shape of the hypothesis list SparseTensor. Must be R-length vector with int64 data type. Only constant value is allowed.
truth_indices (Tensor) - The indices of the truth list SparseTensor. With int64 data type. The shape of tensor is \((M, R)\).
truth_values (Tensor) - The values of the truth list SparseTensor. Must be 1-D vector with length of M.
truth_shape (Tensor) - The shape of the truth list SparseTensor. Must be R-length vector with int64 data type. Only constant value is allowed.
- Outputs:
Tensor, a dense tensor with rank R-1 and float32 data type.
- Raises:
TypeError – If normalize is not a bool.
- Supported Platforms:
Ascend
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> import mindspore.nn as nn >>> import mindspore.ops as ops >>> class EditDistance(nn.Cell): ... def __init__(self, hypothesis_shape, truth_shape, normalize=True): ... super(EditDistance, self).__init__() ... self.edit_distance = ops.EditDistance(normalize) ... self.hypothesis_shape = hypothesis_shape ... self.truth_shape = truth_shape ... ... def construct(self, hypothesis_indices, hypothesis_values, truth_indices, truth_values): ... return self.edit_distance(hypothesis_indices, hypothesis_values, self.hypothesis_shape, ... truth_indices, truth_values, self.truth_shape) ... >>> hypothesis_indices = Tensor(np.array([[0, 0, 0], [1, 0, 1], [1, 1, 1]]).astype(np.int64)) >>> hypothesis_values = Tensor(np.array([1, 2, 3]).astype(np.float32)) >>> hypothesis_shape = Tensor(np.array([1, 1, 2]).astype(np.int64)) >>> truth_indices = Tensor(np.array([[0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1]]).astype(np.int64)) >>> truth_values = Tensor(np.array([1, 3, 2, 1]).astype(np.float32)) >>> truth_shape = Tensor(np.array([2, 2, 2]).astype(np.int64)) >>> edit_distance = EditDistance(hypothesis_shape, truth_shape) >>> output = edit_distance(hypothesis_indices, hypothesis_values, truth_indices, truth_values) >>> print(output) [[1. 1.] [1. 1.]]
-
class
tinyms.primitives.
Eig
(compute_v=False)[source]¶ Computes the eigenvalues and eigenvectors of a square matrix(batch square matrices).
Warning
This is an experimental API that is subject to change or deletion.
- Parameters:
compute_v (bool, optional) – If True, compute both eigenvalues and eigenvectors; If False, just eigenvalues will be computed. Default: False.
- Inputs:
x (Tensor) - Square matrices of shape \((*, N, N)\), with float32, float64, complex64 or complex128 data type.
- Outputs:
eigen_values (Tensor) - Shape \((*, N)\). Each inner most vector represents eigenvalues of the corresponding matrix. The eigenvalues may not have an order.
eigen_vectors (Tensor) - If compute_v is False, it’s an empty tensor. Otherwise, this tensor has shape \((*, N, N)\), whose columns represent normalized (unit length) eigenvectors of corresponding eigenvalues.
- Raises:
TypeError – If compute_v is not a bool.
TypeError – If dtype of x is not one of: float64, float32, complex64 or complex128.
TypeError – If x is not a Tensor.
ValueError – If x is not a square(batch squares).
- Supported Platforms:
Ascend
CPU
Examples
>>> input_x = Tensor(np.array([[1.0, 0.0], [0.0, 2.0]]), mindspore.float32) >>> eig = ops.Eig(compute_v=True) >>> u, v = eig(input_x) >>> print(u) [1.+0.j 2.+0.j] >>> print(v) [[1.+0.j 0.+0.j] [0.+0.j 1.+0.j]]
-
class
tinyms.primitives.
Einsum
(equation)[source]¶ Sums the product of the elements of the input Tensor along dimensions specified notation based on the Einstein summation convention(Einsum). You can use this operator to perform diagonal/reducesum/transpose/matmul/mul/inner product operations, etc.
The inputs must be a tuple of tensors. When the inputs are only one tensor, you can input (tensor, ) dtypes of them should be float16/float32/float64.
- Parameters:
equation (str) – An attribute, represent the operation you want to do. the value can contain only letters([a-z][A-Z]), commas(,), ellipsis(…), and arrow(->). the letters represent inputs’s tensor dimension, commas(,)represent separate tensors, ellipsis(…) indicates the tensor dimension that you do not care about, the left of the arrow(->) indicates the input tensors, and the right of it indicates the desired output dimension.
- Inputs:
x (Tuple) - input tensor used for calculation. the data type of the tensor must be the same.
- Outputs:
Tensor, the shape of it can be obtained from the equation, and the data type is the same as input tensors.
- Raises:
TypeError – If equation itself is invalid, or the equation does not match the input tensor.
- Supported Platforms:
GPU
Examples
>>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) >>> equation = "i->" >>> einsum = ops.Einsum(equation) >>> output = einsum([x]) >>> print(output) [7.] >>> >>> x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32) >>> y = Tensor(np.array([2.0, 4.0, 3.0]), mindspore.float32) >>> equation = "i,i->i" >>> einsum = ops.Einsum(equation) >>> output = einsum((x, y)) >>> print(output) [ 2. 8. 12.] >>> >>> x = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mindspore.float32) >>> y = Tensor(np.array([[2.0, 3.0], [1.0, 2.0], [4.0, 5.0]]), mindspore.float32) >>> equation = "ij,jk->ik" >>> einsum = ops.Einsum(equation) >>> output = einsum((x, y)) >>> print(output) [[16. 22.] [37. 52.]] >>> >>> x = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mindspore.float32) >>> equation = "ij->ji" >>> einsum = ops.Einsum(equation) >>> output = einsum((x,)) >>> print(output) [[1. 4.] [2. 5.] [3. 6.]] >>> >>> x = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mindspore.float32) >>> equation = "ij->j" >>> einsum = ops.Einsum(equation) >>> output = einsum((x,)) >>> print(output) [5. 7. 9.] >>> >>> x = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), mindspore.float32) >>> equation = "...->" >>> einsum = ops.Einsum(equation) >>> output = einsum((x,)) >>> print(output) [21.] >>> >>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32) >>> y = Tensor(np.array([2.0, 4.0, 1.0]), mindspore.float32) >>> equation = "j,i->ji" >>> einsum = ops.Einsum(equation) >>> output = einsum((x, y)) >>> print(output) [[ 2. 4. 1.] [ 4. 8. 2.] [ 6. 12. 3.]]
-
class
tinyms.primitives.
Elu
(alpha=1.0)[source]¶ Exponential Linear Uint activation function.
Applies the exponential linear unit function element-wise. The activation function is defined as:
\[\begin{split}\text{ELU}(x)= \left\{ \begin{array}{align} \alpha(e^{x} - 1) & \text{if } x \le 0\\ x & \text{if } x \gt 0\\ \end{array}\right.\end{split}\]The picture about ELU looks like this ELU .
- Parameters:
alpha (float) – The alpha value of ELU, the data type is float. Only support ‘1.0’ currently. Default: 1.0.
- Inputs:
input_x (Tensor) - The input of ELU is a Tensor of any dimension with data type of float16, float32 or float64.
- Outputs:
Tensor, has the same shape and data type as input_x.
- Raises:
TypeError – If alpha is not a float.
TypeError – If dtype of input_x is neither float16, float32 nor float64.
ValueError – If alpha is not equal to 1.0.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32) >>> elu = ops.Elu() >>> output = elu(input_x) >>> print(output) [[-0.63212055 4. -0.99966455] [ 2. -0.99326205 9. ]]
-
class
tinyms.primitives.
EmbeddingLookup
[source]¶ Returns a slice of input tensor based on the specified indices.
This Primitive has the similar functionality as GatherV2 operating on axis = 0, but has one more inputs: offset.
- Inputs:
input_params (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\). This represents a Tensor slice, instead of the entire Tensor. Currently, the dimension is restricted to be 2.
input_indices (Tensor) - The shape of tensor is \((y_1, y_2, ..., y_S)\). Specifies the indices of elements of the original Tensor. Values can be out of range of input_params, and the exceeding part will be filled with 0 in the output. Values do not support negative and the result is undefined if values are negative. The data type should be int32 or int64.
offset (int) - Specifies the offset value of this input_params slice. Thus the real indices are equal to input_indices minus offset.
- Outputs:
Tensor, the shape of tensor is \((z_1, z_2, ..., z_N)\). The data type is the same with input_params.
- Raises:
TypeError – If dtype of input_indices is not int.
ValueError – If length of shape of input_params is greater than 2.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_params = Tensor(np.array([[8, 9], [10, 11], [12, 13], [14, 15]]), mindspore.float32) >>> input_indices = Tensor(np.array([[5, 2], [8, 5]]), mindspore.int32) >>> offset = 4 >>> output = ops.EmbeddingLookup()(input_params, input_indices, offset) >>> print(output) [[[10. 11.] [ 0. 0.]] [[ 0. 0.] [10. 11.]]]
-
class
tinyms.primitives.
Eps
[source]¶ Create a Tensor with the same data type and shape as input, and the element value is the minimum value that the corresponding data type can be expressed.
- Inputs:
x (Tensor) - Tensor of any dimension used to obtain the minimum value that its data type can be expressed. The data type must be float16, float32 or float64.
- Outputs:
Tensor, has the same type and shape as x, but filled with x dtype minimum val.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor([4, 1, 2, 3], mindspore.float32) >>> output = ops.Eps()(x) >>> print(output) [1.5258789e-05 1.5258789e-05 1.5258789e-05 1.5258789e-05]
-
class
tinyms.primitives.
Equal
[source]¶ Computes the equivalence between two tensors element-wise.
Refer to
mindspore.ops.equal()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> # case 1: The shape of two inputs are different >>> x = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> equal = ops.Equal() >>> output = 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) >>> equal = ops.Equal() >>> output = equal(x, y) >>> print(output) [ True True False]
-
class
tinyms.primitives.
EqualCount
[source]¶ Computes the number of the same elements of two tensors.
The two input tensors must have the same data type and shape.
- Inputs:
x (Tensor) - The first input tensor. If the data type and shape of y are determined, then x must be the same as y, and vice versa. \((N, *)\) where \(*\) means, any number of additional dimensions.
y (Tensor) - The second input tensor. If the data type and shape of x are determined, then y must be the same as x, and vice versa.
- Outputs:
Tensor, with the type same as input tensor and shape as \((1,)\).
- Raises:
TypeError – If x or y is not a Tensor.
ValueError – If shape of x is not equal to shape of y.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([1, 2, 3]), mindspore.int32) >>> y = Tensor(np.array([1, 2, 4]), mindspore.int32) >>> equal_count = ops.EqualCount() >>> output = equal_count(x, y) >>> print(output) [2]
-
class
tinyms.primitives.
Erf
[source]¶ Computes the Gauss error function of x element-wise.
Refer to
mindspore.ops.erf()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([-1, 0, 1, 2, 3]), mindspore.float32) >>> erf = ops.Erf() >>> output = erf(x) >>> print(output) [-0.8427168 0. 0.8427168 0.99530876 0.99997765]
-
class
tinyms.primitives.
Erfc
[source]¶ Computes the complementary error function of x element-wise.
Refer to
mindspore.ops.erfc()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([-1, 0, 1, 2, 3]), mindspore.float32) >>> erfc = ops.Erfc() >>> output = erfc(x) >>> print(output) [1.8427168e+00 1.0000000e+00 1.5728319e-01 4.6912432e-03 2.2351742e-05]
-
class
tinyms.primitives.
Erfinv
[source]¶ Computes the inverse error function of input. The inverse error function is defined in the range (-1, 1).
The formula is defined as:
\[erfinv(erf(x)) = x\]- Inputs:
input_x (Tensor) - The input tensor to compute to, with data type float32, float16 or float64.
- Outputs:
Tensor, has the same shape and dtype as input_x.
- Raises:
TypeError – If dtype of input_x is not float16, float32 or float64.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([0, 0.5, -0.9]), mindspore.float32) >>> erfinv = ops.Erfinv() >>> output = erfinv(x) >>> print(output) [ 0. 0.47695306 -1.1630805 ]
-
class
tinyms.primitives.
Erfinv
[source] Computes the inverse error function of input. The inverse error function is defined in the range (-1, 1).
The formula is defined as:
\[erfinv(erf(x)) = x\]- Inputs:
input_x (Tensor) - The input tensor to compute to, with data type float32, float16 or float64.
- Outputs:
Tensor, has the same shape and dtype as input_x.
- Raises:
TypeError – If dtype of input_x is not float16, float32 or float64.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([0, 0.5, -0.9]), mindspore.float32) >>> erfinv = ops.Erfinv() >>> output = erfinv(x) >>> print(output) [ 0. 0.47695306 -1.1630805 ]
-
class
tinyms.primitives.
EuclideanNorm
(keep_dims=False)[source]¶ Calculates the Euclidean norm(aka L2 norm) of a Tensor along the specified axes. The specified axes are removed by default.
- Parameters:
keep_dims (bool, optional) – whether to retain the reduced dimensions. If true, retains them with length 1. If false, these dimensions are removed. Default: False.
- Inputs:
x (Tensor) - The input Tensor to reduce.
axes (Tensor) - The axes to perform reduction on. Must be one of the following types: int32, int64. It must be in range \([-rank(x), rank(x))\).
- Outputs:
Tensor, has the same type as the ‘x’.
- Raises:
TypeError – If keep_dims is not a bool.
TypeError – If x is not a Tensor.
ValueError – If axes is out of range.
- Supported Platforms:
GPU
Examples
>>> x = Tensor(np.array([[3, 5], [4, 12]])).astype(np.int32) >>> axes = Tensor([0]) >>> op = ops.EuclideanNorm(keep_dims=True) >>> output = op(x, axes) >>> print(output) [[5 13]]
-
class
tinyms.primitives.
Exp
[source]¶ Returns exponential of a tensor element-wise.
Refer to
mindspore.ops.exp()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([0.0, 1.0, 3.0]), mindspore.float32) >>> exp = ops.Exp() >>> output = exp(x) >>> print(output) [ 1. 2.718282 20.085537]
-
class
tinyms.primitives.
Expand
[source]¶ Expands the Tensor along singleton dimensions(dim with size 1) to match given desired shape.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.expand()
for more details.- Supported Platforms:
Ascend
CPU
Examples
>>> x = Tensor(np.array([[1], [2], [3]]), mindspore.float32) >>> shape = Tensor(np.array([3,4]), mindspore.int32) >>> expand = ops.Expand() >>> y = expand(x, shape) >>> print(y) [[1. 1. 1. 1.] [2. 2. 2. 2.] [3. 3. 3. 3.]]
-
class
tinyms.primitives.
ExpandDims
[source]¶ Adds an additional dimension to input_x at the given axis.
Refer to
mindspore.ops.expand_dims()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_tensor = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32) >>> expand_dims = ops.ExpandDims() >>> output = expand_dims(input_tensor, 0) >>> print(output) [[[2. 2.] [2. 2.]]]
-
class
tinyms.primitives.
Expm1
[source]¶ Returns exponential then minus 1 of a tensor element-wise.
Refer to
mindspore.ops.expm1()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([0.0, 2.0, 3.0, 5.0]), mindspore.float32) >>> expm1 = ops.Expm1() >>> output = expm1(x) >>> print(output) [ 0. 6.389056 19.085537 147.41316 ]
-
class
tinyms.primitives.
ExtractGlimpse
(centered=True, normalized=True, uniform_noise=True, noise='uniform')[source]¶ Extracts glimpses(usually subarea of rectangle) from the input image Tensor and return as windows.
Note
If extracted windows and the input image only partially overlap, random noise is filled in those non overlapping areas.
- Parameters:
centered (bool, optional) – An optional bool. Indicates if the offset coordinates are centered relative to the image, in which case the (0, 0) offset is relative to the center of the center of the input images. If false, the (0, 0) offset corresponds to the upper left corner of the input images. Defaults to True.
normalized (bool, optional) – An optional bool. indicates if the offset coordinates are normalized. Defaults to True.
uniform_noise (bool, optional) – An optional bool. indicates if the noise should be generated using a uniform distribution(aka. Gaussian distribution). Defaults to True.
noise (str, optional) –
An optional string specifies the type of noise to fill. The window is determined by size and offsets. When the window and input image tensor don’t not overlap, random noise is filled. The value can be ‘uniform’, ‘gaussian’ and ‘zero’. Default: uniform.
When noise is ‘uniform’ and ‘gaussian’, the result is variable.
When noise is ‘zero’, the value of uniform_noise must be ‘False’ and the filling noise will be zero so that the result is fixed.
When uniform_noise is ‘True’, the value of noise only can be ‘uniform’. When uniform_noise is ‘False’, the value of noise can be ‘uniform’, ‘gaussian’ and ‘zero’.
- Inputs:
x (Tensor) - A 4-D float tensor of shape \((batch_size, height, width, channels)\). Types allowed: float32.
size (Tensor) - A 1-D tensor of 2 elements containing the size of the glimpses to extract. The glimpse height must be specified first, following by the glimpse width. Types allowed: int32. The value of size must be greater than zero.
offsets (Tensor) - A 2-D integer tensor of shape \((batch_size, 2)\) containing the y, x locations of the center of each window. Types allowed: float32.
- Outputs:
A 4-D tensor of shape \((batch_size, glimpse_height, glimpse_width, channels)\) with type: float32.
- Raises:
TypeError – If centered is not a bool.
TypeError – If normalize is not a bool.
TypeError – If uniform_noise is not a bool.
ValueError – If noise is not uniform, gaussian or zero.
ValueError – If the value of size is not constant value.
ValueError – If the batch_size of input is inconsistent with the batch_size of offsets.
ValueError – If the value of offsets[1] is not 2.
ValueError – If the input is not Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor([[[[0.0], [1.0], [2.0]], [[3.0], [4.0], [5.0]], [[6.0], [7.0], [8.0]]]], dtype=mindspore.float32) >>> size = Tensor((2, 2), dtype=mindspore.int32) >>> offsets = Tensor([[1, 1]], dtype=mindspore.float32) >>> ops = P.image_ops.ExtractGlimpse(centered = False, normalized = False, >>> uniform_noise = False, noise = "uniform") >>> output = ops(x, size, offsets) >>> print(output) [[[[0.] [1.]] [[3.] [4.]]]]
-
class
tinyms.primitives.
ExtractImagePatches
(ksizes, strides, rates, padding='valid')[source]¶ Extracts patches from images. The input tensor must be a 4-D tensor and the data format is NCHW.
- Parameters:
ksizes (Union[tuple[int], list[int]]) – The size of sliding window, must be a tuple or a list of integers, and the format is [1, 1, ksize_row, ksize_col].
strides (Union[tuple[int], list[int]]) – Distance between the centers of the two consecutive patches, must be a tuple or list of int, and the format is [1, 1, stride_row, stride_col].
rates (Union[tuple[int], list[int]]) – In each extracted patch, the gap between the corresponding dimension pixel positions, must be a tuple or a list of integers, and the format is [1, 1, rate_row, rate_col].
padding (str) –
The type of padding algorithm, is a string whose value is “same” or “valid”, not case sensitive. Default: “valid”.
same: Means that the patch can take the part beyond the original image, and this part is filled with 0.
valid: Means that the taken patch area must be completely covered in the original image.
- Inputs:
input_x (Tensor) - A 4-D tensor whose shape is [in_batch, in_depth, in_row, in_col] and data type is number.
- Outputs:
Tensor, a 4-D tensor whose data type is same as ‘input_x’, and the shape is [out_batch, out_depth, out_row, out_col], Where the out_batch is the same as the in_batch and
\[out_depth=ksize\_row * ksize\_col * in\_depth\]and if ‘padding’ is “valid”:
\[out\_row=floor((in\_row - (ksize\_row + (ksize\_row - 1) * (rate\_row - 1))) / stride\_row) + 1 out\_col=floor((in\_col - (ksize\_col + (ksize\_col - 1) * (rate\_col - 1))) / stride\_col) + 1\]if ‘padding’ is “same”:
\[out\_row=floor((in\_row - 1) / stride\_row) + 1 out\_col=floor((in\_col - 1) / stride\_col) + 1\]- Supported Platforms:
Ascend
GPU
-
class
tinyms.primitives.
ExtractVolumePatches
(kernel_size, strides, padding)[source]¶ Extract patches from input and put them in the “depth” output dimension. “depth” dimension is the second dim of output.
- Parameters:
kernel_size (Union[int, tuple[int], list[int]]) – A list of ints which’s length is 3 or 5. The size of the sliding window for each dimension of input. Must be: \([1, 1, k_d, k_h, k_w]\) or \([k_d, k_h, k_w]\). If \(k_d = k_h = k_w\), you can enter an integer.
strides (Union[int, tuple[int], list[int]]) – A list of ints which’s length is 3 or 5. How far the centers of two consecutive patches are in input. Must be: \([1, 1, s_d, s_h, s_w]\) or \([s_d, s_h, s_w]\). If \(s_d = s_h = s_w\), you can enter an integer.
padding (str) – A string from: “SAME”, “VALID”. The type of padding algorithm to use.
- Inputs:
input_x (Tensor) - A Tensor. 5-D Tensor with shape \(()\).
- Outputs:
Tensor, has the same type as input. If padding is “VALID”, the shape is \((x_n, k_d * k_h * k_w * x_c, 1 + (x_d - k_d) / s_d, 1 + (x_h - k_h) / s_h, 1 + (x_w - k_w) / s_w)\); if padding is “SAME”, the shape is \(( x_n, k_d * k_h * k_w * x_c, (x_d + s_d - 1) / s_d, (x_h + s_h - 1) / s_h, (x_w + s_w - 1) / s_w)\).
- Raises:
TypeError – If kernel_size or strides is not a list, a tuple or an int.
TypeError – If input_x is not a tensor.
TypeError – If padding is not str.
ValueError – If the length of kernel_size is neither 3 nor 5 and kernel_size is not an integer.
ValueError – If the length of strides is neither 3 nor 5 and strides is not an integer.
ValueError – If padding is neither “VALID” nor “SAME”.
ValueError – If elements of kernel_size or strides are not positive integer.
ValueError – If input_x is not a tensor in dimension 5.
ValueError – If input_x’s shape has zero.
ValueError – If one of kernel_size or strides’ first two numbers is not 1.
ValueError – If padding = “VALID” and \(input\_x - kernel\_size\) is less than 0 in d, h or w dimension.
ValueError – If padding = “SAME” and \(padding\_needed = ((input\_x + strides - 1) / strides - 1) * strides + kernel\_size - input\_x\) is less than 0 in d, h or w dimension.
ValueError – If x_h is not 1 or x_w is not 1 and \(x_w + padding\_needed - k_w - s_w\) is less than 0.
ValueError – If \(x_d * x_h * x_w\) is greater than 2048.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> kernel_size = (1, 1, 2, 2, 2) >>> strides = (1, 1, 1, 1, 1) >>> padding = "VALID" >>> input_x = P.Reshape()(Tensor(np.arange(1, 28), mstype.float16), (1, 1, 3, 3, 3)) >>> output_y = ops.ExtractVolumePatches(kernel_size, strides, padding)(input_x) >>> print(output_y.shape) (1, 8, 2, 2, 2)
-
class
tinyms.primitives.
Eye
[source]¶ Creates a tensor with ones on the diagonal and zeros in the rest.
Refer to
mindspore.ops.eye()
for more details.- Inputs:
n (int) - The number of rows of returned tensor. Constant value only.
m (int) - The number of columns of returned tensor. Constant value only.
t (mindspore.dtype) - MindSpore’s dtype, the data type of the returned tensor. The data type can be bool or Number. Default: None, the data type of the returned tensor is mindspore.float32.
- Outputs:
Tensor, a tensor with ones on the diagonal and the rest of elements are zero. The shape of output depends on the user’s Inputs n and m. And the data type depends on Inputs t.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> eye = ops.Eye() >>> output = eye(2, 2, mindspore.int32) >>> print(output) [[1 0] [0 1]] >>> print(output.dtype) Int32 >>> output = eye(1, 2, mindspore.float64) >>> print(output) [[1. 0.]] >>> print(output.dtype) Float64
-
class
tinyms.primitives.
FFTWithSize
(signal_ndim, inverse, real, norm='backward', onesided=True, signal_sizes=())[source]¶ Fourier transform, can be adjusted by parameters to achieve FFT/IFFT/RFFT/IRFFT.
For fft, it computes the following expression:
\[X[\omega_1, \dots, \omega_d] = \sum_{n_1=0}^{N_1-1} \dots \sum_{n_d=0}^{N_d-1} x[n_1, \dots, n_d] e^{-j\ 2 \pi \sum_{i=0}^d \frac{\omega_i n_i}{N_i}},\]where \(d\) = signal_ndim is number of dimensions for the signal, and \(N_i\) is the size of signal dimension \(i\).
For ifft, it computes the following expression:
\[X[\omega_1, \dots, \omega_d] = \frac{1}{\prod_{i=1}^d N_i} \sum_{n_1=0}^{N_1-1} \dots \sum_{n_d=0}^{N_d-1} x[n_1, \dots, n_d] e^{\ j\ 2 \pi \sum_{i=0}^d \frac{\omega_i n_i}{N_i}},\]where \(d\) = signal_ndim is number of dimensions for the signal, and \(N_i\) is the size of signal dimension \(i\).
Note
FFT/IFFT requires complex64 or complex128 inputs, return complex64 or complex128 outputs.
RFFT requires float32 or float64 inputs, return complex64 or complex128 outputs.
IRFFT requires complex64 or complex128 inputs, return float32 or float64 outputs.
- Parameters:
signal_ndim (int) – The number of dimensions in each signal, this controls how many dimensions of the fourier transform are realized, can only be 1, 2 or 3.
inverse (bool) – Whether it is the inverse transformation.
real (bool) –
Whether it is the real transformation.
”inverse:False real:False” corresponds to FFT.
”inverse:True real:False” corresponds to IFFT.
”inverse:False real:True” corresponds to RFFT.
”inverse:True real:True” corresponds to IRFFT.
norm (str, optional) –
The normalization, optional values: [“backward”, “forward”, “ortho”]. Default value: “backward”.
”backward” has the direct transforms unscaled and the inverse transforms scaled by \(1/n\), where n is the input x’s element numbers.
”ortho” has both direct and inverse transforms are scaled by \(1/\sqrt n\).
”forward” has the direct transforms scaled by \(1/n\) and the inverse transforms unscaled.
onesided (bool, optional) – Controls whether the input is halved to avoid redundancy. Default: True.
signal_sizes (tuple, optional) –
Size of the original signal (the signal before rfft, no batch dimension), only in IRFFT mode and set onesided to True requires the parameter, the following conditions must be satisfied. Default: ().
The length of signal_sizes is equal to the signal_ndim of the IRFFT: \(len(signal_sizes)=signal_ndim\).
The last dimension of signal_sizes divided by 2 is equal to the last dimension of the IRFFT input: \(signal_size[-1]/2+1=x.shape[-1]\).
signal_sizes has exactly the same dimensions as the input shape except for the last dimension: \(signal_sizes[:-1]=x.shape[:-1]\).
- Inputs:
x (Tensor) - The dimension of the input tensor must be greater than or equal to signal_ndim.
- Outputs:
A tensor containing the complex-to-complex, real-to-complex or complex-to-real Fourier transform result.
- Raises:
TypeError – If the input type of FFT/IFFT/IRFF is not one of: complex64, complex128.
TypeError – If the input type of RFFT is not one of: float32, float64.
TypeError – If the input type is not Tensor.
ValueError – If x dimension is less than signal_ndim.
ValueError – If signal_ndim is greater than 3 or less than 1.
ValueError – If norm is none of “backward”, “forward” or “ortho”.
- Supported Platforms:
GPU
CPU
Examples
>>> # case FFT: signal_ndim: 1, inverse: False, real: False. >>> fft_in = Tensor(np.array([2, 1, 2]), mindspore.complex64) >>> fft_net = ops.FFTWithSize(signal_ndim=1, inverse=False, real=False) >>> fft_output = fft_net(fft_in) >>> print(fft_output) [5. +0.j 0.5 +0.86602545j 0.50000006-0.8660255j ] >>> # case IFFT: signal_ndim: 1, inverse: True, real: False. >>> ifft_in = fft_output >>> ifft_net = ops.FFTWithSize(signal_ndim=1, inverse=True, real=False) >>> ifft_output = ifft_net(ifft_in) >>> print(ifft_output) [2. -1.9868216e-08j 0.99999994+0.0000000e+00j 1.9999999 +7.9472862e-08j] >>> # case RFFT2D: signal_ndim: 2, inverse: False, real: True. >>> rfft_in = Tensor(np.array([[2, 1, 2], [3, 1, 6]]), mindspore.float32) >>> rfft_net = ops.FFTWithSize(signal_ndim=2, inverse=False, real=True) >>> rfft_output = rfft_net(rfft_in) >>> print(rfft_output) [[ 1.5000000e+01+1.1920929e-07j -2.3841858e-07+5.1961522e+00j] [-5.0000000e+00-2.9802322e-08j 9.9999988e-01-3.4641016e+00j]] >>> # case IRFFT2D: signal_ndim: 2, inverse: True, real: True. >>> irfft_in = rfft_output >>> irfft_net = ops.FFTWithSize(signal_ndim=2, inverse=True, real=True, signal_sizes=rfft_in.shape) >>> irfft_output = irfft_net(irfft_in) >>> print(irfft_output) [[2. 1. 2. ] [3. 0.99999994 5.9999995 ]]
-
class
tinyms.primitives.
FastGeLU
[source]¶ Fast Gaussian Error Linear Units activation function.
Refer to
mindspore.ops.fast_gelu()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32) >>> fast_gelu = ops.FastGeLU() >>> output = fast_gelu(x) >>> print(output) [[-1.5418735e-01 3.9921875e+00 -9.7473649e-06] [ 1.9375000e+00 -1.0052517e-03 8.9824219e+00]]
-
class
tinyms.primitives.
FastGelu
[source]¶ Same as operator FastGeLU. FastGelu will be deprecated in the future. Please use FastGeLU instead.
-
class
tinyms.primitives.
Fill
[source]¶ The Fill interface is deprecated, please use the
mindspore.ops.FillV2
instead.- Supported Platforms:
Deprecated
-
class
tinyms.primitives.
FillDiagonal
(fill_value, wrap=False)[source]¶ Fills the main diagonal of a Tensor in-place with a specified value and returns the result. The input has at least 2 dimensions, and all dimensions of input must be equal in length when the dimension of input is greater than 2.
- Parameters:
fill_value (float) – The value to fill the diagonal of input_x.
wrap (bool, optional) – Controls whether the diagonal elements continue onto the remaining rows in case of a tall matrix(A matrix has more rows than columns). Examples blow demonstrates how it works on a tall matrix if wrap is set True. Default: False.
- Inputs:
input_x (Tensor) - The shape of tensor is \((x_1, x_2, ..., x_R)\). The data type must be float32, int32 or int64.
- Outputs:
y (Tensor) - Tensor, has the same shape and data type as the input input_x.
- Raises:
TypeError – If data type of input_x is not one of the following: float32, int32, int64.
ValueError – If the dimension of input_x is not greater than 1.
ValueError – If the size of each dimension is not equal, when the dimension is greater than 2.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype(np.float32)) >>> fill_value = 9.9 >>> fill_diagonal = ops.FillDiagonal(fill_value) >>> y = fill_diagonal(x) >>> print(y) [[9.9 2. 3. ] [4. 9.9 6. ] [7. 8. 9.9]] >>> x = Tensor(np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5]]).astype(np.int32)) >>> fill_value = 9.0 >>> fill_diagonal = ops.FillDiagonal(fill_value) >>> y = fill_diagonal(x) >>> print(y) [[9 0 0] [1 9 1] [2 2 9] [3 3 3] [4 4 4] [5 5 5]] >>> x = Tensor(np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], ... [4, 4, 4], [5, 5, 5], [6, 6, 6]]).astype(np.int64)) >>> fill_value = 9.0 >>> wrap = True >>> fill_diagonal = FillDiagonal(fill_value, wrap) >>> y = fill_diagonal(x) >>> print(y) [[9 0 0] [1 9 1] [2 2 9] [3 3 3] [9 4 4] [5 9 5] [6 6 9]]
-
class
tinyms.primitives.
FillV2
[source]¶ Creates a tensor with shape described by shape and fills it with values in value .
- Inputs:
shape (Union[Tuple[int], Tensor[int]]) - 1-D Tensor or Tuple, specify the shape of output tensor. Its dtype must be int32 or int64.
value (Tensor) - A 0-D Tensor, the value to fill the output tensor y .
- Outputs:
y (Tensor) - A tensor, its shape and value are described above.
- Raises:
TypeError – If shape is not a 1-D tensor or tuple.
TypeError – If the data type of shape is not int32 or int64.
ValueError – If value is not a 0-D Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> fillV2 = ops.FillV2() >>> output = fillV2(Tensor([2, 3], mindspore.int32), Tensor(1, mindspore.float32)) >>> print(output) [[1. 1. 1.] [1. 1. 1.]] >>> output = fillV2(Tensor([3, 3], mindspore.int64), Tensor(0, mindspore.int32)) >>> print(output) [[0 0 0] [0 0 0] [0 0 0]]
-
class
tinyms.primitives.
Fills
[source]¶ The Fills primitive is deprecated. Please use
mindspore.ops.fill()
instead.- Supported Platforms:
Deprecated
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> a = Tensor(np.arange(4).reshape((2,2)).astype('float32')) >>> fills = ops.Fills() >>> output = fills(a, float(1)) >>> print(output) [[1. 1.] [1. 1.]]
-
class
tinyms.primitives.
Flatten
[source]¶ Flattens a tensor without changing its batch size on the 0-th axis.
Refer to
mindspore.ops.flatten()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.ones(shape=[1, 2, 3, 4]), mindspore.float32) >>> flatten = ops.Flatten() >>> output = flatten(input_x) >>> print(output.shape) (1, 24)
-
class
tinyms.primitives.
FloatStatus
[source]¶ Determines if the elements contain Not a Number(NaN), infinite or negative infinite. 0 for normal, 1 for overflow.
- Inputs:
x (Tensor) - The input tensor. The data type must be float16, float32 or float64. \((N, *)\) where \(*\) means, any number of additional dimensions.
- Outputs:
Tensor, has the shape of \((1,)\), and the dtype is mindspore.dtype.float32.
- Raises:
TypeError – If dtype of x is not in [float16, float32, float64].
- Supported Platforms:
GPU
Examples
>>> float_status = ops.FloatStatus() >>> x = Tensor(np.array([np.log(-1), 1, np.log(0)]), mindspore.float32) >>> result = float_status(x) >>> print(result) [1.]
-
class
tinyms.primitives.
Floor
[source]¶ Rounds a tensor down to the closest integer element-wise.
Refer to
mindspore.ops.floor()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([1.1, 2.5, -1.5]), mindspore.float32) >>> floor = ops.Floor() >>> output = floor(x) >>> print(output) [ 1. 2. -2.]
-
class
tinyms.primitives.
FloorDiv
[source]¶ Divides the first input tensor by the second input tensor element-wise and round down to the closest integer.
Refer to
mindspore.ops.floor_div()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([2, 4, -1]), mindspore.int32) >>> y = Tensor(np.array([3, 3, 3]), mindspore.int32) >>> floor_div = ops.FloorDiv() >>> output = floor_div(x, y) >>> print(output) [ 0 1 -1]
-
class
tinyms.primitives.
FloorMod
[source]¶ Computes the remainder of division element-wise, and it’s a flooring divide.
Refer to
mindspore.ops.floor_mod()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([2, 4, -1]), mindspore.int32) >>> y = Tensor(np.array([3, 3, 3]), mindspore.int32) >>> floor_mod = ops.FloorMod() >>> output = floor_mod(x, y) >>> print(output) [2 1 2]
-
class
tinyms.primitives.
Fmax
[source]¶ Computes the maximum of input tensors element-wise.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.fmax()
for more detail.- Supported Platforms:
CPU
Examples
>>> x1 = Tensor(np.array([1.0, 5.0, 3.0]), mindspore.float32) >>> x2 = Tensor(np.array([4.0, 2.0, 6.0]), mindspore.float32) >>> fmax = ops.Fmax() >>> output = fmax(x1, x2) >>> print(output) [4. 5. 6.]
-
class
tinyms.primitives.
Fmin
[source]¶ Computes the minimum of input tensors element-wise.
Refer to
mindspore.ops.fmin()
for more detail.Supported Platforms:
Examples
>>> x1 = Tensor(np.array([1.0, 5.0, 3.0]), mstype.float32) >>> x2 = Tensor(np.array([4.0, 2.0, 6.0]), mstype.float32) >>> fmin = ops.Fmin() >>> output = fmin(x1, x2) >>> print(output) [1. 2. 3.]
-
class
tinyms.primitives.
FractionalAvgPool
(pooling_ratio, pseudo_random=False, overlapping=False, deterministic=False, seed=0, seed2=0)[source]¶ Performs fractional avg pooling on the input.
Fractional avg pooling is similar to regular avg pooling, but with the added flexibility of allowing the overall reduction ratio N to be a non-integer value. In regular avg pooling, an input set is reduced in size by taking the average value of N x N (usually 2x2) subsections of the set, with the goal of reducing the set by a factor of N, where N is an integer.
Warning
“pooling_ratio”, currently only supports row and col dimension and should be >= 1.0, the first and last elements must be 1.0 because we don’t allow pooling on batch and channels dimensions.
- Parameters:
pooling_ratio (list(float)) – Decide the shape of output, is a list of floats that has length >= 4. Pooling ratio for each dimension of value should be >=0, currently only support for row and col dimension. The first and last elements must be 1.0 because we don’t allow pooling on batch and channels dimensions.
pseudo_random (bool, optional) – Generate the pooling sequence either randomly or pseudo-randomly. If the pseudo_random parameter is set to True, the sequence will be generated in a pseudo-random fashion, otherwise it will be generated randomly. Refer to Fractional Max-Pooling by Benjamin Graham to understand the distinction between the two. Default: False.
overlapping (bool, optional) – When set to True, the values at the boundary of adjacent pooling cells will be shared by both cells during pooling process. When set to False, the values are not reused. Default: False.
deterministic (bool, optional) – If deterministic is set to True, a fixed pooling region will be used in the computation graph, ensuring that the FractionalAvgPool is deterministic. This is often used in unit tests. When set to False, fixed pool regions will not be used. Default: False.
seed (int, optional) – If either seed or seed2 are set to a non-zero value, the random number generator will be seeded using the specified seed. If neither seed nor seed2 are set, the generator will be seeded by a random seed. Default: 0.
seed2 (int, optional) – The second seed to avoid seed collision. Default: 0.
- Inputs:
x (Tensor) -The data type must be one of the following types: float32, float64, int32, int64. Tensor of shape \((N, H_{in}, W_{in}, C_{in})\).
- Outputs:
y (Tensor) - A tensor, the output of FractionalAvgPool, has the same data type with x. Tensor of shape \((N, H_{out}, W_{out}, C_{out})\).
row_pooling_sequence (Tensor) - A tensor of type int64, the result list of pool boundary rows.
col_pooling_sequence (Tensor) - A tensor of type int64, the result list of pool boundary cols.
- Raises:
TypeError – If data type of x is not float32, float64, int32, int64.
TypeError – If x is not a 4D tensor.
ValueError – If element of x equals 0 or is less than 0.
ValueError – If pooling_ratio is a list whose length is not equal to 4.
ValueError – If the first and last element of pooling_ratio is not equal to 1.0.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]).reshape([1,4,4,1]).astype(np.int64) >>> pooling_ratio=[1.0,1.5,1.5,1.0] >>> fractionalavgpool_op = ops.FractionalAvgPool(pooling_ratio=pooling_ratio) >>> output = fractionalavgpool_op(Tensor(x)) >>> print(output) (Tensor(shape=[1, 2, 2, 1], dtype=Int64, value= [[[[ 3], [ 5]], [[11], [13]]]]), Tensor(shape=[3], dtype=Int64, value= [0, 2, 4]), Tensor(shape=[3], dtype=Int64, value= [0, 2, 4]))
-
class
tinyms.primitives.
FractionalMaxPool
(pooling_ratio, pseudo_random=False, overlapping=False, deterministic=False, seed=0, seed2=0)[source]¶ Performs fractional max pooling on the input.
Fractional max pooling is similar to regular max pooling, but with the added flexibility of allowing the overall reduction ratio N to be a non-integer value. In regular max pooling, an input set is reduced in size by taking the maximum value of N x N (usually 2x2) subsections of the set, with the goal of reducing the set by a factor of N, where N is an integer.
In contrast, fractional max pooling uses randomly generated pool sizes that are fairly uniform in size.
Warning
“pooling_ratio”, currently only supports row and col dimension and should be >= 1.0, the first and last elements must be 1.0 because pooling on batch and channels dimensions is not allowed.
- Parameters:
pooling_ratio (list(float)) – Decide the shape of output, is a list of float numbers has length >= 4. Pooling ratio for each dimension of value should not be less than 0, currently only support for row and col dimension.
pseudo_random (bool, optional) –
Generate the pooling sequence either randomly or pseudo-randomly. If the pseudo_random parameter is set to True, the sequence will be generated in a pseudo-random fashion, otherwise it will be generated randomly. Refer to Fractional Max-Pooling by Benjamin Graham to understand the distinction between the two. Default: False.
overlapping (bool, optional) – When set to True, the values at the boundary of adjacent pooling cells will be shared by both cells during pooling process. When set to False, the values are not reused. Default: False.
deterministic (bool, optional) – If deterministic is set to True, a fixed pooling region will be used in the computation graph, ensuring that the FractionalMaxPool is deterministic. This is often used in unit tests. When set to False, fixed pool regions will not be used. Default: False.
seed (int, optional) – If either seed or seed2 are set to a non-zero value, the random number generator will be seeded using the specified seed. If neither seed nor seed2 are set, the generator will be seeded by a random seed. Default: 0.
seed2 (int, optional) – The second seed to avoid seed collision. Default: 0.
- Inputs:
x (Tensor) -The data type must be one of the following types: float32, float64, int32, int64. Tensor of shape \((N, H_{in}, W_{in}, C_{in})\).
- Outputs:
y (Tensor) - the output of FractionalMaxPool, has the same data type with x. Tensor of shape \((N, H_{out}, W_{out}, C_{out})\).
row_pooling_sequence (Tensor) - A tensor of type int64, the result list of pool boundary rows.
col_pooling_sequence (Tensor) - A tensor of type int64, the result list of pool boundary cols.
- Raises:
TypeError – If data type of x is not float32, float64, int32, int64.
TypeError – If x is not a 4D tensor.
ValueError – If element of x equals 0 or is less than 0.
ValueError – If pooling_ratio is a list whose length is not equal to 4.
ValueError – If the first and last element of pooling_ratio is not equal to 1.0.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]).reshape([1,4,4,1]).astype(np.int64) >>> pooling_ratio=[1.0,1.5,1.5,1.0] >>> fractionalmaxpool_op = ops.FractionalMaxPool(pooling_ratio=pooling_ratio) >>> output = fractionalmaxpool_op(Tensor(x)) >>> print(output) (Tensor(shape=[1, 2, 2, 1], dtype=Int64, value= [[[[ 6], [ 8]], [[14], [16]]]]), Tensor(shape=[3], dtype=Int64, value= [0, 2, 4]), Tensor(shape=[3], dtype=Int64, value= [0, 2, 4]))
-
class
tinyms.primitives.
FractionalMaxPool3DWithFixedKsize
(ksize, output_shape, data_format='NCDHW')[source]¶ Applies a 3D fractional max pooling to an input signal composed of multiple input planes. The max-pooling operation is applied in \((kD, kH, kW)\) regions by a stochastic step size determined by the target output size output_shape.
The number of output features is equal to the number of input planes.
Refer to the paper Fractional MaxPooling by Ben Graham for more details.
The input and output data format can be “NCDHW” and “NDHWC”. N is the batch size, C is the number of channels, D the feature depth, H is the feature height, and W is the feature width.
- Parameters:
ksize (Union[float, tuple]) – Size of the pooling window. ksize can be a tuple of three values specify a shape \((k_D, k_H, k_W)\), or a single int K for \((K, K, K)\).
output_shape (Union[int, tuple]) – The target output shape. output_shape can be a tuple of three values specify a shape \((D_{out}, H_{out}, W_{out})\), or a single float S for \((S, S, S)\).
data_format (str, optional) – The optional value for data format. Currently support ‘NCDHW’ and ‘NHDWC’. Default: ‘NCDHW’.
- Inputs:
x (Tensor) - The input of FractionalMaxPool3DWithFixedKsize, which is a 4D or 5D tensor. Tensor of data type : float16, float32, double, int32, int64. Supported shape \((N, C, D_{in}, H_{in}, W_{in})\) or \((N, D_{in}, H_{in}, W_{in}, C)\).
random_samples (Tensor) - The random step of FractionalMaxPool3DWithFixedKsize, which is a 3D tensor. Tensor of data type : float16, float32, double, and value is between (0, 1). Supported shape \((N, C, 3)\)
- Outputs:
y (Tensor) - A tensor, the output of FractionalMaxPool3DWithFixedKsize. Has the same data type with x. Tensor of shape \((N, C, D_{out}, H_{out}, W_{out})\) or \((N, D_{out}, H_{out}, W_{out}, C)\).
argmax (Tensor) - A tensor, the indices along with the outputs. Has the same shape as the y and int32 or int64 data type.
- Raises:
TypeError – If input_x is not a 4D or 5D tensor.
TypeError – If random_samples is not a 3D tensor.
TypeError – If data type of x is not float16, float32, double, int32, int64.
TypeError – If dtype of random_samples is not float16, float32, double.
TypeError – If dtype of argmax is not int32, int64.
ValueError – If output_shape is a tuple and if output_shape length is not 3.
ValueError – If ksize is a tuple and if ksize length is not 3.
ValueError – If numbers in output_shape or ksize is not positive.
ValueError – If data_format is neither ‘NCDHW’ nor ‘NDHWC’.
ValueError – If the first dimension size of input_x and random_samples is not equal.
ValueError – If the second dimension size of input_x and random_samples is not equal.
ValueError – If the third dimension size of random_samples is not 3.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) ... .reshape([1, 1, 2, 2, 4]), mstype.float32) >>> random_samples = Tensor(np.array([0.7, 0.7, 0.7]).reshape([1, 1, 3]), mstype.float32) >>> ksize = (1, 1, 1) >>> output_shape = (1, 1, 2) >>> net = ops.FractionalMaxPool3DWithFixedKsize(ksize = ksize, output_shape = output_shape) >>> output, argmax = net(x, random_samples) >>> print(output) >>> print(argmax) [[[[[13. 16.]]]]] [[[[[12 15]]]]]
-
class
tinyms.primitives.
FractionalMaxPoolWithFixedKsize
(ksize, output_shape, data_format='NCHW')[source]¶ Applies a 2D fractional max pooling to an input signal composed of multiple input planes. The max-pooling operation is applied in \((kH, kW)\) regions by a stochastic step size determined by the target output size output_shape.
The number of output features is equal to the number of input planes.
Fractional MaxPooling is described in the paper Fractional Max-Pooling.
- Parameters:
ksize (Union[int, tuple[int]]) – Size of the pooling window. ksize can be a tuple of two values specify a shape \((k_H, k_W)\), or a single int K for \((K, K)\).
output_shape (Union[int, tuple[int]]) – The target output shape. output_shape can be a tuple of two values specify a shape \((H_{out}, W_{out})\), or a single float S for \((S, S)\).
data_format (str, optional) – The optional value for data format, is ‘NCHW’. Default: “NCHW”.
- Inputs:
input_x (Tensor) - Tensor of shape \((N, C, H_{in}, W_{in})\), with float16, float32, float64, int32, int64 data type.
random_samples (Tensor) - Tensor of shape \((N, C, 2)\). with float16, float32, float64 data type.
- Outputs:
y (Tensor) - Has the same type as the input_x. Has the shape \((N, C, H_{out}, W_{out})\).
argmax (Tensor) -A tensor whose data type must be int64. Has the same shape as the y.
- Raises:
TypeError – If data type of input_x is not one of the following: float16, float32, float64, int32, int64.
TypeError – If data type of random_samples is not one of the following: float16, float32, float64.
ValueError – If ksize is not a number and ksize is not a tuple of length 2.
ValueError – If output_shape is not a number and output_shape is not a tuple of length 2.
ValueError – If the sum of ksize , output_shape and -1 is larger than the corresponding dimension of input_x.
ValueError – If the dimension of random_samples is not 3.
ValueError – If the first dimension size of input_x and random_samples is not equal.
ValueError – If the second dimension size of input_x and random_samples is not equal.
ValueError – If the third dimension size of random_samples is not 2.
- Supported Platforms:
CPU
Examples
>>> # the ksize is an int number and the output_shape is a tuple. >>> ksize = 2 >>> output_shape = (2,2) >>> data_format = "NCHW" >>> input_x = Tensor(np.array([0.3220, 0.9545, 0.7879, 0.0975, 0.3698, ... 0.5135, 0.5740, 0.3435, 0.1895, 0.8764, ... 0.9581, 0.4760, 0.9014, 0.8522, 0.3664, ... 0.4980, 0.9673, 0.9879, 0.6988, 0.9022, ... 0.9304, 0.1558, 0.0153, 0.1559, 0.9852]).reshape([1, 1, 5, 5]), mstype.float32) >>> random_samples = Tensor(np.array([[[0.8, 0.8]]]), mstype.float32) >>> net = ops.FractionalMaxPoolWithFixedKsize(ksize, output_shape, data_format) >>> y, argmax = net(input_x, random_samples) >>> print(y) [[[[0.9545 0.8764] [0.9673 0.9852]]]] >>> print(argmax) [[[[ 1 9] [16 24]]]]
-
class
tinyms.primitives.
FusedAdaFactor
(enable_scale_parameter=False, enable_first_moment=False, enable_weight_decay=False)[source]¶ Updates gradients by the Adaptive Learning Rates with Sublinear Memory Cost (Adafactor) algorithm.
The Adafactor algorithm is proposed in Adafactor: Adafactor: Adaptive Learning Rates with Sublinear Memory Cost.
Warning
This is an experimental API that is subject to change or deletion.
Adafactor for weight vector are as follows,
\[\begin{split}\begin{array}{l} \\ \alpha_{t}=\max \left(\epsilon_{2}, \operatorname{RMS}\left(X_{t-1}\right)\right) \rho_{t} \\ G_{t}=\nabla f_{t}\left(X_{t-1}\right) \\ \hat{V}_{t}=\hat{\beta}_{2} \hat{V}_{t-1}+\left(1-\hat{\beta}_{2_{t}}\right)\left(G_{t}^{2}+ \\ \epsilon_{1} 1_{n}\right) \\ U_{t}=G_{t} / \sqrt{\hat{V}_{t}} \\ \hat{U}_{t}=U_{t} / \max \left(1, \operatorname{RMS}\left(U_{t}\right) / d\right) \\ X_{t}=X_{t-1}-\alpha_{t} \hat{U}_{t} \end{array}\end{split}\]Adafactor for weight matrices are as follows,
\[\begin{split}\begin{array}{l} \\ \alpha_{t}=\max \left(\epsilon_{2}, \operatorname{RMS}\left(X_{t-1}\right)\right) \rho_{t} \\ G_{t}=\nabla f_{t}\left(X_{t-1}\right) \\ R_{t}=\hat{\beta}_{2 t} R_{t-1}+\left(1-\hat{\beta}_{2 t}\right)\left(G_{t}^{2}+ \\ \epsilon_{1} 1_{n} 1_{m}^{\top}\right) 1_{m} \\ C_{t}=\hat{\beta}_{2 t} C_{t-1}+\left(1-\hat{\beta}_{2 t}\right) 1_{n}^{\top}\left(G_{t}^{2}+ \\ \epsilon_{1} 1_{n} 1_{m}^{\top}\right) \\ \hat{V}_{t}=R_{t} C_{t} / 1_{n}^{\top} R_{t} \\ U_{t}=G_{t} / \sqrt{\hat{V}_{t}} \\ \hat{U}_{t}=U_{t} / \max \left(1, \operatorname{RMS}\left(U_{t}\right) / d\right) \\ X_{t}=X_{t-1}-\alpha_{t} U_{t} \end{array}\end{split}\]Where RMS is:
\[\begin{split}\operatorname{RMS}\left(U_{t}\right)=\operatorname{RMS}_{x \in X}\left(u_{x t}\right)= \\ \sqrt{\operatorname{Mean}_{x \in X}\left(\frac{\left(g_{x t}\right)^{2}}{\hat{v}_{x t}}\right)}\end{split}\]\(x\) is each individual parameter, \(t\) is assumed to be the current number of steps, \(a_{t}\) is the learning rate, \(f(X)\) is the loss function, \(\epsilon1\) and \(\epsilon2\) is a small positive number to prevent errors, \(d\) is the clipping threshold, \(\beta_{2}\) is the moment decay, \(\rho\) is the relative step size, \(R\) is the running averages of the row sums of the squared gradient, \(C\) is the running averages of the column sums of the squared gradient.
- Parameters:
- Inputs:
epsilon (Tensor) - input epsilon pair.
clip_threshold (float) - The threshold of root mean square of final gradient update.
beta1 (float) - The exponential decay rate for the 1nd moment estimations.
beta2 (float) - The exponential decay rate for the 2nd moment estimations.
weight_decay (float) - The weight decay value, must be a scalar tensor with float data type.
learning_rate (float) - The learning rate value.
gradient (Tensor) - Gradient.
param (Tensor) - Weights to be updated.
exp_avg (Tensor) - The exponential moving average of 1st moment optimizer state.
exp_avg_sq_row (Tensor) - The exponential moving average of square of gradient square row factor.
exp_avg_sq_col (Tensor) - The exponential moving average of square of gradient square col factor.
exp_avg_sq (Tensor) - The exponential moving average of square of gradient square.
- Outputs:
dummy_param (Tensor) - The same shape and data type as param.
- Supported Platforms:
CPU
Examples
>>> import numpy as np >>> import mindspore as ms >>> import mindspore.nn as nn >>> import mindspore.ops as ops >>> from mindspore import Tensor, Parameter >>> from mindspore import dtype as mstype >>> param_shape = [2, 3, 2] >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.opt = ops.FusedAdaFactor() ... self.param = Parameter(Tensor(np.ones(param_shape), mstype.float32), name="param") ... self.exp_avg = Parameter(Tensor(np.zeros(param_shape), mstype.float32), name="exp_avg") ... self.exp_avg_sq = Parameter(Tensor(np.zeros(param_shape), mstype.float32), name="exp_avg_sq") ... self.exp_avg_sq_row = Parameter(Tensor(np.zeros([2, 3]), mstype.float32), name="exp_avg_sq_row") ... self.exp_avg_sq_col = Parameter(Tensor(np.zeros([2, 2]), mstype.float32), name="exp_avg_sq_col") ... ... def construct(self, epsilon, clip_threshold, beta1, beta2, weight_decay, lr, grad): ... out = self.opt(epsilon, clip_threshold, beta1, beta2, weight_decay, lr, grad, self.param, ... self.exp_avg, self.exp_avg_sq_row, self.exp_avg_sq_col, self.exp_avg_sq) ... return out >>> ms.set_context(mode=ms.GRAPH_MODE, device_target="CPU") >>> net = Net() >>> gradient = Tensor(np.ones(param_shape), mstype.float32) >>> output = net((1e-30, 1e-3), 1.0, 0.9, 0.8, 1e-2, 0.03, gradient)
-
class
tinyms.primitives.
FusedAdaFactorWithGlobalNorm
(enable_scale_parameter=False, enable_first_moment=False, enable_weight_decay=False)[source]¶ Divide global norm for gradient in FusedAdaFactor, and refer to super class for FusedAdaFactor details
-
class
tinyms.primitives.
FusedCastAdamWeightDecay
(use_locking=False)[source]¶ Updates gradients by the Adaptive Moment Estimation (AdamWeightDecay) algorithm with weight decay. This operator incorporates type conversion when parameters are initialized with dtype of float16.
The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization. The AdamWeightDecay variant was proposed in Decoupled Weight Decay Regularization.
The updating formulas are as follows,
\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ update = \frac{m}{\sqrt{v} + \epsilon} \\ update = \begin{cases} update + weight\_decay * w & \text{ if } weight\_decay > 0 \\ update & \text{ otherwise } \end{cases} \\ w = w - lr * update \end{array}\end{split}\]\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(\beta_1, \beta_2\) represent beta1 and beta2, \(lr\) represents learning_rate, \(w\) represents var, \(decay\) represents weight_decay, \(\epsilon\) represents epsilon.
- Parameters:
use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.
- Inputs:
var (Tensor) - Weights to be updated with the type float16 or float32.
m (Tensor) - The 1st moment vector in the updating formula with the type float32.
v (Tensor) - the 2nd moment vector in the updating formula with the type float32.
lr (float) - \(lr\) in the updating formula.
beta1 (float) - The exponential decay rate for the 1st moment estimations.
beta2 (float) - The exponential decay rate for the 2nd moment estimations.
epsilon (float) - Term added to the denominator to improve numerical stability.
decay (float) - The weight decay value, must be a scalar tensor with float data type.
gradient (Tensor) - Gradient, has the type float16.
- Outputs:
Tuple of 3 Tensor, the updated parameters.
var (Tensor) - The same shape and data type as var.
m (Tensor) - The same shape and data type as m.
v (Tensor) - The same shape and data type as v.
- Supported Platforms:
CPU
Examples
>>> import numpy as np >>> import mindspore as ms >>> import mindspore.nn as nn >>> import mindspore.ops as ops >>> from mindspore import Tensor, Parameter >>> from mindspore import dtype as mstype >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.opt = ops.FusedCastAdamWeightDecay() ... self.var = Parameter(Tensor(np.ones([2, 2]), mstype.float16), name="var") ... self.m = Parameter(Tensor(np.ones([2, 2]), mstype.float32), name="m") ... self.v = Parameter(Tensor(np.ones([2, 2]), mstype.float32), name="v") ... def construct(self, lr, beta1, beta2, epsilon, decay, grad, norm): ... out = self.opt(self.var, self.m, self.v, lr, beta1, beta2, epsilon, decay, grad, norm) ... return out >>> ms.set_context(mode=ms.GRAPH_MODE, device_target="CPU") >>> net = Net() >>> gradient = Tensor(np.ones([2, 2]), mstype.float16) >>> output = net(0.001, 0.9, 0.999, 1e-8, 0.0, gradient, 1.0)
-
class
tinyms.primitives.
FusedSparseAdam
(use_locking=False, use_nesterov=False)[source]¶ Merges the duplicate value of the gradient and then updates parameters by the Adaptive Moment Estimation (Adam) algorithm. This operator is used when the gradient is sparse.
The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.
The updating formulas are as follows,
\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ l = \alpha * \frac{\sqrt{1-\beta_2^t}}{1-\beta_1^t} \\ w = w - l * \frac{m}{\sqrt{v} + \epsilon} \end{array}\end{split}\]\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(l\) represents scaling factor lr, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(\beta_1^t\) and \(\beta_2^t\) represent beta1_power and beta2_power, \(\alpha\) represents learning_rate, \(w\) represents var, \(\epsilon\) represents epsilon.
All of inputs except indices 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.
- Parameters:
use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.
use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. If true, update the gradients using NAG. If false, update the gradients without using NAG. Default: False.
- Inputs:
var (Parameter) - Parameters to be updated with float32 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
m (Parameter) - The 1st moment vector in the updating formula, has the same shape and data type as var.
v (Parameter) - The 2nd moment vector in the updating formula, has the same shape and data type as var. Mean square gradients, has the same type as var with float32 data type.
beta1_power (Tensor) - \(beta_1^t\) in the updating formula with float32 data type. The shape is \((1, )\).
beta2_power (Tensor) - \(beta_2^t\) in the updating formula with float32 data type. The shape is \((1, )\).
lr (Tensor) - \(l\) in the updating formula. With float32 data type. The shape is \((1, )\).
beta1 (Tensor) - The exponential decay rate for the 1st moment estimations with float32 data type. The shape is \((1, )\).
beta2 (Tensor) - The exponential decay rate for the 2nd moment estimations with float32 data type. The shape is \((1, )\).
epsilon (Tensor) - Term added to the denominator to improve numerical stability with float32 data type. The shape is \((1, )\).
gradient (Tensor) - Gradient, has the same data type as var and gradient.shape[1:] = var.shape[1:] if var.shape > 1.
indices (Tensor) - Gradient indices with int32 data type and indices.shape[0] = gradient.shape[0].
- Outputs:
Tuple of 3 Tensors, this operator will update the input parameters directly, the outputs are useless.
var (Tensor) - A Tensor with shape \((N, *)\).
m (Tensor) - A Tensor with shape \((1, )\).
v (Tensor) - A Tensor with shape \((1, )\).
- Raises:
TypeError – If neither use_locking nor use_neserov is a bool.
TypeError – If dtype of var, m, v, beta1_power, beta2_power, lr, beta1, beta2, epsilon, gradient or indices is not float32.
RuntimeError – If the data type of all inputs except indices conversion of Parameter is not supported.
- Supported Platforms:
Ascend
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.sparse_apply_adam = ops.FusedSparseAdam() ... self.var = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="var") ... self.m = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="m") ... self.v = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="v") ... def construct(self, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad, indices): ... out = self.sparse_apply_adam(self.var, self.m, self.v, beta1_power, beta2_power, lr, beta1, beta2, ... epsilon, grad, indices) ... return out ... >>> net = Net() >>> beta1_power = Tensor(0.9, mindspore.float32) >>> beta2_power = Tensor(0.999, mindspore.float32) >>> lr = Tensor(0.001, mindspore.float32) >>> beta1 = Tensor(0.9, mindspore.float32) >>> beta2 = Tensor(0.999, mindspore.float32) >>> epsilon = Tensor(1e-8, mindspore.float32) >>> gradient = Tensor(np.array([[[0.1, 0.1]], [[0.1, 0.1]]]), mindspore.float32) >>> indices = Tensor([0, 1], mindspore.int32) >>> output = net(beta1_power, beta2_power, lr, beta1, beta2, epsilon, gradient, indices) >>> print(net.var.asnumpy()) [[[0.9997121 0.9997121 ]] [[0.9997121 0.9997121 ]] [[0.99971527 0.99971527]]]
-
class
tinyms.primitives.
FusedSparseFtrl
(lr, l1, l2, lr_power, use_locking=False)[source]¶ Merges the duplicate value of the gradient and then updates relevant entries according to the FTRL-proximal scheme.
All inputs except indices 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.
- Parameters:
lr (float) – The learning rate value, must be positive.
l1 (float) – l1 regularization strength, must be greater than or equal to zero.
l2 (float) – l2 regularization strength, must be greater than or equal to zero.
lr_power (float) – Learning rate power controls how the learning rate decreases during training, must be less than or equal to zero. Use fixed learning rate if lr_power is zero.
use_locking (bool) – Use locks for updating operation if true . Default: False.
- Inputs:
var (Parameter) - The variable to be updated. The data type must be float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
accum (Parameter) - The accumulation to be updated, must be same type and shape as var.
linear (Parameter) - the linear coefficient to be updated, must be same type and shape as var.
grad (Tensor) - A tensor of the same type as var and grad.shape[1:] = var.shape[1:] if var.shape > 1.
indices (Tensor) - A vector of indices into the first dimension of var and accum. The type must be int32 and indices.shape[0] = grad.shape[0].
- Outputs:
Tuple of 3 Tensor, this operator will update the input parameters directly, the outputs are useless.
var (Tensor) - A Tensor with shape \((N, *)\).
accum (Tensor) - A Tensor with shape \((1, )\).
linear (Tensor) - A Tensor with shape \((1, )\).
- Raises:
TypeError – If lr, l1, l2 or lr_power is not a float.
ValueError – If shape of lr_power less than or equal to zero.
TypeError – If dtype of var is not float32.
TypeError – If dtype of indices is not int32.
TypeError – If shape of accum, linear or grad is not same as var.
TypeError – If shape of indices is not same as shape of first dimension of grad.
RuntimeError – If the data type of all of inputs except indices conversion of Parameter is not supported.
- Supported Platforms:
Ascend
CPU
Examples
>>> class SparseApplyFtrlNet(nn.Cell): ... def __init__(self): ... super(SparseApplyFtrlNet, self).__init__() ... self.sparse_apply_ftrl = ops.FusedSparseFtrl(lr=0.01, l1=0.0, l2=0.0, lr_power=-0.5) ... self.var = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="var") ... self.accum = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="accum") ... self.linear = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="linear") ... ... def construct(self, grad, indices): ... out = self.sparse_apply_ftrl(self.var, self.accum, self.linear, grad, indices) ... return out ... >>> net = SparseApplyFtrlNet() >>> grad = Tensor(np.array([[[0.1, 0.1]], [[0.1, 0.1]]]).astype(np.float32)) >>> indices = Tensor(np.array([0, 1]).astype(np.int32)) >>> output = net(grad, indices) >>> print(net.var.asnumpy()) [[[-0.00598256 -0.00598256]] [[-0.00598256 -0.00598256]] [[ 1. 1. ]]]
-
class
tinyms.primitives.
FusedSparseLazyAdam
(use_locking=False, use_nesterov=False)[source]¶ Merges the duplicate value of the gradient and then updates parameters by the Adaptive Moment Estimation (Adam) algorithm. This operator is used when the gradient is sparse. The behavior is not equivalent to the original Adam algorithm, as only the current indices parameters will be updated.
The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.
The updating formulas are as follows,
\[\begin{split}\begin{array}{ll} \\ m = \beta_1 * m + (1 - \beta_1) * g \\ v = \beta_2 * v + (1 - \beta_2) * g * g \\ l = \alpha * \frac{\sqrt{1-\beta_2^t}}{1-\beta_1^t} \\ w = w - l * \frac{m}{\sqrt{v} + \epsilon} \end{array}\end{split}\]\(m\) represents the 1st moment vector, \(v\) represents the 2nd moment vector, \(g\) represents gradient, \(l\) represents scaling factor lr, \(\beta_1, \beta_2\) represent beta1 and beta2, \(t\) represents updating step while \(\beta_1^t\) and \(\beta_2^t\) represent beta1_power and beta2_power, \(\alpha\) represents learning_rate, \(w\) represents var, \(\epsilon\) represents epsilon.
All of inputs except indices 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.
- Parameters:
use_locking (bool) – Whether to enable a lock to protect variable tensors from being updated. If true, updates of the var, m, and v tensors will be protected by a lock. If false, the result is unpredictable. Default: False.
use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients. If true, update the gradients using NAG. If false, update the gradients without using NAG. Default: False.
- Inputs:
var (Parameter) - Parameters to be updated with float32 data type. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
m (Parameter) - The 1st moment vector in the updating formula, has the same shape and data type as var.
v (Parameter) - The 2nd moment vector in the updating formula, has the same shape and data type as var. Mean square gradients, has the same type as var with float32 data type.
beta1_power (Tensor) - \(beta_1^t\) in the updating formula with float32 data type. The shape is \((1, )\).
beta2_power (Tensor) - \(beta_2^t\) in the updating formula with float32 data type. The shape is \((1, )\).
lr (Tensor) - \(l\) in the updating formula with float32 data type. The shape is \((1, )\).
beta1 (Tensor) - The exponential decay rate for the 1st moment estimations with float32 data type. The shape is \((1, )\).
beta2 (Tensor) - The exponential decay rate for the 2nd moment estimations with float32 data type. The shape is \((1, )\).
epsilon (Tensor) - Term added to the denominator to improve numerical stability with float32 data type. The shape is \((1, )\).
gradient (Tensor) - Gradient value with float32 data type and gradient.shape[1:] = var.shape[1:] if var.shape > 1.
indices (Tensor) - Gradient indices with int32 data type and indices.shape[0] = gradient.shape[0].
- Outputs:
Tuple of 3 Tensors, this operator will update the input parameters directly, the outputs are useless.
var (Tensor) - A Tensor with shape \((N, *)\).
m (Tensor) - A Tensor with shape \((1, )\).
v (Tensor) - A Tensor with shape \((1, )\).
- Raises:
TypeError – If neither use_locking nor use_nestrov is a bool.
TypeError – If dtype of var, m, v, beta1_power, beta2_power, lr, beta1, beta2, epsilon or gradient is not float32.
TypeError – If dtype of indices is not int32.
RuntimeError – If the data type of all inputs except indices conversion of Parameter is not supported.
- Supported Platforms:
Ascend
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.sparse_apply_lazyadam = ops.FusedSparseLazyAdam() ... self.var = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="var") ... self.m = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="m") ... self.v = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="v") ... def construct(self, beta1_power, beta2_power, lr, beta1, beta2, epsilon, grad, indices): ... out = self.sparse_apply_lazyadam(self.var, self.m, self.v, beta1_power, beta2_power, lr, beta1, ... beta2, epsilon, grad, indices) ... return out ... >>> net = Net() >>> beta1_power = Tensor(0.9, mindspore.float32) >>> beta2_power = Tensor(0.999, mindspore.float32) >>> lr = Tensor(0.001, mindspore.float32) >>> beta1 = Tensor(0.9, mindspore.float32) >>> beta2 = Tensor(0.999, mindspore.float32) >>> epsilon = Tensor(1e-8, mindspore.float32) >>> gradient = Tensor(np.array([[[0.1, 0.1]], [[0.1, 0.1]]]), mindspore.float32) >>> indices = Tensor([0, 1], mindspore.int32) >>> output = net(beta1_power, beta2_power, lr, beta1, beta2, epsilon, gradient, indices) >>> print(net.var.asnumpy()) [[[0.9997121 0.9997121 ]] [[0.9997121 0.9997121 ]] [[1. 1. ]]]
-
class
tinyms.primitives.
FusedSparseProximalAdagrad
(use_locking=False)[source]¶ Merges the duplicate value of the gradient and then updates relevant entries according to the proximal adagrad algorithm.
\[\begin{split}\begin{array}{ll} \\ accum += grad * grad \\ \text{prox_v} = var - lr * grad * \frac{1}{\sqrt{accum}} \\ var = \frac{sign(\text{prox_v})}{1 + lr * l2} * \max(\left| \text{prox_v} \right| - lr * l1, 0) \end{array}\end{split}\]All of inputs except indices 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.
- Parameters:
use_locking (bool) – If true, the variable and accumulation tensors will be protected from being updated. Default: False.
- Inputs:
var (Parameter) - Variable tensor to be updated. The data type must be float32. The shape is \((N, *)\) where \(*\) means, any number of additional dimensions.
accum (Parameter) - Variable tensor to be updated, has the same shape and data type as var.
lr (Tensor) - The learning rate value. The data type must be float32. The shape is \((1, )\).
l1 (Tensor) - l1 regularization strength. The data type must be float32. The shape is \((1, )\).
l2 (Tensor) - l2 regularization strength. The data type must be float32. The shape is \((1, )\).
grad (Tensor) - A tensor of the same data type as var and grad.shape[1:] = var.shape[1:] if var.shape > 1.
indices (Tensor) - A vector of indices into the first dimension of var and accum. The type must be int32 and indices.shape[0] = grad.shape[0].
- Outputs:
Tuple of 2 Tensors, this operator will update the input parameters directly, the outputs are useless.
var (Tensor) - A Tensor with shape \((N, *)\).
accum (Tensor) - A Tensor with shape \((1, )\).
- Raises:
TypeError – If use_locking is not a bool.
TypeError – If dtype of var, accum, lr, l1, l2 or grad is not float32.
TypeError – If dtype of indices is not int32.
RuntimeError – If the data type of all inputs except indices conversion of Parameter is not supported.
- Supported Platforms:
Ascend
CPU
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.sparse_apply_proximal_adagrad = ops.FusedSparseProximalAdagrad() ... self.var = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="var") ... self.accum = Parameter(Tensor(np.ones([3, 1, 2]).astype(np.float32)), name="accum") ... self.lr = Tensor(0.01, mindspore.float32) ... self.l1 = Tensor(0.0, mindspore.float32) ... self.l2 = Tensor(0.0, mindspore.float32) ... def construct(self, grad, indices): ... out = self.sparse_apply_proximal_adagrad(self.var, self.accum, self.lr, self.l1, ... self.l2, grad, indices) ... return out ... >>> net = Net() >>> grad = Tensor(np.array([[[0.1, 0.1]], [[0.1, 0.1]]]).astype(np.float32)) >>> indices = Tensor(np.array([0, 1]).astype(np.int32)) >>> output = net(grad, indices) >>> print(net.var.asnumpy()) [[[0.99900496 0.99900496]] [[0.99900496 0.99900496]] [[1. 1. ]]]
-
class
tinyms.primitives.
FusedWeightScaleApplyMomentum
[source]¶ Optimizer that implements the Momentum algorithm with weight decay and loss scale.
Refer to the paper On the importance of initialization and momentum in deep learning for more details.
Refer to
mindspore.nn.Momentum
for more details about the formula and usage.Inputs of variable, accumulation and gradient 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 relatively highest priority data type. Data type conversion of Parameter is not supported. RuntimeError exception will be thrown.
- Inputs:
weight_decay (Tensor) - The weight decay value, must be a scalar tensor with float data type. Default: 0.0.
loss_scale (Tensor) - The loss scale value, must be a scalar tensor with float data type. Default: 1.0.
variable (Parameter) - Weights to be updated. data type must be float.
accumulation (Parameter) - Accumulated gradient value by moment weight. Has the same data type with variable.
learning_rate (Union[Number, Tensor]) - The learning rate value, must be a float number or a scalar tensor with float data type.
gradient (Tensor) - Gradient, has the same data type as variable.
momentum (Union[Number, Tensor]) - Momentum, must be a float number or a scalar tensor with float data type.
- Outputs:
Tensor, parameters to be updated.
- Supported Platforms:
GPU
Examples
Please refer to the usage in
mindspore.nn.Momentum
, and add weight_decay and loss_scale as inputs.
-
class
tinyms.primitives.
GLU
(axis=-1)[source]¶ Computes GLU (Gated Linear Unit activation function) of input tensors.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.glu()
for more details.- Supported Platforms:
Ascend
CPU
Examples
>>> from mindspore import ops, Tensor >>> from mindspore import dtype as mstype >>> import numpy as np >>> axis = 0 >>> x = Tensor(np.array([0.3220, 0.9545, 0.7879, 0.0975, 0.3698, ... 0.5135, 0.5740, 0.3435, 0.1895, 0.8764, ... 0.4980, 0.9673, 0.9879, 0.6988, 0.9022, ... 0.9304, 0.1558, 0.0153, 0.1559, 0.9852]).reshape([2, 2, 5]), mstype.float32) >>> glu = ops.GLU(axis=axis) >>> y = glu(x) >>> print(y) [[[0.20028052 0.6916126 0.57412136 0.06512236 0.26307625] [0.3682598 0.3093122 0.17306386 0.10212085 0.63814086]]]
-
class
tinyms.primitives.
Gamma
(seed=0, seed2=0)[source]¶ Produces random positive floating-point values x, distributed according to probability density function:
\[\text{P}(x|α,β) = \frac{\exp(-x/β)}{{β^α}\cdot{\Gamma(α)}}\cdot{x^{α-1}}\]Note
Random seed: A set of regular random numbers can be obtained through some complex mathematical algorithms, and the random seed is the initial value of this random number. If the random seed is the same, the random number obtained will not change.
Global random seed and operator-level random seed are not set: Use the default value as the random seed.
Global random seed is set, but operator-level random seed is not set: A global random seed will splice with a randomly generated seed.
Global random seed is not set, operator-level random seed is set: The default global random seed is used, and splices with the operator-level random seed.
Both Global random and operator-level random seed are set: The global random seed will splice with the operator-level random seed.
- Parameters:
- Inputs:
shape (tuple) - The shape of random tensor to be generated. Only constant value is allowed.
alpha (Tensor) - α is the shape parameter of Gamma distribution, which mainly determines the shape of the curve. It must be greater than 0. The data type is float32.
beta (Tensor) - β is the inverse scale parameter of the Gamma distribution, which mainly determines how steep the curve is. It must be greater than 0. The data type is float32.
- Outputs:
Tensor. The shape must be the broadcasted shape of Input “shape” and shapes of alpha and beta. The dtype is float32.
- Raises:
TypeError – If data type of seed or seed2 is not int.
TypeError – If alpha or beta is not a Tensor.
TypeError – If data type of alpha or beta is not float32.
ValueError – If shape is not a constant value.
- Supported Platforms:
Ascend
Examples
>>> shape = (3, 1, 2) >>> alpha = Tensor(np.array([[3, 4], [5, 6]]), mstype.float32) >>> beta = Tensor(np.array([1.0]), mstype.float32) >>> gamma = ops.Gamma(seed=3) >>> output = gamma(shape, alpha, beta) >>> result = output.shape >>> print(result) (3, 2, 2)
-
class
tinyms.primitives.
Gather
(batch_dims=0)[source]¶ Returns the slice of the input tensor corresponding to the elements of input_indices on the specified axis.
The following figure shows the calculation process of Gather commonly:
where params represents the input input_params, and indices represents the index to be sliced input_indices.
Refer to
mindspore.ops.gather()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> # case1: input_indices is a Tensor with shape (5, ). >>> input_params = Tensor(np.array([1, 2, 3, 4, 5, 6, 7]), mindspore.float32) >>> input_indices = Tensor(np.array([0, 2, 4, 2, 6]), mindspore.int32) >>> axis = 0 >>> output = ops.Gather()(input_params, input_indices, axis) >>> print(output) [1. 3. 5. 3. 7.] >>> # case2: input_indices is a Tensor with shape (2, 2). When the input_params has one dimension, the output shape is equal to the input_indices shape. >>> input_indices = Tensor(np.array([[0, 2], [2, 6]]), mindspore.int32) >>> axis = 0 >>> output = ops.Gather()(input_params, input_indices, axis) >>> print(output) [[ 1. 3.] [ 3. 7.]] >>> # case3: input_indices is a Tensor with shape (2, ). input_params is a Tensor with shape (3, 4) and axis is 0. >>> input_params = Tensor(np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]), mindspore.float32) >>> input_indices = Tensor(np.array([0, 2]), mindspore.int32) >>> axis = 0 >>> output = ops.Gather()(input_params, input_indices, axis) >>> print(output) [[1. 2. 3. 4.] [9. 10. 11. 12.]] >>> # case4: input_indices is a Tensor with shape (2, ). >>> # input_params is a Tensor with shape (3, 4) and axis is 1, batch_dims is 1. >>> input_params = Tensor(np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]), mindspore.float32) >>> input_indices = Tensor(np.array([0, 2, 1]), mindspore.int32) >>> axis = 1 >>> batch_dims = 1 >>> output = ops.Gather(batch_dims)(input_params, input_indices, axis) >>> print(output) [ 1. 7. 10.]
-
class
tinyms.primitives.
GatherD
[source]¶ Gathers elements along an axis specified by dim.
Refer to
mindspore.ops.gather_elements()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([[1, 2], [3, 4]]), mindspore.int32) >>> index = Tensor(np.array([[0, 0], [1, 0]]), mindspore.int32) >>> dim = 1 >>> output = ops.GatherD()(x, dim, index) >>> print(output) [[1 1] [4 3]]
-
class
tinyms.primitives.
GatherNd
[source]¶ Gathers slices from a tensor by indices.
Refer to
mindspore.ops.gather_nd()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> op = ops.GatherNd() >>> input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32) >>> indices = Tensor(np.array([[0, 0], [1, 1]]), mindspore.int32) >>> output = op(input_x, indices) >>> print(output) [-0.1 0.5]
-
class
tinyms.primitives.
GatherV2
[source]¶ Same as operator Gather. GatherV2 will be deprecated in the future. Please use Gather instead.
-
class
tinyms.primitives.
Gcd
[source]¶ 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.
Warning
This is an experimental API that is subject to change or deletion.
- 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 precision 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
GPU
CPU
Examples
>>> x1 = Tensor(np.array([7, 8, 9])) >>> x2 = Tensor(np.array([14, 6, 12])) >>> gcd_ = ops.Gcd() >>> y = gcd_(x1, x2) >>> print(y) [7 2 3]
-
class
tinyms.primitives.
GeLU
[source]¶ Gaussian Error Linear Units activation function.
GeLU is described in the paper Gaussian Error Linear Units (GELUs). And also please refer to BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding.
GeLU is defined as follows:
\[GELU(x_i) = x_i*P(X < x_i)\]where \(P\) is the cumulative distribution function of the standard Gaussian distribution, \(x_i\) is the input element.
- Inputs:
x (Tensor) - The input of the activation function GeLU, the data type is float16, float32 or float64.
- Outputs:
Tensor, with the same type and shape as x.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32) >>> gelu = ops.GeLU() >>> result = gelu(x) >>> print(result) [0.841192 1.9545976 2.9963627]
-
class
tinyms.primitives.
GeSwitch
[source]¶ Adds control switch to data.
Switch data flows into false or true branch depending on the condition. If the condition is true, the true branch will be activated, or vise verse.
- Inputs:
data (Union[Tensor, Number]) - The data to be used for switch control.
pred (Tensor) - It must be a scalar whose type is bool and shape is (), It is used as condition for switch control.
- Outputs:
tuple. Output is tuple(false_output, true_output). The Elements in the tuple has the same shape of input data. The false_output connects with the false_branch and the true_output connects with the true_branch.
Examples
>>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.square = ops.Square() ... self.add = ops.Add() ... self.value = Tensor(np.full((1), 3), mindspore.float32) ... self.switch = ops.GeSwitch() ... self.merge = ops.Merge() ... self.less = ops.Less() ... ... def construct(self, x, y): ... cond = self.less(x, y) ... st1, sf1 = self.switch(x, cond) ... st2, sf2 = self.switch(y, cond) ... add_ret = self.add(st1, st2) ... st3, sf3 = self.switch(self.value, cond) ... sq_ret = self.square(sf3) ... ret = self.merge((add_ret, sq_ret)) ... return ret[0] ... >>> x = Tensor(10.0, dtype=mindspore.float32) >>> y = Tensor(5.0, dtype=mindspore.float32) >>> net = Net() >>> output = net(x, y) >>> print(output)
-
class
tinyms.primitives.
Gelu
[source]¶ Same as operator GeLU. Gelu will be deprecated in the future. Please use GeLU instead.
-
class
tinyms.primitives.
Geqrf
[source]¶ Decomposes a matrix into the product of an orthogonal matrix Q and an upper triangular matrix R. The process is called QR decomposition: \(A = QR\).
Both Q and R matrices are stored in the same output tensor y. The elements of R are stored on and above the diagonal, whereas elementary reflectors (or Householder vectors) implicitly defining matrix Q are stored below the diagonal.
This function returns two tensors (y, tau).
Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
x (Tensor) - Tensor of shape \((*, m, n)\), input must be a matrix greater than or equal to 2D, with dtype of float32, float64, complex64, complex128.
- Outputs:
y (Tensor) - Tensor of shape \((*, m, n)\), has the same dtype as the x.
tau (Tensor) - Tensor of shape \((*, p)\) and \(p = min(m, n)\), has the same dtype as the x.
- Raises:
TypeError – If x is not a Tensor.
TypeError – If the dtype of x is neither float32, float64, complex64, complex128.
ValueError – If x dimension is less than 2
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.array([[-2.0, -1.0], [1.0, 2.0]]).astype(np.float32)) >>> geqrf = ops.Geqrf() >>> y, tau = geqrf(input_x) >>> print(y) [[ 2.236068 1.7888544] [-0.236068 1.3416407]] >>> print(tau) [1.8944271 0. ]
-
class
tinyms.primitives.
Ger
[source]¶ Ger product of x1 and x2. Calculate the outer product of two arrays. If x1 is a 1D Tensor of shape \((m,)\) and x2 is a 1D Tensor of shape \((n,)\), then output must be a 2D Tensor of shape \((m, n)\).
Refer to
mindspore.ops.ger()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x1 = Tensor([1., 2., 3., 4.], mindspore.float32) >>> x2 = Tensor([1., 2., 3.], mindspore.float32) >>> ger = ops.Ger() >>> output = ger(x1, x2) >>> print(output) [[ 1. 2. 3.] [ 2. 4. 6.] [ 3. 6. 9.] [ 4. 8. 12.]]
-
class
tinyms.primitives.
GetNext
(types, shapes, output_num, shared_name)[source]¶ Returns the next element in the dataset queue.
Note
The GetNext operation needs to be associated with network and it also depends on the ‘dataset’ interface, For example, please refer to
mindspore.dataset.MnistDataset
. it can’t be used directly as a single operation. For details, please refer tomindspore.connect_network_with_dataset
source code.- Parameters:
- Inputs:
No inputs.
- Outputs:
tuple[Tensor], the output of dataset. The shape is described in shapes and the type is described in types.
- Supported Platforms:
Ascend
GPU
Examples
>>> import mindspore >>> from mindspore import ops >>> from mindspore import dataset as ds >>> from mindspore.common import dtype as mstype >>> data_path = "/path/to/MNIST_Data/train/" >>> train_dataset = ds.MnistDataset(data_path, num_samples=10) >>> dataset_helper = mindspore.DatasetHelper(train_dataset, dataset_sink_mode=True) >>> dataset = dataset_helper.iter.dataset >>> dataset_types, dataset_shapes = dataset_helper.types_shapes() >>> queue_name = dataset.__transfer_dataset__.queue_name >>> get_next = ops.GetNext(dataset_types, dataset_shapes, len(dataset_types), queue_name) >>> data, label = get_next() >>> relu = ops.ReLU() >>> result = relu(data.astype(mstype.float32)) >>> print(result.shape) (28, 28, 1)
-
class
tinyms.primitives.
Greater
[source]¶ Compare the value of the input parameters \(x,y\) element-wise, and the output result is a bool value.
Refer to
mindspore.ops.gt()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([1, 2, 3]), mindspore.int32) >>> y = Tensor(np.array([1, 1, 4]), mindspore.int32) >>> greater = ops.Greater() >>> output = greater(x, y) >>> print(output) [False True False]
-
class
tinyms.primitives.
GreaterEqual
[source]¶ Computes the boolean value of \(x >= y\) element-wise.
Refer to
mindspore.ops.ge()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([1, 2, 3]), mindspore.int32) >>> y = Tensor(np.array([1, 1, 4]), mindspore.int32) >>> greater_equal = ops.GreaterEqual() >>> output = greater_equal(x, y) >>> print(output) [True True False]
-
class
tinyms.primitives.
GridSampler2D
(interpolation_mode='bilinear', padding_mode='zeros', align_corners=False)[source]¶ This operation samples 2d input_x by using interpolation based on flow field grid, which is usually gennerated by
mindspore.ops.affine_grid()
.Warning
This is an experimental API that is subject to change or deletion.
- Parameters:
interpolation_mode (str, optional) – An optional string specifying the interpolation method. The optional values are “bilinear” or “nearest”. Default: “bilinear”.
padding_mode (str, optional) –
An optional string specifying the pad method. The optional values are “zeros”, “border” or “reflection”. Default: “zeros”. When the sampling grid is outside input’s bounds, effects of various padding modes are as follows:
”zeros”: Pads the input tensor with zeros.
”border”: Pads the input tensor with the values of the pixels on the border of the tensor.
”reflection”: Pads the input tensor by reflecting the values of the pixels at the boundary of the tensor.
align_corners (bool, optional) – An optional bool. When set to True, the centers of the corner pixels of the input and output tensors are aligned. When set to False, it is not aligned. Defaults to False.
- Inputs:
input_x (Tensor) - A 4-D tensor with dtype of float16 or float32 and shape of \((N, C, H_{in}, W_{in})\).
grid (Tensor) - A 4-D tensor whose dtype is the same as input_x and whose shape is \((N, H_{out}, W_{out}, 2)\). Used to specify the sampling pixel locations normalized by the input spatial dimensions.
- Outputs:
A 4-D Tensor whose dtype is the same as input_x and whose shape is \((N, C, H_{out}, W_{out})\).
- Raises:
TypeError – If input_x or grid is not a Tensor.
TypeError – If the dtypes of input_x and grid are inconsistent.
TypeError – If the dtype of input_x or grid is not a valid type.
TypeError – If align_corners is not a boolean value.
ValueError – If the rank of input_x or grid is not equal to 4.
ValueError – If the first dimension of input_x is not equal to that of grid.
ValueError – If the forth dimension of grid is not equal to 2.
ValueError – If interpolation_mode is not “bilinear”, “nearest” or a string value.
ValueError – If padding_mode is not “zeros”, “border”, “reflection” or a string value.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> gridsampler = ops.GridSampler2D(interpolation_mode='bilinear', padding_mode='zeros', align_corners=True) >>> input_x = Tensor(np.arange(16).reshape((2, 2, 2, 2)).astype(np.float32)) >>> grid = Tensor(np.arange(-9, 9, 0.5).reshape((2, 3, 3, 2)).astype(np.float32)) >>> output = gridsampler(input_x, grid) >>> print(output) [[[[ 0. 0. 0. ] [ 0. 0. 0. ] [ 0. 0. 0.5 ]] [[ 0. 0. 0. ] [ 0. 0. 0. ] [ 0. 1.5 4.5 ]]] [[[10. 8.25 1.375] [ 0. 0. 0. ] [ 0. 0. 0. ]] [[14. 11.25 1.875] [ 0. 0. 0. ] [ 0. 0. 0. ]]]]
-
class
tinyms.primitives.
GridSampler3D
(interpolation_mode='bilinear', padding_mode='zeros', align_corners=False)[source]¶ Given an input and a grid, the output is calculated using the input values and pixel positions in the grid. Only volume (5-D) input is supported.
Warning
This is an experimental API that is subject to change or deletion.
Refer to
mindspore.ops.grid_sample()
for more details.- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> gridsampler = ops.GridSampler3D(interpolation_mode='bilinear', padding_mode='zeros', align_corners=True) >>> input_x = Tensor(np.arange(32).reshape((2, 2, 2, 2, 2)).astype(np.float32)) >>> grid = Tensor(np.arange(-0.2, 1, 0.1).reshape((2, 2, 1, 1, 3)).astype(np.float32)) >>> output = gridsampler(input_x, grid) >>> print(output) [[[[[ 3.3 ]] [[ 4.35 ]]] [[[11.300001]] [[12.349999]]]] [[[[21.4 ]] [[22.449999]]] [[[29.4 ]] [[30.449999]]]]]
-
class
tinyms.primitives.
HSVToRGB
[source]¶ Transform one single or a batch of images from HSV to RGB color space. Each pixel’s HSV value is converted to its corresponding RGB value. Note that the function is only well-defined for input pixel values in the range [0, 1]. Image format should be “NHWC”.
- Inputs:
x (Tensor) - The input image must be a 4-D tensor of shape \((batch, image\_height, image\_width, channel)\). Number of channel must be 3. Types allowed: float16, float32, float64.
- Outputs:
A 4-D tensor of shape \((batch, image\_height, image\_width, channel)\) with same type of input.