Tensor

class dragon.Tensor(
  shape=None,
  dtype='float32',
  name=None,
  symbolic=False,
  **kwargs
)[source]

A multi-dimensional array for computation.

__init__

Tensor.__init__(
  shape=None,
  dtype='float32',
  name=None,
  symbolic=False,
  **kwargs
)[source]

Create a Tensor.

Parameters:
  • shape (Sequence[int], optional) The tensor shape.
  • dtype (str, optional, default='float32') The data type.
  • name (str, optional) The tensor name.
  • symbolic (bool, optional, default=False) Whether to initialize as a symbolic tensor.

Properties

device

Tensor.device

Return the tensor device.

Returns:
dragon.DeviceSpec The device.

dtype

Tensor.dtype

Return the data type.

Returns:
str The data type.

id

Tensor.id

Return the tensor identity.

Returns:
str The tensor identity.

name

Tensor.name

Return the tensor name.

Returns:
str The tensor name.

ndim

Tensor.ndim

Return the number of dimensions.

Returns:
int The number of dimensions.

requires_grad

Tensor.requires_grad

Return whether the gradient will be recorded.

Returns:
bool True to record gradient otherwise False.

shape

Tensor.shape

Return the tensor shape.

Returns:
Tuple[int] The tensor shape.

size

Tensor.size

Return the total number of elements in this tensor.

Returns:
int The total count of elements.

T

Tensor.T

Return a tensor with axes reversed.

Returns:
dragon.Tensor The output tensor.

Methods

astype

Tensor.astype(
  dtype,
  copy=True
)[source]

Convert the data type to a specific one.

Parameters:
  • dtype (str) The specific data type.
  • copy (bool, optional, default=True) Return a new tensor or converted in-place.
Returns:

dragon.Tensor The output tensor.

See also

dragon.cast(…)

copy

Tensor.copy()[source]

Return a tensor with containing data copied.

Returns:
dragon.Tensor The output tensor.

fill

Tensor.fill(value)[source]

Fill self with a scalar value.

\[\text{self} \leftarrow \text{value} \]
Parameters:
  • value (number) The value to fill.
Returns:

dragon.Tensor The self.

See also

dragon.fill(…)

glorot_normal

Tensor.glorot_normal(
  mode='fan_in',
  scale=2.0
)[source]

Fill self from a glorot normal distribution.

\[\text{self} \sim \mathcal{N}(0, \frac{scale}{\text{fan}}) \]
Parameters:
  • mode ({'fan_in, 'fan_out', 'fan_avg'}, optional) The mode to compute fans.
  • scale (float, optional, default=2.0) The scale factor of distribution.
Returns:

dragon.Tensor The self.

glorot_uniform

Tensor.glorot_uniform(
  mode='fan_in',
  scale=3.0
)[source]

Fill self from a glorot uniform distribution.

\[\text{self} \sim \mathcal{U}(-\sqrt{\frac{scale}{\text{fan}}}, \sqrt{\frac{scale}{\text{fan}}}) \]
Parameters:
  • mode ({'fan_in, 'fan_out', 'fan_avg'}, optional) The mode to compute fans.
  • scale (float, optional, default=3.0) The scale factor of distribution.
Returns:

dragon.Tensor The self.

item

Tensor.item()[source]

Return the value as a python number.

Returns:
number The value.

normal

Tensor.normal(
  mean=0,
  std=1
)[source]

Fill self from a normal distribution.

\[\text{self} \sim \mathcal{N}(\mu, \sigma^{2}) \]
Parameters:
  • mean (number, optional, default=0) The value to \(\mu\).
  • std (number, optional, default=1) The value to \(\sigma\).
Returns:

dragon.Tensor The self.

numpy

Tensor.numpy(copy=False)[source]

Convert tensor into a numpy array.

Parameters:
  • copy (bool, optional, default=False) Whether to copy the data.
Returns:

numpy.ndarray The value array.

reshape

Tensor.reshape(
  shape,
  copy=True
)[source]

Return a tensor containing the same data with new shape.

Parameters:
  • shape (Union[Sequence[int], dragon.Tensor]) The output shape.
  • copy (bool, optional, default=True) Return a new tensor or reshape in-place.
Returns:

dragon.Tensor The output tensor.

tolist

Tensor.tolist()[source]

Return the value as a python list.

Returns:
list The value.

transpose

Tensor.transpose(
  perm=None,
  copy=True
)[source]

Return a tensor with permuted axes.

Parameters:
  • perm (Union[Sequence[int], dragon.Tensor]], optional) The output permutation.
  • copy (bool, optional, default=True) Return a new tensor or transpose in-place.
Returns:

dragon.Tensor The output tensor.

truncated_normal

Tensor.truncated_normal(
  mean=0,
  std=1
)[source]

Fill self from a truncated normal distribution.

\[\text{self} \sim \mathcal{TN}(\mu, \sigma^{2}, \mu - 2\sigma, \mu + 2\sigma) \]
Parameters:
  • mean (number, optional, default=0) The value to \(\mu\).
  • std (number, optional, default=1) The value to \(\sigma\).
Returns:

dragon.Tensor The self.

uniform

Tensor.uniform(
  low=0,
  high=1
)[source]

Fill self from a uniform distribution.

\[\text{self} \sim \mathcal{U}(\alpha, \beta) \]
Parameters:
  • low (number, optional, default=0) The value to \(\alpha\).
  • high (number, optional, default=1) The value to \(\beta\).
Returns:

dragon.Tensor The self.

Overrides

__add__

Tensor.__add__(other)[source]

Compute the element-wise addition.

Parameters:
Returns:

dragon.Tensor The output tensor.

__and__

Tensor.__and__(other)[source]

Compute the element-wise AND bitwise operation.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to compute with.
Returns:

dragon.Tensor The output tensor.

__eq__

Tensor.__eq__(other)[source]

Compute element-wise equal comparison.

Parameters:
Returns:

dragon.Tensor The output tensor.

__float__

Tensor.__float__()[source]

Return the value as a python number.

Returns:
float The float value.

__ge__

Tensor.__ge__(other)[source]

Compute element-wise greater-equal comparison.

Parameters:
Returns:

dragon.Tensor The output tensor.

__getitem__

Tensor.__getitem__(item)[source]

Select elements at the specific index.

Parameters:
Returns:

dragon.Tensor The output tensor.

__gt__

Tensor.__gt__(other)[source]

Compute element-wise greater comparison.

Parameters:
Returns:

dragon.Tensor The output tensor.

__iadd__

Tensor.__iadd__(other)[source]

Compute the element-wise addition.

Parameters:
Returns:

dragon.Tensor The self.

__iand__

Tensor.__iand__(other)[source]

Compute the element-wise AND bitwise operation.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to compute with.
Returns:

dragon.Tensor The self.

__imul__

Tensor.__imul__(other)[source]

Compute the element-wise multiplication.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to multiply.
Returns:

dragon.Tensor The self.

__int__

Tensor.__int__()[source]

Return the value as a python number.

Returns:
int The integer value.

__invert__

Tensor.__invert__()[source]

Compute the element-wise NOT bitwise operation.

Returns:
dragon.Tensor The output tensor.

__ior__

Tensor.__ior__(other)[source]

Compute the element-wise OR bitwise operation.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to compute with.
Returns:

dragon.Tensor The self.

__isub__

Tensor.__isub__(other)[source]

Compute the element-wise subtraction.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to subtract.
Returns:

dragon.Tensor The self.

__itruediv__

Tensor.__itruediv__(other)[source]

Compute the element-wise division.

Parameters:
Returns:

dragon.Tensor The self.

__ixor__

Tensor.__ixor__(other)[source]

Compute the element-wise XOR bitwise operation.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to compute with.
Returns:

dragon.Tensor The self.

__le__

Tensor.__le__(other)[source]

Compute element-wise less-equal comparison.

Parameters:
Returns:

dragon.Tensor The output tensor.

__lt__

Tensor.__lt__(other)[source]

Compute element-wise less comparison.

Parameters:
Returns:

dragon.Tensor The output tensor.

__matmul__

Tensor.__matmul__(other)[source]

Compute the matrix multiplication.

Parameters:
Returns:

dragon.Tensor The output tensor.

__mul__

Tensor.__mul__(other)[source]

Compute the element-wise multiplication.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to multiply.
Returns:

dragon.Tensor The output tensor.

__ne__

Tensor.__ne__(other)[source]

Compute element-wise not-equal comparison.

Parameters:
Returns:

dragon.Tensor The output tensor.

__neg__

Tensor.__neg__()[source]

Compute the element-wise negative.

Returns:
dragon.Tensor The output tensor.

__or__

Tensor.__or__(other)[source]

Compute the element-wise OR bitwise operation.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to compute with.
Returns:

dragon.Tensor The output tensor.

__radd__

Tensor.__radd__(other)[source]

Compute the element-wise addition.

Parameters:
Returns:

dragon.Tensor The output tensor.

__rand__

Tensor.__rand__(other)[source]

Compute the element-wise AND bitwise operation.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to compute with.
Returns:

dragon.Tensor The output tensor.

__rmul__

Tensor.__rmul__(other)[source]

Compute the element-wise multiplication.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to multiply.
Returns:

dragon.Tensor The output tensor.

__ror__

Tensor.__ror__(other)[source]

Compute the element-wise OR bitwise operation.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to compute with.
Returns:

dragon.Tensor The output tensor.

__rsub__

Tensor.__rsub__(other)[source]

Compute the element-wise subtraction.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to be subtracted.
Returns:

dragon.Tensor The output tensor.

__rxor__

Tensor.__rxor__(other)[source]

Compute the element-wise XOR bitwise operation.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to compute with.
Returns:

dragon.Tensor The output tensor.

__setitem__

Tensor.__setitem__(
  key,
  value
)[source]

Set elements at the specific index.

Parameters:

__sub__

Tensor.__sub__(other)[source]

Compute the element-wise subtraction.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to subtract.
Returns:

dragon.Tensor The output tensor.

__rtruediv__

Tensor.__rtruediv__(other)[source]

Compute the element-wise division.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to be divided.
Returns:

dragon.Tensor The output tensor.

__truediv__

Tensor.__truediv__(other)[source]

Compute the element-wise division.

Parameters:
Returns:

dragon.Tensor The output tensor.

__xor__

Tensor.__xor__(other)[source]

Compute the element-wise XOR bitwise operation.

Parameters:
  • other (Union[dragon.Tensor, number]) The value to compute with.
Returns:

dragon.Tensor The output tensor.