Tensor

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

Tensor abstraction under the graph execution.

It is provided to construct operators symbolically, while can also be a navigation to the storage.

__init__

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

Create a Tensor.

Parameters:
  • name (str, optional) – The optional tensor name.
  • shape (sequence, optional) – The optional tensor shape.
  • dtype (str, optional) – The optional data type.

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:
Sequence[int] – The shape.

Methods

astype

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

See also

dragon.cast(…) : Cast the data type of input.

constant

Tensor.constant(value=0)[source]

Register as a variable with constant initializer.

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

dragon.Tensor – The self.

copy

Tensor.copy()

Return a tensor with containing data copied.

Returns:
dragon.Tensor – The output tensor.

See also

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

get_value

Tensor.get_value()

Copy the data from storage.

Returns:
numpy.ndarray – The deep copied value.

See also

dragon.workspace.fetch_tensor(…) : Fetch the value of given tensor.

glorot_normal

Tensor.glorot_normal(scale=2.0)[source]

Register as a variable with glorot normal initializer.

\[\text{self} \leftarrow N(0, \sqrt{\frac{scale}{\text{FanIn}}}) \]
Parameters:
  • scale (number, optional, default=2.) – The scale factor of distribution.
Returns:

dragon.Tensor – The self.

glorot_uniform

Tensor.glorot_uniform(scale=3.0)[source]

Register as a variable with glorot uniform initializer.

\[\text{self} \leftarrow U( -\sqrt{\frac{scale}{\text{FanIn}}}, \sqrt{\frac{scale}{\text{FanIn}}} ) \]
Parameters:
  • scale (number, optional, default=3.) – The scale factor of distribution.
Returns:

dragon.Tensor – The self.

normal

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

Register as a variable with normal initializer.

\[\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.Tensor – The self.

placeholder

Tensor.placeholder()[source]

Register as a placeholder with zero initializer.

\[\text{self} \leftarrow 0 \]
Returns:
dragon.Tensor – The self.

reshape

Tensor.reshape(shape)

Return a tensor containing the same data with new shape.

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

dragon.Tensor – The output tensor.

See also

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

set_value

Tensor.set_value(value)

Feed the const value to storage.

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

dragon.Tensor – The self.

See also

dragon.workspace.feed_tensor(…) : Feed the value to the given tensor.

truncated_normal

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

Register as a variable with truncated normal initializer.

\[\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.Tensor – The self.

uniform

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

Register as a variable with uniform initializer.

\[\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.Tensor – The self.

variable

Tensor.variable()[source]

Register as a variable with zero initializer.

\[self \leftarrow 0 \]
Returns:
dragon.Tensor – The self.

Overrides

__add__

Tensor.__add__(value)

Compute the element-wise addition.

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

dragon.Tensor – The y.

See also

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

__div__

Tensor.__div__(value)

Compute the element-wise division.

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

dragon.Tensor – The output tensor.

See also

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

__float__

Tensor.__float__()[source]

Return a float python scalar.

Returns:
float – The float value.

__ge__

Tensor.__ge__(other)

Compute element-wise greater-equal comparison.

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

dragon.Tensor – The output tensor.

See also

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

__getitem__

Tensor.__getitem__(item)

Select the elements at the specific indices.

Parameters:
Returns:

dragon.Tensor – 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__

Tensor.__gt__(other)

Compute element-wise greater comparison.

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

dragon.Tensor – The output tensor.

See also

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

__int__

Tensor.__int__()[source]

Return a int python scalar.

Returns:
int – The int value.

__le__

Tensor.__le__(other)

Compute element-wise less-equal comparison.

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

dragon.Tensor – The output tensor.

See also

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

__lt__

Tensor.__lt__(other)

Compute element-wise less comparison.

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

dragon.Tensor – The output tensor.

See also

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

__mul__

Tensor.__mul__(value)

Compute the element-wise multiplication.

\[\text{out} = \text{self} \times \text{value} \]
Parameters:
  • value (Union[dragon.Tensor, number]) – The value to multiply.
Returns:

dragon.Tensor – The output tensor.

See also

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

__neg__

Tensor.__neg__()

Compute the element-wise negative.

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

See also

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

__radd__

Tensor.__radd__(value)

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__

Tensor.__rdiv__(value)

Compute the element-wise division.

\[\text{out} = \text{value} \div \text{self} \]
Parameters:
  • value (Union[dragon.Tensor, number]) – The value to be divided.
Returns:

dragon.Tensor – The output tensor.

See also

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

__rmul__

Tensor.__rmul__(value)

Compute the element-wise multiplication.

\[\text{out} = \text{value} \times \text{self} \]
Parameters:
  • value (Union[dragon.Tensor, number]) – The value to multiply.
Returns:

dragon.Tensor – The output tensor.

See also

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

__rsub__

Tensor.__rsub__(value)

Compute the element-wise subtraction.

\[\text{out} = \text{value} - \text{self} \]
Parameters:
  • value (Union[dragon.Tensor, number]) – The value to be subtracted.
Returns:

dragon.Tensor – The output tensor.

See also

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

__setitem__

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

Tensor.__sub__(value)

Compute the element-wise subtraction.

\[\text{out} = \text{self} - \text{value} \]
Parameters:
  • value (Union[dragon.Tensor, number]) – The value to subtract.
Returns:

dragon.Tensor – The output tensor.

See also

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