Tensor

class dragon.Tensor(
  shape=None,
  dtype=None,
  name=None
)[source]

Tensor abstraction for graph executing.

__init__

Tensor.__init__(
  shape=None,
  dtype=None,
  name=None
)[source]

Create a Tensor.

Parameters:
  • shape (Sequence[int], optional) – The optional tensor shape.
  • dtype (str, optional) – The optional data type.
  • name (str, optional) – The optional tensor name.

Properties

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.

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.

Methods

astype

Tensor.astype(
  dtype,
  inplace=False
)[source]

Cast the data type to a specific one.

Parameters:
  • dtype (str) – The specific data type.
  • inplace (bool, optional, default=False) – Whether to do the cast in-place.
Returns:

dragon.Tensor – The output tensor.

See also

dragon.cast(…)

constant

Tensor.constant(value=0)[source]

Register self to initialize from a scalar value.

\[\text{self} \leftarrow \text{value} \]
Parameters:
  • value (number, optional, default=0) – The value to initialize.
Returns:

dragon.Tensor – The self.

See also

dragon.fill(…)

copy

Tensor.copy()[source]

Return a tensor with containing data copied.

Returns:
dragon.Tensor – The output tensor.

See also

dragon.copy(…)

get_value

Tensor.get_value()[source]

Return the value of implementation.

Returns:
numpy.ndarray – The deep-copied value.

glorot_normal

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

Register self to initialize from a glorot uniform 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 to distribution.
Returns:

dragon.Tensor – The self.

glorot_uniform

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

Register self to initialize 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 to distribution.
Returns:

dragon.Tensor – The self.

normal

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

Register self to initialize 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.

reshape

Tensor.reshape(shape)[source]

Return a tensor containing the same data with new shape.

Parameters:
  • shape (Sequence[int]) – The new shape.
Returns:

dragon.Tensor – The output tensor.

set_value

Tensor.set_value(value)[source]

Set value to the implementation.

Parameters:
  • value (array_like) – The value to set.
Returns:

dragon.Tensor – The self.

truncated_normal

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

Register self to initialize 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]

Register self to initialize from an 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.

__float__

Tensor.__float__()[source]

Return a float python scalar.

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.

__int__

Tensor.__int__()[source]

Return an integer python scalar.

Returns:
int – The integer value.

__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.

__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.

__neg__

Tensor.__neg__()[source]

Compute the element-wise negative.

Returns:
dragon.Tensor – The output tensor.

__radd__

Tensor.__radd__(other)[source]

Compute the element-wise addition.

Parameters:
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.

__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.

__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.