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 to 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 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 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 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 Python list / tuple or sequence; a NumPy ndarray 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 from data if not specified.

  • device (device) – The desired device of returned Tensor. Uses get_default_device if not specified.

  • is_const (bool) – Whether make it a ImutableTensor in tracing mode, refer to jit.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 in Tensor class for convenience and historical reasons. But other methods implemented in the functional module will not be added here anymore, it is hard for maintaining and too many candidates will affect code completion experience.

Attributes

T

alias of transpose.

device

Returns a string represents the device a Tensor storaged on.

dmap_callback

callback for device mapping, see load.

dtype

Returns a numpy.dtype object represents the data type of a Tensor.

format

Returns a string represents the memory format of a Tensor.

grad

gradient of this tensor, see autodiff.

name

Returns a string represents the name of a Tensor.

ndim

Returns the number of dimensions of self Tensor.

qparams

Returns a QParams object containing quantization params of a Tensor.

requires_grad

Returns a bool indicates whether the Tensor requires gradient.

shape

Returns a tuple or a Tensor represents tensor dimensions.

size

Returns the size of the self Tensor.

Methods

astype(dtype)

Returns a Tensor with the same data and number of elements with the specified dtype.

detach()

Returns a new Tensor, detached from the current graph.

flatten([start_axis, end_axis])

See flatten.

graph

item(*args)

Returns the value of this Tensor as a standard Python numbers.Number.

max([axis, keepdims])

See max.

mean([axis, keepdims])

See mean.

min([axis, keepdims])

See min.

numpy()

Returns self Tensor as a numpy.ndarray.

prod([axis, keepdims])

See prod.

reset_zero()

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.

value_id

var