Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
7d3a6db0
MegEngine
项目概览
MegEngine 天元
/
MegEngine
大约 1 年 前同步成功
通知
399
Star
4705
Fork
582
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
MegEngine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7d3a6db0
编写于
2月 25, 2022
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
docs(tensor): add more introduction about Tensor
GitOrigin-RevId: 4eba1e6c03a639fe91670619024805099d49ef66
上级
30561d72
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
37 addition
and
154 deletion
+37
-154
imperative/python/megengine/core/tensor/array_method.py
imperative/python/megengine/core/tensor/array_method.py
+5
-150
imperative/python/megengine/tensor.py
imperative/python/megengine/tensor.py
+32
-4
未找到文件。
imperative/python/megengine/core/tensor/array_method.py
浏览文件 @
7d3a6db0
...
@@ -614,166 +614,21 @@ class ArrayMethodMixin(abc.ABC):
...
@@ -614,166 +614,21 @@ class ArrayMethodMixin(abc.ABC):
return
reshape_cpp
(
self
,
(
-
1
,))
return
reshape_cpp
(
self
,
(
-
1
,))
def
sum
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
def
sum
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
r
"""Returns the sum of each row of the input tensor in the given dimension ``axis``.
r
"""See :func:`~.sum`."""
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`).
Args:
axis: the dimension or dimensions to reduce.
keepdims: whether the output tensor has ndim retained or not.
Returns:
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.sum().numpy())
print(b.sum().numpy())
Outputs:
.. testoutput::
2
10.0
"""
return
_reduce
(
"sum"
)(
self
,
axis
,
keepdims
)
return
_reduce
(
"sum"
)(
self
,
axis
,
keepdims
)
def
prod
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
def
prod
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
r
"""Returns the product of each row of the input tensor in the given dimension ``axis``.
r
"""See :func:`~.prod`."""
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`).
Args:
axis: the dimension or dimensions to reduce.
keepdims: whether the output tensor has ndim retained or not.
Returns:
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
)
return
_reduce
(
"product"
)(
self
,
axis
,
keepdims
)
def
min
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
def
min
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
r
"""Returns the min value of each row of the input tensor in the given dimension ``axis``.
r
"""See :func:`~.min`."""
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`).
Args:
axis: the dimension or dimensions to reduce.
keepdims: whether the output tensor has ndim retained or not.
Returns:
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
)
return
_reduce
(
"min"
)(
self
,
axis
,
keepdims
)
def
max
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
def
max
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
r
"""Returns the max value of each row of the input tensor in the given dimension ``axis``.
r
"""See :func:`~.max`."""
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`).
Args:
axis: the dimension or dimensions to reduce.
keepdims: whether the output tensor has ndim retained or not.
Returns:
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
)
return
_reduce
(
"max"
)(
self
,
axis
,
keepdims
)
def
mean
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
def
mean
(
self
,
axis
=
None
,
keepdims
:
bool
=
False
):
r
"""Returns the mean value of each row of the input tensor in the given dimension ``axis``.
r
"""See :func:`~.mean`."""
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`).
Args:
axis: the dimension or dimensions to reduce.
keepdims: whether the output tensor has ndim retained or not.
Returns:
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.mean().numpy())
print(b.mean().numpy())
Outputs:
.. testoutput::
0.5
2.5
"""
return
_reduce
(
"mean"
)(
self
,
axis
,
keepdims
)
return
_reduce
(
"mean"
)(
self
,
axis
,
keepdims
)
imperative/python/megengine/tensor.py
浏览文件 @
7d3a6db0
...
@@ -27,13 +27,41 @@ logger = get_logger(__name__)
...
@@ -27,13 +27,41 @@ logger = get_logger(__name__)
class
Tensor
(
_Tensor
,
ArrayMethodMixin
):
class
Tensor
(
_Tensor
,
ArrayMethodMixin
):
r
"""A tensor object represents a multidimensional, homogeneous array of fixed-size items.
r
"""A tensor object represents a multidimensional, homogeneous array of fixed-size items.
Tensor is the primary MegEngine data structure.
Data type(dtype) describes the format of each element, such as ``float32``, ``int8`` and so on,
see :ref:`tensor-dtype` for more details.
It is similar to :class:`numpy.ndarray` but not the same in the design.
For example, GPU devices can be used to store Tensors and execute calculations in MegEngine.
The concept of `view <https://numpy.org/doc/stable/reference/generated/numpy.ndarray.view.html>`_
does not exist in MegEngine so indexing and other behaviors might be different with NumPy.
All manipulations and operations on/between Tensors could be found in the :mod:`~.megengine.functional` module.
Keep in mind that they are **not in-place**, a new Tensor will always be returned and
the original data will remain constant.
For more information, refer to the :ref:`tensor-guide` topic.
Args:
Args:
data(Tensor, :class:`~.numpy.ndarray`, :class:`list` or python number.): The value of returned Tensor.
data(Tensor, :class:`~.numpy.ndarray`, :class:`list` or Python number):
dtype: The dtype of returned Tensor. Uses data's dtype if not specified.
The data used for construcing Tensor.
device: The desired device of returned Tensor. Uses :func:`get_default_device` if not specified.
Tensor could be constructed from a Python :class:`list` / :class:`tuple` or sequence;
is_const: Whether make it a ``ImutableTensor`` in tracing mode.
a NumPy :class:`~.numpy.ndarray` data structure; MegEngine builtin methods and so on.
Refer to :ref:`tensor-creation` for more details.
dtype(:attr:`~.Tensor.dtype`): The data type of returned Tensor. Infer from ``data`` if not specified.
device(:attr:`~.Tensor.device`): The desired device of returned Tensor. Uses :func:`get_default_device` if not specified.
is_const: Whether make it a ``ImutableTensor`` in tracing mode, refer to :class:`.jit.trace`.
no_cache: Whether cache it for memory sharing.
no_cache: Whether cache it for memory sharing.
name: Used to improve convenience in graph operation on dumped model.
name: Used to improve convenience in graph operation on dumped model.
.. note::
There are some methods like :meth:`~.Tensor.reshape` / :meth:`~.Tensor.flatten` /
:meth:`~.Tensor.transpose` / :meth:`~.Tensor.min` / :meth:`~.Tensor.max` /
:meth:`~.Tensor.mean` / :meth:`~.Tensor.sum` / :meth:`~.Tensor.prod` implemented
in ``Tensor`` class for convenience and historical reasons.
But other methods implemented in the :mod:`~.megengine.functional` module will not be added here anymore,
it is hard for maintaining and too many candidates will affect code completion experience.
"""
"""
grad
=
None
grad
=
None
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录