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,int8and so on, see Tensor data type for more details. It is similar tonumpy.ndarraybut 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 thefunctionalmodule. 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,listor Python number) – The data used for construcing Tensor. Tensor could be constructed from a Pythonlist/tupleor sequence; a NumPyndarraydata 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 fromdataif not specified.device (
device) – The desired device of returned Tensor. Usesget_default_deviceif not specified.is_const (
bool) – Whether make it aImutableTensorin 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/prodimplemented inTensorclass for convenience and historical reasons. But other methods implemented in thefunctionalmodule 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
Tensorstoraged on.callback for device mapping, see
load.Returns a
numpy.dtypeobject 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
QParamsobject containing quantization params of aTensor.Returns a bool indicates whether the
Tensorrequires gradient.Returns the size of the self
Tensor.Methods
astype(dtype)Returns a
Tensorwith 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
Tensoras a standard Pythonnumbers.Number.max([axis, keepdims])See
max.mean([axis, keepdims])See
mean.min([axis, keepdims])See
min.numpy()Returns self
Tensoras 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
Tensorto specified device.tolist()Returns the tensor as a (nested) list.
transpose(*args)See
transpose.