mindspore.ops.composite

Composite operators.

Pre-defined combination of operators.

class mindspore.ops.composite.GradOperation(name, get_all=False, get_by_list=False, sens_param=False)[source]

An metafuncgraph object which is used to get the gradient of output of a network(function).

The GradOperation will convert the network(function) into a back propagation graph.

Parameters
  • get_all (bool) – If True, get all the gradients w.r.t inputs. Default: False.

  • get_by_list (bool) – If True, get all the gradients w.r.t Parameter variables. If get_all and get_by_list are both False, get the gradient w.r.t first input. If get_all and get_by_list are both True, get the gradients w.r.t inputs and Parameter variables at the same time in the form of ((grads w.r.t inputs), (grads w.r.t parameters)). Default: False.

  • sens_param (bool) – Whether append sensitivity as input. If sens_param is False, a ‘ones_like(outputs)’ sensitivity will be attached automatically. Default: False.

class mindspore.ops.composite.HyperMap(ops=None)[source]

Hypermap will apply the set operation on input sequences.

Which will apply the operations of every elements of the sequence.

Parameters

ops (Union[MultitypeFuncGraph, None]) – ops is the operation to apply. If ops is None, the operations should be putted in the first input of the instance.

Inputs:
  • args (Tuple[sequence]) - If ops is not None, all the inputs should be the same length sequences, and each row of the sequences. e.g. If args length is 2, and for i in length of each sequence (args[0][i], args[1][i]) will be the input of the operation.

    If ops is not None, the first input is the operation, and the other is inputs.

Outputs:

sequence, the output will be same type and same length of sequence from input and the value of each element is the result of operation apply each row of element. e.g. operation(args[0][i], args[1][i]).

class mindspore.ops.composite.MultitypeFuncGraph(name)[source]

Generate multiply graph.

MultitypeFuncGraph is a class used to generate graphs for function with different type as input.

Parameters

name (str) – Operator name.

Raises

ValueError – Cannot find matching fn for the given args.

Examples

>>> # `add` is a metagraph object which will add two objects according to
>>> # input type using ".register" decorator.
>>> add = MultitypeFuncGraph('add')
register(*type_names)[source]

Register a function for the given type string.

mindspore.ops.composite.add_flags(fn=None, **flags)[source]

An decorator to add flag for a function.

Note

Only supports bool value.

Parameters
  • fn (Function) – Function or cell to add flag. Default: None.

  • flags (dict) – Flags use kwargs. Default: None.

Returns

Function, the fn added flags.

Examples

>>> add_flags(net, predit=True)
mindspore.ops.composite.clip_by_value(x, clip_value_min, clip_value_max)[source]

Clips tensor values to a specified min and max.

Limits the value of \(x\) to a range, whose lower limit is ‘clip_value_min’ and upper limit is ‘clip_value_max’.

Note

‘clip_value_min’ needs to be less than or equal to ‘clip_value_max’.

Parameters
  • x (Tensor) – Input data.

  • clip_value_min (Tensor) – The minimum value.

  • clip_value_max (Tensor) – The maximum value.

Returns

Tensor, a clipped Tensor.

mindspore.ops.composite.core(fn=None, **flags)[source]

A decorator to add flag to a function.

By default, the function is marked core=True using this decorator to set flag to a graph.

Parameters
  • fn (Function) – Function to add flag. Default: None.

  • flags (dict) – The following flags can be set core, which indicates that this is a core function or other flag. Default: None.

mindspore.ops.composite.gamma(shape, alpha, beta, seed=0)[source]

Generates random numbers according to the Gamma random number distribution.

Parameters
  • shape (tuple) – The shape of random tensor to be generated.

  • alpha (Tensor) – The alpha α distribution parameter. With float32 data type.

  • beta (Tensor) – The beta β distribution parameter. With float32 data type.

  • seed (int) – Seed is used as entropy source for Random number engines generating pseudo-random numbers. Must be non-negative. Default: 0.

Returns

Tensor. The shape should be the broadcasted shape of Input “shape” and shapes of alpha and beta. The dtype is float32.

Examples

>>> shape = (4, 16)
>>> alpha = Tensor(1.0, mstype.float32)
>>> beta = Tensor(1.0, mstype.float32)
>>> output = C.gamma(shape, alpha, beta, seed=5)
mindspore.ops.composite.multinomial(inputs, num_sample, replacement=True, seed=0)[source]

Returns a tensor sampled from the multinomial probability distribution located in the corresponding row of tensor input.

Note

The rows of input do not need to sum to one (in which case we use the values as weights), but must be non-negative, finite and have a non-zero sum.

Parameters

seed (int) – Seed data is used as entropy source for Random number engines generating pseudo-random numbers. Default: 0.

Inputs:
  • input (Tensor) - the input tensor containing probabilities, must be 1 or 2 dims.

  • num_samples (int) - number of samples to draw.

  • replacement (bool, optional) - whether to draw with replacement or not, default True.

Outputs:

Tensor. have the same rows with input, each row has num_samples sampled indices.

Examples

>>> input = Tensor([0, 9, 4, 0], mstype.float32)
>>> output = C.multinomial(input, 2, True)
mindspore.ops.composite.normal(shape, mean, stddev, seed=0)[source]

Generates random numbers according to the Normal (or Gaussian) random number distribution.

Parameters
  • shape (tuple) – The shape of random tensor to be generated.

  • mean (Tensor) – The mean μ distribution parameter, which specifies the location of the peak. With float32 data type.

  • stddev (Tensor) – The deviation σ distribution parameter. With float32 data type.

  • seed (int) – Seed is used as entropy source for Random number engines generating pseudo-random numbers. Must be non-negative. Default: 0.

Returns

Tensor. The shape should be the broadcasted shape of Input “shape” and shapes of mean and stddev. The dtype is float32.

Examples

>>> shape = (4, 16)
>>> mean = Tensor(1.0, mstype.float32)
>>> stddev = Tensor(1.0, mstype.float32)
>>> output = C.normal(shape, mean, stddev, seed=5)
mindspore.ops.composite.poisson(shape, mean, seed=0)[source]

Generates random numbers according to the Poisson random number distribution.

Parameters
  • shape (tuple) – The shape of random tensor to be generated.

  • mean (Tensor) – The mean μ distribution parameter. With float32 data type.

  • seed (int) – Seed is used as entropy source for Random number engines generating pseudo-random numbers. Must be non-negative. Default: 0.

Returns

Tensor. The shape should be the broadcasted shape of Input “shape” and shapes of mean. The dtype is float32.

Examples

>>> shape = (4, 16)
>>> mean = Tensor(1.0, mstype.float32)
>>> output = C.poisson(shape, mean, seed=5)
mindspore.ops.composite.uniform(shape, minval, maxval, seed=0, dtype=mindspore.float32)[source]

Generates random numbers according to the Uniform random number distribution.

Note

The number in tensor minval should be strictly less than maxval at any position after broadcasting.

Parameters
  • shape (tuple) – The shape of random tensor to be generated.

  • minval (Tensor) – The a distribution parameter. It defines the minimum possibly generated value. With int32 or float32 data type. If dtype is int32, only one number is allowed.

  • maxval (Tensor) – The b distribution parameter. It defines the maximum possibly generated value. With int32 or float32 data type. If dtype is int32, only one number is allowed.

  • seed (int) – Seed is used as entropy source for Random number engines generating pseudo-random numbers. Must be non-negative. Default: 0.

Returns

Tensor. The shape should be the broadcasted shape of Input “shape” and shapes of minval and maxval. The dtype is designated as the input dtype.

Examples

>>> For discrete uniform distribution, only one number is allowed for both minval and maxval:
>>> shape = (4, 2)
>>> minval = Tensor(1, mstype.int32)
>>> maxval = Tensor(2, mstype.int32)
>>> output = C.uniform(shape, minval, maxval, seed=5)
>>>
>>> For continuous uniform distribution, minval and maxval can be multi-dimentional:
>>> shape = (4, 2)
>>> minval = Tensor([1.0, 2.0], mstype.float32)
>>> maxval = Tensor([4.0, 5.0], mstype.float32)
>>> output = C.uniform(shape, minval, maxval, seed=5)