EagerTensor

class dragon.EagerTensor(
  *args,
  **kwargs
)[source]

Tensor abstraction for eager executing.

Examples:

# Create a tensor with shape and dtype
x = dragon.EagerTensor(shape=(2, 3), dtype='float32')
y = dragon.EagerTensor(np.ones((2, 3)))

# Create a tensor copied from the constant
x = dragon.EagerTensor([2, 3], dtype='float32')
y = dragon.EagerTensor(np.ones((2, 3)))

# Create a tensor zero-copied from the array
x = np.ones((2, 3))
y = dragon.EagerTensor(x, copy=False)

__init__

EagerTensor.__init__(
  *args,
  **kwargs
)[source]

Create an EagerTensor.

Properties

dtype

EagerTensor.dtype

Return the data type.

Returns:
str – The data type.

id

EagerTensor.id

Return the tensor identity.

Returns:
str – The tensor identity.

name

EagerTensor.name

Return the tensor name.

Returns:
str – The tensor name.

ndim

EagerTensor.ndim

Return the number of dimensions.

Returns:
int – The number of dimensions.

shape

EagerTensor.shape

Return tensor shape.

Returns:
Tuple[int] – The tensor shape.

size

EagerTensor.size

Return the total number of elements in this tensor.

Returns:
int – The total count of elements.

Methods

astype

EagerTensor.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.EagerTensor – The output tensor.

See also

dragon.cast(…)

constant

EagerTensor.constant(value=0)[source]

Fill self with a scalar value.

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

dragon.EagerTensor – The self.

See also

dragon.fill(…)

copy

EagerTensor.copy()[source]

Return a tensor with containing data copied.

Returns:
dragon.EagerTensor – The output tensor.

See also

dragon.copy(…)

get_value

EagerTensor.get_value()[source]

Return the value of implementation.

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

glorot_normal

EagerTensor.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.EagerTensor – The self.

glorot_uniform

EagerTensor.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.EagerTensor – The self.

normal

EagerTensor.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.EagerTensor – The self.

numpy

EagerTensor.numpy(readonly=True)[source]

Create a numpy array sharing the data.

Parameters:
  • readonly (bool, optional, default=True) – False to sync the content with device.
Returns:

numpy.ndarray – The numpy array.

reshape

EagerTensor.reshape(shape)[source]

Return a tensor containing the same data with new shape.

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

dragon.EagerTensor – The output tensor.

set_value

EagerTensor.set_value(value)[source]

Set value to the implementation.

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

dragon.EagerTensor – The self.

truncated_normal

EagerTensor.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.EagerTensor – The self.

uniform

EagerTensor.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.EagerTensor – The self.

Overrides

__add__

EagerTensor.__add__(other)[source]

Compute the element-wise addition.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

__float__

EagerTensor.__float__()[source]

Return a float python scalar.

Returns:
float – The float value.

__ge__

EagerTensor.__ge__(other)[source]

Compute element-wise greater-equal comparison.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

__getitem__

EagerTensor.__getitem__(item)[source]

Select elements at the specific index.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

__gt__

EagerTensor.__gt__(other)[source]

Compute element-wise greater comparison.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

__iadd__

EagerTensor.__iadd__(other)[source]

Compute the element-wise addition.

Parameters:
Returns:

dragon.EagerTensor – The self.

__imul__

EagerTensor.__imul__(other)[source]

Compute the element-wise multiplication.

Parameters:
Returns:

dragon.EagerTensor – The self.

__int__

EagerTensor.__int__()[source]

Return a integer python scalar.

Returns:
int – The integer value.

__isub__

EagerTensor.__isub__(other)[source]

Compute the element-wise subtraction.

Parameters:
Returns:

dragon.EagerTensor – The self.

__itruediv__

EagerTensor.__itruediv__(other)[source]

Compute the element-wise division.

Parameters:
Returns:

dragon.EagerTensor – The self.

__le__

EagerTensor.__le__(other)[source]

Compute element-wise less-equal comparison.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

__lt__

EagerTensor.__lt__(other)[source]

Compute element-wise less comparison.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

__mul__

EagerTensor.__mul__(other)[source]

Compute the element-wise multiplication.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

__neg__

EagerTensor.__neg__()[source]

Compute the element-wise negative.

Returns:
dragon.EagerTensor – The output tensor.

__radd__

EagerTensor.__radd__(other)[source]

Compute the element-wise addition.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

__rmul__

EagerTensor.__rmul__(other)[source]

Compute the element-wise multiplication.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

__rsub__

EagerTensor.__rsub__(other)[source]

Compute the element-wise subtraction.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

__rtruediv__

EagerTensor.__rtruediv__(other)[source]

Compute the element-wise division.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

__setitem__

EagerTensor.__setitem__(
  key,
  value
)[source]

Set elements at the specific index.

Parameters:

__sub__

EagerTensor.__sub__(other)[source]

Compute the element-wise subtraction.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

__truediv__

EagerTensor.__truediv__(other)[source]

Compute the element-wise division.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.