EagerTensor

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

Tensor abstraction under the eager execution.

This abstraction involves the garbage collection, thus, life-cycle should be considered to avoid memory leak.

# 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.name

Return the tensor name.

Returns:
str – The tensor name.

shape

EagerTensor.shape

Return the shape of this tensor.

Returns:
Sequence[int] – The 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
)

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(…) : Cast the data type of input.

constant

EagerTensor.constant(value=0)

Fill self with a constant value.

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

dragon.EagerTensor – The self.

copy

EagerTensor.copy()

Return a tensor with containing data copied.

Returns:
dragon.EagerTensor – The output tensor.

See also

dragon.copy(…) : Copy the value to ref.

get_value

EagerTensor.get_value()[source]

Return the value from storage.

Returns:
numpy.ndarray – The shallow copied value.

glorot_normal

EagerTensor.glorot_normal(
  mode='FAN_IN',
  scale=2.0
)

Fill self from a glorot normal distribution.

\[\text{self} \leftarrow N(0, \sqrt{\frac{scale}{\text{FAN}}}) \]
Parameters:
  • mode ({'FAN_IN, 'FAN_OUT', 'FAN_AVG'}, optional) – The mode to compute fans.
  • scale (number, optional, default=2.) – The scale factor of distribution.
Returns:

dragon.EagerTensor – The self.

glorot_uniform

EagerTensor.glorot_uniform(
  mode='FAN_IN',
  scale=3.0
)

Fill self from a glorot uniform distribution.

\[\text{self} \leftarrow 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 (number, optional, default=3.) – The scale factor of distribution.
Returns:

dragon.EagerTensor – The self.

normal

EagerTensor.normal(
  mean=0,
  std=1
)

Fill self from a normal distribution.

\[\text{self} \leftarrow N(\mu, \sigma) \]
Parameters:
  • mean (number, optional, default=0) – The value of \(\mu\).
  • std (number, optional, default=1) – The value of \(\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)

Return a tensor containing the same data with new shape.

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

dragon.EagerTensor – The output tensor.

See also

dragon.reshape(…) : Change the dimensions of input.

set_value

EagerTensor.set_value(value)[source]

Map the value to storage.

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

dragon.EagerTensor – The self.

truncated_normal

EagerTensor.truncated_normal(
  mean=0,
  std=1
)

Fill self from a truncated normal distribution.

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

dragon.EagerTensor – The self.

uniform

EagerTensor.uniform(
  low=0,
  high=1
)

Fill self from a uniform distribution.

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

dragon.EagerTensor – The self.

Overrides

__add__

EagerTensor.__add__(value)

Compute the element-wise addition.

\[\text{out} = \text{self} + \text{value} \]
Parameters:
Returns:

dragon.EagerTensor – The output tensor.

See also

dragon.math.add(…) : Compute the element-wise addition.

__div__

EagerTensor.__div__(value)

Compute the element-wise division.

\[\text{out} = \text{self} \div \text{value} \]
Parameters:
Returns:

dragon.EagerTensor – The output tensor.

See also

dragon.math.div(…) : Compute the element-wise division.

__float__

EagerTensor.__float__()[source]

Return a float python scalar.

Returns:
float – The float value.

__ge__

EagerTensor.__ge__(other)

Compute element-wise greater-equal comparison.

\[\text{out} = (\text{self} \geq \text{other}) \]
Parameters:
Returns:

dragon.EagerTensor – The output tensor.

See also

dragon.math.greater_equal(…) : Compute element-wise greater-equal comparison.

__getitem__

EagerTensor.__getitem__(item)

Select the elements at the specific indices.

Parameters:
Returns:

dragon.EagerTensor – The output tensor.

See also

dragon.slice(…) : Select the elements according to the given sections.

See also

dragon.masked_select(…) : Select the elements where the given mask is 1.

__gt__

EagerTensor.__gt__(other)

Compute element-wise greater comparison.

\[\text{out} = (\text{self} > \text{other}) \]
Parameters:
Returns:

dragon.EagerTensor – The output tensor.

See also

dragon.math.greater(…) : Compute element-wise greater comparison.

__iadd__

EagerTensor.__iadd__(value)

Compute the element-wise addition.

\[\text{self} \mathrel{+}= \text{value} \]
Parameters:
Returns:

dragon.EagerTensor – The self.

See also

dragon.math.add(…) : Compute the element-wise addition.

__idiv__

EagerTensor.__idiv__(value)

Compute the element-wise division.

\[\text{self} \mathrel{\div}= \text{value} \]
Parameters:
Returns:

dragon.EagerTensor – The self.

See also

dragon.math.div(…) : Compute the element-wise division.

__imul__

EagerTensor.__imul__(value)

Compute the element-wise multiplication.

\[\text{self} \mathrel{\times}= \text{value} \]
Parameters:
Returns:

dragon.EagerTensor – The self.

See also

dragon.math.mul(…) : Compute the element-wise multiplication.

__int__

EagerTensor.__int__()[source]

Return a int python scalar.

Returns:
int – The int value.

__isub__

EagerTensor.__isub__(value)

Compute the element-wise division.

\[\text{self} \mathrel{-}= \text{value} \]
Parameters:
Returns:

dragon.EagerTensor – The self.

See also

dragon.math.sub(…) : Compute the element-wise subtraction.

__le__

EagerTensor.__le__(other)

Compute element-wise less-equal comparison.

\[\text{out} = (\text{self} \leq \text{other}) \]
Parameters:
Returns:

dragon.EagerTensor – The output tensor.

See also

dragon.math.less_equal(…) : Compute element-wise less-equal comparison.

__lt__

EagerTensor.__lt__(other)

Compute element-wise less comparison.

\[\text{out} = (\text{self} < \text{other}) \]
Parameters:
Returns:

dragon.EagerTensor – The output tensor.

See also

dragon.math.less(…) : Compute element-wise less comparison.

__mul__

EagerTensor.__mul__(value)

Compute the element-wise multiplication.

\[\text{out} = \text{self} \times \text{value} \]
Parameters:
Returns:

dragon.EagerTensor – The output tensor.

See also

dragon.math.mul(…) : Compute the element-wise multiplication.

__neg__

EagerTensor.__neg__()

Compute the element-wise negative.

\[y = -x \]
Returns:
dragon.EagerTensor – The output tensor.

See also

dragon.math.negative(…) : Compute the element-wise negative.

__radd__

EagerTensor.__radd__(other)[source]

Compute the element-wise addition.

\[\text{out} = \text{value} + \text{self} \]
Parameters:
Returns:

dragon.Tensor – The output tensor.

See also

dragon.math.add(…) : Compute the element-wise addition.

__rdiv__

EagerTensor.__rdiv__(value)

Compute the element-wise division.

\[\text{out} = \text{value} \div \text{self} \]
Parameters:
Returns:

dragon.EagerTensor – The output tensor.

See also

dragon.math.div(…) : Compute the element-wise division.

__rmul__

EagerTensor.__rmul__(value)

Compute the element-wise multiplication.

\[\text{out} = \text{value} \times \text{self} \]
Parameters:
Returns:

dragon.EagerTensor – The output tensor.

See also

dragon.math.mul(…) : Compute the element-wise multiplication.

__rsub__

EagerTensor.__rsub__(value)

Compute the element-wise subtraction.

\[\text{out} = \text{value} - \text{self} \]
Parameters:
Returns:

dragon.EagerTensor – The output tensor.

See also

dragon.math.sub(…) : Compute the element-wise subtraction.

__setitem__

EagerTensor.__setitem__(
  key,
  value
)

Set the value at the specific indices.

Parameters:

See also

dragon.assign(…) : Assign the value to ref.

See also

dragon.masked_assign(…) : Assign the value to ref where mask is 1.

__sub__

EagerTensor.__sub__(value)

Compute the element-wise subtraction.

\[\text{out} = \text{self} - \text{value} \]
Parameters:
Returns:

dragon.EagerTensor – The output tensor.

See also

dragon.math.sub(…) : Compute the element-wise subtraction.