mindspore.ops.operations
Primitive operator classes.
A collection of operators to build nerual networks or computing functions.
- class
mindspore.ops.operations.
ACos
(*args, **kwargs)[source] Computes arccosine of input element-wise.
- Inputs:
- input_x (Tensor) - The shape of tensor is
.
- Outputs:
- Tensor, has the same shape as input_x.
Examples
- Copy>>> acos = ACos()
- >>> X = Tensor(np.array([0.74, 0.04, 0.30, 0.56]), ms.float32)
- >>> output = acos(X)
- class
mindspore.ops.operations.
Abs
(*args, **kwargs)[source] Returns absolute value of a tensor element-wise.
- Inputs:
- input_x (Tensor) - The input tensor. The shape of tensor is
.
- Outputs:
- Tensor, has the same shape as the input_x.
Examples
- Copy>>> input_x = Tensor(np.array([-1.0, 1.0, 0.0]), mindspore.float32)
- >>> abs = Abs()
- >>> abs(input_x)
- [1.0, 1.0, 0.0]
- class
mindspore.ops.operations.
Adam
(*args, **kwargs)[source] - Updates gradients by Adaptive Moment Estimation (Adam) algorithm.
The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.
The updating formulas are as follows,
represents the 1st moment vector,
represents the 2nd moment vector,
representsgradient,
represents scaling factor lr,
represent beta1 and beta2,
represents updating step while
and
represent beta1_power andbeta2_power,
represents learning_rate,
represents var,
representsepsilon.
- Parameters
use_locking (bool) – Whether to enable a lock to protect updating variable tensors.If True, updating 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, updates the gradients using NAG.If False, updates the gradients without using NAG. Default: False.
Inputs:
var (Tensor) - Weights to be updated.
m (Tensor) - The 1st moment vector in the updating formula.
v (Tensor) - the 2nd moment vector in the updating formula.
beta1_power (float) -
in the updating formula.
-
beta2_power (float) -
in the updating formula.
-
lr (float) -
in the updating formula.
-
beta1 (float) - The exponential decay rate for the 1st moment estimates.
-
beta2 (float) - The exponential decay rate for the 2nd moment estimates.
-
epsilon (float) - Term added to the denominator to improve numerical stability.
-
gradient (Tensor) - Gradients.
- Outputs:
- Tensor, has the same shape and data type as var.
- class
mindspore.ops.operations.
AddN
(*args, **kwargs)[source] - Computes addition of all input tensors element-wise.
All input tensors should have the same shape.
- Inputs:
- input_x (Union(tuple[Tensor], list[Tensor])) - The input tuple or listis made up of multiple tensors whose dtype is number or bool to be added together.
Outputs:
- Tensor, has the same shape and dtype as each entry of the input_x.
Examples
- Copy>>> class NetAddN(nn.Cell):
- >>> def __init__(self):
- >>> super(NetAddN, self).__init__()
- >>> self.addN = AddN()
- >>>
- >>> def construct(self, *z):
- >>> return self.addN(z)
- >>>
- >>> net = NetAddN()
- >>> input_x = Tensor(np.array([1, 2, 3]), mindspore.int32)
- >>> input_y = Tensor(np.array([4, 5, 6]), mindspore.int32)
- >>> net(input_x, input_y, input_x, input_y)
- Tensor([10, 14, 18], shape=(3,), dtype=mindspore.int32)
- class
mindspore.ops.operations.
AllGather
(*args, **kwargs)[source] - Gathers tensors from the specified communication group.
Note
Tensor must have the same shape and format in all processes participating in the collective.
- Parameters
group (str) – The communication group to work on. Default: “hccl_world_group”.
Raises
TypeError – If group is not a string.
ValueError – If the local rank id of the calling process in the group is larger than the group’s rank size.
Inputs:
- input_x (Tensor) - The shape of tensor is
.
- Outputs:
- Tensor. If the number of devices in the group is N,then the shape of output is
.
Examples
- Copy>>> from mindspore.communication.management import init
- >>> import mindspore.ops.operations as P
- >>> init('nccl')
- >>> class Net(nn.Cell):
- >>> def __init__(self):
- >>> super(Net, self).__init__()
- >>> self.allgather = P.AllGather(group="nccl_world_group")
- >>>
- >>> def construct(self, x):
- >>> return self.allgather(x)
- >>>
- >>> input_ = Tensor(np.ones([2, 8]).astype(np.float32))
- >>> net = Net()
- >>> output = net(input_)
- class
mindspore.ops.operations.
AllReduce
(*args, **kwargs)[source] - Reduces the tensor data across all devices in such a way that all devices will get the same final result.
Note
The operation of AllReduce does not support “prod” currently.The input of AllReduce does not support dtype “Bool”.Tensor must have same shape and format in all processes participating in the collective.
- Parameters
Raises
TypeError – If any of op and group is not a string or fusion is not a integer or the input’s dtype is bool.
ValueError – If op is “prod”
Inputs:
- input_x (Tensor) - The shape of tensor is
.
- Outputs:
- Tensor, has the same shape of the input, i.e.,
.The contents depend on the specified operation.
Examples
- Copy>>> from mindspore.communication.management import init
- >>> import mindspore.ops.operations as P
- >>> init('nccl')
- >>> class Net(nn.Cell):
- >>> def __init__(self):
- >>> super(Net, self).__init__()
- >>> self.allreduce_sum = P.AllReduce(ReduceOp.SUM, group="nccl_world_group")
- >>>
- >>> def construct(self, x):
- >>> return self.allreduce_sum(x)
- >>>
- >>> input_ = Tensor(np.ones([2, 8]).astype(np.float32))
- >>> net = Net()
- >>> output = net(input_)
vmimpl
(_x)[source]- Implement by vm mode.
- class
mindspore.ops.operations.
ApplyMomentum
(*args, **kwargs)[source] - Optimizer that implements the Momentum algorithm.
Refer to the paper On the importance of initialization and momentum in deeplearning for more details.
- Parameters
Inputs:
variable (Tensor) - Weights to be updated.
accumulation (Tensor) - Accumulated gradient value by moment weight.
learning_rate (float) - Learning rate.
gradient (Tensor) - Gradients.
momentum (float) - Momentum.
Outputs:
- Tensor, parameters to be updated.
Examples
- Copy>>> net = ResNet50()
- >>> loss = SoftmaxCrossEntropyWithLogits()
- >>> opt = ApplyMomentum(Tensor(np.array([0.001])), Tensor(np.array([0.9])),
- filter(lambda x: x.requires_grad, net.get_parameters()))
- >>> model = Model(net, loss, opt)
- class
mindspore.ops.operations.
ArgMaxWithValue
(*args, **kwargs)[source] - Calculates maximum value with corresponding index.
Calculates maximum value along with given axis for the input tensor. Returns the maximum values and indices.
Note
In auto_parallel and semi_auto_parallel mode, the first output index can not be used.
- Parameters
Inputs:
- input_x (Tensor) - The input tensor, can be any dimension. Set the shape of input tensor as
.
- Outputs:
- Tensor, corresponding index and maximum value of input tensor. If keep_dims is true, the output tensors shapeis
. Else, the shape is
.
Examples
- Copy>>> input = Tensor(np.random.rand(5))
- >>> index, output = ArgMaxWithValue()(input)
- class
mindspore.ops.operations.
ArgMinWithValue
(*args, **kwargs)[source] - Calculates minimum value with corresponding index, return indices and values.
Calculates minimum value along with given axis for the input tensor. Returns the minimum values and indices.
Note
In auto_parallel and semi_auto_parallel mode, the first output index can not be used.
- Parameters
Inputs:
- input_x (Tensor) - The input tensor, can be any dimension. Set the shape of input tensor as
.
- Outputs:
- Tensor, corresponding index and minimum value of input tensor. If keep_dims is true, the output tensors shapeis
. Else, the shape is
.
Examples
- Copy>>> input = Tensor(np.random.rand(5))
- >>> index, output = ArgMinWithValue()(input)
- class
mindspore.ops.operations.
Argmax
(*args, **kwargs)[source] - Returns the indices of the max value of a tensor across the axis.
If the shape of input tensor is
, the output tensor shape is
.
- Parameters
axis (int) – Axis on which Argmax operation applies. Default: -1.
output_type (
mindspore.dtype
) – An optional data type of mindspore.dtype.int32 andmindspore.dtype.int64. Default: mindspore.dtype.int64.
Inputs:
- input_x (Tensor) - Input tensor.
Outputs:
- Tensor, indices of the max value of input tensor across the axis.
Examples
- Copy>>> input = Tensor(np.array([2.0, 3.1, 1.2]))
- >>> index = Argmax()(input)
- >>> assert index == Tensor(1, mindspore.int64)
- class
mindspore.ops.operations.
Argmin
(*args, **kwargs)[source] - Returns the indices of the min value of a tensor across the axis.
If the shape of input tensor is
, the output tensor shape is
.
- Parameters
axis (int) – Axis on which Argmin operation applies. Default: -1.
output_type (
mindspore.dtype
) – An optional data type from: mindspore.dtype.int32,mindspore.dtype.int64. Default: mindspore.dtype.int64.
Inputs:
- input_x (Tensor) - Input tensor.
Outputs:
- Tensor, indices of the min value of input tensor across the axis.
Examples
- Copy>>> input = Tensor(np.array([2.0, 3.1, 1.2]))
- >>> index = Argmin()(input)
- >>> assert index == Tensor(2, mindspore.int64)
- class
mindspore.ops.operations.
Assign
(*args, **kwargs)[source] Assign Parameter with a value.
- Inputs:
variable (Parameter) - The Parameter.
value (Tensor) - The value to assign.
Outputs:
- Tensor, has the same type as original variable.
Examples
- Copy>>> class Net(nn.Cell):
- >>> def __init__(self):
- >>> super(Net, self).__init__()
- >>> self.y = mindspore.Parameter(Tensor([1.0], mindspore.float32), name="y")
- >>>
- >>> def construct(self, x):
- >>> Assign()(self.y, x)
- >>> return x
- >>> x = Tensor([2.0], mindspore.float32)
- >>> net = Net()
- >>> net(x)
- class
mindspore.ops.operations.
AssignAdd
(*args, **kwargs)[source] Updates a Parameter by adding a value to it.
- Inputs:
input_x (Parameter) - The Parameter.
input_y (Union[scalar, Tensor]) - Has the same shape as input_x.
Examples
- Copy>>> class Net(Cell):
- >>> def __init__(self):
- >>> self.AssignAdd = P.AssignAdd()
- >>> self.inputdata = Parameter(initializer(1, [1], mindspore.int64), name="global_step")
- >>>
- >>> def construct(self, x):
- >>> self.AssignAdd(self.inputdata, x)
- >>> return self.inputdata
- >>>
- >>> net = Net()
- >>> x = Tensor(np.ones([1]).astype(np.int64)*100)
- >>> net(x)
- class
mindspore.ops.operations.
AssignSub
(*args, **kwargs)[source] Updates a Parameter by subtracting a value from it.
- Inputs:
input_x (Parameter) - The Parameter.
input_y (Union[scalar, Tensor]) - Has the same shape as input_x.
Examples
- Copy>>> class Net(Cell):
- >>> def __init__(self):
- >>> self.AssignSub = P.AssignSub()
- >>> self.inputdata = Parameter(initializer(1, [1], mindspore.int64), name="global_step")
- >>>
- >>> def construct(self, x):
- >>> self.AssignSub(self.inputdata, x)
- >>> return self.inputdata
- >>>
- >>> net = Net()
- >>> x = Tensor(np.ones([1]).astype(np.int64)*100)
- >>> net(x)
- class
mindspore.ops.operations.
AvgPool
(*args, **kwargs)[source] - Average pooling operation.
Applies a 2D average pooling over an input Tensor which can be regarded as a composition of 2D input planes.Typically the input is of shape
, AvgPool2d outputsregional average in the
-dimension. Given kernel size
and stride
, the operation is as follows.
- Parameters
ksize (Union__[int, tuple[int]__]) – The size of the window to take a average over, that should be a tupleof two int for width and height. Default: 1.
stride (Union__[int, tuple[int]__]) – The stride of the window, that should be a tuple of two int forwidth and height. Default: 1.
padding (str) – The optional values for pad mode “SAME”, “VALID”. Default: “VALID”.
Inputs:
- input (Tensor) - Tensor of shape
.
- Outputs:
- Tensor, with shape
.
- class
mindspore.ops.operations.
BatchMatMul
(*args, **kwargs)[source] - Computes matrix multiplication between two tensors by batch
result[…, :, :] = tensor(a[…, :, :]) * tensor(b[…, :, :]).
The two input tensors must have same rank and the rank must be 3 at least.
- Parameters
Inputs:
- input_x (Tensor) - The first tensor to be multiplied. The shape of the tensor is
,where
represents the batch size which can be multidimensional,
and
are thesize of the last two dimensions. If transpose_a is True, its shape should be
.
-
input_y (Tensor) - The second tensor to be multiplied. The shape of the tensor is
. Iftranspose_b is True, its shape should be
.
- Outputs:
- Tensor, the shape of the output tensor is
.
Examples
- Copy>>> input_x = Tensor(np.ones(shape=[2, 4, 1, 3]), mindspore.float32)
- >>> input_y = Tensor(np.ones(shape=[2, 4, 3, 4]), mindspore.float32)
- >>> batmatmul = BatchMatMul()
- >>> output = batmatmul(input_x, input_y)
- >>>
- >>> input_x = Tensor(np.ones(shape=[2, 4, 3, 1]), mindspore.float32)
- >>> input_y = Tensor(np.ones(shape=[2, 4, 3, 4]), mindspore.float32)
- >>> batmatmul = BatchMatMul(transpose_a=True)
- >>> output = batmatmul(input_x, input_y)
- class
mindspore.ops.operations.
BatchNorm
(*args, **kwargs)[source] - Batch Normalization for input data and updated parameters.
Batch Normalization is widely used in convolutional neural networks. This operationapplies Batch Normalization over input to avoid internal covariate shift as describedin the paper Batch Normalization: Accelerating Deep Network Training by Reducing InternalCovariate Shift. It rescales and recenters thefeatures using a mini-batch of data and the learned parameters which can be describedin the following formula,
where
is scale,
is bias,
is epsilon.
- Parameters
Inputs:
- input_x (Tensor) - Tensor of shape
.
-
scale (Tensor) - Tensor of shape
.
-
bias (Tensor) - Tensor of shape
.
-
mean (Tensor) - Tensor of shape
.
-
variance (Tensor) - Tensor of shape
.
- Outputs:
Tuple of 5 Tensor, the normalized inputs and the updated parameters.
- output_x (Tensor) - The same type and shape as the input_x. The shape is
.
-
updated_scale (Tensor) - Tensor of shape
.
-
updated_bias (Tensor) - Tensor of shape
.
-
reserve_space_1 (Tensor) - Tensor of shape
.
-
reserve_space_2 (Tensor) - Tensor of shape
.
-
reserve_space_3 (Tensor) - Tensor of shape
.
- class
mindspore.ops.operations.
BiasAdd
(*args, **kwargs)[source] - Returns sum of input and bias tensor.
Adds the 1-D bias tensor to the input tensor, and boardcasts the shape on all axisexcept for the channel axis.
- Inputs:
- input_x (Tensor) - Input value, with shape
or
.
-
bias (Tensor) - Bias value, with shape
.
- Outputs:
- Tensor, with the same shape and type as input_x.
- class
mindspore.ops.operations.
BinaryCrossEntropy
(*args, **kwargs)[source] - Computes the Binary Cross Entropy between the target and the output.
Note
Sets input as
, input label as
, output as
.Let,
Then,
- Parameters
reduction (str) – Specifies the reduction to apply to the output.Its value should be one of ‘none’, ‘mean’, ‘sum’. Default: ‘mean’.
Inputs:
input_x (Tensor) - The input Tensor.
input_y (Tensor) - The label Tensor which has same shape as input_x.
weight (Tensor, optional) - A rescaling weight applied to the loss of each batch element.And it should have same shape as input_x. Default: None.
Outputs:
- Tensor or Scalar, if reduction is ‘none’, then output is a tensor and same shape as input_x.Otherwise it is a scalar.
- class
mindspore.ops.operations.
BoundingBoxDecode
(*args, **kwargs)[source] Decode bounding boxes locations.
- Parameters
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).
max_shape (tuple) – The max size limit for decoding box calculation.
wh_ratio_clip (float) – The limit of width and height ratio for decoding box calculation. Default: 0.016.
Inputs:
anchor_box (Tensor) - Anchor boxes.
deltas (Tensor) - Delta of boxes.
Outputs:
- Tensor, decoded boxes.
Examples
- Copy>>> boundingbox_decode = 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)
- >>> bbox = boundingbox_decode(anchor_box, deltas)
- class
mindspore.ops.operations.
BoundingBoxEncode
(*args, **kwargs)[source] Encode bounding boxes locations.
- Parameters
Inputs:
anchor_box (Tensor) - Anchor boxes.
groundtruth_box (Tensor) - Ground truth boxes.
Outputs:
- Tensor, encoded bounding boxes.
Examples
- Copy>>> boundingbox_encode = BoundingBoxEncode(means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0))
- >>> delta_box = boundingbox_encode(anchor_box, groundtruth_box)
- class
mindspore.ops.operations.
Broadcast
(*args, **kwargs)[source] - Broadcasts the tensor to the whole group.
Note
Tensor must have the same shape and format in all processes participating in the collective.
- Parameters
Inputs:
- input_x (Tensor) - The shape of tensor is
.
- Outputs:
- Tensor, has the same shape of the input, i.e.,
.The contents depend on the data of the root_rank device.
- Raises
- TypeError – If root_rank is not a integer or group is not a string.
Examples
- Copy>>> from mindspore.communication.management import init
- >>> import mindspore.ops.operations as P
- >>> init('nccl')
- >>> class Net(nn.Cell):
- >>> def __init__(self):
- >>> super(Net, self).__init__()
- >>> self.broadcast = P.Broadcast(1)
- >>>
- >>> def construct(self, x):
- >>> return self.broadcast((x,))
- >>>
- >>> input_ = Tensor(np.ones([2, 8]).astype(np.float32))
- >>> net = Net()
- >>> output = net(input_)
- class
mindspore.ops.operations.
Cast
(*args, **kwargs)[source] Returns a tensor with the new specified data type.
- Inputs:
- input_x (Union[Tensor, Number]) - The shape of tensor is
.The tensor to be casted.
-
type (dtype.Number) - The valid data type of the output tensor. Only constant value is allowed.
- Outputs:
- Tensor, the shape of tensor is
, same as input_x.
Examples
- Copy>>> input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
- >>> input_x = Tensor(input_np)
- >>> type_dst = mindspore.int32
- >>> cast = Cast()
- >>> result = cast(input_x, type_dst)
- >>> expect = input_np.astype(type_dst)
- class
mindspore.ops.operations.
CheckValid
(*args, **kwargs)[source] - Check bounding box.
Check whether the bounding box cross data and data border.
- Inputs:
bboxes (Tensor) - Bounding boxes tensor with shape (N, 4).
img_metas (Tensor) - Raw image size information, format (height, width, ratio).
Outputs:
- Tensor, the valided tensor.
- class
mindspore.ops.operations.
Concat
(*args, **kwargs)[source] - Concat tensor in specified axis.
Concat input tensors along with the given axis.
Note
The input data is a tuple of tensors. These tensors have the same rank R. Set the given axis as m, and
. Set the number of input tensors as N. For the
-th tensor
hasthe shape
.
is the
-th dimension of the
-th tensor. Then, the output tensor shape is
- Parameters
axis (int) – The specified axis. Default: 0.
Inputs:
- input_x (tuple, list) - Tuple or list of input tensors.
Outputs:
- Tensor, the shape is
.
Examples
- Copy>>> data1 = Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))
- >>> data2 = Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))
- >>> op = Concat()
- >>> output = op((data1, data2))
- class
mindspore.ops.operations.
ConcatOffset
(*args, **kwargs)[source] - primitive for computing Concat’s gradient.
- class
mindspore.ops.operations.
ControlDepend
(*args, **kwargs)[source] - Adds control dependency relation between source and destination operation.
In many cases, we need to control the execution order of operations. ControlDepend is designed for this.ControlDepend will indicate the execution engine to run the operations in specific order. ControlDependtells the engine that the destination operations should depend on the source operation which means the sourceoperations should be executed before the destination.
- Parameters
depend_mode (int) – Use 0 for normal depend, 1 for depend on operations that used the parameter. Default: 0.
Inputs:
src (Any) - The source input. It can be a tuple of operations output or a single operation output. We donot concern about the input data, but concern about the operation that generates the input data.If depend_mode = 1 is specified and the source input is parameter, we will try to find the operations thatused the parameter as input.
dst (Any) - The destination input. It can be a tuple of operations output or a single operation output.We do not concern about the input data, but concern about the operation that generates the input data.If depend_mode = 1 is specified and the source input is parameter, we will try to find the operations thatused the parameter as input.
Outputs:
- Bool. This operation has no actual data output, it will be used to setup the order of relative operations.
Examples
- Copy>>> # In the following example, the data calculation uses original global_step. After the calculation the global
- >>> # step should be increased, so the add operation should depend on the data calculation operation.
- >>> class Net(nn.Cell):
- >>> def __init__(self):
- >>> super(Net, self).__init__()
- >>> self.global_step = Parameter(initializer(0, [1]), name="global_step")
- >>> self.rate = 0.2
- >>> self.control_depend = ControlDepend()
- >>>
- >>> def construct(self, x):
- >>> data = self.rate * self.global_step + x
- >>> added_global_step = self.global_step + 1
- >>> self.global_step = added_global_step
- >>> self.control_depend(data, added_global_step)
- >>> return data
- class
mindspore.ops.operations.
Conv2D
(*args, **kwargs)[source] - 2D convolution layer.
Applies a 2D convolution over an input tensor which is typically of shape
,where
is batch size and
is channel number. For each batch of shape
, the formula is defined as:
where
is cross correlation operator,
is the input channel number,
rangesfrom
to
,
corresponds to
-th channel of the
-thfilter and
corresponds to the
-th channel of the output.
is a sliceof kernel and it has shape
, where
and
are height and width of the convolution kernel. The full kernel has shape
, where group is the group numberto split the input in the channel dimension.
If the ‘pad_mode’ is set to be “valid”, the output height and width will be
and
respectively.
The first introduction can be found in paper Gradient Based Learning Applied to Document Recognition. More detailed introduction can be found here:http://cs231n.github.io/convolutional-networks/.
- Parameters
out_channel (int) – The dimension of the output.
kernel_size (Union__[int, tuple[int]__]) – The kernel size of the 2D convolution.
mode (int) – 0 Math convolutiuon, 1 cross-correlation convolution ,2 deconvolution, 3 depthwise convolution. Default: 1.
pad_mode (str) – “valid”, “same”, “pad” the mode to fill padding. Default: “valid”.
pad (int) – The pad value to fill. Default: 0.
stride (int) – The stride to apply conv filter. Default: 1.
dilation (int) – Specify the space to use between kernel elements. Default: 1.
group (int) – Split input into groups. Default: 1.
Returns
Tensor, the value that applied 2D convolution.
Inputs:
- input (Tensor) - Tensor of shape
.
-
weight (Tensor) - Set size of kernel is
, then the shape is
.
- Outputs:
- Tensor of shape
.
- class
mindspore.ops.operations.
Conv2DBackpropInput
(*args, **kwargs)[source] Computes the gradients of convolution with respect to the 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) – “valid”, “same”, “pad” the mode to fill padding. Default: “valid”.
pad (int) – The pad value to fill. Default: 0.
mode (int) – 0 Math convolutiuon, 1 cross-correlation convolution ,2 deconvolution, 3 depthwise convolution. Default: 1.
stride (int) – The stride to apply conv filter. Default: 1.
dilation (int) – Specifies the dilation rate to use for dilated convolution. Default: 1.
group (int) – Splits input into groups. Default: 1.
Returns
- Tensor, the gradients of convolution.
- class
mindspore.ops.operations.
Cos
(*args, **kwargs)[source] Computes cosine of input element-wise.
- Inputs:
- input_x (Tensor) - The shape of tensor is
.
- Outputs:
- Tensor, has the same shape as input_x.
Examples
- Copy>>> cos = Cos()
- >>> X = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), ms.float32)
- >>> output = cos(X)
- class
mindspore.ops.operations.
CumProd
(*args, **kwargs)[source] Compute the cumulative product of the tensor x along axis.
- Parameters
Inputs:
input_x (Tensor[Number]) - The input tensor.
axis (int) - The dimensions to compute the cumulative product.
Outputs:
- Tensor, has the same shape and dtype as the ‘input_x’.
Examples
- Copy>>> data = Tensor(np.array([a, b, c]).astype(np.float32))
- >>> op0 = CumProd()
- >>> output = op0(data, 0) # output=[a, a * b, a * b * c]
- >>> op1 = CumProd(exclusive=True)
- >>> output = op1(data, 0) # output=[1, a, a * b]
- >>> op2 = CumProd(reverse=True)
- >>> output = op2(data, 0) # output=[a * b * c, b * c, c]
- >>> op3 = CumProd(exclusive=True, reverse=True)
- >>> output = op3(data, 0) # output=[b * c, c, 1]
- class
mindspore.ops.operations.
CumSum
(*args, **kwargs)[source] Computes the cumulative sum of input tensor along axis.
- Parameters
Inputs:
input (Tensor) - The input tensor to accumulate.
axis (int) - The axis to accumulate the tensor’s value.
Outputs:
- Tensor, the shape of the output tensor is consistent with the input tensor’s.
Examples
- Copy>>> input = Tensor(np.array([[3, 4, 6, 10],[1, 6, 7, 9],[4, 3, 8, 7],[1, 3, 7, 9]]).astype(np.float32))
- >>> cumsum = CumSum()
- >>> output = cumsum(input, 1)
- [[ 3. 7. 13. 23.]
- [ 1. 7. 14. 23.]
- [ 4. 7. 15. 22.]
- [ 1. 4. 11. 20.]]
- class
mindspore.ops.operations.
DType
(*args, **kwargs)[source] Returns the data type of input tensor as mindspore.dtype.
- Inputs:
- input_x (Tensor) - The shape of tensor is
.
- Outputs:
- mindspore.dtype, the data type of a tensor.
Examples
- Copy>>> input_tensor = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
- >>> type = DType()(input_tensor)
- class
mindspore.ops.operations.
DepthToSpace
(*args, **kwargs)[source] - Rearrange blocks of depth data into spatial dimensions.
This is the reverse operation of SpaceToDepth.
The output tensor’s height dimension is
.
The output tensor’s weight dimension is
.
The depth of output tensor is
.
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.
Outputs:
- Tensor, the same type as x.
Examples
- Copy>>> x = Tensor(np.random.rand(1,12,1,1), mindspore.float32)
- >>> block_size = 2
- >>> op = DepthToSpace(block_size)
- >>> output = op(x)
- >>> output.asnumpy().shape == (1,3,2,2)
- class
mindspore.ops.operations.
DepthwiseConv2dNative
(*args, **kwargs)[source] - Returns the depth-wise convolution value for the input.
Applies depthwise conv2d for the input, which will generate more channels with channel_multiplier.Given an input tensor of shape
where
is the batch size and afilter tensor with kernel size
, containing
convolutional filters of depth 1; it applies different filters to each input channel (channel_multiplier channelsfor each with default value 1), then concatenates the results together. The output has
channels.
- Parameters
channel_multiplier (int) – The multipiler for the original output conv.
mode (int) – 0 Math convolution, 1 cross-correlation convolution ,2 deconvolution, 3 depthwise convolution. Default: 3.
pad_mode (str) – “valid”, “same”, “pad” the mode to fill padding. Default: “valid”.
pad (int) – The pad value to fill. Default: 0.
stride (int) – The stride to apply conv filter. Default: 1.
dilation (int) – Specifies the dilation rate to use for dilated convolution. Default: 1.
group (int) – Splits input into groups. Default: 1.
Inputs:
- input (Tensor) - Tensor of shape
.
-
weight (Tensor) - Set size of kernel is
, then the shape is
.
- Outputs:
- Tensor of shape
.
- class
mindspore.ops.operations.
Diag
(*args, **kwargs)[source] - Construct a diagonal tensor with a given diagonal values.
Assume input_x has dimensions
, the output is a tensor ofrank 2k with dimensions
where:
and 0 everywhere else.
- Inputs:
- input_x (Tensor) - The input tensor.
Outputs:
- Tensor.
Examples
- Copy>>> input_x = Tensor([1, 2, 3, 4])
- >>> diag = P.Diag()
- >>> diag(x)
- [[1, 0, 0, 0],
- [0, 2, 0, 0],
- [0, 0, 3, 0],
- [0, 0, 0, 4]]
- class
mindspore.ops.operations.
DiagPart
(*args, **kwargs)[source] - Extract the diagonal part from given tensor.
Assume input has dimensions
, the output is a tensorof rank k with dimensions
where:
.
- Inputs:
- input_x (Tensor) - The input Tensor.
Outputs:
Tensor.
Examples
- Copy>>> input_x = Tensor([[1, 0, 0, 0],
- >>> [0, 2, 0, 0],
- >>> [0, 0, 3, 0],
- >>> [0, 0, 0, 4]])
- >>> diag_part = P.DiagPart()
- >>> diag_part(x)
- [1, 2, 3, 4]
- class
mindspore.ops.operations.
Div
(*args, **kwargs)[source] - Computes the quotient of dividing the first input tensor by the second input tensor element-wise.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number]) - The first input is a tensor whose data type is number or a number.
input_y (Union[Tensor, Number]) - The second input is a tensor whose data type is same as ‘input_x’ ora number.
Outputs:
Tensor, the shape is same as the shape after broadcasting, and the data type is same as ‘input_x’.
Raises
- ValueError – When input_x and input_y are not the same dtype.
Examples
- Copy>>> input_x = Tensor(np.array([-4.0, 5.0, 6.0]), mindspore.float32)
- >>> input_y = Tensor(np.array([3.0, 2.0, 3.0]), mindspore.float32)
- >>> div = Div()
- >>> div(input_x, input_y)
- [-2.0, 2.0, 2.0]
- class
mindspore.ops.operations.
DropoutDoMask
(*args, **kwargs)[source] - Applies dropout mask on the input tensor.
Take the mask output of DropoutGenMask as input, and apply dropout on the input.
- Inputs:
input_x (Tensor) - The input tensor.
mask (Tensor) - The mask to be applied on input_x, which is the output of DropoutGenMask. And theshape of input_x must be same as the value of DropoutGenMask’s input shape. If input wrong mask,the output of DropoutDoMask are unpredictable.
keep_prob (Tensor) - The keep rate, between 0 and 1, e.g. keepprob = 0.9,means dropping out 10% of input units. The value of _keep_prob is same as the input keep_prob ofDropoutGenMask.
Outputs:
- Tensor, the value that applied dropout on.
Examples
- Copy>>> x = Tensor(np.ones([20, 16, 50]), mindspore.float32)
- >>> shape = (20, 16, 50)
- >>> keep_prob = Tensor(0.5, mindspore.float32)
- >>> dropout_gen_mask = DropoutGenMask()
- >>> dropout_do_mask = DropoutDoMask()
- >>> mask = dropout_gen_mask(shape, keep_prob)
- >>> output = dropout_do_mask(x, mask, keep_prob)
- >>> assert output.shape() == (20, 16, 50)
- class
mindspore.ops.operations.
DropoutGenMask
(*args, **kwargs)[source] Generates the mask value for the input shape.
- Parameters
Inputs:
shape (tuple[int]) - The shape of target mask.
keep_prob (Tensor) - The keep rate, between 0 and 1, e.g. keep_prob = 0.9,means dropping out 10% of input units.
Outputs:
- Tensor, the value of generated mask for input shape.
Examples
- Copy>>> dropout_gen_mask = DropoutGenMask()
- >>> shape = (20, 16, 50)
- >>> keep_prob = Tensor(0.5, mindspore.float32)
- >>> mask = dropout_gen_mask(shape, keep_prob)
- class
mindspore.ops.operations.
Equal
(*args, **kwargs)[source] - Computes the equivalence between two tensors element-wise.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number, bool]) - The first input is a tensor whose data type is number or bool, ora number or a bool object.
input_y (Union[Tensor, Number, bool]) - The second input tensor whose data type is same as ‘input_x’ ora number or a bool object.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is bool.
Examples
- Copy>>> input_x = Tensor(np.array([1, 2, 3]), mindspore.float32)
- >>> equal = Equal()
- >>> equal(input_x, 2.0)
- [False, True, False]
- >>>
- >>> input_x = Tensor(np.array([1, 2, 3]), mindspore.int32)
- >>> input_y = Tensor(np.array([1, 2, 4]), mindspore.int32)
- >>> equal = Equal()
- >>> equal(input_x, input_y)
- [True, True, False]
- class
mindspore.ops.operations.
EqualCount
(*args, **kwargs)[source] - Computes the number of the same elements of two tensors.
The two input tensors should have same shape.
- Inputs:
input_x (Tensor) - The first input tensor.
input_y (Tensor) - The second input tensor.
Outputs:
- Tensor, has the same shape as the input_x.
Examples
- Copy>>> input_x = Tensor(np.array([1, 2, 3]), mindspore.int32)
- >>> input_y = Tensor(np.array([1, 2, 4]), mindspore.int32)
- >>> equal_count = EqualCount()
- >>> equal_count(input_x, input_y)
- [2]
- class
mindspore.ops.operations.
Exp
(*args, **kwargs)[source] Returns exponential of a tensor element-wise.
- Inputs:
- input_x (Tensor) - The input tensor.
Outputs:
- Tensor, has the same shape as the input_x.
Examples
- Copy>>> input_x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
- >>> exp = Exp()
- >>> exp(input_x)
- [ 2.71828183, 7.3890561 , 54.59815003]
- class
mindspore.ops.operations.
ExpandDims
(*args, **kwargs)[source] - Adds an additional dimension at the given axis.
Note
If the specified axis is a negative number, the index is countedbackward from the end and starts at 1.
- Raises
ValueError – If axis is not an integer or not in the valid range.
Inputs:
- input_x (Tensor) - The shape of tensor is
.
-
axis (int) - Specifies the dimension index at which to expandthe shape of input_x. The value of axis must be in the range[-input_x.dim()-1, input_x.dim()]. Only constant value is allowed.
- Outputs:
- Tensor, the shape of tensor is
if thevalue of axis is 0.
Examples
- Copy>>> input_tensor = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
- >>> expand_dims = ExpandDims()
- >>> output = expand_dims(input_tensor, 0)
- class
mindspore.ops.operations.
Eye
(*args, **kwargs)[source] Creates a tensor with ones on the diagonal and zeros elsewhere.
- Inputs:
n (int) - Number of rows of returned tensor
m (int) - Number of columns of returned tensor
t (mindspore.dtype) - Mindspore’s dtype, The data type of the returned tensor.
Outputs:
- Tensor, a tensor with ones on the diagonal and zeros elsewhere.
Examples
- Copy>>> eye = P.Eye()
- >>> out_tensor = eye(2, 2, mindspore.int32)
- class
mindspore.ops.operations.
Fill
(*args, **kwargs)[source] - Creates a tensor filled with a scalar value.
Creates a tensor with shape described by the first argument and fills it with values in the second argument.
- Inputs:
type (mindspore.dtype) - The specified type of output tensor. Only constant value is allowed.
shape (tuple) - The specified shape of output tensor. Only constant value is allowed.
value (scalar) - Value to fill the returned tensor. Only constant value is allowed.
Outputs:
- Tensor, has the same type and shape as input value.
Examples
- Copy>>> fill = P.Fill()
- >>> fill(P.DType()(x), (2, 2), 1)
- class
mindspore.ops.operations.
Flatten
(*args, **kwargs)[source] Flattens a tensor without changing its batch size on the 0-th axis.
- Inputs:
- input_x (Tensor) - Tensor of shape
to be flattened.
- Outputs:
- Tensor, the shape of the output tensor is
, where
isthe product of the remaining dimension.
Examples
- Copy>>> input_tensor = Tensor(np.ones(shape=[1, 2, 3, 4]), mindspore.float32)
- >>> flatten = Flatten()
- >>> output = flatten(input_tensor)
- >>> assert output.shape() == (1, 24)
- class
mindspore.ops.operations.
Floor
(*args, **kwargs)[source] Round a tensor down to the closest integer element-wise.
- Inputs:
- input_x (Tensor) - The input tensor. Its element data type must be float.
Outputs:
- Tensor, has the same shape as input_x.
Examples
- Copy>>> input_x = Tensor(np.array([1.1, 2.5, -1.5]), mindspore.float32)
- >>> floor = Floor()
- >>> floor(input_x)
- [1.0, 2.0, -2.0]
- class
mindspore.ops.operations.
FloorDiv
(*args, **kwargs)[source] - Divide the first input tensor by the second input tensor element-wise and rounds down to the closest integer.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number]) - The first input is a tensor whose data type is number or a number.
input_y (Union[Tensor, Number]) - The second input is a tensor whose data type is same as ‘input_x’ ora number.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is same as ‘input_x’.
Examples
- Copy>>> input_x = Tensor(np.array([2, 4, -1]), mindspore.int32)
- >>> input_y = Tensor(np.array([3, 3, 3]), mindspore.int32)
- >>> floor_div = FloorDiv()
- >>> floor_div(input_x, input_y)
- [0, 1, -1]
- class
mindspore.ops.operations.
FusedBatchNorm
(*args, **kwargs)[source] - FusedBatchNorm is a BatchNorm that moving mean and moving variance will be computed instead of being loaded.
Batch Normalization is widely used in convolutional networks. This operation appliesBatch Normalization over input to avoid internal covariate shift as described in thepaper Batch Normalization: Accelerating Deep Network Training by Reducing InternalCovariate Shift. It rescales and recenters thefeature using a mini-batch of data and the learned parameters which can be describedin the following formula.
where
is scale,
is bias,
is epsilon.
- Parameters
).Momentum value should be [0, 1]. Default: 0.9.
- Inputs:
- input_x (Tensor) - Tensor of shape
.
-
scale (Tensor) - Tensor of shape
.
-
bias (Tensor) - Tensor of shape
.
-
mean (Tensor) - Tensor of shape
.
-
variance (Tensor) - Tensor of shape
.
- Outputs:
Tuple of 5 Tensor, the normalized input and the updated parameters.
output_x (Tensor) - The same type and shape as the input_x.
updated_scale (Tensor) - Tensor of shape
.
-
updated_bias (Tensor) - Tensor of shape
.
-
updated_moving_mean (Tensor) - Tensor of shape
.
-
updated_moving_variance (Tensor) - Tensor of shape
.
- class
mindspore.ops.operations.
GatherNd
(*args, **kwargs)[source] - Gathers slices from a tensor by indices.
Using given indices to gather slices from a tensor with a specified shape.
- Inputs:
input_x (Tensor) - The target tensor to gather values.
indices (Tensor) - The index tensor.
Outputs:
- Tensor, has the same type as input_x and the shape is indices_shape[:-1] + x_shape[indices_shape[-1]:].
Examples
- Copy>>> 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)
- >>> op = GatherNd()
- >>> output = op(input_x, indices)
- class
mindspore.ops.operations.
GatherV2
(*args, **kwargs)[source] Returns a slice of input tensor based on the specified indices and axis.
- Inputs:
- input_params (Tensor) - The shape of tensor is
.The original Tensor.
-
input_indices (Tensor) - The shape of tensor is
.Specifies the indices of elements of the original Tensor. Must be in the range[0, input_param.shape()[axis]).
-
axis (int) - Specifies the dimension index to gather indices.
- Outputs:
- Tensor, the shape of tensor is
.
Examples
- Copy>>> params = Tensor(np.array([[1, 2, 7, 42], [3, 4, 54, 22], [2, 2, 55, 3]]), mindspore.float32)
- >>> indices = Tensor(np.array([1, 2]), mindspore.int32)
- >>> axis = 1
- >>> out = GatherV2()(params, indices, axis)
- class
mindspore.ops.operations.
GeSwitch
(*args, **kwargs)[source] - Adds control switch to data.
Switch data to flow into false or true branch depend on the condition. If the condition is true,the true branch will be activated, or vise verse.
- Inputs:
data (Tensor) - The data to be used for switch control.
pred (Tensor) - It should be a scalar whose type is bool and shape is (), It is used as condition forswitch 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
- Copy>>> class Net(nn.Cell):
- >>> def __init__(self):
- >>> super(Net, self).__init__()
- >>> self.square = P.Square()
- >>> self.add = P.TensorAdd()
- >>> self.value = Tensor(np.full((1), 3, dtype=np.float32))
- >>> self.switch = P.GeSwitch()
- >>> self.merge = P.Merge()
- >>> self.less = P.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(x_init, dtype=mindspore.float32)
- >>> y = Tensor(y_init, dtype=mindspore.float32)
- >>> net = Net()
- >>> output = net(x, y)
- class
mindspore.ops.operations.
Gelu
(*args, **kwargs)[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..
Defined as follows:
where
is the “Gauss error function” .
- Inputs:
- input_x (Tensor) - Input to compute the Gelu.
Outputs:
- Tensor, with the same type and shape as input.
Examples
- Copy>>> tensor = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32)
- >>> gelu = Gelu()
- >>> result = gelu(tensor)
- class
mindspore.ops.operations.
GetNext
(*args, **kwargs)[source] - Returns the next element in the dataset queue.
Note
GetNext op needs to be associated with network and also depends on the initdataset interface,it can’t be used directly as a single op.For details, please refer to _nn.cell_wrapper.DataWrapper source code.
- Parameters
Inputs:
No inputs.
Outputs:
- tuple[Tensor], the output of Dataset. The shape is described in shapes_and the type is described is _types.
Examples
- Copy>>> get_next = GetNext([mindspore.float32, mindspore.int32], [[32, 1, 28, 28], [10]], 'shared_name')
- >>> feature, label = get_next()
- class
mindspore.ops.operations.
Greater
(*args, **kwargs)[source] - Computes the boolean value of
element-wise.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number]) - The first input is a tensor whose data type is number or a number.
input_y (Union[Tensor, Number]) - The second input is a tensor whose data type is same as ‘input_x’ ora number.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is same as ‘input_x’.
Examples
- Copy>>> input_x = Tensor(np.array([1, 2, 3]), mindspore.int32)
- >>> input_y = Tensor(np.array([1, 1, 4]), mindspore.int32)
- >>> greater = Greater()
- >>> greater(input_x, input_y)
- [False, True, False]
- class
mindspore.ops.operations.
GreaterEqual
(*args, **kwargs)[source] - Computes the boolean value of
element-wise.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number]) - The first input is a tensor whose data type is number or a number.
input_y (Union[Tensor, Number]) - The second input is a tensor whose data type is same as ‘input_x’ ora number.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is bool’.
Examples
- Copy>>> input_x = Tensor(np.array([1, 2, 3]), mindspore.int32)
- >>> input_y = Tensor(np.array([1, 1, 4]), mindspore.int32)
- >>> greater_equal = GreaterEqual()
- >>> greater_equal(input_x, input_y)
- [True, True, False]
- class
mindspore.ops.operations.
IOU
(*args, **kwargs)[source] - Calculate intersection over union for boxes.
Calculate the specific value of overlap and union of the boxes.
- Parameters
mode (string) – The mode is used to specify the calculation method,now support ‘iou’ (intersection over union) or ‘iof’(intersection over foreground) mode. Default: ‘iou’.
Inputs:
anchor_boxes (Tensor) - Anchor boxes, tensor of shape (N, 4).
gt_boxes (Tensor) - Ground truth boxes, tensor of shape (M, 4).
Outputs:
- Tensor, the ‘iou’ values, tensor of shape (M, N).
Examples
- Copy>>> iou = IOU()
- >>> anchor_boxes = Tensor(np.random.randint(1,5, [10, 4]))
- >>> gt_boxes = Tensor(np.random.randint(1,5, [3, 4]))
- >>> iou(anchor_boxes, gt_boxes)
- class
mindspore.ops.operations.
ImageSummary
(*args, **kwargs)[source] Output image tensor to protocol buffer through image summary operator.
- Inputs:
name (str) - The name of the input variable.
value (Tensor) - The value of image.
Examples
- Copy>>> class Net(nn.Cell):
- >>> def __init__(self):
- >>> super(Net, self).__init__()
- >>> self.summary = P.ImageSummary()
- >>>
- >>> def construct(self, x):
- >>> name = "image"
- >>> out = self.summary(name, x)
- >>> return out
- class
mindspore.ops.operations.
InsertGradientOf
(*args, **kwargs)[source] Attach callback to graph node that will be invoked on the node’s gradient.
- Parameters
f (Function) – MindSpore’s Function. Callback function.
Inputs:
- input_x (Tensor) - The graph node to attach to.
Outputs:
- Tensor, returns input_x directly. InsertGradientOf does not affect the forward result.
Examples
- Copy>>> def clip_gradient(dx):
- >>> ret = dx
- >>> if ret > 1.0:
- >>> ret = 1.0
- >>>
- >>> if ret < 0.2:
- >>> ret = 0.2
- >>>
- >>> return ret
- >>>
- >>> clip = P.InsertGradientOf(clip_gradient)
- >>> def InsertGradientOfClipDemo():
- >>> def clip_test(x, y):
- >>> x = clip(x)
- >>> y = clip(y)
- >>> c = x * y
- >>> return c
- >>>
- >>> @ms_function
- >>> def f(x, y):
- >>> return clip_test(x, y)
- >>>
- >>> def fd(x, y):
- >>> return C.grad_all(clip_test)(x, y)
- >>>
- >>> print("forward: ", f(1.1, 0.1))
- >>> print("clip_gradient:", fd(1.1, 0.1))
- class
mindspore.ops.operations.
InvertPermutation
(*args, **kwargs)[source] - Computes the inverse of an index permutation.
Given a tuple input, this operation inserts a dimension of 1 at the dimensionThis operation calculates the inverse of the index replacement. It requires a1-dimensional tuple x, which represents the array starting at zero,and swaps each value with its index position. In other words, for the outputtuple y and the input tuple x, this operation calculates the following:
.
Note
These values must include 0. There must be no duplicate values and thevalues can not be negative.
- Inputs:
- input_x (tuple[int]) - The input tuple is constructed by multipleintegers, i.e.,
representing the indices.The values must include 0. There can be no duplicate values or negative values.
- Outputs:
- tuple[int]. the lenth is same as input.
Examples
- Copy>>> invert = InvertPermutation()
- >>> input_data = (3, 4, 0, 2, 1)
- >>> output = invert(input_data)
- >>> output == (2, 4, 3, 0, 1)
- class
mindspore.ops.operations.
IsInstance
(*args, **kwargs)[source] Check whether an object is an instance of a target type.
- Inputs:
inst (Any Object) - The instance to be check. Only constant value is allowed.
type_ (mindspore.dtype) - The target type. Only constant value is allowed.
Outputs:
- bool, the check result.
Examples
- Copy>>> a = 1
- >>> result = IsInstance()(a, mindspore.int32)
- class
mindspore.ops.operations.
IsSubClass
(*args, **kwargs)[source] Check whether one type is sub class of another type.
- Inputs:
sub_type (mindspore.dtype) - The type to be check. Only constant value is allowed.
type_ (mindspore.dtype) - The target type. Only constant value is allowed.
Outputs:
- bool, the check result.
Examples
- Copy>>> result = IsSubClass()(mindspore.int32, mindspore.intc)
- class
mindspore.ops.operations.
L2Normalize
(*args, **kwargs)[source] - L2 normalization Operator.
This operator will normalizes the input using the given axis. The function is shown as follows:
where
is epsilon.
- Parameters
Inputs:
- input_x (Tensor) - Input to compute the normalization.
Outputs:
- Tensor, with the same type and shape as the input.
- class
mindspore.ops.operations.
LARSUpdate
(*args, **kwargs)[source] Conduct lars (layer-wise adaptive rate scaling) update on the square sum of gradient.
- Parameters
Inputs:
weight (Tensor) - The weight to be updated.
gradient (Tensor) - The gradient of weight, which has the same shape and dtype with weight.
norm_weight (Tensor) - A scalar tensor, representing the square sum of weight.
norm_gradient (Tensor) - A scalar tensor, representing the square sum of gradient.
weight_decay (Union[Number, Tensor]) - Weight decay. It should be a scalar tensor or number.
learning_rate (Union[Number, Tensor]) - Learning rate. It should be a scalar tensor or number.
Outputs:
- Tensor, representing the new gradient.
- class
mindspore.ops.operations.
LSTM
(*args, **kwargs)[source] - Performs the long short term memory(LSTM) on the input.
Detailed information, please refer to nn.LSTM.
- class
mindspore.ops.operations.
LayerNorm
(*args, **kwargs)[source] - Applies the Layer Normalization to the input tensor.
This operator will normalize the input tensor on given axis. LayerNorm is described in the paperLayer Normalization.
where
is scale,
is bias,
is epsilon.
- Parameters
Inputs:
- input_x (Tensor) - Tensor of shape
.The input of LayerNorm.
-
gamma (Tensor) - Tensor of shape
.The learnable parameter gamma as the scale on norm.
-
beta (Tensor) - Tensor of shape
.The learnable parameter beta as the scale on norm.
- Outputs:
tuple[Tensor], tuple of 3 tensors, the normalized input and the updated parameters.
- output_x (Tensor) - The normalized input, has the same type and shape as the input_x.The shape is
.
-
updated_gamma (Tensor) - Tensor of shape
.
-
updated_beta (Tensor) - Tensor of shape
.
- class
mindspore.ops.operations.
Less
(*args, **kwargs)[source] - Computes the boolean value of
element-wise.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number]) - The first input is a tensor whose data type is number or a number.
input_y (Union[Tensor, Number]) - The second input is a tensor whose data type is same as ‘input_x’ ora number.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is bool.
Examples
- Copy>>> input_x = Tensor(np.array([1, 2, 3]), mindspore.int32)
- >>> input_y = Tensor(np.array([1, 1, 4]), mindspore.int32)
- >>> less = Less()
- >>> less(input_x, input_y)
- [False, False, True]
- class
mindspore.ops.operations.
LessEqual
(*args, **kwargs)[source] - Computes the boolean value of
element-wise.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number]) - The first input is a tensor whose data type is number or a number.
input_y (Union[Tensor, Number]) - The second input is a tensor whose data type is same as ‘input_x’ ora number.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is bool.
Examples
- Copy>>> input_x = Tensor(np.array([1, 2, 3]), mindspore.int32)
- >>> input_y = Tensor(np.array([1, 1, 4]), mindspore.int32)
- >>> less_equal = LessEqual()
- >>> less_equal(input_x, input_y)
- [True, False, True]
- class
mindspore.ops.operations.
Log
(*args, **kwargs)[source] Returns the natural logarithm of a tensor element-wise.
- Inputs:
- input_x (Tensor) - The input tensor.
Outputs:
- Tensor, has the same shape as the input_x.
Examples
- Copy>>> input_x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
- >>> log = Log()
- >>> log(input_x)
- [0.0, 0.69314718, 1.38629436]
- class
mindspore.ops.operations.
LogSoftmax
(*args, **kwargs)[source] - Log Softmax activation function.
Applies the Log Softmax function to the input tensor on the specified axis.Suppose a slice along the given aixs
then for each element
the Log Softmax function is shown as follows:
where
is the length of the Tensor.
- Parameters
axis (int) – The axis to do the Log softmax operation. Default: -1.
Inputs:
- logits (Tensor) - The input of Log Softmax.
Outputs:
- Tensor, with the same type and shape as the logits.
- class
mindspore.ops.operations.
LogicalAnd
(*args, **kwargs)[source] - Computes the “logical AND” of two tensors element-wise.
The inputs must be two tensors or one tensor and one bool object.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be bool.When the inputs are one tensor and one bool object, the bool object cannot be a parameter, only can be a constant,and the data type of the tensor should be bool.
- Inputs:
input_x (Union[Tensor, bool]) - The first input is a tensor whose data type is bool or a bool object.
input_y (Union[Tensor, bool]) - The second input is a tensor whose data type is bool or a bool object.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is bool.
Examples
- Copy>>> input_x = Tensor(np.array([True, False, True]), mindspore.bool_)
- >>> input_y = Tensor(np.array([True, True, False]), mindspore.bool_)
- >>> logical_and = LogicalAnd()
- >>> logical_and(input_x, input_y)
- [True, False, False]
- class
mindspore.ops.operations.
LogicalNot
(*args, **kwargs)[source] Computes the “logical NOT” of a tensor element-wise.
- Inputs:
- input_x (Tensor) - The input tensor whose dtype is bool
Outputs:
- Tensor, the shape is same as the input_x, and the dtype is bool.
Examples
- Copy>>> input_x = Tensor(np.array([True, False, True]), mindspore.bool_)
- >>> logical_not = LogicalNot()
- >>> logical_not(input_x)
- [False, True, False]
- class
mindspore.ops.operations.
LogicalOr
(*args, **kwargs)[source] - Computes the “logical OR” of two tensors element-wise.
The inputs must be two tensors or one tensor and one bool object.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be bool.When the inputs are one tensor and one bool object, the bool object cannot be a parameter, only can be a constant,and the data type of the tensor should be bool.
- Inputs:
input_x (Union[Tensor, bool]) - The first input is a tensor whose data type is bool or a bool object.
input_y (Union[Tensor, bool]) - The second input is a tensor whose data type is bool or a bool object.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is bool.
Examples
- Copy>>> input_x = Tensor(np.array([True, False, True]), mindspore.bool_)
- >>> input_y = Tensor(np.array([True, True, False]), mindspore.bool_)
- >>> logical_or = LogicalOr()
- >>> logical_or(input_x, input_y)
- [True, True, True]
- class
mindspore.ops.operations.
MakeRefKey
(*args, **kwargs)[source] Make a RefKey instance by string. RefKey stores the name of Parameter, can be passed through the functions,and used for Assign target.
- Parameters
tag (str) – Parameter name to make the RefKey.
Inputs:
No input.
Outputs:
- RefKeyType, made from the Parameter name.
Examples
- Copy>>> from mindspore.ops import functional as F
- >>> class Net(nn.Cell):
- >>> def __init__(self):
- >>> super(Net, self).__init__()
- >>> self.y = Parameter(Tensor(np.ones([6, 8, 10], np.int32)), name="y")
- >>> self.make_ref_key = MakeRefKey("y")
- >>>
- >>> def construct(self, x):
- >>> key = self.make_ref_key()
- >>> ref = F.make_ref(key, x, self.y)
- >>> return ref + x
- >>>
- >>> x = Tensor(np.ones([3, 4, 5], np.int32))
- >>> net = Net()
- >>> net(x)
- class
mindspore.ops.operations.
MatMul
(*args, **kwargs)[source] - Multiplies matrix a by matrix b.
The rank of input tensors must be 2.
- Parameters
Inputs:
- input_x (Tensor) - The first tensor to be multiplied. The shape of the tensor is
. Iftranspose_a is True, its shape should be
after transposing.
-
input_y (Tensor) - The second tensor to be multiplied. The shape of the tensor is
. Iftranspose_b is True, its shape should be
after transpose.
- Outputs:
- Tensor, the shape of the output tensor is
.
Examples
- Copy>>> input_x = Tensor(np.ones(shape=[1, 3]), mindspore.float32)
- >>> input_y = Tensor(np.ones(shape=[3, 4]), mindspore.float32)
- >>> matmul = MatMul()
- >>> output = matmul(input_x, input_y)
- class
mindspore.ops.operations.
MaxPool
(*args, **kwargs)[source] - Max pooling operation.
Applies a 2D max pooling over an input Tensor which can be regarded as a composition of 2D planes.
Typically the input is of shape
, MaxPool outputsregional maximum in the
-dimension. Given kernel size
and stride
, the operation is as follows.
- Parameters
ksize (Union__[int, tuple[int]__]) – The size of the window to take a max over, that should be a tupleof two int for width and height. Default: 1.
stride (Union__[int, tuple[int]__]) – The stride of the window, that should be a tuple of two int forwidth and height. Default: 1.
padding (str) – The optional values for pad mode “SAME”, “VALID”. Default: “VALID”.
Inputs:
- input (Tensor) - Tensor of shape
.
- Outputs:
- Tensor, with shape
.
- class
mindspore.ops.operations.
MaxPoolWithArgmax
(*args, **kwargs)[source] - Performs max pooling on the input Tensor and return both max values and indices.
Typically the input is of shape
, MaxPool outputsregional maximum in the
-dimension. Given kernel size
and stride
, the operation is as follows.
- Parameters
pad_mode (str) – “valid”, “same”, “pad” the mode to fill padding. Default: “valid”.
window (Union__[int, tuple[int]__]) – The size of window, which is the kernel size, two int for widthand height. Default: 1.
pad (Union__[int, tuple[int]__]) – If pad_mode is pad, the pad value to fill, two int for widthand height. Default: 0.
stride (Union__[int, tuple[int]__]) – The stride of the window, that should be a tuple of two int forwidth and height. Default: 1.
Inputs:
- input (Tensor) - Tensor of shape
.
- Outputs:
Tuple of 2 Tensor, the maxpool result and where max values from.
- output (Tensor) - Maxpooling result, with shape
.
-
mask (Tensor) - Max values’ index represented by the mask.
- class
mindspore.ops.operations.
Maximum
(*args, **kwargs)[source] - Computes the element-wise maximum of input tensors.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number]) - The first input is a tensor whose data type is number or a number.
input_y (Union[Tensor, Number]) - The second input is a tensor whose data type is same as ‘input_x’ ora number.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is same as ‘input_x’.
Examples
- Copy>>> input_x = Tensor(np.array([1.0, 5.0, 3.0]), mindspore.float32)
- >>> input_y = Tensor(np.array([4.0, 2.0, 6.0]), mindspore.float32)
- >>> maximum = Maximum()
- >>> maximum(input_x, input_y)
- [4.0, 5.0, 6.0]
- class
mindspore.ops.operations.
Merge
(*args, **kwargs)[source] - Merges all input data to one.
One and only one of the inputs should be selected as the output
- Inputs:
- inputs (Tuple) - The data to be merged. All tuple elements should have same shape.
Outputs:
- tuple. Output is tuple(data, output_index). The data has the same shape of inputs element.
- class
mindspore.ops.operations.
Minimum
(*args, **kwargs)[source] - Computes the element-wise minimum of input tensors.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number]) - The first input is a tensor whose data type is number or a number.
input_y (Union[Tensor, Number]) - The second input is a tensor whose data type is same as ‘input_x’ ora number.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is same as ‘input_x’.
Examples
- Copy>>> input_x = Tensor(np.array([1.0, 5.0, 3.0]), mindspore.float32)
- >>> input_y = Tensor(np.array([4.0, 2.0, 6.0]), mindspore.float32)
- >>> minimum = Minimum()
- >>> minimum(input_x, input_y)
- [1.0, 2.0, 3.0]
- class
mindspore.ops.operations.
Mul
(*args, **kwargs)[source] - Multiplies two tensors element-wise.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number]) - The first input is a tensor whose data type is number or a number.
input_y (Union[Tensor, Number]) - The second input is a tensor whose data type is same as ‘input_x’ ora number.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is same as ‘input_x’.
Examples
- Copy>>> input_x = Tensor(np.array([1, 2, 3]), mindspore.int32)
- >>> input_y = Tensor(np.array([4, 5, 6]), mindspore.int32)
- >>> mul = Mul()
- >>> mul(input_x, input_y)
- [4, 10, 18]
- class
mindspore.ops.operations.
NMSWithMask
(*args, **kwargs)[source] Select some bounding boxes in descending order of score.
- Parameters
iou_threshold (float) – Specifies the threshold of overlap boxes with respect toIOU. Default: 0.5.
Raises
ValueError – If the iou_threshold is not a float number, or if the first dimension of input Tensor is less than or equal to 0, or if the data type of the input Tensor is not float16 or float32.
Inputs:
- bboxes (Tensor) - The shape of tensor is
. Input bounding boxes.N is the number of input bounding boxes. Every bounding boxcontains 5 values, the first 4 values are the coordinates of boundingbox, and the last value is the score of this bounding box.
- Outputs:
tuple[Tensor], tuple of three tensors, they are selected_boxes, selected_idx and selected_mask.
- selected_boxes (Tensor) - The shape of tensor is
. Bounding boxeslist after non-max suppression calculation.
-
selected_idx (Tensor) - The shape of tensor is
. The indexes list ofvalid input bounding boxes.
-
selected_mask (Tensor) - The shape of tensor is
. A mask list ofvalid output bounding boxes.
Examples
- Copy>>> bbox = np.random.rand(128, 5)
- >>> bbox[:, 2] += bbox[:, 0]
- >>> bbox[:, 3] += bbox[:, 1]
- >>> inputs = Tensor(bbox)
- >>> nms = NMSWithMask(0.5)
- >>> output_boxes, indices, mask = nms(inputs)
- class
mindspore.ops.operations.
NPUAllocFloatStatus
(*args, **kwargs)[source] - Allocates a flag to store the overflow status.
The flag is a tensor whose shape is (8,) and data type is mindspore.dtype.float32.
Note
Examples: see NPUGetFloatStatus.
- Outputs:
- Tensor, has the shape of (8,).
Examples
- Copy>>> alloc_status = NPUAllocFloatStatus()
- >>> init = alloc_status()
- Tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], shape=(8,), dtype=mindspore.float32)
- class
mindspore.ops.operations.
NPUClearFloatStatus
(*args, **kwargs)[source] - Clear the flag which stores the overflow status.
Note
The flag is in the register on the Ascend device. It will be reset and can not be reused again after theNPUClearFloatStatus is called.
Examples: see NPUGetFloatStatus.
- Inputs:
- input_x (Tensor) - The output tensor of NPUAllocFloatStatus.
Outputs:
- Tensor, has the same shape as input_x. All the elements in the tensor will be zero.
Examples
- Copy>>> alloc_status = NPUAllocFloatStatus()
- >>> get_status = NPUGetFloatStatus()
- >>> clear_status = NPUClearFloatStatus()
- >>> init = alloc_status()
- >>> flag = get_status(init)
- >>> clear = clear_status(init)
- Tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], shape=(8,), dtype=mindspore.float32)
- class
mindspore.ops.operations.
NPUGetFloatStatus
(*args, **kwargs)[source] - Updates the flag which is the output tensor of NPUAllocFloatStatus with latest overflow status.
The flag is a tensor whose shape is (8,) and data type is mindspore.dtype.float32.If the sum of the flag equals 0, there is no overflow happened. If the sum of the flag is bigger than 0, thereis overflow happened.
- Inputs:
- input_x (Tensor) - The output tensor of NPUAllocFloatStatus.
Outputs:
- Tensor, has the same shape as input_x. All the elements in the tensor will be zero.
Examples
- Copy>>> alloc_status = NPUAllocFloatStatus()
- >>> get_status = NPUGetFloatStatus()
- >>> init = alloc_status()
- >>> flag = get_status(init)
- Tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], shape=(8,), dtype=mindspore.float32)
- class
mindspore.ops.operations.
Neg
(*args, **kwargs)[source] Returns a tensor with negative values of the input tensor element-wise.
- Inputs:
- input_x (Tensor) - The input tensor whose dtype is number.
Outputs:
- Tensor, has the same shape and dtype as input.
- class
mindspore.ops.operations.
NotEqual
(*args, **kwargs)[source] - Computes the non-equivalence of two tensors element-wise.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number, bool]) - The first input is a tensor whose data type is number or bool, ora number or a bool object.
input_y (Union[Tensor, Number, bool]) - The second input tensor whose data type is same as ‘input_x’ ora number or a bool object.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is bool.
Examples
- Copy>>> input_x = Tensor(np.array([1, 2, 3]), mindspore.float32)
- >>> not_equal = NotEqual()
- >>> not_equal(input_x, 2.0)
- [True, False, True]
- >>>
- >>> input_x = Tensor(np.array([1, 2, 3]), mindspore.int32)
- >>> input_y = Tensor(np.array([1, 2, 4]), mindspore.int32)
- >>> not_equal = NotEqual()
- >>> not_equal(input_x, input_y)
- [False, False, True]
- class
mindspore.ops.operations.
OneHot
(*args, **kwargs)[source] - Computes a one-hot tensor.
Makes a new tensor, whose locations represented by indices in indices take value on_value, while allother locations take value off_value.
Note
If the input indices is rank N, the output will have rank N+1. The new axis is created at dimension axis.
- Parameters
axis (int) – Position to insert the value. e.g. If indices shape is [n, c], and axis is -1 the output shapewill be [n, c, depth], If axis is 0 the output shape will be [depth, n, c]. Default: -1.
Inputs:
- indices (Tensor) - A tensor of indices. Tensor of shape
.
-
depth (int) - A scalar defining the depth of the one hot dimension.
-
on_value (Tensor) - A value to fill in output when indices[j] = i.
-
off_value (Tensor) - A value to fill in output when indices[j] != i.
- Outputs:
- Tensor, one_hot tensor. Tensor of shape
.
Examples
- Copy>>> indices = Tensor(np.array([0, 1, 2]), mindspore.int32)
- >>> depth, on_value, off_value = 3, Tensor(1.0, mindspore.float32), Tensor(0.0, mindspore.float32)
- >>> onehot = OneHot()
- >>> result = onehot(indices, depth, on_value, off_value)
- [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
- class
mindspore.ops.operations.
OnesLike
(*args, **kwargs)[source] - Creates a new tensor. All elements’ value are 1.
Returns a tensor of ones with the same shape and type as the input.
- Inputs:
- input_x (Tensor) - Input tensor.
Outputs:
- Tensor, has the same shape and type as input_x but filled with ones.
Examples
- Copy>>> oneslike = P.OnesLike()
- >>> x = Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))
- >>> output = oneslike(x)
- class
mindspore.ops.operations.
PReLU
(*args, **kwargs)[source] - Parametric Rectified Linear Unit activation function.
PReLU is described in the paper Delving Deep into Rectifiers: Surpassing Human-Level Performance onImageNet Classification. Defined as follows:
where
is an element of an channel of the input.
- Inputs:
input_x (Tensor) - Float tensor, representing the output of the preview layer.
weight (Tensor) - Float Tensor, w > 0, there is only two shapes are legitimate,1 or the number of channels at input.
Outputs:
- Tensor, with the same type as input_x.
Detailed information, please refer to nn.PReLU.
- class
mindspore.ops.operations.
Pad
(*args, **kwargs)[source] Pads input tensor according to the paddings.
- Parameters
paddings (tuple) – The shape of parameter paddings is (N, 2). N is the rank of input data. All elements ofpaddings are int type. For D th dimension of input, paddings[D, 0] indicates how many sizes to beextended ahead of the D th dimension of the input tensor, and paddings[D, 1] indicates how many sizes tobe extended behind of the D th dimension of the input tensor.
Inputs:
- input_x (Tensor) - The input tensor.
Outputs:
- Tensor, the tensor after padding.
Examples
- Copy>>> input_tensor = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
- >>> pad_op = Pad(((1, 2), (2, 1)))
- >>> output_tensor = pad_op(input_tensor)
- >>> assert output_tensor == Tensor(np.array([[ 0. , 0. , 0. , 0. , 0. , 0. ],
- >>> [ 0. , 0. , -0.1, 0.3, 3.6, 0. ],
- >>> [ 0. , 0. , 0.4, 0.5, -3.2, 0. ],
- >>> [ 0. , 0. , 0. , 0. , 0. , 0. ],
- >>> [ 0. , 0. , 0. , 0. , 0. , 0. ]]), mindspore.float32)
- class
mindspore.ops.operations.
Pow
(*args, **kwargs)[source] Computes a tensor to the power of the second input.
- Inputs:
input_x (Tensor) - The input tensor.
input_y (Union[Tensor, Number]) - The exponent part. If exponent is a tensor, its shape must be able tobroadcast to the shape of the input_x.
Outputs:
- Tensor, has the same shape as the input_x.
Examples
- Copy>>> input_x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
- >>> input_y = 3.0
- >>> pow = Pow()
- >>> pow(input_x, input_y)
- [1.0, 8.0, 64.0]
- >>>
- >>> input_x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
- >>> input_y = Tensor(np.array([2.0, 4.0, 3.0]), mindspore.float32)
- >>> pow = Pow()
- >>> pow(input_x, input_y)
- [1.0, 16.0, 64.0]
- class
mindspore.ops.operations.
Print
(*args, **kwargs)[source] Output tensor to stdout.
- Inputs:
- input_x (Tensor) - The graph node to attach to.
Examples
- Copy>>> class PrintDemo(nn.Cell):
- >>> def __init__(self,):
- >>> super(PrintDemo, self).__init__()
- >>> self.print = P.Print()
- >>>
- >>> def construct(self, x):
- >>> self.print(x)
- >>> return x
- class
mindspore.ops.operations.
ROIAlign
(*args, **kwargs)[source] - Computes Region of Interest (RoI) Align operator.
The operator computes the value of each sampling point by bilinear interpolation from the nearby grid points on thefeature map. No quantization is performed on any coordinates involved in the RoI, its bins, or the samplingpoints. The details of (RoI) Align operator are described in Mask R-CNN.
- Parameters
pooled_height (int) – The output features’ height.
pooled_width (int) – The output features’ width.
spatial_scale (float) – A scaling factor that maps the raw image coordinates to the inputfeature map coordinates. Suppose the height of a RoI is ori_h in the raw image and fea_h in theinput feature map, the spatial_scale should be fea_h / ori_h.
sample_num (int) – Number of sampling points. Default: 2.
Inputs:
features (Tensor) - The input features, whose shape should be (N, C, H, W).
rois (Tensor) - The shape is (rois_n, 5). rois_n represents the number of RoI. The size ofthe second dimension should be 5 and the 5 colunms are(image_index, top_left_x, top_left_y, bottom_right_x, bottom_right_y). image_index represents theindex of image. top_left_x and top_left_y represent the x, y coordinates of the top left cornerof corresponding RoI, respectively. bottom_right_x and bottom_right_y represent the _x, y_coordinates of the bottom right corner of corresponding RoI, respectively.
Outputs:
- Tensor, the shape is (rois_n, C, pooled_height, pooled_width).
Examples
- Copy>>> input_tensor = Tensor(np.array([[[[1., 2.], [3., 4.]]]]), mindspore.float32)
- >>> rois = Tensor(np.array([[0, 0.2, 0.3, 0.2, 0.3]]), mindspore.float32)
- >>> roi_align = P.ROIAlign(1, 1, 0.5, 2)
- >>> output_tensor = roi_align(input_tensor, rois)
- >>> assert output_tensor == Tensor(np.array([[[[2.15]]]]), mindspore.float32)
- class
mindspore.ops.operations.
RandomChoiceWithMask
(*args, **kwargs)[source] - Generates a random samply as index tensor with a mask tensor from a given tensor.
The input must be a tensor of rank >= 2, the first dimension specify the number of sample.The index tensor and the mask tensor have the same and fixed shape. The index tensor denotes the indexof the nonzero sample, while the mask tensor denotes which element in the index tensor are valid.
- Parameters
Inputs:
- input_x (Tensor) - The input tensor.
Outputs:
- Tuple, two tensors, the first one is the index tensor and the other one is the mask tensor.
Examples
- Copy>>> rnd_choice_mask = RandomChoiceWithMask()
- >>> input_x = Tensor(np.ones(shape=[240000, 4]), ms.bool_)
- >>> output_y, output_mask = rnd_choice_mask(input_x)
- class
mindspore.ops.operations.
Rank
(*args, **kwargs)[source] - Returns the rank of a tensor.
Returns a 0-D int32 Tensor representing the rank of input; the rank of a tensoris the number of indices required to uniquely select each element of the tensor.
- Inputs:
- input_x (Tensor) - The shape of tensor is
.
- Outputs:
- Tensor. 0-D int32 Tensor representing the rank of input, i.e.,
.
Examples
- Copy>>> input_tensor = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
- >>> rank = Rank()
- >>> rank(input_tensor)
- class
mindspore.ops.operations.
ReLU
(*args, **kwargs)[source] - Computes ReLU(Rectified Linear Unit) of input tensor element-wise.
It returns
element-wise.
- Inputs:
- input_x (Tensor) - The input tensor.
Outputs:
- Tensor, with the same type and shape as the input_x.
Examples
- Copy>>> input_x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]], np.float32))
- >>> relu = ReLU()
- >>> result = relu(input_x)
- [[0, 4.0, 0.0], [2.0, 0.0, 9.0]]
- class
mindspore.ops.operations.
ReLU6
(*args, **kwargs)[source] - Computes ReLU(Rectified Linear Unit) upper bounded by 6 of input tensor element-wise.
It returns
element-wise.
- Inputs:
- input_x (Tensor) - The input tensor.
Outputs:
- Tensor, with the same type and shape as the input_x.
Examples
- Copy>>> input_x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]], np.float32))
- >>> relu6 = ReLU6()
- >>> result = relu6(input_x)
- >>> assert result.asnumpy() == Tensor(np.array([[0, 4.0, 0.0], [2.0, 0.0, 6.0]], np.float32)).asnumpy()
- class
mindspore.ops.operations.
RealDiv
(*args, **kwargs)[source] - Divide the first input tensor by the second input tensor in floating-point type element-wise.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number]) - The first input is a tensor whose data type is number or a number.
input_y (Union[Tensor, Number]) - The second input is a tensor whose data type is same as ‘input_x’ ora number.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is same as ‘input_x’.
Examples
- Copy>>> input_x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32)
- >>> input_y = Tensor(np.array([4.0, 5.0, 6.0]), mindspore.float32)
- >>> realdiv = RealDiv()
- >>> realdiv(input_x, input_y)
- [0.25, 0.4, 0.5]
- class
mindspore.ops.operations.
Reciprocal
(*args, **kwargs)[source] Returns reciprocal of a tensor element-wise.
- Inputs:
- input_x (Tensor) - The input tensor.
Outputs:
- Tensor, has the same shape as the input_x.
Examples
- Copy>>> input_x = Tensor(np.array([1.0, 2.0, 4.0]), mindspore.float32)
- >>> reciprocal = Reciprocal()
- >>> reciprocal(input_x)
- [1.0, 0.5, 0.25]
- class
mindspore.ops.operations.
ReduceAll
(*args, **kwargs)[source] - Reduce a dimension of a tensor by the “logical and” of all elements in the dimension.
The dtype of the tensor to be reduced is bool.
- Parameters
keep_dims (bool) – If True, keep these reduced dimensions and the length is 1.If False, don’t keep these dimensions.Default : False, don’t keep these reduced dimensions.
Inputs:
input_x (Tensor[bool]) - The input tensor.
axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions.Only constant value is allowed.
Outputs:
Tensor, the dtype is bool.
If axis is (), and keep_dims is false,the output is a 0-D tensor representing the “logical and” of of all elements in the input tensor.
If axis is int, set as 2, and keep_dims is false,and keep_dims is false, the shape of output is
.
-
If axis is tuple(int), set as (2, 3), and keep_dims is false,the shape of output is
.
Examples
- Copy>>> data = Tensor(np.array([[True, False], [True, True]]))
- >>> op = ReduceAll(keep_dims=True)
- >>> output = op(data, 1)
- class
mindspore.ops.operations.
ReduceMax
(*args, **kwargs)[source] - Reduce a dimension of a tensor by the maximum value in this dimension.
The dtype of the tensor to be reduced is number.
- Parameters
keep_dims (bool) – If True, keep these reduced dimensions and the length is 1.If False, don’t keep these dimensions.Default : False, don’t keep these reduced dimensions.
Inputs:
input_x (Tensor[Number]) - The input tensor.
axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions.Only constant value is allowed.
Outputs:
Tensor, has the same dtype as the ‘input_x’.
If axis is (), and keep_dims is false,the output is a 0-D tensor representing the maximum of all elements in the input tensor.
If axis is int, set as 2, and keep_dims is false,the shape of output is
.
-
If axis is tuple(int), set as (2, 3), and keep_dims is false,the shape of output is
.
Examples
- Copy>>> data = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
- >>> op = ReduceMax(keep_dims=True)
- >>> output = op(data, 1)
- class
mindspore.ops.operations.
ReduceMean
(*args, **kwargs)[source]
Reduce a dimension of a tensor by averaging all elements in the dimension.
The dtype of the tensor to be reduced is number.
- Parameters
keep_dims (bool) – If True, keep these reduced dimensions and the length is 1.If False, don’t keep these dimensions. Default : False.
Inputs:
input_x (Tensor[Number]) - The input tensor.
axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions.Only constant value is allowed.
Outputs:
Tensor, has the same dtype as the ‘input_x’.
If axis is (), and keep_dims is false,the output is a 0-D tensor representing the sum of all elements in the input tensor.
If axis is int, set as 2, and keep_dims is false,the shape of output is
.
-
If axis is tuple(int), set as (2, 3), and keep_dims is false,the shape of output is
.
Examples
- Copy>>> data = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
- >>> op = ReduceMean(keep_dims=True)
- >>> output = op(data, 1)
- class
mindspore.ops.operations.
ReduceMin
(*args, **kwargs)[source] - Reduce a dimension of a tensor by the minimum value in the dimension.
The dtype of the tensor to be reduced is number.
- Parameters
keep_dims (bool) – If True, keep these reduced dimensions and the length is 1.If False, don’t keep these dimensions.Default : False, don’t keep these reduced dimensions.
Inputs:
input_x (Tensor[Number]) - The input tensor.
axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions.Only constant value is allowed.
Outputs:
Tensor, has the same dtype as the ‘input_x’.
If axis is (), and keep_dims is false,the output is a 0-D tensor representing the minimum of all elements in the input tensor.
If axis is int, set as 2, and keep_dims is false,the shape of output is
.
-
If axis is tuple(int), set as (2, 3), and keep_dims is false,the shape of output is
.
Examples
- Copy>>> data = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
- >>> op = ReduceMin(keep_dims=True)
- >>> output = op(data, 1)
- class
mindspore.ops.operations.
ReduceOp
[source] - Operation options for reduce tensors.
There are four kinds of operation options, “SUM”,”MAX”,”MIN”,”PROD”.
SUM: Take the sum.
MAX: Take the maximum.
MIN: Take the minimum.
PROD: Take the product.
- class
mindspore.ops.operations.
ReduceProd
(*args, **kwargs)[source] - Reduce a dimension of a tensor by multiplying all elements in the dimension.
The dtype of the tensor to be reduced is number.
- Parameters
keep_dims (bool) – If True, keep these reduced dimensions and the length is 1.If False, don’t keep these dimensions.Default : False, don’t keep these reduced dimensions.
Inputs:
input_x (Tensor[Number]) - The input tensor.
axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions.
Outputs:
Tensor, has the same dtype as the ‘input_x’.
If axis is (), and keep_dims is false,the output is a 0-D tensor representing the product of all elements in the input tensor.
If axis is int, set as 2, and keep_dims is false,the shape of output is
.
-
If axis is tuple(int), set as (2, 3), and keep_dims is false,the shape of output is
.
Examples
- Copy>>> data = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
- >>> op = ReduceProd(keep_dims=True)
- >>> output = op(data, 1)
- class
mindspore.ops.operations.
ReduceScatter
(*args, **kwargs)[source]
Reduces and scatters tensors from the specified communication group.
Note
The back propagation of the op is not surported yet. Stay tuned for more.Tensor must have the same shape and format in all processes participating in the collective.
- Parameters
Raises
TypeError – If any of op and group is not a string
ValueError – If the first dimension of input can not be divided by rank size.
Examples
- Copy>>> from mindspore.communication.management import init
- >>> import mindspore.ops.operations as P
- >>> init('nccl')
- >>> class Net(nn.Cell):
- >>> def __init__(self):
- >>> super(Net, self).__init__()
- >>> self.reducescatter = P.ReduceScatter(ReduceOp.SUM, group="nccl_world_group")
- >>>
- >>> def construct(self, x):
- >>> return self.reducescatter(x)
- >>>
- >>> input_ = Tensor(np.ones([2, 8]).astype(np.float32))
- >>> net = Net()
- >>> output = net(input_)
- class
mindspore.ops.operations.
ReduceSum
(*args, **kwargs)[source] - Reduce a dimension of a tensor by summing all elements in the dimension.
The dtype of the tensor to be reduced is number.
- Parameters
keep_dims (bool) – If True, keep these reduced dimensions and the length is 1.If False, don’t keep these dimensions. Default : False.
Inputs:
input_x (Tensor[Number]) - The input tensor.
axis (Union[int, tuple(int), list(int)]) - The dimensions to reduce. Default: (), reduce all dimensions.Only constant value is allowed.
Outputs:
Tensor, has the same dtype as the ‘input_x’.
If axis is (), and keep_dims is false,the output is a 0-D tensor representing the sum of all elements in the input tensor.
If axis is int, set as 2, and keep_dims is false,the shape of output is
.
-
If axis is tuple(int), set as (2, 3), and keep_dims is false,the shape of output is
.
Examples
- Copy>>> data = Tensor(np.random.randn(3, 4, 5, 6).astype(np.float32))
- >>> op = ReduceSum(keep_dims=True)
- >>> output = op(data, 1)
- class
mindspore.ops.operations.
Reshape
(*args, **kwargs)[source] Reshapes input tensor with the same values based on a given shape tuple.
- Raises
ValueError – Given a shape tuple, if it has more than one -1; or if the product of its elements is less than or equal to 0 or cannot be divided by the product of the input tensor shape; or if it does not match the input’s array size.
Inputs:
- input_x (Tensor) - The shape of tensor is
.
-
input_shape (tuple[int]) - The input tuple is constructed by multipleintegers, i.e.,
. Only constant value is allowed.
- Outputs:
- Tensor, the shape of tensor is
.
Examples
- Copy>>> input_tensor = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
- >>> reshape = Reshape()
- >>> output = reshape(input_tensor, (3, 2))
- class
mindspore.ops.operations.
ResizeBilinear
(*args, **kwargs)[source] - Resizes the image to certain size using bilinear interpolation.
The resizing only affects the lower two dimensions which represent the height and width. The input imagescan be represented by different data types, but the data types of output images are always float32.
- Parameters
size (tuple[int]) – A tuple of 2 int elements (new_height, new_width), the new size for the images.
align_corners (bool) – If it’s true, rescale input by (new_height - 1) / (height - 1),which exactly aligns the 4 corners of images and resized images. If it’s false,rescale by new_height / height. Default: False.
Inputs:
- input (Tensor) - Image to be resized. Tensor of shape (N_i, …, N_n, height, width).
Outputs:
- Tensor, resized image. Tensor of shape (N_i, …, N_n, new_height, new_width) in float32.
Examples
- Copy>>> tensor = Tensor([[[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]]], mindspore.int32)
- >>> resize_bilinear = P.ResizeBilinear((5, 5))
- >>> result = resize_bilinear(tensor)
- >>> assert result.shape() == (5, 5)
- class
mindspore.ops.operations.
ResizeNearestNeighbor
(*args, **kwargs)[source] - Resize the input tensor by using nearest neighbor algorithm.
Resize input tensor to given size by using nearest neighbor algorithm. The nearestneighbor algorithm selects the value of the nearest point and does not consider thevalues of neighboring points at all, yielding a piecewise-constant interpolant.
- Parameters
Inputs:
- input_x (Tensor) - The input tensor. The shape of the tensor is
.
- Outputs:
- Tensor, the shape of the output tensor is
.
Examples
- Copy>>> input_tensor = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
- >>> resize = ResizeNearestNeighbor((2, 2))
- >>> output = resize(input_tensor)
- class
mindspore.ops.operations.
Round
(*args, **kwargs)[source] Returns half to even of a tensor element-wise.
- Inputs:
- input_x (Tensor) - The input tensor.
Outputs:
- Tensor, has the same shape and type as the input_x.
Examples
- Copy>>> input_x = Tensor(np.array([0.8, 1.5, 2.3, 2.5, -4.5]), mindspore.float32)
- >>> round = Round()
- >>> round(input_x)
- [1.0, 2.0, 2.0, 2.0, -4.0]
- class
mindspore.ops.operations.
Rsqrt
(*args, **kwargs)[source] Computes reciprocal of square root of input tensor element-wise.
- Inputs:
- input_x (Tensor) - The input of Rsqrt. Each element should be a non-negative number.
Outputs:
- Tensor, has the same type and shape as input_x.
Examples
- Copy>>> input_tensor = Tensor([[4, 4], [9, 9]], mindspore.float32)
- >>> rsqrt = Rsqrt()
- >>> rsqrt(input_tensor)
- [[0.5, 0.5], [0.333333, 0.333333]]
- class
mindspore.ops.operations.
SGD
(*args, **kwargs)[source] - Computes stochastic gradient descent (optionally with momentum).
Nesterov momentum is based on the formula from On the importance ofinitialization and momentum in deep learning.
Note
For details, please refer to nn.SGD source code.
- Parameters
Inputs:
parameters (Tensor) - Parameters to be updated.
gradient (Tensor) - Gradients.
learning_rate (Tensor) - Learning rate. e.g. Tensor(0.1, mindspore.float32).
accum (Tensor) - Accum(velocity) to be updated.
momentum (Tensor) - Momentum. e.g. Tensor(0.1, mindspore.float32).
stat (Tensor) - States to be updated with the same shape as gradient.
Outputs:
- Tensor, parameters to be updated.
- class
mindspore.ops.operations.
SameTypeShape
(*args, **kwargs)[source] Checks whether data type and shape of two tensors are the same.
- Raises
ValueError – If not the same.
Inputs:
- input_x (Tensor) - The shape of tensor is
.
-
input_y (Tensor) - The shape of tensor is
.
- Outputs:
- Tensor, the shape of tensor is
,if data type and shape of input_x and input_y are the same.
Examples
- Copy>>> input_x = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
- >>> input_y = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
- >>> out = SameTypeShape()(input_x, input_y)
- class
mindspore.ops.operations.
ScalarCast
(*args, **kwargs)[source] Cast the input scalar to another type.
- Inputs:
input_x (scalar) - The input scalar. Only constant value is allowed.
input_y (mindspore.dtype) - The type should cast to be. Only constant value is allowed.
Outputs:
- Scalar. The type is same as the python type corresponding to input_y.
Examples
- Copy>>> scalar_cast = P.ScalarCast()
- >>> output = scalar_cast(255.0, mindspore.int32)
- class
mindspore.ops.operations.
ScalarSummary
(*args, **kwargs)[source] Output scalar to protocol buffer through scalar summary operator.
- Inputs:
name (str) - The name of the input variable.
value (Tensor) - The value of scalar.
Examples
- Copy>>> class SummaryDemo(nn.Cell):
- >>> def __init__(self,):
- >>> super(SummaryDemo, self).__init__()
- >>> self.summary = P.ScalarSummary()
- >>> self.add = P.TensorAdd()
- >>>
- >>> def construct(self, x, y):
- >>> name = "x"
- >>> self.summary(name, x)
- >>> x = self.add(x, y)
- >>> return x
- class
mindspore.ops.operations.
ScalarToArray
(*args, **kwargs)[source] Converts scalar to Tensor.
- Inputs:
- input_x (Union[int, float]) - The input is a scalar. Only constant value is allowed.
Outputs:
- Tensor. 0-D Tensor and the content is the input.
Examples
- Copy>>> op = ScalarToArray()
- >>> data = 1.0
- >>> output = op(data)
- class
mindspore.ops.operations.
ScalarToTensor
(*args, **kwargs)[source] Converts scalar to Tensor, and convert data type to specified type.
- Inputs:
input_x (Union[int, float]) - The input is a scalar. Only constant value is allowed.
dtype (mindspore.dtype) - The target data type. Default: mindspore.float32. Onlyconstant value is allowed.
Outputs:
- Tensor. 0-D Tensor and the content is the input.
Examples
- Copy>>> op = ScalarToTensor()
- >>> data = 1
- >>> output = op(data, mindspore.float32)
- class
mindspore.ops.operations.
ScatterNd
(*args, **kwargs)[source] - Scatters a tensor into a new tensor depending on the specified indices.
Creates an empty tensor, and set values by scattering the update tensor depending on indices.
- Inputs:
indices (Tensor) - The index of scattering in the new tensor.
update (Tensor) - The source Tensor to be scattered.
shape (tuple[int]) - Define the shape of the output tensor. Has the same type as indices.
Outputs:
- Tensor, the new tensor, has the same type as update and the same shape as shape.
Examples
- Copy>>> op = ScatterNd()
- >>> update = Tensor(np.array([3.2, 1.1]), mindspore.float32)
- >>> indices = Tensor(np.array([[0, 1], [1, 1]]), mindspore.int32)
- >>> shape = (3, 3)
- >>> output = op(indices, update, shape)
- class
mindspore.ops.operations.
ScatterNdUpdate
(*args, **kwargs)[source] - Update tensor value by using input indices and value.
Using given values to update tensor value, along with the input indices.
- Parameters
use_locking (bool) – Whether protect the assignment by a lock. Defaule: True.
Inputs:
input_x (Tensor) - The target tensor.
indices (Tensor) - The index of input tensor.
update (Tensor) - The tensor to add to the input tensor, has the same type as input.
Outputs:
- Tensor, has the same shape and type as input_x.
Examples
- Copy>>> 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)
- >>> update = Tensor(np.array([1.0, 2.2]), mindspore.float32)
- >>> op = ScatterNdUpdate()
- >>> output = op(input_x, indices, update)
- class
mindspore.ops.operations.
Select
(*args, **kwargs)[source] - Return the selected elements, either from input
or input
, depending on the condition.
Given a tensor as input, this operation inserts a dimension of 1 at the dimension,if both
and
are none, the operation returns the coordinates of the trueelement in the condition, the coordinates are returned as a two-dimensionaltensor, where the first dimension (row) represents the number of true elementsand the second dimension (columns) represents the coordinates of the trueelements. Keep in mind that the shape of the output tensor can vary dependingon how much of the true value is in the input. Indexes are output in row-firstorder.
If neither is None,
and
must have the same shape. If
and
arescalars, the conditional tensor must be a scalar. If
and
arehigher-demensional vectors, the condition must be a vector whose size matches thefirst dimension of
, or must have the same shape as
.
The conditional tensor acts as an optional compensation (mask), whichdetermines whether the corresponding element / row in the output should beselected from
(if true) or
(if false) based on the value of eachelement.
If condition is a vector, then
and
are higher-demensional matrices, then itchooses to copy that row (external dimensions) from
and
. If condition hasthe same shape as
and
, you can choose to copy these elements from
and
.
- Inputs:
- input_x (Tensor[bool]) - The shape is
.The condition tensor, decides whose element is chosen.
-
input_y (Tensor) - The shape is
.The first input tensor.
-
input_z (Tensor) - The shape is
.The second input tensor.
- Outputs:
- Tensor, has the same shape as input_y. The shape is
.
Examples
- Copy>>> select = Select()
- >>> input_x = Tensor([True, False])
- >>> input_y = Tensor([2,3], mindspore.float32)
- >>> input_z = Tensor([1,2], mindspore.float32)
- >>> select(input_x, input_y, input_z)
- class
mindspore.ops.operations.
Shape
(*args, **kwargs)[source] Returns the shape of input tensor.
- Inputs:
- input_x (Tensor) - The shape of tensor is
.
- Outputs:
- tuple[int], the output tuple is constructed by multiple integers,
.
Examples
- Copy>>> input_tensor = Tensor(np.ones(shape=[3, 2, 1]), mindspore.float32)
- >>> shape = Shape()
- >>> output = shape(input_tensor)
- class
mindspore.ops.operations.
Sigmoid
(*args, **kwargs)[source] - Sigmoid activation function.
Computes Sigmoid of input element-wise. The Sigmoid function is defined as:
where
is the element of the input.
- Inputs:
- input_x (Tensor) - The input of Sigmoid.
Outputs:
- Tensor, with the same type and shape as the input_x.
- class
mindspore.ops.operations.
SigmoidCrossEntropyWithLogits
(*args, **kwargs)[source] - Uses the given logits to compute sigmoid cross entropy.
Note
Sets input logits as X, input label as Y, output as loss. Then,
- Inputs:
logits (Tensor) - Input logits.
label (Tensor) - Ground truth label.
Outputs:
- Tensor, with the same shape and type as input logits.
- class
mindspore.ops.operations.
Sign
(*args, **kwargs)[source] - Perform
on tensor element-wise.
Note
- Inputs:
- input_x (Tensor) - The input tensor.
Outputs:
- Tensor, has the same shape and type as the input_x.
Examples
- Copy>>> input_x = Tensor(np.array([[2.0, 0.0, -1.0]]), mindspore.float32)
- >>> sign = Sign()
- >>> output = sign(input_x)
- [[1.0, 0.0, -1.0]]
- class
mindspore.ops.operations.
Sin
(*args, **kwargs)[source] Computes sine of input element-wise.
- Inputs:
- input_x (Tensor) - The shape of tensor is
.
- Outputs:
- Tensor, has the same shape as input_x.
Examples
- Copy>>> sin = Sin()
- >>> X = Tensor(np.array([0.62, 0.28, 0.43, 0.62]), ms.float32)
- >>> output = sin(X)
- class
mindspore.ops.operations.
Size
(*args, **kwargs)[source] - Returns the elements count size of a tensor.
Returns an int scalar representing the elements size of input, the total number of elements in the tensor.
- Inputs:
- input_x (Tensor) - The shape of tensor is
.
- Outputs:
- int, a scalar representing the elements size of input_x, tensor is the number of elementsin a tensor,
.
Examples
- Copy>>> input_tensor = Tensor(np.array([[2, 2], [2, 2]]), mindspore.float32)
- >>> size = Size()
- >>> output = size(input_tensor)
- class
mindspore.ops.operations.
Slice
(*args, **kwargs)[source] Slice a tensor in specified shape.
Examples
- Copy>>> data = Tensor(np.array([3,2,3]).astype(np.int32))
- >>> type = P.Slice()(data, (1, 0, 0), (1, 1, 3))
- class
mindspore.ops.operations.
SmoothL1Loss
(*args, **kwargs)[source] - Computes smooth L1 loss, a robust L1 loss.
SmoothL1Loss is a Loss similar to MSELoss but less sensitive to outliers as described in theFast R-CNN by Ross Girshick.
Note
Sets input prediction as X, input target as Y, output as loss. Then,
- Parameters
sigma (float) – A parameter used to control the point where the function will change fromquadratic to linear. Default: 1.0.
Inputs:
prediction (Tensor) - Predict data.
target (Tensor) - Ground truth data, with the same type and shape as prediction.
Outputs:
- Tensor, with the same type and shape as prediction.
- class
mindspore.ops.operations.
Softmax
(*args, **kwargs)[source] - Softmax operation.
Applies the Softmax operation to the input tensor on the specified axis.Suppose a slice along the given aixs
then for each element
the Softmax function is shown as follows:
where
is the length of the tensor.
- Parameters
axis (Union__[int, tuple]) – The axis to do the Softmax operation. Default: -1.
Inputs:
- logits (Tensor) - The input of Softmax.
Outputs:
- Tensor, with the same type and shape as the logits.
- class
mindspore.ops.operations.
SoftmaxCrossEntropyWithLogits
(*args, **kwargs)[source] - Gets the softmax cross-entropy value between logits and labels which shoule be one-hot encoding.
Note
Sets input logits as X, input label as Y, output as loss. Then,
- Inputs:
- logits (Tensor) - Input logits, with shape
.
-
labels (Tensor) - Ground truth labels, with shape
.
- Outputs:
- Tuple of 2 Tensor, the loss shape is (N,), and the dlogits with the same shape as logits.
- class
mindspore.ops.operations.
SpaceToDepth
(*args, **kwargs)[source] - Rearrange blocks of spatial data into depth.
The output tensor’s height dimension is
.
The output tensor’s weight dimension is
.
The depth of output tensor is
.
The input tensor’s height and width must be divisible by block_size.The data format is “NCHW”.
- Parameters
block_size (int) – The block size used to divide spatial data. It must be >= 2.
Inputs:
- x (Tensor) - The target tensor.
Outputs:
- Tensor, the same type as x.
Examples
- Copy>>> x = Tensor(np.random.rand(1,3,2,2), mindspore.float32)
- >>> block_size = 2
- >>> op = SpaceToDepth(block_size)
- >>> output = op(x)
- >>> output.asnumpy().shape == (1,12,1,1)
- class
mindspore.ops.operations.
SparseApplyAdagrad
(*args, **kwargs)[source] - Update relevant entries according to the adagrad scheme.
- Parameters
Inputs:
var (Tensor) - Variable to be updated. The type must be float32.
accum (Tensor) - Accum to be updated. The shape must be the same as var’s shape,the type must be float32.
grad (Tensor) - Gradient. The shape must be the same as var’s shapeexcept first dimension, the type must be float32.
indices (Tensor) - A vector of indices into the first dimension of var and accum.The shape of indices must be the same as grad in first dimension, the type must be int32.
Outputs:
- Tensor, has the same shape and type as var.
- class
mindspore.ops.operations.
SparseSoftmaxCrossEntropyWithLogits
(*args, **kwargs)[source] - Computes the softmax cross-entropy value between logits and sparse encoding labels.
Note
Sets input logits as X, input label as Y, output as loss. Then,
- Parameters
is_grad (bool) – If it’s true, this operation returns the computed gradient. Default: False.
Inputs:
- logits (Tensor) - Input logits, with shape
.
-
labels (Tensor) - Ground truth labels, with shape
.
- Outputs:
- Tensor, if is_grad is False, the output tensor is the value of loss which is a scalar tensor;if is_grad is True, the output tensor is the gradient of input with the same shape as logits.
- class
mindspore.ops.operations.
Split
(*args, **kwargs)[source] Splits input tensor into output_num of tensors along the given axis and output numbers.
- Parameters
Raises
ValueError – If axis is out of the range [-len(input_x.shape()), len(input_x.shape())), or if the output_num is less than or equal to 0, or if the dimension which to split cannot be evenly divided by output_num.
Inputs:
- input_x (Tensor) - The shape of tensor is
.
- Outputs:
- tuple[Tensor], the shape of each output tensor is same, which is
.
Examples
- Copy>>> split = Split(1, 2)
- >>> x = Tensor(np.array([[1, 1, 1, 1], [2, 2, 2, 2]]))
- >>> output = split(x)
- class
mindspore.ops.operations.
Sqrt
(*args, **kwargs)[source] Returns square root of a tensor element-wise.
- Inputs:
- input_x (Tensor) - The input tensor whose dtype is number.
Outputs:
- Tensor, has the same shape as the input_x.
Examples
- Copy>>> input_x = Tensor(np.array([1.0, 4.0, 9.0]), mindspore.float32)
- >>> sqrt = Sqrt()
- >>> sqrt(input_x)
- [1.0, 2.0, 3.0]
- class
mindspore.ops.operations.
Square
(*args, **kwargs)[source] Returns square of a tensor element-wise.
- Inputs:
- input_x (Tensor) - The input tensor whose dtype is number.
Outputs:
- Tensor, has the same shape and dtype as the input_x.
Examples
- Copy>>> input_x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32)
- >>> square = Square()
- >>> square(input_x)
- [1.0, 4.0, 9.0]
- class
mindspore.ops.operations.
Squeeze
(*args, **kwargs)[source] - Returns a tensor with the same type but dimensions of 1 being removed based on axis.
Note
The dimension index starts at 0 and must be in the range [-input.dim(), input.dim()).
- Raises
ValueError – If the corresponding dimension of the specified axis does not equal to 1.
Parameters
axis (int) – Specifies the dimension indexes of shape to be removed, which will removeall the dimensions that are equal to 1. If specified, it must be int32 or int64.Default: (), an empty tuple.
Inputs:
- input_x (Tensor) - The shape of tensor is
.
- Outputs:
- Tensor, the shape of tensor is
.
Examples
- Copy>>> input_tensor = Tensor(np.ones(shape=[3, 2, 1]), mindspore.float32)
- >>> squeeze = Squeeze(2)
- >>> output = squeeze(input_tensor)
- class
mindspore.ops.operations.
StridedSlice
(*args, **kwargs)[source] - Extracts a strided slice of a tensor.
Given an input tensor, this operation inserts a dimension of length 1 at the dimension.This operation extracts a fragment of size (end-begin)/stride from the given‘input_tensor’. Starting from the position specified by the begin, the fragmentcontinues adding stride to the index until all dimensions are not less than end.
Note
The stride may be negative value, which causes reverse slicing.The shape of begin, end and strides should be the same.
- Parameters
Inputs:
input_x (Tensor) - The input Tensor.
begin (tuple[int]) - A tuple which represents the location where to start. Onlyconstant value is allowed.
end (tuple[int]) - A tuple or which represents the maximum location where to stop.Only constant value is allowed.
strides (tuple[int]) - A tuple which represents the stride continuously addedbefore reach the maximum location. Only constant value is allowed.
Outputs:
- Tensor.Explain with the following example.
In the 0th dim, begin is 1, end is 2, and strides is 1,
, the interval is .
because
Thus, return the element with in 0th dim, i.e., [[3, 3, 3], [4, 4, 4]].In the 1st dim, similarly, the interval is
.
Based on the return value of the 0th dim, return the element with ,
i.e., [3, 3, 3].In the 2nd dim, similarly, the interval is
.
Based on the return value of the 1st dim, return the element with ,
i.e., [3, 3, 3].Finally, the output is [3, 3, 3].
- Examples
- Copy>>> input_x = Tensor([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]])
- >>> slice = StridedSlice()
- >>> output = slice(input_x, (1, 0, 0), (2, 1, 3), (1, 1, 1))
- >>> output.shape()
- (1, 1, 3)
- >>> output
- [[[3, 3, 3]]]
- class
mindspore.ops.operations.
Sub
(*args, **kwargs)[source] - Subtracts the second input tensor from the first input tensor element-wise.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number]) - The first input is a tensor whose data type is number or a number.
input_y (Union[Tensor, Number]) - The second input is a tensor whose data type is same as ‘input_x’ ora number.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is same as ‘input_x’.
Examples
- Copy>>> input_x = Tensor(np.array([1, 2, 3]), mindspore.int32)
- >>> input_y = Tensor(np.array([4, 5, 6]), mindspore.int32)
- >>> sub = Sub()
- >>> sub(input_x, input_y)
- [-3, -3, -3]
- class
mindspore.ops.operations.
Tanh
(*args, **kwargs)[source] - Tanh activation function.
Computes hyperbolic tangent of input element-wise. The Tanh function is defined as:
where
is an element of the input Tensor.
- Inputs:
- input_x (Tensor) - The input of Tanh.
Outputs:
- Tensor, with the same type and shape as the input_x.
- class
mindspore.ops.operations.
TensorAdd
(*args, **kwargs)[source] - Adds two input tensors element-wise.
The inputs must be two tensors or one tensor and one scalar.When the inputs are two tensors, the shapes of them could be broadcast,and the data types of them should be same.When the inputs are one tensor and one scalar, the scalar cannot be a parameter, only can be a constant,and the type of the scalar is the same as the data type of the tensor.
- Inputs:
input_x (Union[Tensor, Number]) - The first input is a tensor whose data type is number or a number.
input_y (Union[Tensor, Number]) - The second input is a tensor whose data type is same as ‘input_x’ ora number.
Outputs:
- Tensor, the shape is same as the shape after broadcasting, and the data type is same as ‘input_x’.
Examples
- Copy>>> add = P.TensorAdd()
- >>> x = Tensor(np.array([1,2,3]).astype(np.float32))
- >>> y = Tensor(np.array([4,5,6]).astype(np.float32))
- >>> add(x, y)
- [5,7,9]
- class
mindspore.ops.operations.
TensorSummary
(*args, **kwargs)[source] Output tensor to protocol buffer through tensor summary operator.
- Inputs:
name (str) - The name of the input variable.
value (Tensor) - The value of tensor.
Examples
- Copy>>> class SummaryDemo(nn.Cell):
- >>> def __init__(self,):
- >>> super(SummaryDemo, self).__init__()
- >>> self.summary = P.TensorSummary()
- >>> self.add = P.TensorAdd()
- >>>
- >>> def construct(self, x, y):
- >>> x = self.add(x, y)
- >>> name = "x"
- >>> self.summary(name, x)
- >>> return x
- class
mindspore.ops.operations.
Tile
(*args, **kwargs)[source] - Replicates a tensor with given multiples times.
Creates a new tensor by replicating input multiples times. The dimension ofoutput tensor is the larger of the dimension length of input and the length of multiples.
- Inputs:
- input_x (Tensor) - 1-D or higher Tensor. Set the shape of input tensor as
.
-
multiples (tuple[int]) - The input tuple is constructed by multipleintegers, i.e.,
. The length of multiples_can’t be smaller than the length of shape in _input_x.
- Outputs:
Tensor, has the same type as the input_x.
- If the length of multiples is the same as the length of shape in input_x,then the shape of their corresponding positions can be multiplied, andthe shape of Outputs is
.
-
If the length of multiples is larger than the length of shape in input_x,fill in multiple 1 in front of the shape in input_x until their lengths are consistent.Such as set the shape of input_x as
,then the shape of their corresponding positions can be multiplied, andthe shape of Outputs is
.
- class
mindspore.ops.operations.
TopK
(*args, **kwargs)[source] Finds values and indices of the k largest entries along the last dimension.
- Parameters
sorted (bool) – If true, the resulting elements willbe sorted by the values in descending order. Default: False.
Inputs:
input_x (Tensor) - Input to be computed.
k (int) - Number of top elements to be computed along the last dimension, constant input is needed.
Outputs:
Tuple of 2 Tensor, the values and the indices.
values (Tensor) - The k largest elements along each last dimensional slice.
indices (Tensor) - The indices of values within the last dimension of input.
Examples
- Copy>>> topk = TopK(sorted=True)
- >>> x = Tensor(np.array([1, 2, 3, 4, 5]).astype(np.float16))
- >>> values, indices = topk(x)
- >>> assert values == Tensor(np.array([5, 4, 3]))
- >>> assert indices == Tensor(np.array([4, 3, 2]))
- class
mindspore.ops.operations.
Transpose
(*args, **kwargs)[source] Permutes the dimensions of input tensor according to input perm.
- Inputs:
- input_x (Tensor) - The shape of tensor is
.
-
input_perm (tuple[int]) - The permutation to be converted. The input tuple is constructed by multipleindexes. The length of input_perm and the shape of input_x should be the same. Only constant value isallowed.
- Outputs:
- Tensor, the type of output tensor is same as input_x and the shape of output tensor is decided by theshape of input_x and the value of input_perm.
Examples
- Copy>>> input_tensor = Tensor(np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]), mindspore.float32)
- >>> perm = (0, 2, 1)
- >>> expect = np.array([[[1, 4], [2, 5], [3, 6]], [[7, 10], [8, 11], [9, 12]]])
- >>> transpose = Transpose()
- >>> output = transpose(input_tensor, perm)
- class
mindspore.ops.operations.
TruncatedNormal
(*args, **kwargs)[source] - Returns a tensor of the specified shape filled with truncated normal values.
The generated values follow a normal distribution.
- Parameters
seed (int) – A int number used to create random seed. Default: 0.
dtype (
mindspore.dtype
) – Data type. Default: mindspore.float32.
Inputs:
- shape (Tensor) - Shape of output tensor. The shape is a 1-D tensor, and type is int.
Outputs:
- Tensor, type of output tensor is same as attribute dtype.
Examples
- Copy>>> input_shape = Tensor(np.array([1, 2, 3]))
- >>> truncated_normal = TruncatedNormal()
- >>> output = truncated_normal(input_shape)
- class
mindspore.ops.operations.
TupleToArray
(*args, **kwargs)[source] - Converts a tuple to tensor.
If the first number type of tuple is int, the output tensor type is int. Else, the output tensor type is float.
- Inputs:
- input_x (tuple) - A tuple of numbers. These numbers have the same type. Only constant value is allowed.
Outputs:
- Tensor, if the input tuple contain N numbers, then the output tensor shape is (N,).
Examples
- Copy>>> type = TupleToArray()((1,2,3))
- class
mindspore.ops.operations.
UnsortedSegmentSum
(*args, **kwargs)[source] - Computes the sum along segments of a tensor.
Calculates a tensor such that
, where
is a tuple describing the index of element in data. segment_ids selects which elements in data to sumup. Segment_ids does not need to be sorted, and it does not need to cover all values in the entire valid valuerange.
If the sum of the given segment_ids
is empty, then
. If the given segment_idsis negative, the value will be ignored. ‘num_segments’ should be equal to the number of different segment_ids.
- Inputs:
- input_x (Tensor) - The shape is
.
-
segment_ids (Tensor) - Set the shape as
, where 0 < N <= R. Type must be int.
-
num_segments (int) - Set
as num_segments.
- Outputs:
- Tensor, the shape is
.
Examples
- Copy>>> input_x = [1, 2, 3, 4]
- >>> segment_ids = [0, 0, 1, 2]
- >>> num_segments = 4
- >>> type = P.UnsortedSegmentSum()(input_x, segment_ids, num_segments)
- class
mindspore.ops.operations.
ZerosLike
(*args, **kwargs)[source] - Creates a new tensor. All elements value are 0.
Returns a tensor of zeros with the same shape and type as the input tensor.
- Inputs:
- input_x (Tensor) - Input tensor.
Outputs:
- Tensor, has the same shape and type as input_x but filled with zeros.
Examples
- Copy>>> zeroslike = P.ZerosLike()
- >>> x = Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))
- >>> output = zeroslike(x)