megengine.Tensor¶
- class Tensor(data, dtype=None, device=None, is_const=False, no_cache=False, name=None, format='default')[source]¶
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 Tensor data type for more details. It is similar tonumpy.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 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 thefunctional
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 Deep understanding of Tensor data structure topic.
- Parameters
data (Tensor,
ndarray
,list
or Python number) – The data used for construcing Tensor. Tensor could be constructed from a Pythonlist
/tuple
or sequence; a NumPyndarray
data structure; MegEngine builtin methods and so on. Refer to How to create a Tensor for more details.dtype (
dtype
) – The data type of returned Tensor. Infer fromdata
if not specified.device (
device
) – The desired device of returned Tensor. Usesget_default_device
if not specified.is_const (
bool
) – Whether make it aImutableTensor
in tracing mode, refer tojit.trace
.no_cache (
bool
) – Whether cache it for memory sharing.name (
Optional
[str
]) – Used to improve convenience in graph operation on dumped model.format (
str
) – Used to indicate which memory format Tensor uses. It will not affect actual memory order or stride, but may affect some operators related to indexing and dimension. Only support “default”, “nchw” and “nhwc”.
Note
There are some methods like
reshape
/flatten
/transpose
/min
/max
/mean
/sum
/prod
implemented inTensor
class for convenience and historical reasons. But other methods implemented in thefunctional
module will not be added here anymore, it is hard for maintaining and too many candidates will affect code completion experience.Attributes
alias of
transpose
.Returns a string represents the device a
Tensor
storaged on.callback for device mapping, see
load
.Returns a
numpy.dtype
object represents the data type of aTensor
.Returns a string represents the memory format of a
Tensor
.gradient of this tensor, see
autodiff
.Returns a string represents the name of a
Tensor
.Returns the number of dimensions of self
Tensor
.Returns a
QParams
object containing quantization params of aTensor
.Returns a bool indicates whether the
Tensor
requires gradient.Returns the size of the self
Tensor
.Methods
astype
(dtype)Returns a
Tensor
with the same data and number of elements with the specifieddtype
.detach
()Returns a new
Tensor
, detached from the current graph.flatten
([start_axis, end_axis])See
flatten
.item
(*args)Returns the value of this
Tensor
as a standard Pythonnumbers.Number
.max
([axis, keepdims])See
max
.mean
([axis, keepdims])See
mean
.min
([axis, keepdims])See
min
.numpy
()Returns self
Tensor
as anumpy.ndarray
.prod
([axis, keepdims])See
prod
.Deprecated since version 1.0.
reshape
(*args)See
reshape
.set_value
(value)Deprecated since version 1.0.
sum
([axis, keepdims])See
sum
.to
(device, *[, _borrow])Copy self
Tensor
to specified device.tolist
()Returns the tensor as a (nested) list.
transpose
(*args)See
transpose
.