mindspore.Tensor
- class mindspore.Tensor(input_data=None, dtype=None, shape=None, init=None, internal=False, const_arg=False, device=None)[source]
Tensor is a data structure that stores an n-dimensional array.
Note
If init interface is used to initialize Tensor, the Tensor.init_data API needs to be called to load the actual data to Tensor.
All modes of CPU and GPU, and Atlas training series with graph mode (mode=mindspore.GRAPH_MODE) do not supported in-place operations yet.
Warning
To convert dtype of a Tensor, it is recommended to use Tensor.astype() rather than Tensor(sourceTensor, dtype=newDtype).
- 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 isNone, the data type of the output Tensor will be the same as the input_data. Default:None.shape (Union[tuple, list, int,
mindspore.Symbol]) – Used to indicate the shape of the output Tensor. If input_data is available, shape doesn't need to be set. IfNoneor Symbol exists in shape , a tensor of dynamic shape is created, input_data doesn't need to be set; if only integers exist 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, when using init, dtype and shape must be set. 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.device (str) – This parameter is reserved and does not need to be configured. Default:
None.
- Outputs:
Tensor.
Note
The default value
Noneof input_data works as a placeholder, it does not mean that we can create a NoneType Tensor. Tensor with shape contains 0 is not fully tested and supported.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
Returns absolute value of a tensor element-wise. |
|
Alias for |
|
Computes arccosine of self element-wise. |
|
Computes inverse hyperbolic cosine of self element-wise. |
|
In-place version of |
|
Adds other value to self element-wise. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
In-place version of |
|
Performs a matrix-vector product of mat and vec, and add self to the final result. |
|
For details, please refer to |
|
For details, please refer to |
|
Reduces a dimension of self by the "logical AND" of all elements in the dimension, by default. |
|
Returns a new Tensor with boolean elements representing if each element of self is "close" to the corresponding element of other. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Reduces a dimension of self by the "logical OR" of all elements in the dimension, by default. |
|
For details, please refer to |
|
For details, please refer to |
|
Alias for |
|
Alias for |
|
Alias for |
|
Alias for |
|
Alias for |
|
Alias for |
|
Alias for |
|
Return the indices of the maximum values of self across a dimension. |
|
Compute the max value of input Tensor on the specified axis, and return the max value and index. |
|
Returns the indices of the minimum value of self across the axis. |
|
Compute the max value of input Tensor on the specified axis, return the minimum value and index. |
|
Sorts self along the given dimension in specified order and return the sorted indices. |
|
For details, please refer to |
|
Computes arcsine of self element-wise. |
|
Computes inverse hyperbolic sine of self element-wise. |
|
Convert tensor to numpy array. |
|
Assign another tensor value to this tensor. |
|
Return a copy of the tensor, cast to a specified type. |
|
Computes the trigonometric inverse tangent of self element-wise. |
|
Returns arctangent of self/other element-wise. |
|
Computes inverse hyperbolic tangent of self element-wise. |
|
For details, please refer to |
|
For details, please refer to |
|
Converts input tensor dtype to bfloat16. |
|
Count the occurrences of each value in the self. |
|
For details, please refer to |
|
Returns bitwise not of self. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Converts input tensor dtype to bool. |
|
For details, please refer to |
|
Converts input tensor dtype to uint8. |
|
Fills the tensor with numbers drawn from the Cauchy distribution. |
|
Rounds a tensor up to the closest integer element-wise. |
|
For details, please refer to |
|
For details, please refer to |
|
Construct a tensor from an index tensor and a list of tensors to choose from. |
|
Cut the self Tensor into chunks sub-tensors along the specified dimension. |
|
Clamps tensor values between the specified minimum value and maximum value. |
|
In-place version of |
|
Alias for |
|
Returns a copy of self. |
|
For details, please refer to |
|
For details, please refer to |
|
Converts a Tensor into a continuous-memory Tensor that contains the same data as the original Tensor. |
|
Return a copy of the tensor. |
|
Copies the elements from src into self tensor and returns self. |
|
For details, please refer to |
|
Computes cosine of self element-wise. |
|
Computes hyperbolic cosine of self element-wise. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Computes the cumulative sum of self Tensor along dim. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Divides the self tensor by the other input tensor in floating-point type element-wise. |
|
In-place version of |
|
Alias for |
|
Computes the dot product of two 1D tensor. |
|
Converts input tensor dtype to float64. |
|
For details, please refer to |
|
Return the dtype of the tensor ( |
|
For details, please refer to |
|
Computes the equivalence between two tensors element-wise. |
|
For details, please refer to |
|
Computes the Gauss error function of self element-wise. |
|
Computes the complementary error function of self element-wise. |
|
For details, please refer to |
|
Returns exponential of a tensor element-wise. |
|
Inplace version of |
|
For details, please refer to |
|
Expand the shape of the input tensor to be the same as the another input tensor. |
|
For details, please refer to |
|
Returns exponential then minus 1 of a tensor element-wise. |
|
Fills self tensor with the specified value . |
|
Fills the main diagonal of a Tensor with a specified value and returns the result. |
|
Flatten a tensor along dimensions from start_dim to end_dim. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Converts input tensor dtype to float32. |
|
For details, please refer to |
|
Rounds a tensor down to the closest integer element-wise. |
|
In-place version of |
|
For details, please refer to |
|
Flush cache data to host if tensor is cache enable. |
|
For details, please refer to |
|
Computes the floating-point remainder of the division operation self/other. |
|
For details, please refer to |
|
Calculates the fractional part of each element in self. |
|
Convert numpy array to Tensor. |
|
Gather data from a tensor by indices. |
|
For details, please refer to |
|
For details, please refer to |
|
Computes greatest common divisor of input tensors element-wise. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Compare the value of the input parameters \(self > other\) element-wise, and the output result is a bool value. |
|
Computes the boolean value of \(self >= other\) element-wise. |
|
For details, please refer to :func:'mindspore.Tensor.greater'. |
|
Returns a view of a matrix (2-D tensor) conjugated and transposed. |
|
Converts input tensor dtype to float16. |
|
Hard Shrink activation function. |
|
Whether tensor is initialized. |
|
For details, please refer to |
|
Computes the histogram of self. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Accumulate the elements of alpha times source into the self by adding to the index in the order given in index. |
|
For details, please refer to |
|
Based on the indices in indices, replace the corresponding elements in Tensor self with the values in values. |
|
Based on the indices in indices, replace the corresponding elements in Tensor self with the values in values. |
|
Generates a new Tensor that accesses the values of self along the specified axis dimension using the indices specified in index. |
|
Get the tensor format data of this Tensor. |
|
For details, please refer to |
|
For details, please refer to |
|
Converts input tensor dtype to int32. |
|
For details, please refer to |
|
Compute the inverse of the self matrix. |
|
For details, please refer to |
|
Returns a tensor of Boolean values indicating whether each element of input is "close" to the corresponding element of other. |
|
Determine which elements are finite for each position. |
|
For details, please refer to |
|
Determines whether the memory of tensor is contiguous. |
|
For details, please refer to |
|
Determines which elements are inf or -inf for each position. |
|
For details, please refer to |
|
Determines which elements are -inf for each position. |
|
For details, please refer to |
|
For details, please refer to |
|
Judge whether the data type of tensor is a signed data type. |
|
Return the value of this tensor as standard Python number. |
|
Insert scalar into a tensor (scalar is cast to tensor's dtype, if possible). |
|
Return the length of one tensor element in bytes. |
|
For details, please refer to |
|
For details, please refer to |
|
Computes the boolean value of \(self <= other\) element-wise. |
|
For more details, please refer to |
|
Computes the boolean value of \(self < other\) element-wise. |
|
Computes the boolean value of \(self <= other\) element-wise. |
|
Returns the natural logarithm of a tensor element-wise. |
|
Returns the logarithm to the base 10 of a tensor element-wise. |
|
Returns the natural logarithm of one plus the self tensor element-wise. |
|
Returns the logarithm to the base 2 of a tensor element-wise |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Computes the "logical AND" of two tensors element-wise. |
|
Computes the "logical NOT" of a tensor element-wise. |
|
Computes the "logical OR" of two tensors element-wise. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Fills the elements of the input tensor with log normal values initialized by given mean and std: |
|
Converts input tensor dtype to int64. |
|
For more details, please refer to |
|
For details, please refer to |
|
Fills elements of Tensor with value where mask is True. |
|
In-place version of |
|
Updates the value in the "self Tensor" with the tensor value according to the mask, and returns a Tensor. |
|
Returns a new 1-D Tensor which indexes self according to the boolean mask. |
|
Returns the matrix product of two tensors. |
|
Returns the maximum value of the self tensor. |
|
Computes the maximum of input tensors element-wise. |
|
Reduces all dimension of a tensor by averaging all elements in the dimension, by default. |
|
Computes the median and indices of input tensor. |
|
Transpose self . |
|
Accessing this property is equivalent to Calling self.adjoint(). |
|
Returns the minimum value of the self tensor. |
|
Computes the minimum of input tensors element-wise. |
|
Returns the matrix product of two arrays. |
|
For details, please refer to |
|
For details, please refer to |
|
Copy Tensor to target device synchronously or asynchronously, default synchronously. |
|
For details, please refer to |
|
Returns the Tensor that exchanges the last two dimensions. |
|
Multiplies two tensors element-wise. |
|
Multiplies two tensors element-wise. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Replace the NaN, positive infinity and negative infinity values of the self with the specified values in nan, posinf and neginf respectively. |
|
For details, please refer to |
|
For details, please refer to |
|
Computes sum of input Tensor over a given dimension, treating NaNs as zero. |
|
Obtains a tensor of a specified length at a specified start position along a specified axis. |
|
Return the total number of bytes taken by the tensor. |
|
Return the number of tensor dimensions. |
|
Alias for |
|
Alias for |
|
Returns a tensor with negative values of self element-wise. |
|
Alias for |
|
Alias for |
|
Return a tensor of size filled with ones. |
|
Return a tensor of size filled with zeros. |
|
For details, please refer to |
|
Return the positions of all non-zero values. |
|
For details, please refer to |
|
Update the self tensor in place by generating random numbers sampled from the normal distribution which constructed by the parameters mean and std. |
|
Computes the non-equivalence of two tensors element-wise. |
|
Returns a Scalar of type int that represents the total number of elements in the Tensor. |
|
Alias for |
|
For details, please refer to |
|
For details, please refer to |
|
Return outer product of self and vec2. |
|
Tensor.permute supports unpacking the axis argument automatically when it is passed as an indefinite number of positional arguments, which has a slight difference from the input parameter of |
|
For details, please refer to |
|
Calculates the exponent power of each element in self. |
|
Reduces a dimension of a tensor by multiplying all elements in the dimension, by default. |
|
The name of the function comes from the acronym for "peak to peak". |
|
For details, please refer to |
|
Fill the tensor with numbers sampled from a discrete uniform distribution over an interval \([from\_, to-1]\). |
|
For details, please refer to |
|
Return a contiguous flattened tensor. |
|
For details, please refer to |
|
Returns reciprocal of a tensor element-wise. |
|
Registers a backward hook for tensor. |
|
Computes the remainder of self divided by other element-wise. |
|
For details, please refer to |
|
Repeat elements of a tensor. |
|
Repeat elements of a tensor along a dim, like |
|
Rearranges self Tensor based on the given shape. |
|
Change the shape of the Tensor to the shape of other without changing the data. |
|
Changes shape and size of tensor in-place. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Returns half to even of a tensor element-wise. |
|
Computes reciprocal of square root of self tensor element-wise. |
|
Update the value in src to self according to the specified index. |
|
Update the value in src to update self according to the specified index. |
|
Add all elements in src to the index specified by index to self along dimension specified by dim. |
|
Add all elements in src to the index specified by index to self along dimension specified by dim, scatter_add is an in-place operation. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
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. |
|
Slices the self tensor along the selected dimension at the given index. |
|
For details, please refer to |
|
Specify whether the tensor is a constant when it is used for the argument of a network. |
|
For details, please refer to |
|
For details, please refer to |
|
Return a copy of the tensor, cast to int16 type, equivalent to self.astype(mstype.int16). |
|
Computes Sigmoid of self element-wise. |
|
For details, please refer to |
|
For details, please refer to |
|
Computes sine of self element-wise. |
|
Computes the normalized sinc of self. |
|
Computes hyperbolic sine of the self element-wise. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Sorts the elements of the self tensor along the given dimension in the specified order. |
|
Splits the Tensor into chunks along the given dim. |
|
Returns sqrt of self element-wise. |
|
Returns square of self element-wise. |
|
For details, please refer to |
|
For details, please refer to |
|
Tensor's offset in the underlying storage in terms of the number of storage elements. |
|
The stride to jump from one element to the next in the input dim. |
|
Return the tuple of bytes to step in each dimension when traversing a tensor. |
|
Subtracts scaled other value from self Tensor. |
|
For details, please refer to |
|
This interface is deprecated from version 2.4 and will be removed in a future version. |
|
Calculate sum of Tensor elements over a given dim. |
|
Sum self Tensor to the size. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Return the transposed tensor. |
|
Transpose self . |
|
Takes elements from a tensor along an axis. |
|
Computes tangent of self element-wise. |
|
Computes hyperbolic tangent of self element-wise. |
|
For details, please refer to |
|
Replicates an tensor with given dims times. |
|
Performs tensor dtype conversion. |
|
Convert a Tensor to COOTensor. |
|
Convert a Tensor to CSRTensor. |
|
Convert a Tensor to List. |
|
Finds values and indices of the k largest or smallest entries along a given dimension. |
|
Return the sum along diagonals of the tensor. |
|
Interchange two axes of a tensor. |
|
Returns the lower triangle part of input (elements that contain the diagonal and below), and set the other elements to zeros. |
|
Returns the upper triangle part of 'self' (elements that contain the diagonal and below), and set the other elements to zeros. |
|
Alias for Tensor.div() with \(rounding\_mode=None\). |
|
Returns a new tensor with the truncated integer values of the elements of the input tensor. |
|
Change the dtype of the Tensor to the dtype . |
|
Returns self tensor cast to the type of the with the input other tensor. |
|
Unbind the tensor dimension in specified axis. |
|
For details, please refer to |
|
Generates random numbers that follows a uniform distribution within the half-open interval \([from\_, to)\). |
|
Update the self tensor in place by generating random numbers sampled from uniform distribution in the half-open interval \([from\_, to)\). |
|
Returns the unique elements of self. |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
For details, please refer to |
|
Adds an additional dimension to self at the given dim. |
|
Compute the variance along the specified axis. |
|
Reshape the tensor according to the input shape. |
|
View self Tensor as the same shape as other . |
|
For details, please refer to |
|
Selects elements from self or y based on condition and returns a tensor. |
|
For details, please refer to |
|
For details, please refer to |
|
Return a tensor filled with zeros. |