megengine.Tensor

class Tensor(data, dtype=None, device=None, is_const=False, no_cache=False, name=None, format='default')[源代码]

一个张量对象表示固定大小元素的多维同构数组。

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 数据类型 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 深入理解 Tensor 数据结构 topic.

参数
  • 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 如何创建一个 Tensor for more details.

  • dtype (dtype) – The data type of returned Tensor. Infer from data if not specified.

  • device (device) – 张量的所在设备。如果没有特别指定则为 get_default_device 返回值。

  • is_const (bool) – Whether make it a ImutableTensor in tracing mode, refer to jit.trace.

  • no_cache (bool) – 是否为了内存共享缓存张量。

  • name (Optional[str]) – 张量的名字可用来简化存储的模型上的图操作。

  • 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”.

注解

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.

属性

T

transpose 的别名。

device

返回一个字符串,表示当前 Tensor 所存储在的设备。

dmap_callback

dtype

返回一个 numpy.dtype 对象,表示当前 Tensor 的数据类型。

format

rtype

str

grad

name

ndim

返回自身 Tensor 的维数。

qparams

返回一个 QParams 对象,其中包含当前 Tensor 的量化参数。

requires_grad

shape

返回一个 tupleTensor, 表示当前张量的维度信息。

size

返回当前 Tensor 的大小。

方法

astype(dtype)

返回一个含有完全相同的数据和元素数目,但是数据类型被 dtype 指定的新 Tensor.

detach()

返回一个与当前图解绑后的新 Tensor .

flatten()

参考 flatten.

graph

item(*args)

以标准 Python numbers.Number 类型返回当前 Tensor 元素的值。

max([axis, keepdims])

See max.

mean([axis, keepdims])

See mean.

min([axis, keepdims])

See min.

numpy()

返回一个将当前 Tensor 转换为 numpy.ndarray 后的结果。

prod([axis, keepdims])

See prod.

reset_zero()

1.0 版后已移除.

reshape(*args)

参考 reshape.

set_value(value)

1.0 版后已移除.

sum([axis, keepdims])

See sum.

to(device, *[, _borrow])

将当前 Tensor 拷贝到指定设备。

tolist()

返回将当前张量转换成(嵌套)列表后的结果。

transpose(*args)

参考 transpose.

var