mindspore.Tensor
- class mindspore.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. 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.
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
Neural Network Layer Methods
Neural Network
API Name |
Description |
Supported Platforms |
Return a copy of the tensor collapsed into one dimension. |
|
Activation Function
API Name |
Description |
Supported Platforms |
Apply the Hard Shrink function for tensor. |
|
|
Apply the soft shrink function for a tensor. |
|
Element-wise Methods
API Name |
Description |
Supported Platforms |
Return absolute value element-wisely. |
|
|
Performs the element-wise division of tensor x1 by tensor x2, multiply the result by the scalar value and add it to input_data. |
|
|
Performs the element-wise product of tensor x1 and tensor x2, multiply the result by the scalar value and add it to input_data. |
|
|
Returns arctangent of x/y element-wise. |
|
|
Randomly set the elements of output to 0 or 1 with the probability of P which follows the Bernoulli distribution. |
|
|
Returns bitwise and of two tensors element-wise. |
|
|
Returns bitwise or of two tensors element-wise. |
|
|
Returns bitwise xor of two tensors element-wise. |
|
|
Rounds a tensor up to the closest integer element-wise. |
|
|
Computes hyperbolic cosine of x element-wise. |
|
|
Computes the Gauss error function of self tensor element-wise. |
|
|
Computes the complementary error function of self tensor element-wise. |
|
|
Returns exponential of a tensor element-wise. |
|
|
Computes Reciprocal of this Tensor element-wise. |
|
|
Flips all bits of this Tensor element-wise. |
|
|
Does a linear interpolation of two tensors start and end based on a float or tensor weight. |
|
|
Returns the natural logarithm of self Tensor element-wise. |
|
|
Returns the natural logarithm of one plus the input tensor element-wise. |
|
|
Calculate the logit of a tensor element-wise. |
|
|
Calculate the power of Tensor. |
|
|
Returns half to even of the tensor element-wise. |
|
|
Sigmoid activation function. |
|
|
Returns sqrt of a tensor element-wise. |
|
|
Returns square of a tensor element-wise. |
|
|
Compute the standard deviation along the specified axis. |
|
|
Refer to |
|
|
Computes the singular value decompositions of one or more matrices. |
|
|
Computes tangent of x element-wise. |
|
|
Tanh activation function. |
|
|
Compute the variance along the specified axis. |
|
|
Divides self tensor by the input tensor element-wise. |
|
|
Computes the self tensor multiplied by the logarithm of input tensor element-wise. |
|
Reduction Methods
API Name |
Description |
Supported Platforms |
Reduces a dimension of a tensor by the maximum value in the dimension, by default. |
|
|
Reduces a dimension of a tensor by the minimum value in the dimension, by default. |
|
|
Return the indices of the maximum values along an axis. |
|
|
Return the indices of the minimum values along an axis. |
|
|
Returns the maximum value with corresponding index. |
|
|
Returns the minimum value with corresponding index. |
|
|
Return the maximum of a tensor or maximum along an axis. |
|
|
Reduces a dimension of a tensor by averaging all elements in the dimension, by default. |
|
|
Computes the median of input tensor. |
|
|
Return the minimum of a tensor or minimum along an axis. |
|
|
Returns the matrix norm or vector norm of a given tensor. |
|
|
Reduces a dimension of a tensor by product all elements in the dimension, by default. |
|
|
Renormalizes the sub-tensors along dimension dim, and each sub-tensor's p-norm should not exceed the 'maxnorm'. |
|
Comparison Methods
API Name |
Description |
Supported Platforms |
Check all tensor elements along a given axis evaluate to True. |
|
|
Check any tensor element along a given axis evaluate to True. |
|
|
Returns True if abs(x-y) is smaller than tolerance element-wise, otherwise False. |
|
|
Whether tensor is initialized. |
|
|
Returns a boolean Tensor where two Tensors are element-wise equal within a tolerance. |
|
|
Determines which elements are finite for each position. |
|
|
Finds values and indices of the k largest entries along the last dimension. |
|
Linear Algebraic Methods
API Name |
Description |
Supported Platforms |
Ger product of self and x. |
|
|
Computes the sign and the log of the absolute value of the determinant of one or more square matrices. |
|
|
Computes the determinant of one or more square matrices. |
|
Tensor Operation Methods
Tensor Construction
API Name |
Description |
Supported Platforms |
Construct a tensor from an index tensor and a list of tensors to choose from. |
|
|
Fill the tensor with a scalar value. |
|
|
Create a tensor of the same shape and type as the input tensor and fill it with specified value. |
|
|
Reshape the tensor according to the input shape. |
|
Random Generation Methods
API Name |
Description |
Supported Platforms |
Generates random samples from a given categorical distribution tensor. |
|
Array Methods
API Name |
Description |
Supported Platforms |
Broadcasts input tensor to a given shape. |
|
|
Combines an array of sliding local blocks into a large containing tensor. |
|
|
Return a copy of the tensor. |
|
|
Returns a tuple (values,indices) where 'values' is the cumulative maximum value of self Tensor along the dimension axis, and indices is the index location of each maximum value. |
|
|
Returns a tuple (values,indices) where 'values' is the cumulative minimum value of self Tensor along the dimension axis, and indices is the index location of each minimum value. |
|
|
Return the cumulative sum of the elements along a given axis. |
|
|
Constructs a diagonal tensor with a given diagonal values. |
|
|
Return specified diagonals. |
|
|
Return the dtype of the tensor ( |
|
|
Expand the dimension of target tensor to the dimension of input tensor. |
|
|
Insert a dimension of shape 1 at the specified axis of Tensor |
|
|
Returns the slice of the input tensor corresponding to the elements of input_indices on the specified axis. |
|
|
Gathers elements along an axis specified by dim. |
|
|
Gathers slices from a input tensor by indices. |
|
|
Fills the elements under the dim dimension of the self Tensor with the input value by selecting the indices in the order given in index. |
|
|
Get the tensor format data of this Tensor. |
|
|
Update some rows of a tensor with values of v according to the specified indices. |
|
|
Get the item at the specified index of the tensor. |
|
|
Insert scalar into a tensor (scalar is cast to tensor's dtype, if possible). |
|
|
Return the length of one tensor element in bytes. |
|
|
Fills elements of self tensor with value where mask is True. |
|
|
Returns a new 1-D Tensor which indexes the self tensor according to the boolean mask. |
|
|
Computes the minimum of self and input tensor element-wise. |
|
|
Return the total number of bytes taken by the tensor. |
|
|
Return the number of tensor dimensions. |
|
|
Return a Tensor of the positions of all non-zero values. |
|
|
Returns a narrowed tensor from input tensor. |
|
|
The name of the function comes from the acronym for "peak to peak". |
|
|
Return a contiguous flattened tensor. |
|
|
Repeat elements of a tensor. |
|
|
Give a new shape to a tensor without changing its data. |
|
|
Changes shape and size of tensor in-place. |
|
|
Creates a new tensor by adding the values from the positions in self tensor indicated by indices, with values from updates. |
|
|
Creates a new tensor by dividing the values from the positions in self tensor indicated by indices, with values from updates. |
|
|
By comparing the value at the position indicated by indices in x with the value in the updates, the value at the index will eventually be equal to the largest one to create a new tensor. |
|
|
By comparing the value at the position indicated by indices in self tensor with the value in the updates, the value at the index will eventually be equal to the smallest one to create a new tensor. |
|
|
Creates a new tensor by multiplying the values from the positions in self tensor indicated by indices, with values from updates. |
|
|
Creates a new tensor by subtracting the values from the positions in self tensor indicated by indices, with values from updates. |
|
|
Finds indices where elements should be inserted to maintain order. |
|
|
The conditional tensor determines whether the corresponding element in the output must be selected from the current Tensor (if true) or \(y\) (if false) based on the value of each element. |
|
|
Returns the shape of the tensor as a tuple. |
|
|
Returns the total number of elements in tensor. |
|
|
Splits a tensor into output_num of tensors along the given axis and output numbers. |
|
|
Remove the dimension of shape 1 from the Tensor |
|
|
Return the tuple of bytes to step in each dimension when traversing a tensor. |
|
|
Return sum of tensor elements over a given axis. |
|
|
Interchange two axes of a tensor. |
|
|
Return the transposed tensor. |
|
|
Takes elements from a tensor along an axis. |
|
|
Return init_data() and get the tensor format data of this Tensor. |
|
|
Return the sum along diagonals of the tensor. |
|
|
Return a tensor with axes transposed. |
|
|
Returns the elements that are unique in each consecutive group of equivalent elements in the input tensor. |
|
|
Returns unique elements and relative indexes in 1-D tensor, filled with padding num. |
|
|
Computes the maximum along segments of a tensor. |
|
|
Computes the minimum of a tensor along segments. |
|
|
Computes the product of a tensor along segments. |
|
Type Conversion
API Name |
Description |
Supported Platforms |
Convert tensor to numpy array. |
|
|
Return a copy of the tensor, cast to a specified type. |
|
|
Convert numpy array to Tensor. |
|
|
Convert a Tensor to COOTensor. |
|
|
Convert a Tensor to CSRTensor. |
|
Gradient Clipping
API Name |
Description |
Supported Platforms |
Clips (limits) the values in a Tensor. |
|
Parameter Operation Methods
API Name |
Description |
Supported Platforms |
Assign another tensor value to this tensor. |
|
Other Methods
API Name |
Description |
Supported Platforms |
Flush cache data to host if tensor is cache enable. |
|
|
Specify whether the tensor is a constant when it is used for the argument of a network. |
|