@@ -362,6 +362,9 @@ class ArrayMethodMixin(abc.ABC):
@property
defndim(self):
r"""
Returns the number of dimensions of self :class:`~.Tensor`.
"""
shape=self._tuple_shape
ifshapeisNone:
raiseValueError("unkown ndim")
...
...
@@ -369,6 +372,10 @@ class ArrayMethodMixin(abc.ABC):
@property
defsize(self):
r"""
Returns the size of the self :class:`~.Tensor`.
The returned value is a subclass of :class:`tuple`.
"""
shape=self.shape
ifshape.__class__istuple:
returnnp.prod(self.shape).item()
...
...
@@ -376,9 +383,16 @@ class ArrayMethodMixin(abc.ABC):
@property
defT(self):
r"""
alias of :attr:`~.Tensor.transpose`.
"""
returnself.transpose()
defitem(self,*args):
r"""
Returns the value of this :class:`~.Tensor` as a standard Python :class:`numbers.Number`.
This only works for tensors with one element. For other cases, see :meth:`~.tolist`.
"""
ifnotargs:
ifisinstance(self.size,int):
assertself.size==1
...
...
@@ -386,12 +400,26 @@ class ArrayMethodMixin(abc.ABC):
returnself[args].item()
deftolist(self):
r"""
Returns the tensor as a (nested) list.
For scalars, a standard Python number is returned, just like with :meth:`~.item`.
Tensors are automatically moved to the CPU first if necessary.
This operation is not differentiable.
"""
returnself.numpy().tolist()
defastype(self,dtype):
r"""
Returns a :class:`Tensor` with the same data and number of elements
with the specified :attr:`~.Tensor.dtype`.
"""
returnutils.astype(self,dtype)
defreshape(self,*args):
r"""
See :func:`~.reshape`.
"""
return_reshape(self,_expand_args(args))
# FIXME: remove this method
...
...
@@ -399,6 +427,9 @@ class ArrayMethodMixin(abc.ABC):
return_broadcast(self,_expand_args(args))
deftranspose(self,*args):
r"""
See :func:`~.transpose`.
"""
ifself.ndim==0:
assert(
len(args)==0
...
...
@@ -411,19 +442,22 @@ class ArrayMethodMixin(abc.ABC):
return_transpose(self,_expand_args(args))
defflatten(self):
r"""
See :func:`~.flatten`.
"""
returnself.reshape(-1)
defsum(self,axis=None,keepdims:bool=False):
r"""
Returns the sum of each row of the input tensor in the given dimension ``axis``.
If ``axis`` is a list of axises, reduce over all of them.
If ``keepdims`` is ``True``, the shape of output tensor is the same as the input tensor, except in the dimension(s) ``axis`` where it is of size 1. Otherwise, ``axis`` is squeezed(see :meth:`~.functional.tensor.squeeze`).
Same for prod/mean/max/min.
If ``keepdims`` is ``True``, the shape of output tensor is the same as the input tensor,
except in the dimension(s) ``axis`` where it is of size 1.
Otherwise, ``axis`` is squeezed (see :func:`~.squeeze`).
:param axis: the dimension or dimensions to reduce.
:param keepdim: whether the output tensor has ndim retained or not.
:param keepdims: whether the output tensor has ndim retained or not.
:return: output tensor.
Examples:
...
...
@@ -441,12 +475,139 @@ class ArrayMethodMixin(abc.ABC):
.. testoutput::
2
10.
10.0
"""
return_reduce("SUM")(self,axis,keepdims)
prod=_reduce("PRODUCT")
min=_reduce("MIN")
max=_reduce("MAX")
mean=_reduce("MEAN")
defprod(self,axis=None,keepdims:bool=False):
r"""
Returns the product of each row of the input tensor in the given dimension ``axis``.
If ``axis`` is a list of axises, reduce over all of them.
If ``keepdims`` is ``True``, the shape of output tensor is the same as the input tensor,
except in the dimension(s) ``axis`` where it is of size 1.
Otherwise, ``axis`` is squeezed (see :func:`~.squeeze`).
:param axis: the dimension or dimensions to reduce.
:param keepdims: whether the output tensor has ndim retained or not.
:return: output tensor.
Examples:
.. testcode::
from megengine import tensor
a = tensor([False, True, True, False])
b = tensor([1.0, 2.0, 3.0, 4.0])
print(a.prod().numpy())
print(b.prod().numpy())
Outputs:
.. testoutput::
0
24.0
"""
return_reduce("PRODUCT")(self,axis,keepdims)
defmin(self,axis=None,keepdims:bool=False):
r"""
Returns the min value of each row of the input tensor in the given dimension ``axis``.
If ``axis`` is a list of axises, reduce over all of them.
If ``keepdims`` is ``True``, the shape of output tensor is the same as the input tensor,
except in the dimension(s) ``axis`` where it is of size 1.
Otherwise, ``axis`` is squeezed (see :func:`~.squeeze`).
:param axis: the dimension or dimensions to reduce.
:param keepdims: whether the output tensor has ndim retained or not.
:return: output tensor.
Examples:
.. testcode::
from megengine import tensor
a = tensor([False, True, True, False])
b = tensor([1.0, 2.0, 3.0, 4.0])
print(a.min().numpy())
print(b.min().numpy())
Outputs:
.. testoutput::
False
1.0
"""
return_reduce("MIN")(self,axis,keepdims)
defmax(self,axis=None,keepdims:bool=False):
r"""
Returns the max value of each row of the input tensor in the given dimension ``axis``.
If ``axis`` is a list of axises, reduce over all of them.
If ``keepdims`` is ``True``, the shape of output tensor is the same as the input tensor,
except in the dimension(s) ``axis`` where it is of size 1.
Otherwise, ``axis`` is squeezed (see :func:`~.squeeze`).
:param axis: the dimension or dimensions to reduce.
:param keepdims: whether the output tensor has ndim retained or not.
:return: output tensor.
Examples:
.. testcode::
from megengine import tensor
a = tensor([False, True, True, False])
b = tensor([1.0, 2.0, 3.0, 4.0])
print(a.max().numpy())
print(b.max().numpy())
Outputs:
.. testoutput::
True
4.0
"""
return_reduce("MAX")(self,axis,keepdims)
defmean(self,axis=None,keepdims:bool=False):
r"""
Returns the mean value of each row of the input tensor in the given dimension ``axis``.
If ``axis`` is a list of axises, reduce over all of them.
If ``keepdims`` is ``True``, the shape of output tensor is the same as the input tensor,
except in the dimension(s) ``axis`` where it is of size 1.
Otherwise, ``axis`` is squeezed (see :func:`~.squeeze`).
:param axis: the dimension or dimensions to reduce.
:param keepdims: whether the output tensor has ndim retained or not.