tinyms¶
Top-level reference to dtype of common module. This module also provides Numpy-like interfaces in TinyMS.
Examples
>>> import tinyms as ts
>>>
>>> print(ts.ones([2, 3]))
[[1. 1. 1.]
[1. 1. 1.]]
-
class
tinyms.
QuantDtype
[source]¶ An enum for quant datatype, contains INT1 ~ INT16, UINT1 ~ UINT16.
QuantDtype is defined in mindspore/common/dtype.py, use command below to import:
from mindspore import QuantDtype
-
tinyms.
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.
-
tinyms.
dtype_to_pytype
(type_)[source]¶ Convert MindSpore dtype to python data type.
- Parameters:
type_ (
mindspore.dtype
) – MindSpore’s dtype.- Returns:
Type of python.
-
tinyms.
pytype_to_dtype
(obj)[source]¶ Convert python type to MindSpore type.
- Parameters:
obj (type) – A python type object.
- Returns:
Type of MindSpore type.
- Raises:
NotImplementedError – If the python type cannot be converted to MindSpore type.
-
tinyms.
get_py_obj_dtype
(obj)[source]¶ Get the MindSpore data type, which corresponds to python type or variable.
- Parameters:
obj (type) – An object of python type, or a variable of python type.
- Returns:
Type of MindSpore type.
-
class
tinyms.
Tensor
(input_data=None, dtype=None, shape=None, init=None, internal=False, const_arg=False)[source]¶ Tensor is a data structure that stores an n-dimensional array.
- Parameters:
input_data (Union[Tensor, float, int, bool, tuple, list, numpy.ndarray]) – The data to be stored. It can be another Tensor, Python number or NumPy ndarray. Default: None.
dtype (
mindspore.dtype
) – Used to indicate the data type of the output Tensor. The argument should be defined in mindspore.dtype. If it is None, the data type of the output Tensor will be the same as the input_data. Default: None.shape (Union[tuple, list, int]) – Used to indicate the shape of the output Tensor. The argument should be a list of integers, a tuple of integers or an integer. If input_data is available, shape doesn’t need to be set. If None in shape, a tensor of dynamic shape is created, input_data doesn’t need to be set; if None not in shape, a tensor of static shape is created, input_data or init must be set. Default: None.
init (Initializer) – The information of init data. ‘init’ is used for delayed initialization in parallel mode. Usually, it is not recommended to use ‘init’ interface to initialize Tensor in the other conditions. If ‘init’ interface is used to initialize Tensor, the Tensor.init_data API needs to be called to convert Tensor to the actual data. Default: None.
internal (bool) – Whether it is created by the framework. ‘True’ means that the tensor is created by framework. ‘False’ means that the tensor is created by user. Default: False
const_arg (bool) – Whether the tensor is a constant when it is used for the argument of a network. Default: False.
- Outputs:
Tensor.
Note
The default value None of input_data works as a placeholder, it does not mean that we can create a NoneType Tensor.
Examples
>>> import numpy as np >>> import mindspore as ms >>> from mindspore import Tensor >>> from mindspore.common.initializer import One >>> # initialize a tensor with numpy.ndarray >>> t1 = Tensor(np.zeros([1, 2, 3]), ms.float32) >>> print(t1) [[[0. 0. 0.] [0. 0. 0.]]] >>> print(type(t1)) <class 'mindspore.common.tensor.Tensor'> >>> print(t1.shape) (1, 2, 3) >>> print(t1.dtype) Float32 >>> >>> # initialize a tensor with a float scalar >>> t2 = Tensor(0.1) >>> print(t2) 0.1 >>> print(type(t2)) <class 'mindspore.common.tensor.Tensor'> >>> print(t2.shape) () >>> print(t2.dtype) Float32 >>> >>> # initialize a tensor with a tuple >>> t3 = Tensor((1, 2)) >>> print(t3) [1 2] >>> print(type(t3)) <class 'mindspore.common.tensor.Tensor'> >>> print(t3.shape) (2,) >>> print(t3.dtype) Int64 ... >>> # initialize a tensor with init >>> t4 = Tensor(shape = (1, 3), dtype=ms.float32, init=One()) >>> print(t4) [[1. 1. 1.]] >>> print(type(t4)) <class 'mindspore.common.tensor.Tensor'> >>> print(t4.shape) (1, 3) >>> print(t4.dtype) Float32
-
property
H
¶ Returns a view of a matrix (2-D tensor) conjugated and transposed. x.H is equivalent to mindspore.Tensor.swapaxes(0, 1).conj() for complex matrices and mindspore.Tensor.swapaxes(0, 1) for real matrices.
-
property
T
¶ Return the transposed tensor.
-
addbmm
(batch1, batch2, *, beta=1, alpha=1)[source]¶ For details, please refer to
mindspore.ops.addbmm()
.
-
amax
(axis=None, keepdims=False, *, initial=None, where=None)[source]¶ For details, please refer to
mindspore.ops.amax()
.
-
amin
(axis=None, keepdims=False, *, initial=None, where=None)[source]¶ For details, please refer to
mindspore.ops.amin()
.
-
approximate_equal
(other, tolerance=1e-05)[source]¶ For details, please refer to
mindspore.ops.approximate_equal()
.
-
argmax_with_value
(axis=0, keep_dims=False)[source]¶ Returns the maximum value with corresponding index.
Compute the max value of input Tensor on the specified axis, and return the max value and index.
Note
In auto_parallel and semi_auto_parallel mode, the first output index can not be used.
Warning
If there are multiple maximum values, the index of the first maximum value is used.
The value range of axis is [-dims, dims - 1]. dims is the dimension length of this tensor.
- Parameters:
- Returns:
tuple (Tensor), tuple of 2 tensors, containing the corresponding index and the maximum value of the input tensor.
index (Tensor) - The index for the maximum value of the input tensor. If keep_dims is true, the shape of output tensors is \((x_1, x_2, ..., x_{axis-1}, 1, x_{axis+1}, ..., x_N)\). Otherwise, the shape is \((x_1, x_2, ..., x_{axis-1}, x_{axis+1}, ..., x_N)\) .
value (Tensor) - The maximum value of input tensor, with the same shape as index.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([0.0, 0.4, 0.6, 0.7, 0.1]), mindspore.float32) >>> output, index = x.argmax_with_value() >>> print(output, index) 0.7 3 >>> output, index = x.argmax_with_value(keep_dims=True) >>> print(output, index) [0.7] [3]
-
argmin_with_value
(axis=0, keep_dims=False)[source]¶ Returns the minimum value with corresponding index.
Note
In auto_parallel and semi_auto_parallel mode, the first output index can not be used.
Warning
If there are multiple minimum values, the index of the first minimum value is used.
The value range of axis is [-dims, dims - 1]. dims is the dimension length of this tensor.
- Parameters:
- Returns:
tuple (Tensor), tuple of 2 tensors, containing the corresponding index and the minimum value of the input tensor.
index (Tensor) - The index for the minimum value of the input tensor. If keep_dims is true, the shape of output tensors is \((x_1, x_2, ..., x_{axis-1}, 1, x_{axis+1}, ..., x_N)\). Otherwise, the shape is \((x_1, x_2, ..., x_{axis-1}, x_{axis+1}, ..., x_N)\) .
value (Tensor) - The minimum value of input tensor, with the same shape as index.
- Raises:
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([0.0, 0.4, 0.6, 0.7, 0.1]), mindspore.float32) >>> output, index = x.argmin_with_value() >>> print(output, index 0.0 0 >>> output, index = x.argmin_with_value(keep_dims=True) >>> print(output, index) [0.0] [0]
-
asnumpy
()[source]¶ Convert tensor to numpy array. Returns self tensor as a NumPy ndarray. This tensor and the returned ndarray share the same underlying storage. Changes to self tensor will be reflected in the ndarray.
- Returns:
A numpy ndarray which shares the same underlying storage with the tensor.
Examples
>>> from mindspore import Tensor >>> import numpy as np >>> x = Tensor(np.array([1, 2], dtype=np.float32)) >>> y = x.asnumpy() >>> y[0] = 11 >>> print(x) [11. 2.] >>> print(y) [11. 2.]
-
asnumpy_of_slice_persistent_data
(param_key, slice_index)[source]¶ Convert a slice of tensor data to numpy array. A slice is part of tensor data. Returns as a NumPy ndarray. This slice tensor data and the returned ndarray share the same underlying storage. Changes to self tensor will be reflected in the ndarray.
- Returns:
A numpy ndarray which shares the same underlying storage with the slice of tensor data.
-
assign_value
(value)[source]¶ Assign another tensor value to this tensor.
- Parameters:
value (Tensor) – Tensor for assignment.
- Returns:
Tensor, Tensor that’s been assigned.
-
assign_value_cpp
(self: mindspore._c_expression.Tensor, arg0: mindspore._c_expression.Tensor) → mindspore._c_expression.Tensor¶ Assign another tensor value to this.
- Arg:
value (
mindspore.tensor
): The value tensor.
Examples
>>> data = mindspore.Tensor(np.ones((1, 2), np.float32)) >>> data2 = mindspore.Tensor(np.ones((2, 2), np.float32)) >>> data.assign_value(data2) >>> data.shape (2, 2)
-
astype
(dtype, copy=True)[source]¶ Return a copy of the tensor, cast to a specified type.
- Parameters:
dtype (Union[
mindspore.dtype
, numpy.dtype, str]) – Designated tensor dtype, can be in format of mindspore.dtype.float32 or numpy.float32 or float32.copy (bool, optional) – By default, astype always returns a newly allocated tensor. If this is set to false, the input tensor is returned instead of a copy. Default: True.
- Returns:
Tensor, with the designated dtype.
- Raises:
TypeError – If the specified dtype cannot be understood.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.ones((1,2,2,1), dtype=np.float32)) >>> x = x.astype("int32") >>> print(x.dtype) Int32
-
baddbmm
(batch1, batch2, beta=1, alpha=1)[source]¶ For details, please refer to
mindspore.ops.baddbmm()
.
-
bitwise_right_shift
(other)[source]¶ For details, please refer to
mindspore.ops.bitwise_right_shift()
.
-
bool
()[source]¶ Converts input tensor dtype to bool. If the value in tensor is zero, it will be False, otherwise it will be True.
- Returns:
Tensor, converted to the bool dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.ones([2,2]), mindspore.float32) >>> output = input_x.bool() >>> print(output.dtype) Bool
-
cholesky_inverse
(upper=False)[source]¶ For details, please refer to
mindspore.ops.cholesky_inverse()
.
-
choose
(choices, mode='clip')[source]¶ Construct a tensor from an index tensor and a list of tensors to choose from.
- Parameters:
choices (Union[tuple, list, Tensor]) – Choice tensors. The input tensor and all of the choices must be broadcasted to the same shape. If choices is itself a tensor, then its outermost dimension (i.e., the one corresponding to
choices.shape[0]
) is taken as defining the “sequence”.mode ('raise', 'wrap', 'clip', optional) –
Specifies how indices outside
[0, n-1]
will be treated:raise: Raises an error;
wrap: Wraps around;
clip: Clips to the range. ‘clip’ mode means that values greater than n-1 are mapped to n-1. Note that this disables indexing with negative numbers.
Default: ‘clip’.
- Returns:
Tensor, the merged result.
- Raises:
ValueError – If the input tensor and any of the choices cannot be broadcast.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> choices = [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]] >>> x = Tensor(np.array([2, 3, 1, 0])) >>> print(x.choose(choices)) [20 31 12 3]
-
col2im
(output_size, kernel_size, dilation, padding_value, stride)[source]¶ For details, please refer to
mindspore.ops.col2im()
.
-
copy
()[source]¶ Return a copy of the tensor.
Note
The current implementation does not support order argument.
- Returns:
Copied tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> a = Tensor(np.ones((3,3)).astype("float32")) >>> output = a.copy() >>> print(output) [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]]
-
cov
(*, correction=1, fweights=None, aweights=None)[source]¶ For details, please refer to
mindspore.ops.cov()
.
-
data_sync
(self: mindspore._c_expression.Tensor, arg0: bool) → None¶
-
diagonal
(offset=0, axis1=0, axis2=1)[source]¶ For details, please refer to
mindspore.ops.diagonal()
.
-
diff
(n=1, axis=-1, prepend=None, append=None)[source]¶ For details, please refer to
mindspore.ops.diff()
.
-
dim
(self: mindspore._c_expression.Tensor) → int¶ Get tensor’s data dimension.
- Returns:
int, the dimension of tensor.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> data.dim() 2
-
property
dtype
¶ Return the dtype of the tensor (
mindspore.dtype
).
-
expand_as
(x)[source]¶ Expand the dimension of target tensor to the dimension of input tensor.
- Parameters:
x (Tensor) – The input tensor. The shape of the input tensor must obey the broadcasting rule.
- Returns:
Tensor, has the same dimension as input tensor.
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> from mindspore import dtype as mstype >>> x = Tensor([1, 2, 3], dtype=mstype.float32) >>> y = Tensor(np.ones((2, 3)), dtype=mstype.float32) >>> output = x.expand_as(y) >>> print(output) [[1. 2. 3.] [1. 2. 3.]]
-
flatten
(order='C', *, start_dim=0, end_dim=-1)[source]¶ For details, please refer to
mindspore.ops.flatten()
.
-
float
()[source]¶ Converts input tensor dtype to float32.
- Returns:
Tensor, converted to the float32 dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.ones([2,2]), mindspore.int32) >>> output = input_x.float() >>> print(output.dtype) Float32
-
flush_from_cache
()[source]¶ Flush cache data to host if tensor is cache enable.
Examples
>>> from mindspore import Tensor >>> import numpy as np >>> x = Tensor(np.array([1, 2], dtype=np.float32)) >>> y = x.flush_from_cache() >>> print(y) None
-
fold
(output_size, kernel_size, dilation=1, padding=0, stride=1)[source]¶ For details, please refer to
mindspore.ops.fold()
.
-
static
from_numpy
(array)[source]¶ Convert numpy array to Tensor. If the data is not C contiguous, the data will be copied to C contiguous to construct the tensor. Otherwise, The tensor will be constructed using this numpy array without copy.
- Parameters:
array (numpy.array) – The input array.
- Returns:
Tensor, has the same data type as input array.
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = np.array([1, 2]) >>> output = Tensor.from_numpy(x) >>> print(output) [1 2]
-
gather
(input_indices, axis, batch_dims=0)[source]¶ For details, please refer to
mindspore.ops.gather()
.
-
getitem_index_info
(self: object, arg0: object, arg1: bool_) → object¶
-
half
()[source]¶ Converts input tensor dtype to float16.
- Returns:
Tensor, converted to the float16 dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.ones([2,2]), mindspore.int32) >>> output = input_x.half() >>> print(output.dtype) Float16
-
property
has_init
¶ Whether tensor is initialized.
-
imag
()[source]¶ Returns a new tensor containing imaginary value of the input tensor. If input tensor is real, it will return zeros.
- Returns:
Tensor, the shape is the same as the input tensor.
- Supported Platforms:
GPU
CPU
Examples
>>> x = Tensor(np.asarray(np.complex(1.3 + 0.4j)), mindspore.complex64) >>> output = x.imag() >>> print(output) 0.4
-
index_add
(dim, index, source, *, alpha=1)[source]¶ For details, please refer to
mindspore.ops.index_add()
.
-
init_data
(slice_index=None, shape=None, opt_shard_group=None)[source]¶ Get the tensor format data of this Tensor.
Note
The init_data function can be called once for the same tensor.
- Parameters:
slice_index (int) – Slice index of a parameter’s slices. It is used when initialize a slice of a parameter, it guarantees that devices using the same slice can generate the same tensor. Default: None.
shape (list[int]) – Shape of the slice, it is used when initialize a slice of the parameter. Default: None.
opt_shard_group (str) – Optimizer shard group which is used in auto or semi auto parallel mode to get one shard of a parameter’s slice. Default: None.
- Returns:
Initialized Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> from mindspore.common.initializer import initializer, Constant >>> x = initializer(Constant(1), [2, 2], ms.float32) >>> out = x.init_data() >>> print(out) [[1. 1.] [1. 1.]]
-
int
()[source]¶ Converts input tensor dtype to int32. If the value in tensor is float or half, the decimal will be discarded.
- Returns:
Tensor, converted to the int32 dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.ones([2,2]), mindspore.float32) >>> output = input_x.int() >>> print(output.dtype) Int32
-
is_init
(self: mindspore._c_expression.Tensor) → bool¶ Get tensor init_flag.
- Returns:
bool, whether the tensor init.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> data.is_init() False
-
is_persistent_data
()[source]¶ Check if size of tensor is huge, and need save data to persistent storage. If size of tensor is bigger then MS_EMBEDDING_REMOTE_CACHE_MEMORY_SIZE, it will use persistent storage to save tensor data. And will spilt data to some slice.
- Returns:
True or False
-
is_signed
()[source]¶ Judge whether the data type of tensor is a signed data type.
- Returns:
Bool. If the dtype of self is a signed data type, return True. Otherwise, return False.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> from mindspore import Tensor >>> x = ms.Tensor([1, 2, 3], ms.int64) >>> y = ms.Tensor([1, 2, 3], ms.uint64) >>> output = x.is_signed() >>> output2 = y.is_signed() >>> print(output) True >>> print(output2) False
-
isclose
(x2, rtol=1e-05, atol=1e-08, equal_nan=False)[source]¶ For details, please refer to
mindspore.ops.isclose()
.
-
item
(index=None)[source]¶ Get the item at the specified index of the tensor.
Note
Tensor.item returns a Tensor scalar instead of a Python scalar.
- Parameters:
index (Union[None, int, tuple(int)]) – The index in Tensor. Default: None.
- Returns:
A Tensor scalar, dtype is the same with the original Tensor.
- Raises:
ValueError – If the length of the index is not equal to self.ndim.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.array([[1,2,3],[4,5,6]], dtype=np.float32)) >>> x = x.item((0,1)) >>> print(x) 2.0
-
itemset
(*args)[source]¶ Insert scalar into a tensor (scalar is cast to tensor’s dtype, if possible).
There must be at least 1 argument, and define the last argument as item. Then, tensor.itemset(*args) is equivalent to \(Tensor[args] = item\).
- Parameters:
args (Union[(numbers.Number), (int/tuple(int), numbers.Number)]) – The arguments that specify the index and value. If args contain one argument (a scalar), it is only used in case tensor is of size 1. If args contain two arguments, the last argument is the value to be set and must be a scalar, the first argument specifies a single tensor element location. It is either an int or a tuple.
- Returns:
A new tensor that doesn’t affect the original tensor, with value set by \(Tensor[args] = item\).
- Raises:
ValueError – If the length of the first argument is not equal to self.ndim.
IndexError – If only one argument is provided, and the original Tensor is not scalar.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.array([[1,2,3],[4,5,6]], dtype=np.float32)) >>> print(x.itemset((0,1), 4)) [[1. 4. 3.] [4. 5. 6.]] >>> print(x) [[1. 2. 3.] [4. 5. 6.]]
-
property
itemsize
¶ Return the length of one tensor element in bytes.
-
log_matrix_determinant
()[source]¶ For details, please refer to
mindspore.ops.log_matrix_determinant()
.
-
long
()[source]¶ Converts input tensor dtype to int64. If the value in tensor is float or half, the decimal will be discarded.
- Returns:
Tensor, converted to the int64 dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_x = Tensor(np.ones([2,2]), mindspore.int32) >>> output = input_x.long() >>> print(output.dtype) Int64
-
property
mH
¶ Accessing this property is equivalent to Calling self.adjoint(). For details, please refer to
mindspore.ops.adjoint()
.
-
property
mT
¶ Returns the Tensor that exchanges the last two dimensions. Accessing the attribute, x.mT, is equal to calling the method, x.swapaxes(-2, -1). For details, please refer to
mindspore.Tensor.swapaxes()
.
-
max
(axis=None, keepdims=False, *, initial=None, where=True, return_indices=False)[source]¶ Return the maximum of a tensor or maximum along an axis.
- Parameters:
axis (Union[None, int, list, tuple of ints], optional) – Axis or axes along which to operate. By default, flattened input is used. If this is a tuple of ints, the maximum is selected over multiple axes, instead of a single axis or all the axes as before. Default: None.
keepdims (bool, optional) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. Default: False.
- Keyword Arguments:
initial (scalar, optional) – The minimum value of an output element. Must be present to allow computation on empty slice. Default: None.
where (Tensor[bool], optional) – A boolean tensor which is broadcasted to match the dimensions of array, and selects elements to include in the reduction. If non-default value is passed, initial must also be provided. Default: True.
return_indices (bool, optional) – Whether to return the index of the maximum value. Default: False. If axis is a list or tuple of ints, it must be False.
- Returns:
Tensor or scalar, maximum of input tensor. If axis is None, the result is a scalar value. If axis is given, the result is a tensor of dimension
self.ndim - 1
.- Raises:
TypeError – If arguments have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.argmin()
: Return the indices of the minimum values along an axis.mindspore.Tensor.argmax()
: Return the indices of the maximum values along an axis.mindspore.Tensor.min()
: Return the minimum of a tensor or minimum along an axis.Examples
>>> import numpy as np >>> from mindspore import Tensor >>> a = Tensor(np.arange(4).reshape((2, 2)).astype('float32')) >>> output = a.max() >>> print(output) 3.0 >>> value, indices = a.max(axis=0, return_indices=True) >>> print(value) [2. 3.] >>> print(indices) [1 1]
-
min
(axis=None, keepdims=False, *, initial=None, where=True, return_indices=False)[source]¶ Return the minimum of a tensor or minimum along an axis.
- Parameters:
axis (Union[None, int, list, tuple of ints], optional) – An axis or axes along which to operate. By default, flattened input is used. If axis is a tuple of ints, the minimum is selected over multiple axes, instead of a single axis or all the axes as before. Default: None.
keepdims (bool, optional) – If True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. Default: False.
- Keyword Arguments:
initial (scalar, optional) – The minimum value of an output element. Must be present to allow computation on empty slice. Default: None.
where (bool Tensor, optional) – A boolean tensor which is broadcasted to match the dimensions of array, and selects elements to include in the reduction. If non-default value is passed, initial must also be provided. Default: True.
return_indices (bool, optional) – Whether to return the index of the minimum value. Default: False. If axis is a list or tuple of ints, it must be False.
- Returns:
Tensor or scalar, minimum of input tensor. If axis is None, the result is a scalar value. If axis is given, the result is a tensor of dimension
self.ndim - 1
.- Raises:
TypeError – If arguments have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.argmin()
: Return the indices of the minimum values along an axis.mindspore.Tensor.argmax()
: Return the indices of the maximum values along an axis.mindspore.Tensor.max()
: Return the minimum of a tensor or minimum along an axis.Examples
>>> import numpy as np >>> from mindspore import Tensor >>> a = Tensor(np.arange(4).reshape((2, 2)).astype('float32')) >>> output = a.min() >>> print(output) 0.0 >>> output = a.min(axis=0) >>> print(output) [0. 1.] >>> output = a.min(axis=0, initial=9, where=Tensor([False])) >>> print(output) [9. 9.] >>> output = a.min(axis=0, initial=9, where=Tensor([False, True])) >>> print(output) [9. 1.] >>> value, indices = a.min(axis=0, return_indices=True) >>> print(value) [0. 1.] >>> print(indices) [0 0]
-
multinomial
(num_samples, replacement=True, seed=None)[source]¶ For details, please refer to
mindspore.ops.multinomial()
.
-
nan_to_num
(nan=0.0, posinf=None, neginf=None)[source]¶ For details, please refer to
mindspore.ops.nan_to_num()
.
-
nansum
(axis=None, keepdims=False, dtype=None)[source]¶ For details, please refer to
mindspore.ops.nansum()
.
-
property
nbytes
¶ Return the total number of bytes taken by the tensor.
-
property
ndim
¶ Return the number of tensor dimensions.
-
new_ones
(size, *, dtype=None)[source]¶ Return a tensor of size filled with ones.
- Parameters:
size (Union[int, tuple, list]) – An int, list or tuple of integers defining the output shape.
- Keyword Arguments:
dtype (mindspore.dtype, optional) – The desired dtype of the output tensor. If None, the returned tensor has the same dtype as self. Default: None.
- Returns:
Tensor, the shape and dtype is defined above and filled with ones.
- Raises:
TypeError – If size is not an int, list or tuple of integers.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> output = x.new_ones((2, 2)) >>> print(output) [[1. 1.] [1. 1.]]
-
new_zeros
(size, *, dtype=None)[source]¶ Return a tensor of size filled with zeros.
- Parameters:
size (Union[int, tuple, list]) – An int, list or tuple of integers defining the output shape.
- Keyword Arguments:
dtype (mindspore.dtype, optional) – The desired dtype of the output tensor. If None, the returned tensor has thesame dtype as self. Default: None.
- Returns:
Tensor, the shape and dtype is defined above and filled with zeros.
- Raises:
TypeError – If size is not an int, list or tuple of integers.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> output = x.new_zeros((2, 2)) >>> print(output) [[0. 0.] [0. 0.]]
-
norm
(ord=None, dim=None, keepdim=False, *, dtype=None)[source]¶ For details, please refer to
mindspore.ops.norm()
.
-
persistent_data_from_numpy
(self: array, arg0: int_) → mindspore._c_expression.Tensor¶ Creates a Tensor from a numpy.ndarray without copy. Use persistent data tensor.
- Arg:
array (numpy.ndarray): The input ndarray. slice_num (int): The slice num of persistent data tensor.
- Returns:
Tensor, tensor with shared data to input ndarray.
Examples
>>> a = np.ones((2, 3)) >>> t = mindspore.Tensor.persistent_data_from_numpy(a, 1)
-
ptp
(axis=None, keepdims=False)[source]¶ The name of the function comes from the acronym for “peak to peak”. Calculate the difference between the maximum value and the minimum value along the axis.
Note
Numpy argument out is not supported.
- Parameters:
axis (Union[None, int, tuple(int)]) – Axis or axes along which the range is computed. The default is to compute the variance of the flattened tensor. Default: None.
keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the tensor. Default is False.
- Returns:
Tensor.
- Raises:
TypeError – If self is not a tensor, or axis and keepdims have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> from mindspore import Tensor >>> x = Tensor([[4.0, 9.0, 2.0, 10.0], [6.0, 9.0, 7.0, 12.0]]).astype("float32") >>> print(x.ptp(axis=1)) [8. 6.] >>> print(x.ptp(axis=0)) [2. 0. 5. 2.]
-
random_categorical
(num_sample, seed=0, dtype=mindspore.int64)[source]¶ For details, please refer to
mindspore.ops.random_categorical()
.
-
ravel
()[source]¶ Return a contiguous flattened tensor.
- Returns:
Tensor, a 1-D tensor, containing the same elements of the input.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.reshape()
: Give a new shape to a tensor without changing its data.mindspore.Tensor.flatten()
: Return a copy of the tensor collapsed into one dimension.Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.ones((2,3,4), dtype=np.float32)) >>> output = x.ravel() >>> print(output.shape) (24,)
-
repeat
(repeats, axis=None)[source]¶ Repeat elements of a tensor.
- Parameters:
- Returns:
Tensor, has the same shape as input tensor except along the given axis.
- Raises:
ValueError – If the axis is out of range.
TypeError – If arguments have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.reshape()
: Give a new shape to a tensor without changing its data.mindspore.Tensor.resize()
: Changes shape and size of tensor in-place.Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.array(3)) >>> print(x.repeat(4)) [3 3 3 3] >>> x = Tensor(np.array([[1, 2],[3, 4]])) >>> print(x.repeat(2)) [1 1 2 2 3 3 4 4] >>> print(x.repeat(3, axis=1)) [[1 1 1 2 2 2] [3 3 3 4 4 4]] >>> print(x.repeat([1,2], axis=0)) [[1 2] [3 4] [3 4]]
-
repeat_interleave
(repeats, dim=None)[source]¶ For details, please refer to
mindspore.ops.repeat_interleave()
.
-
reshape_as
(other)[source]¶ Change the shape of the Tensor to the shape of other without changing the data.
- Parameters:
other (Tensor) – The result tensor has the same shape as other.
- Returns:
Tensor, has the same shape as other.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> from mindspore import Tensor >>> import numpy as np >>> x = Tensor([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]], dtype=ms.float32) >>> y = Tensor(np.arange(6).reshape(3,2)) >>> output = x.reshape_as(y) >>> print(output) [[-0.1 0.3] [ 3.6 0.4] [ 0.5 -3.2]]
-
resize
(*new_shape)[source]¶ Changes shape and size of tensor in-place.
If the shape of the new tensor is larger than the shape of the original tensor, the new tensor will be filled with 0. And if the shape of the new tensor is smaller than the shape of the original tensor, the new tensor is filled with the elements of the original tensor in order.
Note
Instead of changing the size of the input tensor and returns nothing as in numpy, this method returns a new Tensor with the input size. Numpy argument refcheck is not supported.
- Parameters:
new_shape (Union[ints, tuple of ints]) – Shape of resized tensor.
- Returns:
Tensor.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.reshape()
: Give a new shape to a tensor without changing its data.mindspore.Tensor.repeat()
: Repeat elements of a tensor.Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)) >>> y = x.resize(3, 3) >>> print(y) [[1. 2. 3.] [4. 5. 6.] [0. 0. 0.]] >>> y = x.resize(2, 2) >>> print(y) [[1. 2.] [3. 4.]]
-
reverse_sequence
(seq_lengths, seq_dim=0, batch_dim=0)[source]¶ For details, please refer to
mindspore.ops.reverse_sequence()
.
-
scatter_sub
(indices, updates)[source]¶ Creates a new tensor by subtracting the values from the positions in self tensor indicated by indices, with values from updates. When multiple values are provided for the same index, the result of the update will be to subtract these values respectively. This operation is almost equivalent to using
mindspore.ops.ScatterNdSub
, except that the updates are applied on output Tensor instead of input Parameter.The last axis of indices is the depth of each index vectors. For each index vector, there must be a corresponding value in updates. The shape of updates should be equal to the shape of self[indices]. For more details, see use cases.
Note
On GPU, if some values of the indices are out of bound, instead of raising an index error, the corresponding updates will not be updated to self tensor. On CPU, if some values of the indices are out of bound, raising an index error. On Ascend, out of bound checking is not supported, if some values of the indices are out of bound, unknown errors may be caused.
- Parameters:
- Returns:
Tensor, has the same shape and type as self tensor.
- Raises:
TypeError – If dtype of indices is neither int32 nor int64.
ValueError – If length of shape of self tensor is less than the last dimension of shape of indices.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]).astype('float32')) >>> indices = Tensor(np.array([[0, 0], [0, 0]]).astype('int32')) >>> updates = Tensor(np.array([1.0, 2.2]).astype('float32')) >>> output = x.scatter_sub(indices, updates) >>> print(output) [[-3.3000002 0.3 3.6 ] [ 0.4 0.5 -3.2 ]]
-
searchsorted
(v, side='left', sorter=None)[source]¶ Finds indices where elements should be inserted to maintain order.
- Parameters:
v (Union[int, float, bool, list, tuple, Tensor]) – Values to insert into the tensor.
side (str, optional) – If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index. If there is no suitable index, return either 0 or N (where N is the length of the tensor). Default: ‘left’.
sorter (Union[int, float, bool, list, tuple, Tensor]) – 1-D optional tensor of integer indices that sort the tensor into ascending order. They are typically the result of argsort. Default: None.
- Returns:
Tensor, array of insertion points with the same shape as v.
- Raises:
ValueError – If argument for side or sorter is invalid.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.array([1, 2, 3, 4, 5])) >>> print(x.searchsorted(3)) 2
-
set_cast_dtype
(self: mindspore._c_expression.Tensor, dtype: mindspore._c_expression.typing.Type = None) → None¶
-
set_const_arg
(const_arg=True)[source]¶ Specify whether the tensor is a constant when it is used for the argument of a network.
- Parameters:
const_arg (bool) – Whether the tensor is a constant when it is used for the argument of a network. Default: True.
- Returns:
Tensor, has been specified whether to be a const network argument.
- Raises:
TypeError – If const_arg is not a bool.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.array([[1,2,3],[4,5,6]], dtype=np.float32)) >>> x.set_const_arg(True)
-
set_dtype
(self: mindspore._c_expression.Tensor, arg0: mindspore._c_expression.typing.Type) → mindspore._c_expression.typing.Type¶ Set the tensor’s data type.
- Arg:
dtype (
mindspore.dtype
): The type of output tensor.
Examples
>>> data = mindspore.Tensor(np.ones((1, 2), np.float32)) >>> data.set_dtype(mindspore.int32) mindspore.int32
-
set_init_flag
(self: mindspore._c_expression.Tensor, arg0: bool) → None¶ Set tensor init_flag.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> data.set_init_flag(True)
-
setitem_index_info
(self: object, arg0: object, arg1: object, arg2: bool_) → object¶
-
property
shape
¶ For details, please refer to
mindspore.ops.shape()
.
-
short
()[source]¶ Return a copy of the tensor, cast to int16 type, equivalent to self.astype(mstype.int16). If the value in tensor is float or half, the decimal will be discarded. For details, please refer to
mindspore.Tensor.astype()
.- Returns:
Tensor, converted to the int16 dtype.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> import numpy as np >>> x = ms.Tensor(np.array([1,2,3,4,5]), ms.int32) >>> output = x.short() >>> output Tensor(shape=[5], dtype=Int16, value= [1, 2, 3, 4, 5])
-
property
size
¶ For details, please refer to
mindspore.ops.size()
.
-
slice_num_of_persistent_data
()[source]¶ Get slice num of a tensor which use persistent storage.
- Returns:
Num of slice.
-
slice_shape_of_persistent_data
()[source]¶ Get slice shape of tensor after cut to slice size.
- Returns:
The slice shape of tensor.
-
property
strides
¶ Return the tuple of bytes to step in each dimension when traversing a tensor.
-
sum
(axis=None, dtype=None, keepdims=False, initial=None)[source]¶ Return sum of tensor elements over a given axis.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
axis (Union[None, int, tuple(int)]) – Axis or axes along which a sum is performed. Default: None. If None, sum all the elements of the input tensor. If the axis is negative, it counts from the last to the first axis. If the axis is a tuple of ints, a sum is performed on all the axes specified in the tuple instead of a single axis or all the axes as before.
dtype (
mindspore.dtype
, optional) – defaults to None. Overrides the dtype of the output Tensor.keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however any non-default value will be. If the sub-class method does not implement keepdims any exceptions will be raised. Default: False.
initial (scalar) – Starting value for the sum. Default: None.
- Returns:
Tensor. A tensor with the same shape as input, with the specified axis removed. If the input tensor is a 0-d array, or if the axis is None, a scalar is returned.
- Raises:
TypeError – If input is not array_like, or axis is not int or tuple of ints, or keepdims is not integer, or initial is not scalar.
ValueError – If any axis is out of range or duplicate axes exist.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.cumsum()
: Return the cumulative sum of the elements along a given axis.Examples
>>> import numpy as np >>> from mindspore import Tensor >>> input_x = Tensor(np.array([-1, 0, 1]).astype(np.float32)) >>> print(input_x.sum()) 0.0 >>> input_x = Tensor(np.arange(10).reshape(2, 5).astype(np.float32)) >>> print(input_x.sum(axis=1)) [10. 35.]
-
sum_to_size
(*size)[source]¶ Sum self Tensor to the size. size must be expandable to the Tensor size.
- Parameters:
size (Union[tuple(int), int]) – The expected shape of output Tensor.
- Returns:
Tensor, the sum result of self Tensor according to the size.
- Raises:
ValueError – If size is not expandable to the size of self Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> x = Tensor(np.random.randn(3, 3, 3, 3, 3, 3), mindspore.float32) >>> output = x.sum_to_size((1, 3, 1, 3)) >>> print(output.shape) (1, 3, 1, 3)
-
svd
(full_matrices=False, compute_uv=True)[source]¶ For details, please refer to
mindspore.ops.svd()
.
-
take
(indices, axis=None, mode='clip')[source]¶ Takes elements from a tensor along an axis.
- Parameters:
indices (Tensor) – The indices with shape (Nj…) of the values to extract.
axis (int, optional) – The axis over which to select values. By default, the flattened input tensor is used. Default: None.
mode ('raise', 'wrap', 'clip', optional) –
raise: Raises an error;
wrap: Wraps around;
clip: Clips to the range. ‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.
Default: ‘clip’.
- Returns:
Tensor, the indexed result.
- Raises:
ValueError – If axis is out of range, or mode has values other than (‘raise’, ‘wrap’, ‘clip’)
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> a = Tensor(np.array([4, 3, 5, 7, 6, 8])) >>> indices = Tensor(np.array([0, 1, 4])) >>> output = a.take(indices) >>> print(output) [4 3 6]
-
tensor_split
(indices_or_sections, axis=0)[source]¶ For details, please refer to
mindspore.ops.tensor_split()
.
-
to
(dtype)[source]¶ Performs tensor dtype conversion.
- Parameters:
dtype (Number) – The valid data type of the output tensor. Only constant value is allowed.
- Returns:
Tensor, converted to the specified dtype.
- Raises:
TypeError – If dtype is not a Number.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> input_np = np.random.randn(2, 3, 4, 5).astype(np.float32) >>> input_x = Tensor(input_np) >>> dtype = mindspore.int32 >>> output = input_x.to(dtype) >>> print(output.dtype) Int32
-
to_coo
()[source]¶ Convert a Tensor to COOTensor.
Note
Only 2-D tensor is supported for now.
- Returns:
COOTensor, a sparse representation of the original dense tensor, containing the following parts.
indices (Tensor): 2-D integer tensor, indicates the positions of values of the dense tensor.
values (Tensor): 1-D tensor, indicates the non-zero values of the dense tensor.
shape (tuple(int)): the shape of the COOTensor, is the same as the original dense tensor.
- Raises:
ValueError – If input tensor is not 2-D.
- Supported Platforms:
GPU
Examples
>>> import numpy as np >>> import mindspore >>> from mindspore import Tensor >>> x = Tensor(np.array([[1, 0], [-5, 0]]), mindspore.float32) >>> output = x.to_coo() >>> print(output.indices, output.values, output.shape) [[0 0] [1 0]] [ 1. -5.] (2, 2)
-
to_csr
()[source]¶ Convert a Tensor to CSRTensor.
Note
Only 2-D tensor is supported for now.
- Returns:
CSRTensor, a sparse representation of the original dense tensor, containing the following parts.
indptr (Tensor): 1-D integer tensor, indicates the start and end point for values in each row.
indices (Tensor): 1-D integer tensor, indicates the column positions of all non-zero values of the input.
values (Tensor): 1-D tensor, indicates the non-zero values of the dense tensor.
shape (tuple(int)): the shape of the CSRTensor, is the same as the original dense tensor.
- Raises:
ValueError – If input tensor is not 2-D.
- Supported Platforms:
GPU
Examples
>>> import numpy as np >>> import mindspore >>> from mindspore import Tensor >>> x = Tensor(np.array([[1, 0], [-5, 0]]), mindspore.float32) >>> output = x.to_csr() >>> print(output.indptr, output.indices, output.values, output.shape) [0 1 2] [0 0] [ 1. -5.] (2, 2)
-
topk
(k, dim=None, largest=True, sorted=True)[source]¶ For details, please refer to
mindspore.ops.topk()
.
-
trace
(offset=0, axis1=0, axis2=1, dtype=None)[source]¶ Return the sum along diagonals of the tensor.
- Parameters:
offset (int, optional) – Offset of the diagonal from the main diagonal. Can be positive or negative. Defaults to main diagonal.
axis1 (int, optional) – Axis to be used as the first axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to first axis (0).
axis2 (int, optional) – Axis to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to second axis.
dtype (
mindspore.dtype
, optional) – defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor, the sum along diagonals.
- Raises:
ValueError – If the input tensor has less than two dimensions.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.diagonal()
: Return specified diagonals.Examples
>>> import numpy as np >>> from mindspore import Tensor >>> x = Tensor(np.eye(3, dtype=np.float32)) >>> print(x.trace()) 3.0
-
true_divide
(value)[source]¶ Alias for Tensor.div() with \(rounding\_mode=None\). For details, please refer to
mindspore.ops.div()
.
-
unfold
(kernel_size, dilation=1, padding=0, stride=1)[source]¶ For details, please refer to
mindspore.ops.unfold()
.
-
unique_consecutive
(return_idx=False, return_counts=False, axis=None)[source]¶ For details, please refer to
mindspore.ops.unique_consecutive()
.
-
unsorted_segment_max
(segment_ids, num_segments)[source]¶ For details, please refer to
mindspore.ops.unsorted_segment_max()
.
-
unsorted_segment_min
(segment_ids, num_segments)[source]¶ For details, please refer to
mindspore.ops.unsorted_segment_min()
.
-
unsorted_segment_prod
(segment_ids, num_segments)[source]¶ For details, please refer to
mindspore.ops.unsorted_segment_prod()
.
-
value
()[source]¶ Get the value of the tensor or the parameter.
- Returns:
The value of the tensor or the parameter.
Examples
>>> from mindspore import Tensor >>> import numpy as np >>> x = Tensor(np.array([1, 2], dtype=np.float32)) >>> x_value = x.value() >>> print(x_value) [1. 2.]
-
var
(axis=None, ddof=0, keepdims=False)[source]¶ Compute the variance along the specified axis.
The variance is the average of the squared deviations from the mean, i.e., \(var = mean(abs(x - x.mean())**2)\).
Return the variance, which is computed for the flattened array by default, otherwise over the specified axis.
Note
Numpy arguments dtype, out and where are not supported.
- Parameters:
axis (Union[None, int, tuple(int)]) – Axis or axes along which the variance is computed. The default is to compute the variance of the flattened array. Default: None.
ddof (int) – Means Delta Degrees of Freedom. Default: 0. The divisor used in calculations is \(N - ddof\), where \(N\) represents the number of elements.
keepdims (bool) – Default: False.
- Returns:
Variance tensor.
- Supported Platforms:
Ascend
GPU
CPU
See also
mindspore.Tensor.mean()
: Reduce a dimension of a tensor by averaging all elements in the dimension.mindspore.Tensor.std()
: Compute the standard deviation along the specified axis.Examples
>>> import numpy as np >>> from mindspore import Tensor >>> input_x = Tensor(np.array([1., 2., 3., 4.], np.float32)) >>> output = input_x.var() >>> print(output) 1.25
-
view
(*shape)[source]¶ Reshape the tensor according to the input shape. It’s the same as
mindspore.Tensor.reshape()
, implemented by the underlying reshape operator.- Parameters:
shape (Union[tuple(int), int]) – Dimension of the output tensor.
- Returns:
Tensor, which dimension is the input shape’s value.
Examples
>>> from mindspore import Tensor >>> import numpy as np >>> a = Tensor(np.array([[1, 2, 3], [2, 3, 4]], dtype=np.float32)) >>> output = a.view((3, 2)) >>> print(output) [[1. 2.] [3. 2.] [3. 4.]]
-
view_as
(other)[source]¶ View self Tensor as the same shape as other .
- Parameters:
other (Tensor) – The returned Tensor has the same shape as other.
- Returns:
Tensor, has the same shape as other.
- Raises:
TypeError – If other is not a Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> a = Tensor([[1, 2, 3], [2, 3, 4]], mstype.float32) >>> b = Tensor([1, 1, 1, 1, 1, 1], mstype.float32) >>> output = a.view_as(b) >>> print(output) [1. 2. 3. 2. 3. 4.]
-
class
tinyms.
RowTensor
(indices=None, values=None, shape=None, row_tensor=None)[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, :, :, :, …].
- For example, if indices is [0], values is [[1, 2]], shape is
\((3, 2)\) , then the dense representation of the row tensor will be:
[[1, 2], [0, 0], [0, 0]]
Warning
This is an experimental API that is subjected to change or deletion.
- Parameters:
indices (Tensor) – A 1-D integer Tensor of shape \((D0)\) . Default: None.
values (Tensor) – A Tensor of any dtype of shape \((D0, D1, ..., Dn)\) . Default: None.
shape (tuple(int)) – An integer tuple which contains the shape of the corresponding dense tensor. Default: None.
row_tensor (RowTensor) – A RowTensor object. Default: None.
- Returns:
RowTensor, composed of indices, values, and shape.
Examples
>>> import mindspore as ms >>> from mindspore import Tensor, RowTensor >>> indices = Tensor([0]) >>> values = Tensor([[1, 2]], dtype=ms.float32) >>> shape = (3, 2) >>> x = RowTensor(indices, values, shape) >>> print(x.values) [[1. 2.]] >>> print(x.indices) [0] >>> print(x.dense_shape) (3, 2)
-
property
dense_shape
¶ Return RowTensor’s shape.
-
property
indices
¶ Return RowTensor’s indices.
-
property
values
¶ Return RowTensor’s non-zero values.
-
class
tinyms.
SparseTensor
(indices, values, shape)[source]¶ A sparse representation of a set of nonzero elements from a tensor at given indices.
SparseTensor can only be used in the Cell’s construct method.
For a tensor dense, its SparseTensor(indices, values, dense_shape) has dense[indices[i]] = values[i].
For example, if indices is [[0, 1], [1, 2]], values is [1, 2], dense_shape is (3, 4), then the dense representation of the sparse tensor will be:
[[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 0]]
Note
The interface is deprecated from version 1.7 and will be removed in a future version. Please use ‘COOTensor’ instead.
- 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.
shape (tuple(int)) – An integer tuple of size ndims, which specifies the shape of the sparse tensor.
- Returns:
SparseTensor, composed of indices, values, and shape.
Examples
>>> import mindspore as ms >>> import mindspore.nn as nn >>> from mindspore import Tensor, SparseTensor >>> indices = Tensor([[0, 1], [1, 2]]) >>> values = Tensor([1, 2], dtype=ms.float32) >>> shape = (3, 4) >>> x = SparseTensor(indices, values, shape) >>> print(x.values) [1. 2.] >>> print(x.indices) [[0 1] [1 2]] >>> print(x.shape) (3, 4)
-
property
indices
¶ Return SparseTensor’s indices.
-
property
shape
¶ Return SparseTensor’s shape.
-
property
values
¶ Return SparseTensor’s non-zero values.
-
class
tinyms.
COOTensor
(indices=None, values=None, shape=None, coo_tensor=None)[source]¶ A sparse representation of a set of nonzero elements from a tensor at given indices.
For a tensor dense, its COOTensor(indices, values, shape) has dense[indices[i]] = values[i].
For example, if indices is [[0, 1], [1, 2]], values is [1, 2], shape is (3, 4), then the dense representation of the sparse tensor will be:
[[0, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 0]]
Common arithmetic operations include: addition (+), subtraction (-), multiplication (*), and division (/). For details about operations supported by COOTensor, see operators.
Warning
This is an experimental API that is subject to change or deletion.
Currently, duplicate coordinates in the indices will not be coalesced. If the indices contain out-of-bound values, the result will be undefined.
- 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 COOTensor, respectively. Currently, ndims must be 2. Please make sure that the indices are in range of the given shape.
values (Tensor) – A 1-D tensor of any type and shape \((N)\), which supplies the values for each element in indices.
shape (tuple(int)) – An integer tuple of size ndims, which specifies the dense_shape of the sparse tensor.
coo_tensor (COOTensor) – A COOTensor object.
- Returns:
COOTensor, composed of indices, values, and shape.
Examples
>>> import mindspore as ms >>> import mindspore.nn as nn >>> from mindspore import Tensor, COOTensor >>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32) >>> values = Tensor([1, 2], dtype=ms.float32) >>> shape = (3, 4) >>> x = COOTensor(indices, values, shape) >>> print(x.values) [1. 2.] >>> print(x.indices) [[0 1] [1 2]] >>> print(x.shape) (3, 4)
-
abs
() → mindspore.common.sparse_tensor.COOTensor[source]¶ Return absolute value element-wisely.
- Returns:
COOTensor.
- Supported Platforms:
Ascend
GPU
CPU
-
add
(other: mindspore.common.sparse_tensor.COOTensor, thresh: mindspore.common.tensor.Tensor) → mindspore.common.sparse_tensor.COOTensor[source]¶ Return the sum with another COOTensor.
- Parameters:
- Returns:
COOTensor, representing the sum.
- Raises:
ValueError – If any input(self/other)’s indices’s dim is not equal to 2.
ValueError – If any input(self/other)’s values’s dim is not equal to 1.
ValueError – If any input(self/other)’s shape’s dim is not equal to 1.
ValueError – If thresh’s dim is not equal to 0.
TypeError – If any input(self/other)’s indices’s type is not equal to int64.
TypeError – If any input(self/other)’s shape’s type is not equal to int64.
ValueError – If any input(self/other)’s indices’s length is not equal to its values’s length.
TypeError – If any input(self/other)’s values’s type is not equal to anf of (int8/int16/int32/int64/float32/float64/complex64/complex128)
TypeError – If thresh’s type is not equal to anf of (int8/int16/int32/int64/float32/float64)
TypeError – If self’s indices’s type is not equal to other’s indices’s type
TypeError – If self’s values’s type is not equal to other’s values’s type
TypeError – If self’s shape’s type is not equal to other’s shape’s type
TypeError – If (self/other)’s value’s type is not matched with thresh’s type
- Supported Platforms:
GPU
CPU
Examples
>>> from mindspore import Tensor, COOTensor >>> from mindspore import dtype as mstype >>> indics0 = Tensor([[0, 1], [1, 2]], dtype=mstype.int64) >>> values0 = Tensor([1, 2], dtype=mstype.int32) >>> shape0 = (3, 4) >>> input0 = COOTensor(indics0, values0, shape0) >>> indics1 = Tensor([[0, 0], [1, 1]], dtype=mstype.int64) >>> values1 = Tensor([3, 4], dtype=mstype.int32) >>> shape1 = (3, 4) >>> input1 = COOTensor(indics1, values1, shape1) >>> thres = Tensor(0, dtype=mstype.int32) >>> out = input0.add(input1, thres) >>> print(out) COOTensor(shape=[3, 4], dtype=Int32, indices=Tensor(shape=[4, 2], dtype=Int64, value= [[0 0] [0 1] [1 1] [1 2]]), values=Tensor(shape=[4], dtype=Int32, value=[3 1 4 2]))
-
astype
(dtype: mstype) → COOTensor[source]¶ Return a copy of the COOTensor, cast its values to a specified type.
- Parameters:
dtype (Union[
mindspore.dtype
, numpy.dtype, str]) – Designated tensor dtype.- Returns:
COOTensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> from mindspore import Tensor, COOTensor >>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32) >>> values = Tensor([1, 2], dtype=ms.float32) >>> shape = (3, 4) >>> coo_tensor = COOTensor(indices, values, shape) >>> print(coo_tensor.astype(ms.float64).dtype) Float64
-
coalesce
() → mindspore.common.sparse_tensor.COOTensor[source]¶ Returns a coalesced copy of an uncoalesced sparse tensor.
- Returns:
A COOTensor.
- Supported Platforms:
GPU
Examples
>>> import mindspore as ms >>> import mindspore.ops as ops >>> from mindspore import Tensor, COOTensor >>> x_indices = Tensor([[0, 0, 1], [1, 1, 2]], dtype=ms.int64) >>> x_values = Tensor([1, 5, 4], dtype=ms.float32) >>> x_shape = (3, 3) >>> coo_tensor = COOTensor(x_indices.transpose(), x_values, x_shape) >>> res = coo_tensor.coalesce() >>> print(res) COOTensor(shape=[3, 3], dtype=Float32, indices=Tensor(shape=[2, 2], dtype=Int64, value=[[0 1] [1 2]]), values=Tensor(shape=[2], dtype=Float32, value=[6.00000000e+00 4.00000000e+00]))
-
property
dtype
¶ Return the dtype of the values of COOTensor (
mindspore.dtype
).
-
property
indices
¶ Return COOTensor’s indices.
-
property
itemsize
¶ Return the length of one tensor element in bytes.
-
property
ndim
¶ Return the number of tensor dimensions.
-
property
shape
¶ Return COOTensor’s shape.
-
property
size
¶ Return the number of non-zero values.
-
to_csr
() → mindspore.common.sparse_tensor.CSRTensor[source]¶ Converts COOTensor to CSRTensor.
Note
Currently only supports CPU backend with LLVM 12.0.1 installed.
- Returns:
CSRTensor.
- Supported Platforms:
GPU
CPU
-
to_dense
() → mindspore.common.tensor.Tensor[source]¶ Converts COOTensor to Dense Tensor.
- Returns:
Tensor.
- Supported Platforms:
GPU
-
to_tuple
() → Tuple[mindspore.common.tensor.Tensor, mindspore.common.tensor.Tensor, Tuple[int, ...]][source]¶ Return indices, values and shape as a tuple.
- Returns:
Tuple.
- Supported Platforms:
Ascend
GPU
CPU
-
property
values
¶ Return COOTensor’s non-zero values.
-
class
tinyms.
CSRTensor
(indptr=None, indices=None, values=None, shape=None, csr_tensor=None)[source]¶ Constructs a sparse tensor in CSR (Compressed Sparse Row) format, with specified values indicated by values and row and column positions indicated by indptr and indices.
For example, if indptr is [0, 1, 2, 2], indices is [1, 2], values is [1., 2.], shape is (3, 4), then the dense representation of the sparse tensor will be:
[[0., 1., 0., 0.], [0., 0., 2., 0.], [0., 0., 0., 0.]]
Common arithmetic operations include: addition (+), subtraction (-), multiplication (*), and division (/). For details about operations supported by CSRTensor, see operators.
Warning
This is an experimental API that is subjected to change.
If the values given by indptr or indices are invalid, the results may be undefined. Invalid values include when the length of values or indices exceeds the range indicated by indptr, and when the columns indicated by indices are repeated on the same row.
- Parameters:
indptr (Tensor) – 1-D Tensor of shape \((M)\), which equals to shape[0] + 1, which indicates the start and end point for values in each row. Default: None. If provided, must be int16, int32 or int64.
indices (Tensor) – 1-D Tensor of shape \((N)\), which has the same length as values. indices indicates the which column values should be placed. Default: None. If provided, must be int16, int32 or int64.
values (Tensor) – Tensor, which has the same length as indices (values.shape[0] == indices.shape[0]). values stores the data for CSRTensor. Default: None.
shape (tuple(int)) – A tuple indicates the shape of the CSRTensor, and shape[0] must equal to M - 1, which all equal to number of rows of the CSRTensor. Default: None.
csr_tensor (CSRTensor) – A CSRTensor object. Values’ feature dimension should match with CSRTensor’s feature dimension (values.shape[1:] == csr_tensor.shape[2:]). Default: None.
- Outputs:
CSRTensor, with shape defined by shape, and dtype inferred from value.
Examples
>>> import mindspore as ms >>> from mindspore import Tensor, CSRTensor >>> # initialize a csr_tensor with indptr, indices, values and shape >>> indptr = Tensor([0, 1, 2], dtype=ms.int32) >>> indices = Tensor([0, 1], dtype=ms.int32) >>> values = Tensor([1, 2], dtype=ms.float32) >>> shape = (2, 4) >>> csr_tensor = CSRTensor(indptr, indices, values, shape) >>> # access a data member of CSRTensor >>> print(indptr == csr_tensor.indptr) [ True True True]
-
abs
() → mindspore.common.sparse_tensor.CSRTensor[source]¶ Return absolute value element-wisely.
- Returns:
CSRTensor, with all values being non-negative.
- Supported Platforms:
Ascend
GPU
CPU
-
add
(b: mindspore.common.sparse_tensor.CSRTensor, alpha: mindspore.common.tensor.Tensor, beta: mindspore.common.tensor.Tensor) → mindspore.common.sparse_tensor.CSRTensor[source]¶ Addition of two CSR Tensors : C = alpha * A + beta * B
- Parameters:
- Returns:
CSRTensor.
- Supported Platforms:
GPU
CPU
Examples
>>> from mindspore import Tensor, CSRTensor >>> import mindspore.common.dtype as mstype >>> indptr = Tensor([0, 1, 2], dtype=mstype.int32) >>> indices = Tensor([0, 1], dtype=mstype.int32) >>> values_a = Tensor([2, 1], dtype=mstype.float32) >>> values_b = Tensor([1, 2], dtype=mstype.float32) >>> dense_shape = (2, 4) >>> alpha = Tensor(1, mstype.float32) >>> beta = Tensor(1, mstype.float32) >>> a = CSRTensor(indptr, indices, values_a, dense_shape) >>> b = CSRTensor(indptr, indices, values_b, dense_shape) >>> print(a.add(b, alpha, beta)) CSRTensor(shape=[2,4], dtype=Float32, indptr=Tensor(shape=[3], dtype=Int32, value = [0, 1, 2]), indices=Tensor(shape=[2], dtype=Int32, value = [0, 1]), values=Tensor(shape=[2], dtype=Float32, value = [3.0, 3.0]))
-
astype
(dtype: mstype) → CSRTensor[source]¶ Return a copy of the CSRTensor, cast its values to a specified type.
- Parameters:
dtype (Union[
mindspore.dtype
, numpy.dtype, str]) – Designated tensor dtype.- Returns:
CSRTensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> from mindspore import Tensor, CSRTensor >>> indptr = Tensor([0, 1, 2], dtype=ms.int32) >>> indices = Tensor([0, 1], dtype=ms.int32) >>> values = Tensor([1, 2], dtype=ms.float32) >>> shape = (2, 4) >>> csr_tensor = CSRTensor(indptr, indices, values, shape) >>> print(csr_tensor.astype(ms.float64).dtype) Float64
-
property
dtype
¶ Return the dtype of the values of CSRTensor (
mindspore.dtype
).
-
property
indices
¶ Return CSRTensor’s column indices.
-
property
indptr
¶ Return CSRTensor’s row indices pointers.
-
property
itemsize
¶ Return the length of one tensor element in bytes.
-
mm
(matrix: Union[mindspore.common.tensor.Tensor, mindspore.common.sparse_tensor.CSRTensor]) → Union[mindspore.common.tensor.Tensor, mindspore.common.sparse_tensor.CSRTensor][source]¶ Return the matrix multiplication result of the right-multiply matrix(dense or CSRTensor) of the CSRTensor. The CSRTensor with shape [M, N] needs to adapt the right matrix with shape [N, K] to get the dense matrix or CSRTensor with result [M, K].
Note
If right matrix is CSRTensor, currently only supports GPU backend. if right matrix is Tensor, currently supports CPU backend with LLVM 12.0.1 or GPU backend.
- Parameters:
matrix (Tensor or CSRTensor) – A dense Tensor or CSRTensor, its shape[0] should be equal to csr_tensor.shape[1]
- Returns:
Tensor or CSRTensor.
- Supported Platforms:
GPU
CPU
Examples
>>> from mindspore import Tensor, CSRTensor >>> from mindspore import dtype as mstype >>> indptr = Tensor([0, 1, 2], dtype=mstype.int32) >>> indices = Tensor([0, 1], dtype=mstype.int32) >>> values = Tensor([2, 1], dtype=mstype.float32) >>> dense_shape = (2, 4) >>> csr_tensor = CSRTensor(indptr, indices, values, dense_shape) >>> dense_matrix = Tensor([[1., 2.], [1, 2.], [1, 2.], [1., 2.]], dtype=mstype.float32) >>> print(csr_tensor.mm(dense_matrix)) [[2. 4.] [1. 2.]]
-
mv
(dense_vector: mindspore.common.tensor.Tensor) → mindspore.common.tensor.Tensor[source]¶ Return the matrix multiplication result of the right-multiply dense matrix of the CSRTensor. The CSRTensor with shape [M, N] needs to adapt the dense vector with shape [N, 1] to get the dense vector with result [M, 1].
Note
Currently only supports CPU backend with LLVM 12.0.1 installed.
- Parameters:
dense_vector (Tensor) – A dense Tensor, its shape must be (csr_tensor.shape[1], 1)
- Returns:
Tensor.
- Supported Platforms:
GPU
CPU
Examples
>>> from mindspore import Tensor, CSRTensor >>> from mindspore import dtype as mstype >>> indptr = Tensor([0, 1, 2], dtype=mstype.int32) >>> indices = Tensor([0, 1], dtype=mstype.int32) >>> values = Tensor([2, 1], dtype=mstype.float32) >>> dense_shape = (2, 4) >>> csr_tensor = CSRTensor(indptr, indices, values, dense_shape) >>> dense = Tensor([[1], [1], [1], [1]], dtype=mstype.float32) >>> print(csr_tensor.mv(dense)) [[2.] [1.]]
-
property
ndim
¶ Return the number of tensor dimensions.
-
property
shape
¶ Return CSRTensor’s shape.
-
property
size
¶ Return the number of non-zero values.
-
sum
(axis: int) → mindspore.common.tensor.Tensor[source]¶ Reduces a dimension of a CSRTensor by summing all elements in the dimension.
Note
Currently only supports CPU backend with LLVM 12.0.1 installed.
- Parameters:
axis (int) – The dimensions to reduce.
- Returns:
Tensor, the dtype is the same as CSRTensor.values.
- Supported Platforms:
GPU
CPU
Examples
>>> from mindspore import Tensor, CSRTensor >>> from mindspore import dtype as mstype >>> indptr = Tensor([0, 1, 2], dtype=mstype.int32) >>> indices = Tensor([0, 1], dtype=mstype.int32) >>> values = Tensor([2, 1], dtype=mstype.float32) >>> dense_shape = (2, 4) >>> csr_tensor = CSRTensor(indptr, indices, values, dense_shape) >>> print(csr_tensor.sum(1)) [[2.] [1.]]
-
to_coo
() → mindspore.common.sparse_tensor.COOTensor[source]¶ Converts CSRTensor to COOTensor.
Note
Currently only supports CPU backend with LLVM 12.0.1 installed.
- Returns:
COOTensor.
- Supported Platforms:
GPU
CPU
-
to_dense
() → mindspore.common.tensor.Tensor[source]¶ Converts CSRTensor to Dense Tensor.
- Returns:
Tensor.
- Supported Platforms:
GPU
-
to_tuple
() → Tuple[mindspore.common.tensor.Tensor, mindspore.common.tensor.Tensor, mindspore.common.tensor.Tensor, Tuple[int, ...]][source]¶ Return indptr, indices, values and shape as a tuple.
- Returns:
Tuple.
- Supported Platforms:
Ascend
GPU
CPU
-
property
values
¶ Return CSRTensor’s non-zero values.
-
tinyms.
ms_function
(fn=None, input_signature=None, hash_args=None, jit_config=None)[source]¶ Create a callable MindSpore graph from a Python function.
This allows the MindSpore runtime to apply optimizations based on graph.
Note
ms_function will be deprecated and removed in a future version. Please use jit instead. If input_signature is specified, each input of fn must be a Tensor. And the input arguments for fn will not accept **kwargs.
- Parameters:
fn (Function) – The Python function that will be run as a graph. Default: None.
input_signature (Tensor) – The Tensor which describes the input arguments. The shape and dtype of the Tensor 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.
hash_args (Union[Object, List or Tuple of Objects]) – The local free variables used inside fn, like functions or objects of class defined outside fn. Calling fn again with change of hash_args will trigger recompilation.
jit_config (JitConfig) – Jit config for compile. 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.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> from mindspore import ops >>> from mindspore import ms_function ... >>> x = Tensor(np.ones([1, 1, 3, 3]).astype(np.float32)) >>> y = Tensor(np.ones([1, 1, 3, 3]).astype(np.float32)) ... >>> # create a callable MindSpore graph by calling ms_function >>> def tensor_add(x, y): ... z = x + y ... return z ... >>> tensor_add_graph = ms_function(fn=tensor_add) >>> out = tensor_add_graph(x, y) ... >>> # create a callable MindSpore graph through decorator @ms_function >>> @ms_function ... def tensor_add_with_dec(x, y): ... z = x + y ... return z ... >>> out = tensor_add_with_dec(x, y) ... >>> # create a callable MindSpore graph through decorator @ms_function with input_signature parameter >>> @ms_function(input_signature=(Tensor(np.ones([1, 1, 3, 3]).astype(np.float32)), ... Tensor(np.ones([1, 1, 3, 3]).astype(np.float32)))) ... def tensor_add_with_sig(x, y): ... z = x + y ... return z ... >>> out = tensor_add_with_sig(x, y) ... ... # Set hash_args as fn, otherwise cache of compiled `closure_fn` will not be reused. ... # While fn differs during calling again, recompilation will be triggered. >>> def func(x): ... return ops.exp(x) ... >>> def closure_fn(x, fn): ... @ms_function(hash_args=fn) ... def inner_fn(a): ... return fn(a) ... return inner_fn(x) ... >>> inputs = Tensor(np.ones([10, 10, 10]).astype(np.float32)) >>> for i in range(10): ... closure_fn(inputs, func)
-
tinyms.
ms_class
(cls)[source]¶ Class decorator for user-defined classes.
This allows MindSpore to identify user-defined classes and thus obtain their attributes and methods.
Note
ms_class will be deprecated and removed in a future version. Please use jit_class instead.
- Parameters:
cls (Class) – User-defined class.
- Returns:
Class.
- Raises:
TypeError – If ms_class is used for non-class types or nn.Cell.
AttributeError – If the private attributes or magic methods of the class decorated with ms_class is called.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.nn as nn >>> from mindspore import ms_class ... >>> @ms_class ... class UserDefinedNet: ... def __init__(self): ... self.value = 10 ... ... def func(self, x): ... return 2 * x ... >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.net = UserDefinedNet() ... ... def construct(self, x): ... out = self.net.value + self.net.func(x) ... return out ... >>> net = Net() >>> out = net(5) >>> print(out) 20
-
tinyms.
jit
(fn=None, input_signature=None, hash_args=None, jit_config=None)[source]¶ Create a callable MindSpore graph from a Python function.
This allows the MindSpore runtime to apply optimizations based on graph.
Note
If input_signature is specified, each input of fn must be a Tensor. And the input arguments for fn will not accept **kwargs.
- Parameters:
fn (Function) – The Python function that will be run as a graph. Default: None.
input_signature (Tensor) – The Tensor which describes the input arguments. The shape and dtype of the Tensor 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.
hash_args (Union[Object, List or Tuple of Objects]) – The local free variables used inside fn, like functions or objects of class defined outside fn. Calling fn again with change of hash_args will trigger recompilation.
jit_config (JitConfig) – Jit config for compile. 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.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor >>> from mindspore import ops >>> from mindspore import jit ... >>> x = Tensor(np.ones([1, 1, 3, 3]).astype(np.float32)) >>> y = Tensor(np.ones([1, 1, 3, 3]).astype(np.float32)) ... >>> # create a callable MindSpore graph by calling decorator @jit >>> def tensor_add(x, y): ... z = x + y ... return z ... >>> tensor_add_graph = jit(fn=tensor_add) >>> out = tensor_add_graph(x, y) ... >>> # create a callable MindSpore graph through decorator @jit >>> @jit ... def tensor_add_with_dec(x, y): ... z = x + y ... return z ... >>> out = tensor_add_with_dec(x, y) ... >>> # create a callable MindSpore graph through decorator @jit with input_signature parameter >>> @jit(input_signature=(Tensor(np.ones([1, 1, 3, 3]).astype(np.float32)), ... Tensor(np.ones([1, 1, 3, 3]).astype(np.float32)))) ... def tensor_add_with_sig(x, y): ... z = x + y ... return z ... >>> out = tensor_add_with_sig(x, y) ... ... # Set hash_args as fn, otherwise cache of compiled `closure_fn` will not be reused. ... # While fn differs during calling again, recompilation will be triggered. >>> def func(x): ... return ops.exp(x) ... >>> def closure_fn(x, fn): ... @jit(hash_args=fn) ... def inner_fn(a): ... return fn(a) ... return inner_fn(x) ... >>> inputs = Tensor(np.ones([10, 10, 10]).astype(np.float32)) >>> for i in range(10): ... closure_fn(inputs, func)
-
tinyms.
jit_class
(cls)[source]¶ Class decorator for user-defined classes.
This allows MindSpore to identify user-defined classes and thus obtain their attributes and methods.
- Parameters:
cls (Class) – User-defined class.
- Returns:
Class.
- Raises:
TypeError – If jit_class is used for non-class types or nn.Cell.
AttributeError – If the private attributes or magic methods of the class decorated with jit_class is called.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.nn as nn >>> from mindspore import jit_class ... >>> @jit_class ... class UserDefinedNet: ... def __init__(self): ... self.value = 10 ... ... def func(self, x): ... return 2 * x ... >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.net = UserDefinedNet() ... ... def construct(self, x): ... out = self.net.value + self.net.func(x) ... return out ... >>> net = Net() >>> out = net(5) >>> print(out) 20
-
class
tinyms.
Parameter
(default_input, name=None, requires_grad=True, layerwise_parallel=False, parallel_optimizer=True)[source]¶ Parameter is a Tensor subclass, when they are assigned as Cell attributes they are automatically added to the list of its parameters, and will appear, e.g. in cell.get_parameters() iterator.
Note
In auto_parallel mode of “semi_auto_parallel” and “auto_parallel”, if init Parameter by a Tensor, the type of Parameter will be Tensor. Tensor will save 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. 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. Give each Parameter a unique name to facilitate subsequent operations and updates. If there are two or more Parameter objects with the same name in a network, will be prompted to set a unique name when defining.
- Parameters:
default_input (Union[Tensor, int, float, numpy.ndarray, list]) – Parameter data, to initialize the parameter data.
name (str) –
Name of the parameter. Default: None.
1) If the parameter is not given a name, the default name is its variable name. For example, the name of param_a below is name_a, and the name of param_b is the variable name param_b.
self.param_a = Parameter(Tensor([1], ms.float32), name="name_a") self.param_b = Parameter(Tensor([2], ms.float32))
2) If parameter in list or tuple is not given a name, will give it a unique name. For example, the names of parameters below are Parameter$1 and Parameter$2.
self.param_list = [Parameter(Tensor([3], ms.float32)), Parameter(Tensor([4], ms.float32))]
3) If the parameter is given a name, and the same name exists between different parameters, an exception will be thrown. For example, “its name ‘name_a’ already exists.” will be thrown.
self.param_a = Parameter(Tensor([1], ms.float32), name="name_a") self.param_tuple = (Parameter(Tensor([5], ms.float32), name="name_a"), Parameter(Tensor([6], ms.float32)))
4) If a parameter appear multiple times in list or tuple, check the name of the object only once. For example, the following example will not throw an exception.
self.param_a = Parameter(Tensor([1], ms.float32), name="name_a") self.param_tuple = (self.param_a, self.param_a)
requires_grad (bool) – True if the parameter requires gradient. Default: True.
layerwise_parallel (bool) – When layerwise_parallel is true in data/hybrid parallel mode, broadcast and gradients communication would not be applied to parameters. Default: False.
parallel_optimizer (bool) – It is used to filter the weight shard operation in semi auto or auto parallel mode. It works only when enable parallel optimizer in mindspore.set_auto_parallel_context(). Default: True.
Examples
>>> import numpy as np >>> from mindspore import Parameter, Tensor >>> import mindspore.ops as ops >>> import mindspore.nn as nn >>> import mindspore >>> >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.matmul = ops.MatMul() ... self.weight = Parameter(Tensor(np.ones((1, 2)), mindspore.float32), name="w", requires_grad=True) ... ... def construct(self, x): ... out = self.matmul(self.weight, x) ... return out >>> net = Net() >>> x = Tensor(np.ones((2, 1)), mindspore.float32) >>> print(net(x)) [[2.]] >>> net.weight.set_data(Tensor(np.zeros((1, 2)), mindspore.float32)) >>> print(net(x)) [[0.]]
-
asnumpy
(self: mindspore._c_expression.Tensor) → array¶ Convert tensor to numpy.ndarray.
- Returns:
numpy.ndarray.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> array = data.asnumpy() >>> array array([[1., 1., 1.], [1., 1., 1.]])
-
asnumpy_of_slice_persistent_data
(self: mindspore._c_expression.Tensor, arg0: int, arg1: int) → array¶ Convert tensor to numpy.ndarray of a slice.
- Returns:
numpy.ndarray.
Examples
>>> data = mindspore.Tensor(np.ones((2000000000, 256))) >>> data.asnumpy_of_slice_persistent_data(0, 1)
-
assign_value_cpp
(self: mindspore._c_expression.Tensor, arg0: mindspore._c_expression.Tensor) → mindspore._c_expression.Tensor¶ Assign another tensor value to this.
- Arg:
value (
mindspore.tensor
): The value tensor.
Examples
>>> data = mindspore.Tensor(np.ones((1, 2), np.float32)) >>> data2 = mindspore.Tensor(np.ones((2, 2), np.float32)) >>> data.assign_value(data2) >>> data.shape (2, 2)
-
property
cache_enable
¶ Return whether the parameter is cache enable.
-
property
cache_shape
¶ Return the cache shape corresponding to the parameter if use cache.
-
clone
(init='same')[source]¶ Clone the parameter.
- Parameters:
init (Union[Tensor, str, numbers.Number]) – Initialize the shape and dtype of the parameter. If init is a Tensor or numbers.Number, clone a new parameter with the same shape and dtype, and the data of the new parameter will be set according to init. If init is a str, the init should be the alias of the class inheriting from Initializer. For example, if init is ‘same’, clone a new parameter with the same data, shape, and dtype. Default: ‘same’.
- Returns:
Parameter, a new parameter.
-
property
comm_fusion
¶ Get the fusion type (int) for communication operators corresponding to this parameter.
In AUTO_PARALLEL and SEMI_AUTO_PARALLEL mode, some communication operators used for parameters or gradients aggregation are inserted automatically. The value of fusion must be greater than or equal to 0. When the value of fusion is 0, operators will not be fused together.
-
property
data
¶ Return the parameter object.
-
data_sync
(self: mindspore._c_expression.Tensor, arg0: bool) → None¶
-
dim
(self: mindspore._c_expression.Tensor) → int¶ Get tensor’s data dimension.
- Returns:
int, the dimension of tensor.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> data.dim() 2
-
property
dtype
¶ Get the MetaTensor’s dtype.
-
from_numpy
(self: array) → mindspore._c_expression.Tensor¶ Creates a Tensor from a numpy.ndarray without copy.
- Arg:
array (numpy.ndarray): The input ndarray.
- Returns:
Tensor, tensor with shared data to input ndarray.
Examples
>>> a = np.ones((2, 3)) >>> t = mindspore.Tensor.from_numpy(a)
-
getitem_index_info
(self: object, arg0: object, arg1: bool_) → object¶
-
init_data
(layout=None, set_sliced=False)[source]¶ Initialize the parameter’s data.
- Parameters:
The parameter’s layout info. layout [dev_mat, tensor_map, slice_shape, filed_size, uniform_split, opt_shard_group]. Default: None. It’s not None only in ‘SEMI_AUTO_PARALLEL’ or ‘AUTO_PARALLEL’ mode.
dev_mat (list(int)): The parameter’s device matrix.
tensor_map (list(int)): The parameter’s tensor map.
slice_shape (list(int)): The parameter’s slice shape.
filed_size (int): The parameter’s filed size.
uniform_split (bool): Whether the parameter is split evenly.
opt_shard_group (str): The group of the parameter while running optimizer parallel.
set_sliced (bool) – True if the parameter is set sliced after initializing the data. Default: False.
- Returns:
Parameter, the Parameter after initializing data. If current Parameter was already initialized before, returns the same initialized Parameter.
- Raises:
RuntimeError – If it is from Initializer, and parallel mode has changed after the Initializer created.
ValueError – If the length of the layout is less than 6.
TypeError – If layout is not tuple.
-
property
inited_param
¶ Get the new parameter after call the init_data.
Default is a None, If self is a Parameter without data, after call the init_data the initialized Parameter with data will be recorded here.
-
is_init
(self: mindspore._c_expression.Tensor) → bool¶ Get tensor init_flag.
- Returns:
bool, whether the tensor init.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> data.is_init() False
-
is_persistent_data
(self: mindspore._c_expression.Tensor) → bool¶ Check if tensor have persistent data.
- Returns:
Bool.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> data.is_persistent_data()
-
property
key
¶ Return the parameter unique key.
-
property
layerwise_parallel
¶ Get the layerwise parallel status(bool) of the parameter.
When layerwise_parallel is true in DATA_PARALLEL and HYBRID_PARALLEL parallel mode, broadcast and gradients communication would not be applied to parameters.
-
property
name
¶ Get the name of the parameter.
-
property
parallel_optimizer
¶ Get the optimizer parallel status(bool) of the parameter.
It is used to filter the weight shard operation in AUTO_PARALLEL and SEMI_AUTO_PARALLEL mode. It works only when enable parallel optimizer in mindspore.set_auto_parallel_context().
-
property
parallel_optimizer_comm_recompute
¶ Get the communication recompute status(bool) of optimizer parallel for the parameter.
In AUTO_PARALLEL and SEMI_AUTO_PARALLEL mode, when applying parallel optimizer, some
mindspore.ops.AllGather
operators used for parameters gathering are inserted automatically. It is used to control the recompute attr for thosemindspore.ops.AllGather
operators.Note
Only Graph mode is supported.
It is recommended to use cell.recompute(parallel_optimizer_comm_recompute=True/False) to configure the AllGather operators introducing by parallel optimizer rather than using this interface directly.
-
persistent_data_from_numpy
(self: array, arg0: int_) → mindspore._c_expression.Tensor¶ Creates a Tensor from a numpy.ndarray without copy. Use persistent data tensor.
- Arg:
array (numpy.ndarray): The input ndarray. slice_num (int): The slice num of persistent data tensor.
- Returns:
Tensor, tensor with shared data to input ndarray.
Examples
>>> a = np.ones((2, 3)) >>> t = mindspore.Tensor.persistent_data_from_numpy(a, 1)
-
property
requires_grad
¶ Return whether the parameter requires gradient.
-
set_cast_dtype
(self: mindspore._c_expression.Tensor, dtype: mindspore._c_expression.typing.Type = None) → None¶
-
set_dtype
(self: mindspore._c_expression.Tensor, arg0: mindspore._c_expression.typing.Type) → mindspore._c_expression.typing.Type¶ Set the tensor’s data type.
- Arg:
dtype (
mindspore.dtype
): The type of output tensor.
Examples
>>> data = mindspore.Tensor(np.ones((1, 2), np.float32)) >>> data.set_dtype(mindspore.int32) mindspore.int32
-
set_init_flag
(self: mindspore._c_expression.Tensor, arg0: bool) → None¶ Set tensor init_flag.
Examples
>>> data = mindspore.Tensor(np.ones((2, 3))) >>> data.set_init_flag(True)
-
set_param_fl
(push_to_server=False, pull_from_server=False, requires_aggr=True)[source]¶ Set the way of parameter and server interaction.
-
set_param_ps
(init_in_server=False)[source]¶ Set whether the trainable parameter is updated by parameter server and whether the trainable parameter is initialized on server.
Note
It only works when a running task is in the parameter server mode. It is supported only in graph mode.
- Parameters:
init_in_server (bool) – Whether trainable parameter updated by parameter server is initialized on server. Default: False.
-
setitem_index_info
(self: object, arg0: object, arg1: object, arg2: bool_) → object¶
-
property
shape
¶ Get the MetaTensor’s shape.
-
property
sliced
¶ Get slice status of the parameter.
-
property
unique
¶ Whether the parameter is already unique or not.
-
value
()[source]¶ Return the value of parameter object.
Examples
>>> from mindspore import Tensor, Parameter >>> import numpy as np >>> x = Parameter(Tensor(np.array([1, 2], dtype=np.float32)), name="param") >>> x_value = x.value() >>> print(x_value) [1. 2.]
-
class
tinyms.
ParameterTuple
[source]¶ Inherited from tuple, ParameterTuple is used to save multiple parameter.
Note
It is used to store the parameters of the network into the parameter tuple collection.
-
clone
(prefix, init='same')[source]¶ Clone the parameters in ParameterTuple element-wisely to generate a new ParameterTuple.
- Parameters:
prefix (str) – Namespace of parameter, the prefix string will be added to the names of parameters in parametertuple.
init (Union[Tensor, str, numbers.Number]) –
Clone the shape and dtype of Parameters in ParameterTuple and set data according to init. Default: ‘same’.
If init is a Tensor , set the new Parameter data to the input Tensor.
If init is numbers.Number , set the new Parameter data to the input number.
If init is a str, data will be set according to the initialization method of the same name in the Initializer.
If init is ‘same’, the new Parameter has the same value with the original Parameter.
- Returns:
Tuple, the new Parameter tuple.
-
count
()¶ Return number of occurrences of value.
-
index
()¶ Return first index of value.
Raises ValueError if the value is not present.
-
tinyms.
set_seed
(seed)[source]¶ Set global seed.
Note
The global seed is used by numpy.random, mindspore.common.Initializer, mindspore.ops.function.random_func 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.function.random_func 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.
In semi_auto_parallel/auto_parallel mode, when using set_seed, weights with same shape and same sharding strategy in the same device would be initialized to the same result, otherwise, they would be initialized to the different result.
- Parameters:
seed (int) – The seed to be set.
- Raises:
ValueError – If seed is invalid (< 0).
TypeError – If seed isn’t an int.
Examples
>>> import numpy as np >>> import mindspore.ops as ops >>> from mindspore import Tensor, set_seed, Parameter >>> from mindspore.common.initializer import initializer >>> import mindspore as ms >>> # Note: (1) Please make sure the code is running in PYNATIVE MODE; >>> # (2) Because Composite-level ops need parameters to be Tensors, for below examples, >>> # when using ops.uniform operator, minval and maxval are initialised as: >>> minval = Tensor(1.0, ms.float32) >>> maxval = Tensor(2.0, ms.float32) >>> >>> # 1. If global seed is not set, numpy.random and initializer will choose a random seed: >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A1 >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A2 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W1 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W2 >>> # Rerun the program will get different results: >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A3 >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A4 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W3 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W4 >>> >>> # 2. If global seed is set, numpy.random and initializer will use it: >>> set_seed(1234) >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A1 >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A2 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W1 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W2 >>> # Rerun the program will get the same results: >>> set_seed(1234) >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A1 >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A2 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W1 >>> w1 = Parameter(initializer("uniform", [2, 2], ms.float32), name="w1") # W2 >>> >>> # 3. If neither global seed nor op seed is set, mindspore.ops.function.random_func and >>> # mindspore.nn.probability.distribution will choose a random seed: >>> c1 = ops.uniform((1, 4), minval, maxval) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval) # C2 >>> # Rerun the program will get different results: >>> c1 = ops.uniform((1, 4), minval, maxval) # C3 >>> c2 = ops.uniform((1, 4), minval, maxval) # C4 >>> >>> # 4. If global seed is set, but op seed is not set, mindspore.ops.function.random_func and >>> # mindspore.nn.probability.distribution will calculate a seed according to global seed and >>> # default op seed. Each call will change the default op seed, thus each call get different >>> # results. >>> set_seed(1234) >>> c1 = ops.uniform((1, 4), minval, maxval) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval) # C2 >>> # Rerun the program will get the same results: >>> set_seed(1234) >>> c1 = ops.uniform((1, 4), minval, maxval) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval) # C2 >>> >>> # 5. If both global seed and op seed are set, mindspore.ops.function.random_func and >>> # mindspore.nn.probability.distribution will calculate a seed according to global seed and >>> # op seed counter. Each call will change the op seed counter, thus each call get different >>> # results. >>> set_seed(1234) >>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # C2 >>> # Rerun the program will get the same results: >>> set_seed(1234) >>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # C2 >>> >>> # 6. If op seed is set but global seed is not set, 0 will be used as global seed. Then >>> # mindspore.ops.function.random_func and mindspore.nn.probability.distribution act as in >>> # condition 5. >>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # C2 >>> # Rerun the program will get the different results: >>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1 >>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # C2 >>> >>> # 7. Recall set_seed() in the program will reset numpy seed and op seed counter of >>> # mindspore.ops.function.random_func and mindspore.nn.probability.distribution. >>> set_seed(1234) >>> np_1 = np.random.normal(0, 1, [1]).astype(np.float32) # A1 >>> c1 = ops.uniform((1, 4), minval, maxval, seed=2) # C1 >>> set_seed(1234) >>> np_2 = np.random.normal(0, 1, [1]).astype(np.float32) # still get A1 >>> c2 = ops.uniform((1, 4), minval, maxval, seed=2) # still get C1
-
tinyms.
set_dump
(target, enabled=True)[source]¶ Enable or disable dump for the target and its contents.
target should be an instance of
mindspore.nn.Cell
ormindspore.ops.Primitive
. Please note that this API takes effect only when Asynchronous Dump is enabled and the dump_mode field in dump config file is “2”. See the dump document for details. The default enabled status for amindspore.nn.Cell
ormindspore.ops.Primitive
is False.Warning
This is an experimental API that is subject to change or deletion.
Note
This API is only effective for GRAPH_MODE with Ascend backend.
This API only supports being called before training starts. If you call this API during training, it may not be effective.
After using set_dump(Cell, True) , operators in forward and backward computation (computation generated by the grad operations) of the cell will be dumped.
For
mindspore.nn.SoftmaxCrossEntropyWithLogits
layer, the forward computation and backward computation use the same set of operators. So you can only see dump data from backward computation. Please note thatmindspore.nn.SoftmaxCrossEntropyWithLogits
layer will also use the above operators internally when initialized with sparse=True and reduction=”mean” .
- Parameters:
target (Union[Cell, Primitive]) – The Cell instance or Primitive instance to which the dump flag is set.
enabled (bool, optional) – True means enable dump, False means disable dump. Default: True.
- Supported Platforms:
Ascend
Examples
>>> # Please set the dump config file and environment variable before >>> # running this example to actually get the dump data. >>> # See the document of this API for details. >>> import numpy as np >>> import mindspore as ms >>> import mindspore.nn as nn >>> from mindspore import Tensor, set_dump >>> >>> ms.set_context(device_target="Ascend", mode=ms.GRAPH_MODE) >>> >>> class MyNet(nn.Cell): ... def __init__(self): ... super().__init__() ... self.conv1 = nn.Conv2d(5, 6, 5, pad_mode='valid') ... self.relu1 = nn.ReLU() ... ... def construct(self, x): ... x = self.conv1(x) ... x = self.relu1(x) ... return x >>> >>> if __name__ == "__main__": ... net = MyNet() ... set_dump(net.conv1) ... input_tensor = Tensor(np.ones([1, 5, 10, 10], dtype=np.float32)) ... output = net(input_tensor)
-
tinyms.
ms_memory_recycle
()[source]¶ Recycle memory used by MindSpore. When train multi Neural network models in one process, memory used by MindSpore is very large, this is because MindSpore cached runtime memory for every model. To recycle these cached memory, users can call this function after training of one model.
-
tinyms.
mutable
(input_data, dynamic_len=False)[source]¶ Make a constant value mutable.
Currently, all the inputs of Cell except Tensor such as scalar, tuple, list and dict, are regarded as constant values. The constant values are non-differentiable and used to do constant folding in the optimization process.
Besides, currently when the network input is tuple[Tensor], list[Tensor] or dict[Tensor], even without changing the shape and dtype of the Tensors, the network will be re-compiled when calling this network repeatedly because the these inputs are regarded as constant values.
To solve the above problems, we provide api mutable to make the constant inputs of Cell ‘mutable’. A ‘mutable’ input means that it is changed to be a variable input just like Tensor and the most important thing is that it will be differentiable.
When the input_data is tuple or list and dynamic_len is False, mutable will return a constant length tuple or list with all mutable elements. If dynamic_len is True, the length of the return tuple or list will be dynamic.
If a dynamic length tuple or list is used as the input of the network and the network is repeatedly called, and the length of the tuple or list is different for each run, it does not need to be re-compiled.
- Parameters:
input_data (Union[int, float, Tensor, tuple, list, dict]) – The input data to be made mutable. If ‘input_data’ is list/tuple/dict, the type of each element should also in the valid types.
dynamic_len (bool) – Whether to set the whole sequence to be dynamic length. In graph compilation, if dynamic_len is True, the input_data must be list or tuple and the elements of input_data must have the same type and shape. Default: False.
Warning
This is an experimental API that is subject to change or deletion.
Note
Currently this api only works in GRAPH mode.
- Returns:
The origin input data which has been set mutable.
- Raises:
TypeError – If input_data is not one of int, float, Tensor, tuple, list, dict or their nested structure.
TypeError – If dynamic_len is True and input_data is not tuple or list.
ValueError – If dynamic_len is True, input_data is tuple or list but the elements within input_data do not have the same shape and type.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.nn as nn >>> import mindspore.ops as ops >>> from mindspore.common import mutable >>> from mindspore.common import dtype as mstype >>> from mindspore import Tensor >>> from mindspore import context >>> context.set_context(mode=context.GRAPH_MODE) >>> class Net(nn.Cell): ... def __init__(self): ... super(Net, self).__init__() ... self.matmul = ops.MatMul() ... ... def construct(self, z): ... x = z[0] ... y = z[1] ... out = self.matmul(x, y) ... return out ... >>> class GradNetWrtX(nn.Cell): ... def __init__(self, net): ... super(GradNetWrtX, self).__init__() ... self.net = net ... self.grad_op = ops.GradOperation() ... ... def construct(self, z): ... gradient_function = self.grad_op(self.net) ... return gradient_function(z) ... >>> z = mutable((Tensor([[0.5, 0.6, 0.4], [1.2, 1.3, 1.1]], dtype=mstype.float32), ... Tensor([[0.01, 0.3, 1.1], [0.1, 0.2, 1.3], [2.1, 1.2, 3.3]], dtype=mstype.float32))) >>> output = GradNetWrtX(Net())(z) >>> print(output) (Tensor(shape=[2, 3], dtype=Float32, value= [[ 1.41000009e+00, 1.60000002e+00, 6.59999943e+00], [ 1.41000009e+00, 1.60000002e+00, 6.59999943e+00]]), Tensor(shape=[3, 3], dtype=Float32, value= [[ 1.70000005e+00, 1.70000005e+00, 1.70000005e+00], [ 1.89999998e+00, 1.89999998e+00, 1.89999998e+00], [ 1.50000000e+00, 1.50000000e+00, 1.50000000e+00]]))
-
class
tinyms.
JitConfig
(jit_level='O1', exc_mode='auto', **kwargs)[source]¶ Jit config for compile.
Warning
This is an experimental API that is subject to change or deletion.
- Parameters:
jit_level (str) –
Option for argument level for Optimization of lift graph. Supports [“O0”, “O1”, “O2”, “O3”]. Default: “O1”.
”O0”: Basic optimization.
”O1”: Manual optimization.
”O2”: Manual optimization and graph computation fusion.
”O3”: Performance optimization, no generalization guaranteed.
exc_mode (str) –
Mode for execute the network. Supports [“auto”, “sink”, “no_sink”]. Default: “auto”.
”auto”: Automatic Policies.
”sink”: Build computational graphs with the sink mode.
”no_sink”: Build computational graphs with no sink mode.
**kwargs (dict) – A dictionary of keyword arguments that the class needs.
Examples
>>> from mindspore import JitConfig >>> >>> jitconfig = JitConfig(jit_level="O1") >>> net = LeNet5() >>> >>> net.set_jit_config(jitconfig)
-
tinyms.
abs
(x, dtype=None)¶ Calculates the absolute value element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Currently the backend kernel only supports float calculation, if the input is not a float, then it will be casted to
mstype.float32
and casted back.- Parameters:
x (Tensor) – Tensor to be used for calculation.
dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor.
- Raises:
TypeError – If input arguments have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.asarray([1, 2, 3, -4, -5], np.float32) >>> output = np.absolute(x) >>> print(output) [1. 2. 3. 4. 5.]
-
tinyms.
absolute
(x, dtype=None)[source]¶ Calculates the absolute value element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Currently the backend kernel only supports float calculation, if the input is not a float, then it will be casted to
mstype.float32
and casted back.- Parameters:
x (Tensor) – Tensor to be used for calculation.
dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor.
- Raises:
TypeError – If input arguments have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.asarray([1, 2, 3, -4, -5], np.float32) >>> output = np.absolute(x) >>> print(output) [1. 2. 3. 4. 5.]
-
tinyms.
add
(x1, x2, dtype=None)[source]¶ Adds arguments element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
- Returns:
Tensor or scalar, the sum of x1 and x2, element-wise. This is a scalar if both x1 and x2 are scalars.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x1 = np.full((3, 2), [1, 2]) >>> x2 = np.full((3, 2), [3, 4]) >>> output = np.add(x1, x2) >>> print(output) [[4 6] [4 6] [4 6]]
-
tinyms.
amax
(a, axis=None, keepdims=False, initial=None, where=True)[source]¶ Returns the maximum of an array or maximum along an axis.
Note
Numpy argument out is not supported. On GPU, the supported dtypes are np.float16, and np.float32.
- Parameters:
a (Tensor) – Input data.
axis (None or int or tuple of integers, optional) – Defaults to None. Axis or axes along which to operate. By default, flattened input is used. If this is a tuple of integers, the maximum is selected over multiple axes, instead of a single axis or all the axes as before.
keepdims (boolean, optional) – Defaults to False. If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
initial (scalar, optional) – Defaults to None. The minimum value of an output element. Must be present to allow computation on empty slice.
where (boolean Tensor, optional) – Defaults to True. A boolean array which is broadcasted to match the dimensions of array, and selects elements to include in the reduction. If non-default value is passed, initial must also be provided.
- Returns:
Tensor or scalar, maximum of a. If axis is None, the result is a scalar value. If axis is given, the result is an array of dimension
a.ndim - 1
.- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> a = np.arange(4).reshape((2,2)).astype('float32') >>> output = np.amax(a) >>> print(output) 3.0 >>> output = np.amax(a, axis=0) >>> print(output) [2. 3.] >>> output = np.amax(a, axis=1) >>> print(output) [1. 3.] >>> output = np.amax(a, where=np.array([False, True]), initial=-1, axis=0) >>> print(output) [-1. 3.]
-
tinyms.
amin
(a, axis=None, keepdims=False, initial=None, where=True)[source]¶ Returns the minimum of an array or minimum along an axis.
Note
Numpy argument out is not supported. On GPU, the supported dtypes are np.float16, and np.float32.
- Parameters:
a (Tensor) – Input data.
axis (None or int or tuple of integers, optional) – Defaults to None. Axis or axes along which to operate. By default, flattened input is used. If this is a tuple of integers, the minimum is selected over multiple axes, instead of a single axis or all the axes as before.
keepdims (bool, optional) – Defaults to False. If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
initial (Number, optional) – Defaults to None. The maximum value of an output element. Must be present to allow computation on empty slice.
where (bool Tensor, optional) – Defaults to True. A boolean array which is broadcasted to match the dimensions of array, and selects elements to include in the reduction. If non-default value is passed, initial must also be provided.
- Returns:
Tensor or scalar, minimum of a. If axis is None, the result is a scalar value. If axis is given, the result is an array of dimension
a.ndim - 1
.- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> a = np.arange(4).reshape((2,2)).astype('float32') >>> output = np.amin(a) >>> print(output) 0.0 >>> output = np.amin(a, axis=0) >>> print(output) [0. 1.] >>> output = np.amin(a, axis=1) >>> print(output) [0. 2.] >>> output = np.amin(a, where=np.array([False, True]), initial=10, axis=0) >>> print(output) [10. 1.]
-
tinyms.
append
(arr, values, axis=None)[source]¶ Appends values to the end of a tensor.
- Parameters:
arr (Tensor) – Values are appended to a copy of this tensor.
values (Tensor) – These values are appended to a copy of arr. It must be of the correct shape (the same shape as arr, excluding axis). If axis is not specified, values can be any shape and will be flattened before use.
axis (None, int, optional) – The axis along which values are appended. If axis is not given, both arr and values are flattened before use, default is
None
.
- Returns:
Tensor, a copy of tensor with values appended to axis.
- Raises:
TypeError – If input arguments have types not specified above.
ValueError – If specified axis exceeds arr.ndim.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> a = np.ones((2, 3)) >>> b = np.ones((2, 1)) >>> print(np.append(a, b, axis=1).shape) (2, 4)
-
tinyms.
apply_along_axis
(func1d, axis, arr, *args, **kwargs)[source]¶ Applies a function to 1-D slices along the given axis. Executes
func1d(a, *args, **kwargs)
where func1d operates on 1-D arrays and a is a 1-D slice of arr along axis.- Parameters:
func1d (function) – Maps (M,) -> (Nj…). This function should accept 1-D arrays. It is applied to 1-D slices of arr along the specified axis.
axis (int) – Axis along which arr is sliced.
arr (Tensor) – Input array with shape (Ni…, M, Nk…).
args (any) – Additional arguments to func1d.
kwargs (any) – Additional named arguments to func1d.
- Returns:
Tensor with shape (Ni…, Nj…, Nk…), the output array. Its shape is identical to the shape of arr, except along the axis dimension. This axis is removed, and replaced with new dimensions equal to the shape of the return value of func1d. So if func1d returns a scalar, the output will have one fewer dimensions than arr.
- Supported Platforms:
Ascend
GPU
CPU
- Raises:
ValueError – If axis is out of the range.
Examples
>>> import mindspore.numpy as np >>> b = np.array([[1,2,3], [4,5,6], [7,8,9]]) >>> print(np.apply_along_axis(np.diag, -1, b)) [[[1 0 0] [0 2 0] [0 0 3]] [[4 0 0] [0 5 0] [0 0 6]] [[7 0 0] [0 8 0] [0 0 9]]]
-
tinyms.
apply_over_axes
(func, a, axes)[source]¶ Applies a function repeatedly over multiple axes.
func is called as res = func(a, axis), where axis is the first element of axes. The result res of the function call must have either the same dimensions as a or one less dimension. If res has one less dimension than a, a dimension is inserted before axis. The call to func is then repeated for each axis in axes, with res as the first argument.
- Parameters:
- Returns:
Tensor. The number of dimensions is the same as a, but the shape can be different. This depends on whether func changes the shape of its output with respect to its input.
- Raises:
TypeError – If input a is not array_like or axes is not int or sequence of ints.
ValueError – If any axis is out of range or duplicate axes exist.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.arange(10).reshape(2, 5).astype('float32') >>> print(x) [[0. 1. 2. 3. 4.] [5. 6. 7. 8. 9.]] >>> print(np.apply_over_axes(np.sum, x, axes=0)) [[ 5. 7. 9. 11. 13.]]
-
tinyms.
arange
(start, stop=None, step=None, dtype=None)[source]¶ Returns evenly spaced values within a given interval.
- Parameters:
start (Union[int, float]) – Start of interval. The interval includes this value. When stop is provided as a position argument, start must be given, when stop is a normal argument, start can be optional, and default is 0. Please see additional examples below.
stop (Union[int, float], optional) – End of interval. The interval does not include this value, except in some cases where step is not an integer and floating point round-off affects the length of out.
step (Union[int, float], optional) – Spacing between values. For any output out, this is the distance between two adjacent values, \(out[i+1] - out[i]\). The default step size is 1. If step is specified as a position argument, start must also be given.
dtype (Union[
mindspore.dtype
, str], optional) – Designated tensor dtype. If dtype is None, the data type of the new tensor will be inferred from start, stop and step. Default is None.
- Returns:
Tensor with evenly spaced values.
- Raises:
TypeError(PyNative Mode) – If input arguments have types not specified above, or arguments are not given in the correct orders specified above.
RuntimeError(Graph Mode) – The inputs that lead to TypeError in Pynative Mode will lead to RuntimeError in Graph Mode.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.arange(0, 5, 1)) [0 1 2 3 4] >>> print(np.arange(3)) [0 1 2] >>> print(np.arange(start=0, stop=3)) [0 1 2] >>> print(np.arange(0, stop=3, step=0.5)) [0. 0.5 1. 1.5 2. 2.5]
-
tinyms.
arccos
(input, dtype=None)[source]¶ Trigonometric inverse cosine, element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
input (Tensor) – Input tensor. x-coordinate on the unit circle. For real arguments, the domain is \([-1, 1]\).
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor.
- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> input = np.asarray([1, -1], np.float32) >>> output = np.arccos(input) >>> print(output) [0. 3.1415927]
-
tinyms.
arccosh
(x, dtype=None)[source]¶ Inverse hyperbolic cosine, element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.arange(1, 5).astype('float32') >>> print(np.arccosh(x)) [0. 1.316958 1.7627472 2.063437 ]
-
tinyms.
arcsin
(x, dtype=None)[source]¶ Inverse sine, element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor. y-coordinate on the unit circle.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Output Tensor.
- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.asarray([1, -1], np.float32) >>> output = np.arcsin(x) >>> print(output) [ 1.5707964 -1.5707964]
-
tinyms.
arcsinh
(x, dtype=None)[source]¶ Inverse hyperbolic sine element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.array([1., 2., 3., 4.], dtype=np.float32) >>> print(np.arcsinh(x)) [0.8813736 1.4436355 1.8184465 2.0947125]
-
tinyms.
arctan
(x, dtype=None)[source]¶ Trigonometric inverse tangent, element-wise.
The inverse of tan, so that if \(y = tan(x)\) then \(x = arctan(y)\).
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.arange(5).astype('float32') >>> print(np.arctan(x)) [0. 0.7853982 1.1071488 1.2490457 1.3258177]
-
tinyms.
arctan2
(x1, x2, dtype=None)[source]¶ Element-wise arc tangent of \(x1/x2\) choosing the quadrant correctly.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
- Returns:
Tensor or scalar, the sum of x1 and x2, element-wise. This is a scalar if both x1 and x2 are scalars.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x1 = np.array([-1, +1, +1, -1]) >>> x2 = np.array([-1, -1, +1, +1]) >>> output = np.arctan2(x1, x2) >>> print(output) [-2.3561945 2.3561945 0.78539819 -0.78539819]
-
tinyms.
arctanh
(x, dtype=None)[source]¶ Inverse hyperbolic tangent element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.array([-0.99, -0.75, -0.5, 0, 0.5]).astype('float32') >>> print(np.arctanh(x)) [-2.646653 -0.97295505 -0.54930615 0. 0.54930615]
-
tinyms.
argmax
(a, axis=None)[source]¶ Returns the indices of the maximum values along an axis.
Note
Numpy argument out is not supported. On Ascend, in case of multiple occurrences of the maximum values, the return indices may not necessarily correspond to the first occurrence.
- Parameters:
- Returns:
Tensor, array of indices into the array. It has the same shape as a.shape with the dimension along axis removed.
- Raises:
ValueError – If axis is out of range.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> a = np.arange(10, 16).reshape(2, 3) >>> print(np.argmax(a)) 5 >>> print(np.argmax(a, axis=0)) [1 1 1] >>> print(np.argmax(a, axis=1)) [2 2]
-
tinyms.
argmin
(a, axis=None)[source]¶ Returns the indices of the minimum values along an axis.
Note
Numpy argument out is not supported.
- Parameters:
- Returns:
Tensor, array of indices into the array. It has the same shape as a.shape with the dimension along axis removed.
- Raises:
ValueError – If axis is out of range.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> a = np.arange(10, 16).reshape(2, 3) >>> print(np.argmin(a)) 0 >>> print(np.argmin(a, axis=0)) [0 0 0] >>> print(np.argmin(a, axis=1)) [0 0]
-
tinyms.
array
(obj, dtype=None, copy=True, ndmin=0)[source]¶ Creates a tensor.
This function creates tensors from an array-like object.
- Parameters:
obj (Union[int, float, bool, list, tuple]) – Input data, in any form that can be converted to a Tensor. This includes Tensor, list, tuple and numbers.
dtype (Union[
mindspore.dtype
, str], optional) – Designated tensor dtype, can be in format of np.int32, or ‘int32’. If dtype isNone
, the data type of the new tensor will be inferred from obj. Default isNone
.copy (bool) – If True, then the object is copied. Otherwise, a copy will only be made if necessary. Default: True.
ndmin (int) – Specifies the minimum number of dimensions that the resulting tensor should have. Ones will be pre-pended to the shape as needed to meet this requirement. Default: 0
- Returns:
Tensor, generated tensor with the specified dtype.
- Raises:
TypeError – If input arguments have types not specified above.
ValueError – If input obj has different sizes at different dimensions.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.array([1,2,3])) [1 2 3]
-
tinyms.
array_equal
(a1, a2, equal_nan=False)[source]¶ Returns True if input arrays have same shapes and all elements equal.
Note
In mindspore, a bool tensor is returned instead, since in Graph mode, the value cannot be traced and computed at compile time.
Since on Ascend,
nan
is treated differently, currently the argument equal_nan is not supported on Ascend.- Parameters:
- Returns:
Scalar bool tensor, value is True if inputs are equal, False otherwise.
- Raises:
TypeError – If inputs have types not specified above.
- Supported Platforms:
GPU
CPU
Ascend
Examples
>>> import mindspore.numpy as np >>> a = [0,1,2] >>> b = [[0,1,2], [0,1,2]] >>> print(np.array_equal(a,b)) False
-
tinyms.
array_equiv
(a1, a2)[source]¶ Returns True if input arrays are shape consistent and all elements equal.
Shape consistent means they are either the same shape, or one input array can be broadcasted to create the same shape as the other one.
Note
In mindspore, a bool tensor is returned instead, since in Graph mode, the value cannot be traced and computed at compile time.
- Parameters:
a1/a2 (Union[int, float, bool, list, tuple, Tensor]) – Input arrays.
- Returns:
Scalar bool tensor, value is True if inputs are equivalent, False otherwise.
- Raises:
TypeError – If inputs have types not specified above.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> a = [0,1,2] >>> b = [[0,1,2], [0,1,2]] >>> print(np.array_equiv(a,b)) True
-
tinyms.
array_split
(x, indices_or_sections, axis=0)[source]¶ Splits a tensor into multiple sub-tensors.
Note
Currently, array_split only supports
mindspore.float32
onCPU
.The only difference between
np.split
andnp.array_split
is thatnp.array_split
allows indices_or_sections to be an integer that does not equally divide the axis. For a tensor of length l that should be split into n sections, it returns \(l % n\) sub-arrays of size \(l//n + 1\) and the rest of size \(l//n\).- Parameters:
x (Tensor) – A Tensor to be divided.
indices_or_sections (Union[int, tuple(int), list(int)]) – If integer, \(N\), the tensor will be divided into \(N\) tensors along axis. If tuple(int), list(int) or of sorted integers, the entries indicate where along axis the array is split. For example, \([2, 3]\) would, for \(axis=0\), result in three sub-tensors \(x[:2]\), \(x[2:3]`and :math:`x[3:]\). If an index exceeds the dimension of the array along axis, an empty sub-array is returned correspondingly.
axis (int) – The axis along which to split. Default: 0.
- Returns:
A list of sub-tensors.
- Raises:
TypeError – If argument indices_or_sections is not integer, tuple(int) or list(int) or argument axis is not integer.
ValueError – If argument axis is out of range of \([-x.ndim, x.ndim)\).
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> input_x = np.arange(9).astype("float32") >>> output = np.array_split(input_x, 4) >>> print(output) (Tensor(shape=[3], dtype=Float32, value= [ 0.00000000e+00, 1.00000000e+00, 2.00000000e+00]), Tensor(shape=[2], dtype=Float32, value= [ 3.00000000e+00, 4.00000000e+00]), Tensor(shape=[2], dtype=Float32, value= [ 5.00000000e+00, 6.00000000e+00]), Tensor(shape=[2], dtype=Float32, value= [ 7.00000000e+00, 8.00000000e+00]))
-
tinyms.
array_str
(a)[source]¶ Returns a string representation of the data in an array.
The data in the array is returned as a single string. This function is similar to array_repr, the difference being that array_repr also returns information on the kind of array and its data type.
Note
Numpy argument max_line_width, precision and suppress_small are not supported. Graph mode does not support the function.
- Parameters:
a (Tensor) – Input data.
- Returns:
String.
- Supported Platforms:
Ascend
GPU
CPU
- Raises:
TypeError – If input is not tensor.
Examples
>>> import mindspore.numpy as np >>> x = np.arange(5) >>> np.array_str(x) '[0 1 2 3 4]'
-
tinyms.
asarray
(a, dtype=None)[source]¶ Converts the input to tensor.
This function converts tensors from an array-like object.
- Parameters:
a (Union[int, float, bool, list, tuple, Tensor]) – Input data, in any form that can be converted to a Tensor. This includes Tensor, list, tuple and numbers.
dtype (Union[
mindspore.dtype
, str], optional) – Designated tensor dtype, can be in format of np.int32, or ‘int32’. If dtype isNone
, the data type of the new tensor will be inferred from obj. Default isNone
.
- Returns:
Tensor, generated tensor with the specified dtype.
- Raises:
TypeError – If input arguments have types not specified above.
ValueError – If input a has different sizes at different dimensions.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.asarray([1,2,3])) [1 2 3]
-
tinyms.
asfarray
(a, dtype=mindspore.float32)[source]¶ Similar to asarray, converts the input to a float tensor.
If non-float dtype is defined, this function will return a float32 tensor instead.
- Parameters:
a (Union[int, float, bool, list, tuple, Tensor]) – Input data, in any form that can be converted to a Tensor. This includes Tensor, list, tuple and numbers.
dtype (Union[
mindspore.dtype
, str], optional) – Designated tensor dtype, can be in format of np.int32, or ‘int32’. If dtype isNone
, the data type of the new tensor will be inferred from a. Default ismindspore.float32
.
- Returns:
Tensor, generated tensor with the specified float dtype.
- Raises:
TypeError – If input arguments have types not specified above.
ValueError – If input a has different sizes at different dimensions.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.asfarray([1,2,3])) [1. 2. 3.]
-
tinyms.
atleast_1d
(*arys)[source]¶ Converts inputs to arrays with at least one dimension.
Scalar inputs are converted to 1-dimensional arrays, whilst higher-dimensional inputs are preserved.
Note
In graph mode, returns a tuple of tensor instead of a list of tensors.
- Parameters:
*arys (Tensor) – one or more input tensors.
- Returns:
Tensor, or list of tensors, each with
a.ndim >= 1
.- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> a = np.ones((2, 3)) >>> b = np.ones(()) >>> c = np.ones(5) >>> output = np.atleast_1d(a, b, c) >>> print(output) [Tensor(shape=[2, 3], dtype=Float32, value= [[1.00000000e+00, 1.00000000e+00, 1.00000000e+00], [1.00000000e+00, 1.00000000e+00, 1.00000000e+00]]), Tensor(shape=[1], dtype=Float32, value= [1.00000000e+00]), Tensor(shape=[5], dtype=Float32, value= [1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00])]
-
tinyms.
atleast_2d
(*arys)[source]¶ Reshapes inputs as arrays with at least two dimensions.
Note
In graph mode, returns a tuple of tensor instead of a list of tensors.
- Parameters:
*arys (Tensor) – one or more input tensors.
- Returns:
Tensor, or list of tensors, each with
a.ndim >= 2
.- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> a = np.ones((2, 3)) >>> b = np.ones(()) >>> c = np.ones(5) >>> output = np.atleast_2d(a, b, c) >>> print(output) [Tensor(shape=[2, 3], dtype=Float32, value= [[1.00000000e+00, 1.00000000e+00, 1.00000000e+00], [1.00000000e+00, 1.00000000e+00, 1.00000000e+00]]), Tensor(shape=[1, 1], dtype=Float32, value= [[1.00000000e+00]]), Tensor(shape=[1, 5], dtype=Float32, value= [[1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00, 1.00000000e+00]])]
-
tinyms.
atleast_3d
(*arys)[source]¶ Reshapes inputs as arrays with at least three dimensions.
Note
In graph mode, returns a tuple of tensor instead of a list of tensors.
- Parameters:
*arys (Tensor) – one or more input tensors.
- Returns:
Tensor, or list of tensors, each with
a.ndim >= 3
. For example, a 1-D array of shape (N,) becomes a tensor of shape (1, N, 1), and a 2-D array of shape (M, N) becomes a tensor of shape (M, N, 1).- Raises:
TypeError – If the input is not a tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> a = np.ones((2, 3)) >>> b = np.ones(()) >>> c = np.ones(5) >>> output = np.atleast_3d(a, b, c) >>> print(output) [Tensor(shape=[2, 3, 1], dtype=Float32, value= [[[1.00000000e+00], [1.00000000e+00], [1.00000000e+00]], [[1.00000000e+00], [1.00000000e+00], [1.00000000e+00]]]), Tensor(shape=[1, 1, 1], dtype=Float32, value= [[[1.00000000e+00]]]), Tensor(shape=[1, 5, 1], dtype=Float32, value= [[[1.00000000e+00], [1.00000000e+00], [1.00000000e+00], [1.00000000e+00], [1.00000000e+00]]])]
-
tinyms.
average
(x, axis=None, weights=None, returned=False)[source]¶ Computes the weighted average along the specified axis.
- Parameters:
x (Tensor) – A Tensor to be averaged.
axis (Union[None, int, tuple(int)]) – Axis along which to average x. Default: None. If the axis is None, it will average over all of the elements of the tensor x. If the axis is negative, it counts from the last to the first axis.
weights (Union[None, Tensor]) – Weights associated with the values in x. Default: None. If weights is None, all the data in x are assumed to have a weight equal to one. If weights is 1-D tensor, the length must be the same as the given axis. Otherwise, weights should have the same shape as x.
returned (bool) – Default: False. If True, the tuple (average, sum_of_weights) is returned. If False, only the average is returned.
- Returns:
Averaged Tensor. If returned is True, return tuple.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> input_x = np.array([[1., 2.], [3., 4.]]) >>> output = np.average(input_x, axis=0, weights=input_x, returned=True) >>> print(output) (Tensor(shape=[2], dtype=Float32, value= [ 2.50000000e+00, 3.33333325e+00]), Tensor(shape=[2], dtype=Float32, value= [ 4.00000000e+00, 6.00000000e+00]))
-
tinyms.
bartlett
(M)[source]¶ Returns the Bartlett window. The Bartlett window is very similar to a triangular window, except that the end points are at zero. It is often used in signal processing for tapering a signal, without generating too much ripple in the frequency domain.
- Parameters:
M (int) – Number of points in the output window. If zero or less, an empty array is returned.
- Returns:
Tensor, the triangular window, with the maximum value normalized to one (the value one appears only if the number of samples is odd), with the first and last samples equal to zero.
- Raises:
TypeError – If M is not an int.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.bartlett(12)) [0. 0.18181819 0.36363637 0.5454545 0.72727275 0.9090909 0.9090909 0.72727275 0.5454545 0.36363637 0.18181819 0. ]
-
tinyms.
bincount
(x, weights=None, minlength=0, length=None)[source]¶ Count number of occurrences of each value in array of non-negative ints. The number of bins (of size 1) is one larger than the largest value in x. If minlength is specified, there will be at least this number of bins in the output array (though it will be longer if necessary, depending on the contents of x). Each bin gives the number of occurrences of its index value in x. If weights is specified the input array is weighted by it, i.e. if a value n is found at position i,
out[n] += weight[i]
instead ofout[n] += 1
.Note
The additional argument length specifies the number of bins (overriding
x.max() + 1
), which must be provided in graph mode. If x contains negative values, no error will be raised, and negative values are treated as zeros instead.- Parameters:
- Returns:
Tensor, the result of binning the input array. The length of out is equal to
np.amax(x)+1
.- Raises:
ValueError – If x is not one-dimensional, or if x and weights do not have the same shape.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.bincount(np.arange(5))) [1. 1. 1. 1. 1.] >>> print(np.bincount(np.array([0, 1, 1, 3, 2, 1, 7]))) [1. 3. 1. 1. 0. 0. 0. 1.] >>> w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights >>> x = np.array([0, 1, 1, 2, 2, 2]) >>> print(np.bincount(x, weights=w)) [0.3 0.7 1.1]
-
tinyms.
bitwise_and
(x1, x2, dtype=None)[source]¶ Computes the bit-wise AND of two arrays element-wise. Computes the bit-wise AND of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator &.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x1 (Tensor) – Input array.
x2 (Tensor) – Input array. Only integer and boolean types are handled. If
x1.shape != x2.shape
, they must be broadcastable to a common shape (which becomes the shape of the output).dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar, this is a scalar if both x1 and x2 are scalars.
- Supported Platforms:
Ascend
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.bitwise_and(13, 17)) 1
-
tinyms.
bitwise_not
(x, dtype=None)¶ Computes bit-wise inversion, or bit-wise NOT, element-wise. Computes the bit-wise NOT of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator ~. For signed integer inputs, the two’s complement is returned. In a two’s-complement system negative numbers are represented by the two’s complement of the absolute value. This is the most common method of representing signed integers on computers [1]. A N-bit two’s-complement system can represent every integer in the range
-2^{N-1}
to+2^{N-1}-1
.Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Supported dtypes on Ascend: np.int16, np.uint16.
- Parameters:
x (Tensor) – Only integer and boolean types are handled.
dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar.
- Supported Platforms:
Ascend
Examples
>>> import mindspore.numpy as np >>> print(np.invert(np.array(13, dtype=np.uint16))) 65522
-
tinyms.
bitwise_or
(x1, x2, dtype=None)[source]¶ Computes the bit-wise OR of two arrays element-wise. Computes the bit-wise OR of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator |.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x1 (Tensor) – Input array.
x2 (Tensor) – Input array. Only integer and boolean types are handled. If
x1.shape != x2.shape
, they must be broadcastable to a common shape (which becomes the shape of the output).dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar, this is a scalar if both x1 and x2 are scalars.
- Supported Platforms:
Ascend
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.bitwise_or(13, 16)) 29
-
tinyms.
bitwise_xor
(x1, x2, dtype=None)[source]¶ Computes the bit-wise XOR of two arrays element-wise. Computes the bit-wise XOR of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator ^.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x1 (Tensor) – Input array.
x2 (Tensor) – Input array. Only integer and boolean types are handled. If
x1.shape != x2.shape
, they must be broadcastable to a common shape (which becomes the shape of the output).dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar, this is a scalar if both x1 and x2 are scalars.
- Supported Platforms:
Ascend
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.bitwise_xor(13, 17)) 28
-
tinyms.
blackman
(M)[source]¶ Returns the Blackman window. The Blackman window is a taper formed by using the first three terms of a summation of cosines. It was designed to have close to the minimal leakage possible. It is close to optimal, only slightly worse than a Kaiser window.
- Parameters:
M (int) – Number of points in the output window. If zero or less, an empty array is returned.
- Returns:
Tensor, the window, with the maximum value normalized to one (the value one appears only if the number of samples is odd).
- Raises:
TypeError – If M is not an int.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> print(np.blackman(12)) [-1.4901161e-08 3.2606430e-02 1.5990365e-01 4.1439798e-01 7.3604518e-01 9.6704674e-01 9.6704674e-01 7.3604518e-01 4.1439798e-01 1.5990365e-01 3.2606430e-02 -1.4901161e-08]
-
tinyms.
broadcast_arrays
(*args)[source]¶ Broadcasts any number of arrays against each other.
Note
Numpy argument subok is not supported. In graph mode, returns a tuple of Tensor instead of a list of Tensor.
- Parameters:
*args (Tensor) – The arrays to broadcast.
- Returns:
List of Tensor.
- Raises:
ValueError – If arrays cannot be broadcast.
- Supported Platforms:
Ascend
GPU
CPU
Example
>>> import mindspore.numpy as np >>> x = np.array([[1,2,3]]) >>> y = np.array([[4],[5]]) >>> output = np.broadcast_arrays(x, y) >>> print(output) [Tensor(shape=[2, 3], dtype=Int32, value= [[1, 2, 3], [1, 2, 3]]), Tensor(shape=[2, 3], dtype=Int32, value= [[4, 4, 4], [5, 5, 5]])]
-
tinyms.
broadcast_to
(array, shape)[source]¶ Broadcasts an array to a new shape.
- Parameters:
- Returns:
Tensor, original array broadcast to the given shape.
- Raises:
ValueError – If array cannot be broadcast to shape.
- Supported Platforms:
Ascend
GPU
CPU
Example
>>> import mindspore.numpy as np >>> x = np.array([1, 2, 3]) >>> output = np.broadcast_to(x, (3, 3)) >>> print(output) [[1 2 3] [1 2 3] [1 2 3]]
-
tinyms.
cbrt
(x, dtype=None)[source]¶ Returns the cube-root of a tensor, element-wise.
Note
Numpy arguments casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> a = np.asarray([1, -1, 3, -8, 64]) >>> output = np.cbrt(a) >>> print(output) [ 1. -1. 1.4422495 -2. 4. ]
-
tinyms.
ceil
(x, dtype=None)[source]¶ Returns the ceiling of the input, element-wise.
The ceil of the scalar x is the smallest integer i, such that
i >= x
.Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. On GPU, the supported dtypes are np.float16, and np.float32.
- Parameters:
x (Tensor) – input values.
dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar, the floor of each element in x. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> output = np.ceil(a) >>> print(output) [-1. -1. -0. 1. 2. 2. 2.]
-
tinyms.
choose
(a, choices, mode='clip')[source]¶ Construct an array from an index array and a list of arrays to choose from. Given an “index” array a of integers and a sequence of n arrays (choices), a and each choice array are first broadcast, as necessary, to arrays of a common shape; calling these Ba and Bchoices[i], i = 0,…,n-1 we have that, necessarily,
Ba.shape == Bchoices[i].shape
for each i. Then, a new array withshape Ba.shape
is created as follows:if
mode='raise'
(the default), then, first of all, each element of a (and thus Ba) must be in the range [0, n-1]; now, suppose that i (in that range) is the value at the (j0, j1, …, jm) position in Ba - then the value at the same position in the new array is the value inBchoices[i]
at that same position;if
mode='wrap'
, values in a (and thus Ba) may be any (signed) integer; modular arithmetic is used to map integers outside the range[0, n-1]
back into that range; and then the new array is constructed as above;if
mode='clip'
, values in a (and thus Ba) may be any (signed) integer; negative integers are mapped to 0; values greater than n-1 are mapped to n-1; and then the new array is constructed as above.
Note
Numpy argument out is not supported.
mode = 'raise'
is not supported, and the default mode is ‘clip’ instead.- Parameters:
a (int array) – This array must contain integers in
[0, n-1]
, where n is the number of choices, unlessmode=wrap
ormode=clip
, in which cases any integers are permissible.choices (sequence of arrays) – Choice arrays. a and all of the choices must be broadcastable to the same shape. If choices is itself an array, then its outermost dimension (i.e., the one corresponding to
choices.shape[0]
) is taken as defining the “sequence”.mode ('raise', 'wrap', 'clip', optional) –
Specifies how indices outside
[0, n-1]
will be treated:’raise’ – raise an error;
’wrap’ – wrap around;
’clip’ – clip to the range. ‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.
- Returns:
Tensor, the merged result.
- Raises:
ValueError – If a and any of the choices cannot be broadcast.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> choices = [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]] >>> print(np.choose([2, 3, 1, 0], choices)) [20 31 12 3] >>> print(np.choose([2, 4, 1, 0], choices, mode='clip')) [20 31 12 3] >>> print(np.choose([2, 4, 1, 0], choices, mode='wrap')) [20 1 12 3] >>> a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]] >>> choices = [-10, 10] >>> print(np.choose(a, choices)) [[ 10 -10 10] [-10 10 -10] [ 10 -10 10]]
-
tinyms.
clip
(x, xmin, xmax, dtype=None)[source]¶ Clips (limits) the values in an array.
Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of \([0, 1]\) is specified, values smaller than 0 become 0, and values larger than 1 become 1.
- Parameters:
x (Tensor) – Tensor containing elements to clip.
xmin (Tensor, scalar, None) – Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of xmin and xmax may be None.
xmax (Tensor, scalar, None) – Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of xmin and xmax may be None. If xmin or xmax are tensors, then the three tensors will be broadcasted to match their shapes.
dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor, a tensor with the elements of x, but where values < xmin are replaced with xmin, and those > xmax with xmax.
- Raises:
TypeError – If inputs have types not specified above.
ValueError – If the shapes of x1 and x2 cannot broadcast, or both xmin and xmax are None.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.asarray([1, 2, 3, -4, 0, 3, 2, 0]) >>> output = np.clip(x, 0, 2) >>> print(output) [1 2 2 0 0 2 2 0]
-
tinyms.
column_stack
(tup)[source]¶ Stacks 1-D tensors as columns into a 2-D tensor. 2-D tensors are stacked as-is, like np.hstack.
- Parameters:
tup (Union[Tensor, tuple, list]) – A sequence of 1-D or 2-D tensors. All of them must have the same shape except the axis to be concatenated.
- Returns:
2-D Tensor, formed by stacking the given tensors.
- Supported Platforms:
Ascend
GPU
CPU
- Raises:
TypeError – If tup is not Tensor, list or tuple.
ValueError – If tup is empty.
Examples
>>> import mindspore.numpy as np >>> x1 = np.array([1, 2, 3]).astype('int32') >>> x2 = np.array([4, 5, 6]).astype('int32') >>> output = np.column_stack((x1, x2)) >>> print(output) [[1 4] [2 5] [3 6]]
-
tinyms.
concatenate
(arrays, axis=0)[source]¶ Joins a sequence of tensors along an existing axis.
Note
To match Numpy behaviour, \(axis >= 32\) will not cause value error, the axis will be treated as
None
instead.- Parameters:
- Returns:
A tensor concatenated from a tensor or a list of tensors.
- Raises:
TypeError – If input arguments have types not specified above.
ValueError – If axis is not in the range of \([-ndim, ndim-1]\), and less than 32.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x1 = np.ones((1,2,3)) >>> x2 = np.ones((1,2,1)) >>> x = np.concatenate((x1, x2), axis=-1) >>> print(x.shape) (1, 2, 4)
-
tinyms.
convolve
(a, v, mode='full')[source]¶ Returns the discrete, linear convolution of two one-dimensional sequences.
Note
If v is longer than a, the tensors are swapped before computation.
- Parameters:
a (Union[list, tuple, Tensor]) – First one-dimensional input tensor.
v (Union[list, tuple, Tensor]) – Second one-dimensional input tensor.
mode (str, optional) – By default, mode is ‘full’. This returns the convolution at each point of overlap, with an output shape of \((N+M-1,)\). At the end-points of the convolution, the signals do not overlap completely, and boundary effects may be seen. If mode is ‘same’, it returns output of length \(max(M, N)\). Boundary effects are still visible. If mode is ‘valid’, it returns output of length \(max(M, N) - min(M, N) + 1\). The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect.
- Returns:
Tensor, discrete, linear convolution of a and v.
- Raises:
TypeError – If the inputs have types not specified above.
ValueError – If a and v are empty or have wrong dimensions
- Supported Platforms:
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> output = np.convolve([1., 2., 3., 4., 5.], [2., 3.], mode="valid") >>> print(output) [ 7. 12. 17. 22.]
-
tinyms.
copysign
(x1, x2, dtype=None)[source]¶ Changes the sign of x1 to that of x2, element-wise.
If x2 is a scalar, its sign will be copied to all elements of x1.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported. Complex inputs are not supported now.
- Parameters:
x1 (Union[int, float, list, tuple, Tensor]) – Values to change the sign of.
x2 (Union[int, float, list, tuple, Tensor]) – The sign of x2 is copied to x1. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).
dtype (
mindspore.dtype
, optional) – Defaults to None. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. The values of x1 with the sign of x2. This is a scalar if both x1 and x2 are scalars.
- Raises:
TypeError – If dtype of the input is not in the given types or the input can not be converted to tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> output = np.copysign(np.array([1, -1, -1]), np.array([-1, 1, -1])) >>> print(output) [-1 1 -1]
-
tinyms.
corrcoef
(x, y=None, rowvar=True, dtype=None)[source]¶ Returns Pearson product-moment correlation coefficients.
Please refer to the documentation for cov for more detail. The relationship between the correlation coefficient matrix, R, and the covariance matrix, C, is \(R_{ij} = \frac{ C_{ij} } { \sqrt{ C_{ii} * C_{jj} } }\) The values of R are between -1 and 1, inclusive.
Note
Currently, complex numbers are not supported.
- Parameters:
x (Union[int, float, bool, tuple, list, Tensor]) – A 1-D or 2-D array containing multiple variables and observations. Each row of x represents a variable, and each column a single observation of all those variables. Also see rowvar below.
y (Union[int, float, bool, tuple, list, Tensor], optional) – An additional set of variables and observations. Default: None.
rowvar (bool, optional) – If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations. Default: True.
dtype (
mindspore.dtype
, optional) – Data-type of the result. By default, the return data-type will have at least float32 precision. Default: None.
- Returns:
Tensor, The correlation coefficient matrix of the variables.
- Raises:
TypeError – If the inputs have types not specified above.
ValueError – If x and y have wrong dimensions.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> output = np.corrcoef([[2., 3., 4., 5.], [0., 2., 3., 4.], [7., 8., 9., 10.]]) >>> print(output) [[1. 0.9827076 1. ] [0.9827077 0.99999994 0.9827077 ] [1. 0.9827076 1. ]]
-
tinyms.
correlate
(a, v, mode='valid')[source]¶ Cross-correlation of two 1-dimensional sequences.
This function computes the correlation as generally defined in signal processing texts:
\(c_{av}[k] = sum_n a[n+k] * conj(v[n])\)
with a and v sequences being zero-padded where necessary and conj being the conjugate.
Note
Currently, complex numbers are not supported.
- Parameters:
mode (str, optional) – By default, mode is ‘valid’. If mode is ‘valid’, it returns output of length \(max(M, N) - min(M, N) + 1\). The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect. If mode is ‘full’, it returns the convolution at each point of overlap, with an output shape of \((N + M - 1,)\). At the end-points of the convolution, the signals do not overlap completely, and boundary effects may be seen. If mode is ‘same’, it returns output of length \(max(M, N)\). Boundary effects are still visible.
- Returns:
Tensor. Discrete cross-correlation of a and v.
- Raises:
TypeError – If the inputs can not be converted to tensor.
ValueError – If a and v are empty or have wrong dimensions
- Supported Platforms:
GPU
Examples
>>> import mindspore.numpy as np >>> output = np.correlate([1, 2, 3], [0, 1, 0.5]) >>> print(output) [3.5] >>> output = np.correlate([1, 2, 3], [0, 1, 0.5], mode="same") >>> print(output) [2. 3.5 3. ] >>> output = np.correlate([1, 2, 3, 4, 5], [1, 2], mode="same") >>> print(output) [ 2. 5. 8. 11. 14.]
-
tinyms.
cos
(x, dtype=None)[source]¶ Cosine element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.arange(5).astype('float32') >>> print(np.cos(x)) [ 1. 0.5403023 -0.41614684 -0.9899925 -0.6536436 ]
-
tinyms.
cosh
(x, dtype=None)[source]¶ Hyperbolic cosine, element-wise.
Note
Numpy arguments out, where, casting, order, subok, signature, and extobj are not supported.
- Parameters:
x (Tensor) – Input tensor.
dtype (
mindspore.dtype
, optional) – Default:None
. Overrides the dtype of the output Tensor.
- Returns:
Tensor or scalar. This is a scalar if x is a scalar.
- Supported Platforms:
Ascend
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.arange(5).astype('float32') >>> print(np.cosh(x)) [ 1. 1.5430807 3.7621956 10.067662 27.308233 ]
-
tinyms.
count_nonzero
(x, axis=None, keepdims=False)[source]¶ Counts the number of non-zero values in the tensor x.
- Parameters:
x (Tensor) – The tensor for which to count non-zeros.
axis (Union[int,tuple], optional) – Axis or tuple of axes along which to count non-zeros. Default is None, meaning that non-zeros will be counted along a flattened version of x. Default: None.
keepdims (bool, optional) – If this is set to True, the axes that are counted are left in the result as dimensions with size one. With this option, the result will broadcast correctly against x. Default: False.
- Returns:
Tensor, indicating number of non-zero values in the x along a given axis. Otherwise, the total number of non-zero values in x is returned.
- Raises:
TypeError – If axis is not int or tuple.
ValueError – If axis is not in range [-x.ndim, x.ndim).
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.asarray([1, 2, 3, -4, 0, 3, 2, 0]) >>> output = np.count_nonzero(x) >>> print(output) 6
-
tinyms.
cov
(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None, dtype=None)[source]¶ Estimates a covariance matrix, given data and weights.
Covariance indicates the level to which two variables vary together. If we examine N-dimensional samples, \(X = [x_1, x_2, ... x_N]^T\), then the covariance matrix element \(C_{ij}\) is the covariance of \(x_i\) and \(x_j\). The element \(C_{ii}\) is the variance of \(x_i\).
Note
fweights and aweights must be all positive, in Numpy if negative values are detected, a value error will be raised, in MindSpore we converts all values to positive instead.
- Parameters:
m (Union[Tensor, list, tuple]) – A 1-D or 2-D tensor containing multiple variables and observations. Each row of m represents a variable, and each column represents a single observation of all those variables. Also see rowvar below.
y (Union[Tensor, list, tuple], optional) – An additional set of variables and observations. y has the same form as that of m, default is
None
.rowvar (bool, optional) – If rowvar is
True
(default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations.bias (bool, optional) – Default Normalization (
False
) is by \((N - 1)\), where \(N\) is the number of observations given (unbiased estimate). If bias isTrue
, then Normalization is by N. These values can be overridden by using the keyword ddof.ddof (int, optional) – If not
None
, the default value implied by bias is overridden. Note that \(ddof=1\) will return the unbiased estimate, even if both fweights and aweights are specified, and \(ddof=0\) will return the simple average. See the notes for the details. The default value isNone
.fweights (Union[Tensor, list, tuple], optional) – 1-D tensor of integer frequency weights; the number of times each observation vector should be repeated. The default value is
None
.aweights (Union[Tensor, list, tuple], optional) – 1-D tensor of observation vector weights. These relative weights are typically larger for observations considered more important and smaller for observations considered less important. If \(ddof=0\) the tensor of weights can be used to assign probabilities to observation vectors. The default value is
None
.dtype (Union[
mindspore.dtype
, str], optional) – Data-type of the result. By default, the return data-type will have mstype.float32 precision. Default isNone
.
- Returns:
Tensor, the covariance matrix of the variables.
- Raises:
TypeError – If the inputs have types not specified above.
ValueError – If m and y have wrong dimensions.
RuntimeError – If aweights and fweights have dimensions > 2.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> output = np.cov([[2., 3., 4., 5.], [0., 2., 3., 4.], [7., 8., 9., 10.]]) >>> print(output) [[1.6666666 2.1666667 1.6666666] [2.1666667 2.9166667 2.1666667] [1.6666666 2.1666667 1.6666666]]
-
tinyms.
cross
(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None)[source]¶ Returns the cross product of two (arrays of) vectors.
The cross product of a and b in \(R^3\) is a vector perpendicular to both a and b. If a and b are arrays of vectors, the vectors are defined by the last axis of a and b by default, and these axes can have dimensions 2 or 3. Where the dimension of either a or b is 2, the third component of the input vector is assumed to be zero and the cross product calculated accordingly. In cases where both input vectors have dimension 2, the z-component of the cross product is returned.
- Parameters:
a (Union[list, tuple, Tensor]) – Components of the first vector(s).
b (Union[list, tuple, Tensor]) – Components of the second vector(s).
axisa (int, optional) – Axis of a that defines the vector(s). By default, the last axis.
axisb (int, optional) – Axis of b that defines the vector(s). By default, the last axis.
axisc (int, optional) – Axis of c containing the cross product vector(s). Ignored if both input vectors have dimension 2, as the return is scalar. By default, the last axis.
axis (int, optional) – If defined, the axis of a, b and c that defines the vector(s) and cross product(s). Overrides axisa, axisb and axisc. Defaults to None.
- Returns:
Tensor, vector cross product(s).
- Raises:
ValueError – when the dimensions of the vector(s) in a and/or b does not equal 2 or 3.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> x = np.array([[1,2,3], [4,5,6]]) >>> y = np.array([[4,5,6], [1,2,3]]) >>> output = np.cross(x, y) >>> print(output) [[-3 6 -3] [ 3 -6 3]] >>> output = np.cross(x, y, axisc=0) >>> print(output) [[-3 3] [ 6 -6] [-3 3]]
-
tinyms.
cumprod
(a, axis=None, dtype=None)[source]¶ Returns the cumulative product of elements along a given axis.
Note
Numpy argument out is not supported.
- Parameters:
- Returns:
Tensor.
- Raises:
TypeError – If the input can not be converted to tensor or axis is not integer.
ValueError – If axis is out of range.
- Supported Platforms:
Ascend
GPU
Examples
>>> import mindspore.numpy as np >>> x = np.array([1, 2, 3]) >>> print(np.cumprod(x)) [1 2 6]
-
tinyms.
cumsum
(a, axis=None, dtype=None)[source]¶ Returns the cumulative sum of the elements along a given axis.
Note
If
a.dtype
isint8
,int16
orbool
, the result dtype will be elevated toint32
.- Parameters:
a (Tensor) – Input tensor.
axis (int, optional) – Axis along which the cumulative sum is computed. The default (None) is to compute the cumsum over the flattened array.
dtype (
mindspore.dtype
, optional) – If not specified, stay the same as a, unless a has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used. Default: None.
- Returns:
Tensor.
- Raises:
TypeError – If input arguments have types not specified above.
ValueError – If axis is out of range.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore.numpy as np >>> output = np.cumsum(np.ones((3,3)), axis=0) >>> print(output) [[1. 1. 1.] [2. 2. 2.] [3. 3. 3.]]
-