MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / Tensor

Class Tensor

imperative/python/megengine/tensor.py:22–279  ·  view source on GitHub ↗

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 s

Source from the content-addressed store, hash-verified

20
21
22class Tensor(_Tensor, ArrayMethodMixin):
23 r"""A tensor object represents a multidimensional, homogeneous array of fixed-size items.
24
25 Tensor is the primary MegEngine data structure.
26 Data type(dtype) describes the format of each element, such as ``float32``, ``int8`` and so on,
27 see :ref:`tensor-dtype` for more details.
28 It is similar to :class:`numpy.ndarray` but not the same in the design.
29 For example, GPU devices can be used to store Tensors and execute calculations in MegEngine.
30 The concept of `view <https://numpy.org/doc/stable/reference/generated/numpy.ndarray.view.html>`_
31 does not exist in MegEngine so indexing and other behaviors might be different with NumPy.
32 All manipulations and operations on/between Tensors could be found in the :mod:`~.megengine.functional` module.
33 Keep in mind that they are **not in-place**, a new Tensor will always be returned and
34 the original data will remain constant.
35
36 For more information, refer to the :ref:`tensor-guide` topic.
37
38 Args:
39 data(Tensor, :class:`~.numpy.ndarray`, :class:`list` or Python number):
40 The data used for construcing Tensor.
41 Tensor could be constructed from a Python :class:`list` / :class:`tuple` or sequence;
42 a NumPy :class:`~.numpy.ndarray` data structure; MegEngine builtin methods and so on.
43 Refer to :ref:`tensor-creation` for more details.
44
45 dtype(:attr:`~.Tensor.dtype`): The data type of returned Tensor. Infer from ``data`` if not specified.
46 device(:attr:`~.Tensor.device`): The desired device of returned Tensor. Uses :func:`get_default_device` if not specified.
47 is_const: Whether make it a ``ImutableTensor`` in tracing mode, refer to :class:`.jit.trace`.
48 no_cache: Whether cache it for memory sharing.
49 name: Used to improve convenience in graph operation on dumped model.
50 format: Used to indicate which memory format Tensor uses. It will not affect actual memory order or stride,
51 but may affect some operators related to indexing and dimension. Only support "default", "nchw" and "nhwc".
52
53 .. note::
54
55 There are some methods like :meth:`~.Tensor.reshape` / :meth:`~.Tensor.flatten` /
56 :meth:`~.Tensor.transpose` / :meth:`~.Tensor.min` / :meth:`~.Tensor.max` /
57 :meth:`~.Tensor.mean` / :meth:`~.Tensor.sum` / :meth:`~.Tensor.prod` implemented
58 in ``Tensor`` class for convenience and historical reasons.
59 But other methods implemented in the :mod:`~.megengine.functional` module will not be added here anymore,
60 it is hard for maintaining and too many candidates will affect code completion experience.
61
62 """
63
64 grad = None #: gradient of this tensor, see :mod:`~.autodiff`.
65 dmap_callback = None #: callback for device mapping, see :func:`~.load`.
66 _qparams = None
67 _custom_name = ""
68 _name = None
69 _short_name = None
70 _prefix = None
71
72 def __init__(
73 self,
74 data: Union["Tensor", np.ndarray, list, int, float],
75 dtype: np.dtype = None,
76 device: str = None,
77 is_const: bool = False,
78 no_cache: bool = False,
79 name: str = None,

Callers 15

test_training_convergeFunction · 0.90
test_gaussian_opFunction · 0.90
test_uniform_opFunction · 0.90
test_multinomial_opFunction · 0.90
test_GammaRNGFunction · 0.90
test_BetaRNGFunction · 0.90
test_PoissonRNGFunction · 0.90
test_MultinomialRNGFunction · 0.90
funcFunction · 0.90
test_ShuffleRNGFunction · 0.90

Calls

no outgoing calls

Tested by 15

test_training_convergeFunction · 0.72
test_gaussian_opFunction · 0.72
test_uniform_opFunction · 0.72
test_multinomial_opFunction · 0.72
test_GammaRNGFunction · 0.72
test_BetaRNGFunction · 0.72
test_PoissonRNGFunction · 0.72
test_MultinomialRNGFunction · 0.72
funcFunction · 0.72
test_ShuffleRNGFunction · 0.72