Browse Source

!11886 Bug fix for api in tensor.py

From: @yanglf1121
Reviewed-by: @c_34,@liangchenghui
Signed-off-by: @liangchenghui
tags/v1.2.0-rc1
mindspore-ci-bot Gitee 4 years ago
parent
commit
ff7c2727f1
1 changed files with 38 additions and 42 deletions
  1. +38
    -42
      mindspore/common/tensor.py

+ 38
- 42
mindspore/common/tensor.py View File

@@ -252,22 +252,22 @@ class Tensor(Tensor_):


@property @property
def shape(self): def shape(self):
"""The shape of tensor is a tuple."""
"""Returns the shape of the tensor as a tuple."""
return self._shape return self._shape


@property @property
def dtype(self): def dtype(self):
"""The dtype of tensor is a mindspore type."""
"""Returns the dtype of the tensor (:class:`mindspore.dtype`)."""
return self._dtype return self._dtype


@property @property
def size(self): def size(self):
"""The size reflects the total number of elements in tensor."""
"""Returns the total number of elements in tensor."""
return self._size return self._size


@property @property
def ndim(self): def ndim(self):
"""The ndim of tensor is an integer."""
"""Returns the number of tensor dimensions."""
return len(self._shape) return len(self._shape)


@property @property
@@ -277,22 +277,22 @@ class Tensor(Tensor_):


@property @property
def itemsize(self): def itemsize(self):
"""The length of one tensor element in bytes."""
"""Returns the length of one tensor element in bytes."""
return self._itemsize return self._itemsize


@property @property
def strides(self): def strides(self):
"""The tuple of bytes to step in each dimension when traversing a tensor."""
"""Returns the tuple of bytes to step in each dimension when traversing a tensor."""
return self._strides return self._strides


@property @property
def nbytes(self): def nbytes(self):
"""The total number of bytes taken by the tensor."""
"""Returns the total number of bytes taken by the tensor."""
return self._nbytes return self._nbytes


@property @property
def T(self): def T(self):
"""The transposed tensor."""
"""Returns the transposed tensor."""
return self.transpose() return self.transpose()


@property @property
@@ -425,22 +425,21 @@ class Tensor(Tensor_):
return tensor_operator_registry.get('mean')(keep_dims)(self, axis) return tensor_operator_registry.get('mean')(keep_dims)(self, axis)


def transpose(self, *axes): def transpose(self, *axes):
"""
Returns a view of the array with axes transposed.
r"""
Returns a view of the tensor with axes transposed.


For a 1-D array this has no effect, as a transposed vector is simply the
same vector. For a 2-D array, this is a standard matrix transpose. For an
n-D array, if axes are given, their order indicates how the axes are permuted
(see Examples). If axes are not provided and a.shape = (i[0], i[1],...
i[n-2], i[n-1]), then a.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0]).
For a 1-D tensor this has no effect, as a transposed vector is simply the
same vector. For a 2-D tensor, this is a standard matrix transpose. For a
n-D tensor, if axes are given, their order indicates how the axes are permuted.
If axes are not provided and tensor.shape = (i[0], i[1],...i[n-2], i[n-1]),
then tensor.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0]).


Args: Args:
axes(Union[None, tuple(int), list(int), n ints], optional):
None or no argument: reverses the order of the axes.
Tuple of ints: i in the j-th place in the tuple means a’s i-th
axis becomes a.transpose()’s j-th axis.
n ints: this form is intended simply as a `convenience alternative
to the tuple form.
axes(Union[None, tuple(int), list(int), \*int], optional): If axes is None or
blank, tensor.transpose() will reverse the order of the axes. If axes is tuple(int)
or list(int), tensor.transpose() will transpose the tensor to the new axes order.
If axes is \*int, this form is simply intended as a convenience alternative to the
tuple/list form.


Returns: Returns:
Tensor, has the same dimension as input tensor, with axes suitably permuted. Tensor, has the same dimension as input tensor, with axes suitably permuted.
@@ -451,17 +450,16 @@ class Tensor(Tensor_):


def reshape(self, *shape): def reshape(self, *shape):
""" """
Gives a new shape to an array without changing its data.
Gives a new shape to a tensor without changing its data.


Args: Args:
shape(Union[int, tuple(int), list(int)]): The new shape should be compatible shape(Union[int, tuple(int), list(int)]): The new shape should be compatible
with the original shape. If an integer, then the result will be a 1-D
array of that length. One shape dimension can be -1. In this case, the
value is inferred from the length of the array and remaining dimensions.
with the original shape. If an integer, then the result will be a 1-D
array of that length. One shape dimension can be -1. In this case, the
value is inferred from the length of the array and remaining dimensions.


Returns: Returns:
reshaped_tensor(Tensor): This will be a new view object if possible;
otherwise, it will be a copy.
Tensor, with new specified shape.
""" """
self.init_check() self.init_check()
new_shape = validator.check_reshape_shp(shape) new_shape = validator.check_reshape_shp(shape)
@@ -470,10 +468,9 @@ class Tensor(Tensor_):
def ravel(self): def ravel(self):
""" """
Returns a contiguous flattened tensor. Returns a contiguous flattened tensor.
A 1-D tensor, containing the elements of the input, is returned.


Returns: Returns:
Tensor, has the same data type as x.
Tensor, a 1-D tensor, containing the same elements of the input.
""" """
self.init_check() self.init_check()
reshape_op = tensor_operator_registry.get('reshape')() reshape_op = tensor_operator_registry.get('reshape')()
@@ -481,15 +478,15 @@ class Tensor(Tensor_):


def flatten(self, order='C'): def flatten(self, order='C'):
""" """
Returns a copy of the array collapsed into one dimension.
Returns a copy of the tensor collapsed into one dimension.


Args: Args:
order (str, optional): Can choose between `C` and `F`. `C` means to
flatten in row-major (C-style) order. ‘F’ means to flatten in column-major
(Fortran- style) order. Only `C` and `F` are supported.
order (str, optional): Can choose between \'C\' and \'F\'. \'C\' means to
flatten in row-major (C-style) order. \'F\' means to flatten in column-major
(Fortran- style) order. Only \'C\' and \'F\' are supported.


Returns: Returns:
Tensor, has the same data type as x.
Tensor, has the same data type as input.
""" """
self.init_check() self.init_check()
reshape_op = tensor_operator_registry.get('reshape')() reshape_op = tensor_operator_registry.get('reshape')()
@@ -511,7 +508,7 @@ class Tensor(Tensor_):
axis2 (int): Second axis. axis2 (int): Second axis.


Returns: Returns:
Transposed tensor, has the same data type as the original tensor x.
Transposed tensor, has the same data type as the input.
""" """
self.init_check() self.init_check()
axis1, axis2 = validator.check_swapaxes_axis((axis1, axis2), self.ndim) axis1, axis2 = validator.check_swapaxes_axis((axis1, axis2), self.ndim)
@@ -534,10 +531,10 @@ class Tensor(Tensor_):


def squeeze(self, axis=None): def squeeze(self, axis=None):
""" """
Removes single-dimensional entries from the shape of an tensor.
Removes single-dimensional entries from the shape of a tensor.


Args: Args:
axis: Union[None, int, list(int), tuple(list)]. Default is None.
axis (Union[None, int, list(int), tuple(list)], optional): Default is None.


Returns: Returns:
Tensor, with all or a subset of the dimensions of length 1 removed. Tensor, with all or a subset of the dimensions of length 1 removed.
@@ -550,14 +547,13 @@ class Tensor(Tensor_):


def astype(self, dtype, copy=True): def astype(self, dtype, copy=True):
""" """
Returns a copy of the array, cast to a specified type.
Returns a copy of the tensor, casted to a specified type.


Args: Args:
dtype(Union[mstype.dtype, numpy.dtype, str]): Designated tensor dtype,
can be in format of np.float32, mstype.float32 or `float32`. Default
is mstype.float32.
dtype (Union[:class:`mindspore.dtype`, str]): Designated tensor dtype, can be in format
of :class:`mindspore.dtype.float32` or \'float32\'. Default is :class:`mindspore.dtype.float32`


copy(bool, optional): By default, astype always returns a newly allocated
copy (bool, optional): By default, astype always returns a newly allocated
tensor. If this is set to false, the input tensor is returned instead tensor. If this is set to false, the input tensor is returned instead
of a copy if possible. of a copy if possible.




Loading…
Cancel
Save