mindspore

class mindspore.dtype

Create a data type object of MindSpore.

The actual path of dtype is /mindspore/common/dtype.py. Run the following command to import the package:

import mindspore.common.dtype as mstype

or

from mindspore import dtype as mstype
  • Numeric Type

    Currently, MindSpore supports Int type, Uint type and Float type. The following table lists the details.

    Definition

    Description

    mindspore.int8 , mindspore.byte

    8-bit integer

    mindspore.int16 , mindspore.short

    16-bit integer

    mindspore.int32 , mindspore.intc

    32-bit integer

    mindspore.int64 , mindspore.intp

    64-bit integer

    mindspore.uint8 , mindspore.ubyte

    unsigned 8-bit integer

    mindspore.uint16 , mindspore.ushort

    unsigned 16-bit integer

    mindspore.uint32 , mindspore.uintc

    unsigned 32-bit integer

    mindspore.uint64 , mindspore.uintp

    unsigned 64-bit integer

    mindspore.float16 , mindspore.half

    16-bit floating-point number

    mindspore.float32 , mindspore.single

    32-bit floating-point number

    mindspore.float64 , mindspore.double

    64-bit floating-point number

  • Other Type

    For other defined types, see the following table.

    Type

    Description

    tensor

    MindSpore’s tensor type. Data format uses NCHW. For details, see [tensor](https://www.gitee.com/mindspore/mindspore/blob/r1.0/mindspore/common/tensor.py).

    MetaTensor

    A tensor only has data type and shape. For details, see [MetaTensor](https://www.gitee.com/mindspore/mindspore/blob/r1.0/mindspore/common/parameter.py).

    bool_

    Boolean True or False.

    int_

    Integer scalar.

    uint

    Unsigned integer scalar.

    float_

    Floating-point scalar.

    number

    Number, including int_ , uint , float_ and bool_ .

    list_

    List constructed by tensor , such as List[T0,T1,...,Tn] , where the element Ti can be of different types.

    tuple_

    Tuple constructed by tensor , such as Tuple[T0,T1,...,Tn] , where the element Ti can be of different types.

    function

    Function. Return in two ways, when function is not None, returns Func directly, the other returns Func(args: List[T0,T1,…,Tn], retval: T) when function is None.

    type_type

    Type definition of type.

    type_none

    No matching return type, corresponding to the type(None) in Python.

    symbolic_key

    The value of a variable is used as a key of the variable in env_type .

    env_type

    Used to store the gradient of the free variable of a function, where the key is the symbolic_key of the free variable’s node and the value is the gradient.

  • Tree Topology

    The relationships of the above types are as follows:

    └─────── number
        │   ├─── bool_
        │   ├─── int_
        │   │   ├─── int8, byte
        │   │   ├─── int16, short
        │   │   ├─── int32, intc
        │   │   └─── int64, intp
        │   ├─── uint
        │   │   ├─── uint8, ubyte
        │   │   ├─── uint16, ushort
        │   │   ├─── uint32, uintc
        │   │   └─── uint64, uintp
        │   └─── float_
        │       ├─── float16
        │       ├─── float32
        │       └─── float64
        ├─── tensor
        │   ├─── Array[Float32]
        │   └─── ...
        ├─── list_
        │   ├─── List[Int32,Float32]
        │   └─── ...
        ├─── tuple_
        │   ├─── Tuple[Int32,Float32]
        │   └─── ...
        ├─── function
        │   ├─── Func
        │   ├─── Func[(Int32, Float32), Int32]
        │   └─── ...
        ├─── MetaTensor
        ├─── type_type
        ├─── type_none
        ├─── symbolic_key
        └─── env_type
    
class mindspore.Parameter(default_input, name, *args, **kwargs)[source]

Parameter types of cell models.

After initialized Parameter is a subtype of Tensor.

In auto_parallel mode of “semi_auto_parallel” and “auto_parallel”, if init Parameter by an Initializer, the type of Parameter will be MetaTensor not Tensor. MetaTensor only saves the shape and type info of a tensor with no memory usage. The shape can be changed while compiling for auto-parallel. Call init_data will return a Tensor Parameter with initialized data.

Note

Each parameter of Cell is represented by Parameter class. A Parameter has to belong to a Cell. If there is an operator in the network that requires part of the inputs to be Parameter, then the Parameters as this part of the inputs are not allowed to be cast.

Parameters
  • default_input (Union[Tensor, Initializer, Number]) – Parameter data, to be set initialized.

  • name (str) – Name of the child parameter.

  • requires_grad (bool) – True if the parameter requires gradient. Default: True.

  • layerwise_parallel (bool) – A kind of model parallel mode. When layerwise_parallel is true in parallel mode, broadcast and gradients communication would not be applied to parameters. Default: False.

Example

>>> from mindspore import Parameter, Tensor
>>> from mindspore.common import initializer as init
>>> from mindspore.ops import operations as P
>>> from mindspore.nn import Cell
>>> import mindspore
>>> import numpy as np
>>> from mindspore import context
>>>
>>> class Net(Cell):
>>>     def __init__(self):
>>>         super(Net, self).__init__()
>>>         self.matmul = P.MatMul()
>>>         self.weight = Parameter(Tensor(np.ones((1,2))), name="w", requires_grad=True)
>>>
>>>     def construct(self, x):
>>>         out = self.matmul(self.weight, x)
>>>         return out
>>> context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
>>> net = Net()
>>> x = Tensor(np.ones((2,1)))
>>> net(x)
[[2.]]
>>> net.weight.set_data(Tensor(np.zeros((1,2))))
>>> net(x)
[[0.]]
clone(prefix, init='same')[source]

Clone the parameter.

Parameters
  • prefix (str) – Namespace of parameter. The cloned Parameter name is combined of prefix and current name: f”{perfix}.{self.name}”.

  • init (Union[Tensor, str, Initializer, numbers.Number]) – Initialize the shape of the parameter. Default: ‘same’.

Returns

Parameter, a new parameter.

init_data(layout=None, set_sliced=False)[source]

Initialize the parameter data.

Parameters
  • layout (list[list[int]]) –

    Parameter slice layout [dev_mat, tensor_map, slice_shape].

    • dev_mat (list[int]): Device matrix.

    • tensor_map (list[int]): Tensor map.

    • slice_shape (list[int]): Shape of slice.

  • set_sliced (bool) – True if the parameter is set sliced after initializing the data. Default: False.

Raises

RuntimeError – If it is from Initializer, and parallel mode has changed after the Initializer created.

Returns

Parameter, the Parameter after initializing data. If current Parameter was already initialized before, returns the same initialized Parameter.

property inited_param

Get the new parameter after call the init_data.

Default is a None, If self is a Parameter with out data, after call the init_data the initialized Parameter with data will be recorded here.

property is_init

Get the initialization status of the parameter.

In GE backend, the Parameter need a “init graph” to sync the data from host to device. This flag indicates whether the data as been sync to the device.

This flag only work in GE, and it will be set to False in other backend.

property name

Get the name of the parameter.

property requires_grad

Return whether the parameter requires gradient.

set_data(data, slice_shape=False)[source]

Set set_data of current Parameter.

Parameters
  • data (Union[Tensor, Initializer, int, float]) – new data.

  • slice_shape (bool) – If slice the Parameter, will not check if shape is match. Default: False.

Retruns:

Parameter, the parameter after set data.

property sliced

Get slice status of the parameter.

class mindspore.ParameterTuple(iterable)[source]

Class for storing tuple of parameters.

Note

It is used to store the parameters of the network into the parameter tuple collection.

clone(prefix, init='same')[source]

Clone the parameter.

Parameters
  • prefix (str) – Namespace of parameter.

  • init (str) – Initialize the shape of the parameter. Default: ‘same’.

Returns

Tuple, the new Parameter tuple.

class mindspore.RowTensor(indices, values, dense_shape)[source]

A sparse representation of a set of tensor slices at given indices.

An RowTensor is typically used to represent a subset of a larger tensor dense of shape [L0, D1, .. , DN] where L0 >> D0.

The values in indices are the indices in the first dimension of the slices that have been extracted from the larger tensor.

The dense tensor dense represented by an RowTensor slices has dense[slices.indices[i], :, :, :, …] = slices.values[i, :, :, :, …].

RowTensor can only be used in the Cell’s construct method.

It is not supported in pynative mode at the moment.

Parameters
  • indices (Tensor) – A 1-D integer Tensor of shape [D0].

  • values (Tensor) – A Tensor of any dtype of shape [D0, D1, …, Dn].

  • dense_shape (tuple) – An integer tuple which contains the shape of the corresponding dense tensor.

Returns

RowTensor, composed of indices, values, and dense_shape.

Examples

>>> class Net(nn.Cell):
>>>     def __init__(self, dense_shape):
>>>         super(Net, self).__init__()
>>>         self.dense_shape = dense_shape
>>>     def construct(self, indices, values):
>>>         x = RowTensor(indices, values, self.dense_shape)
>>>         return x.values, x.indices, x.dense_shape
>>>
>>> indices = Tensor([0])
>>> values = Tensor([[1, 2]], dtype=ms.float32)
>>> Net((3, 2))(indices, values)
class mindspore.SparseTensor(indices, values, dense_shape)[source]

A sparse representation of a set of nonzero elememts from a tensor at given indices.

SparseTensor can only be used in the Cell’s construct method.

Pynative mode not supported at the moment.

For a tensor dense, its SparseTensor(indices, values, dense_shape) has dense[indices[i]] = values[i].

Parameters
  • indices (Tensor) – A 2-D integer Tensor of shape [N, ndims], where N and ndims are the number of values and number of dimensions in the SparseTensor, respectively.

  • values (Tensor) – A 1-D tensor of any type and shape [N], which supplies the values for each element in indices.

  • dense_shape (tuple) – A integer tuple of size ndims, which specifies the dense_shape of the sparse tensor.

Returns

SparseTensor, composed of indices, values, and dense_shape.

Examples

>>> class Net(nn.Cell):
>>>     def __init__(self, dense_shape):
>>>         super(Net, self).__init__()
>>>         self.dense_shape = dense_shape
>>>     def construct(self, indices, values):
>>>         x = SparseTensor(indices, values, self.dense_shape)
>>>         return x.values, x.indices, x.dense_shape
>>>
>>> indices = Tensor([[0, 1], [1, 2]])
>>> values = Tensor([1, 2], dtype=ms.float32)
>>> Net((3, 4))(indices, values)
class mindspore.Tensor(input_data, dtype=None)[source]

Tensor is used for data storage.

Tensor inherits tensor object in C++. Some functions are implemented in C++ and some functions are implemented in Python.

Parameters
  • input_data (Tensor, float, int, bool, tuple, list, numpy.ndarray) – Input data of the tensor.

  • dtype (mindspore.dtype) – Input data should be None, bool or numeric type defined in mindspore.dtype. The argument is used to define the data type of the output tensor. If it is None, the data type of the output tensor will be as same as the input_data. Default: None.

Outputs:

Tensor, with the same shape as input_data.

Examples

>>> # initialize a tensor with input data
>>> t1 = Tensor(np.zeros([1, 2, 3]), mindspore.float32)
>>> assert isinstance(t1, Tensor)
>>> assert t1.shape == (1, 2, 3)
>>> assert t1.dtype == mindspore.float32
>>>
>>> # initialize a tensor with a float scalar
>>> t2 = Tensor(0.1)
>>> assert isinstance(t2, Tensor)
>>> assert t2.dtype == mindspore.float64
all(axis=(), keep_dims=False)[source]

Check all array elements along a given axis evaluate to True.

Parameters
  • axis (Union[None, int, tuple(int)) – Dimensions of reduction, when axis is None or empty tuple, reduce all dimensions. Default: (), reduce all dimensions.

  • keep_dims (bool) – Whether to keep the reduced dimensions. Default : False, don’t keep these reduced dimensions.

Returns

Tensor, has the same data type as x.

any(axis=(), keep_dims=False)[source]

Check any array element along a given axis evaluate to True.

Parameters
  • axis (Union[None, int, tuple(int)) – Dimensions of reduction, when axis is None or empty tuple, reduce all dimensions. Default: (), reduce all dimensions.

  • keep_dims (bool) – Whether to keep the reduced dimensions. Default : False, don’t keep these reduced dimensions.

Returns

Tensor, has the same data type as x.

asnumpy()[source]

Convert tensor to numpy array.

property dtype

The dtype of tensor is a mindspore type.

static from_numpy(array)[source]

Convert numpy array to Tensor without copy data.

property shape

The shape of tensor is a tuple.

property virtual_flag

Mark tensor is virtual.

mindspore.dtype_to_nptype(type_)[source]

Convert MindSpore dtype to numpy data type.

Parameters

type (mindspore.dtype) – MindSpore’s dtype.

Returns

The data type of numpy.

mindspore.dtype_to_pytype(type_)[source]

Convert MindSpore dtype to python data type.

Parameters

type (mindspore.dtype) – MindSpore’s dtype.

Returns

Type of python.

mindspore.get_level()[source]

Get the logger level.

Returns

str, the Log level includes 3(ERROR), 2(WARNING), 1(INFO), 0(DEBUG).

Examples

>>> import os
>>> os.environ['GLOG_v'] = '0'
>>> from mindspore import log as logger
>>> logger.get_level()
mindspore.get_log_config()[source]

Get logger configurations.

Returns

Dict, the dictionary of logger configurations.

Examples

>>> import os
>>> os.environ['GLOG_v'] = '1'
>>> os.environ['GLOG_logtostderr'] = '0'
>>> os.environ['GLOG_log_dir'] = '/var/log/mindspore'
>>> os.environ['logger_maxBytes'] = '5242880'
>>> os.environ['logger_backupCount'] = '10'
>>> from mindspore import log as logger
>>> logger.get_log_config()
mindspore.get_py_obj_dtype(obj)[source]

Get the MindSpore data type which corresponds to python type or variable.

Parameters

obj – An object of python type, or a variable in python type.

Returns

Type of MindSpore type.

mindspore.get_seed()[source]

Get global random seed.

mindspore.issubclass_(type_, dtype)[source]

Determine whether type_ is a subclass of dtype.

Parameters
Returns

bool, True or False.

mindspore.ms_function(fn=None, obj=None, input_signature=None)[source]

Create a callable MindSpore graph from a python function.

This allows the MindSpore runtime to apply optimizations based on graph.

Parameters
  • fn (Function) – The Python function that will be run as a graph. Default: None.

  • obj (Object) – The Python Object that provides the information for identifying the compiled function.Default: None.

  • input_signature (MetaTensor) – The MetaTensor which describes the input arguments. The MetaTensor specifies the shape and dtype of the Tensor and they will be supplied to this function. If input_signature is specified, each input to fn must be a Tensor. And the input parameters of fn cannot accept **kwargs. The shape and dtype of actual inputs should keep the same as input_signature. Otherwise, TypeError will be raised. Default: None.

Returns

Function, if fn is not None, returns a callable function that will execute the compiled function; If fn is None, returns a decorator and when this decorator invokes with a single fn argument, the callable function is equal to the case when fn is not None.

Examples

>>> def tensor_add(x, y):
>>>     z = F.tensor_add(x, y)
>>>     return z
>>>
>>> @ms_function
>>> def tensor_add_with_dec(x, y):
>>>     z = F.tensor_add(x, y)
>>>     return z
>>>
>>> @ms_function(input_signature=(MetaTensor(mindspore.float32, (1, 1, 3, 3)),
>>>                               MetaTensor(mindspore.float32, (1, 1, 3, 3))))
>>> def tensor_add_with_sig(x, y):
>>>     z = F.tensor_add(x, y)
>>>     return z
>>>
>>> x = Tensor(np.ones([1, 1, 3, 3]).astype(np.float32))
>>> y = Tensor(np.ones([1, 1, 3, 3]).astype(np.float32))
>>>
>>> tensor_add_graph = ms_function(fn=tensor_add)
>>> out = tensor_add_graph(x, y)
>>> out = tensor_add_with_dec(x, y)
>>> out = tensor_add_with_sig(x, y)
mindspore.pytype_to_dtype(obj)[source]

Convert python type to MindSpore type.

Parameters

obj (type) – A python type object.

Returns

Type of MindSpore type.

mindspore.set_seed(seed)[source]

Set global random seed.

Note

The global seed is used by numpy.random, mindspore.common.Initializer, mindspore.ops.composite.random_ops and mindspore.nn.probability.distribution.

If global seed is not set, these packages will use their own default seed independently, numpy.random and mindspore.common.Initializer will choose a random seed, mindspore.ops.composite.random_ops and mindspore.nn.probability.distribution will use zero.

Seed set by numpy.random.seed() only used by numpy.random, while seed set by this API will also used by numpy.random, so just set all seed by this API is recommended.

Parameters

seed (int) – The seed to be set.

Raises