Tensor

Quick Reference

List Brief
Tensor.name Return or Set the name.
Tensor.shape Return or Set the shape.
Tensor.get_shape Return the shape.
Tensor.dtype Return or Set the data type.
Tensor.set_value Feed the values to C++ backend.
Tensor.get_value Fetch the values from C++ backend.
Tensor.convert_to Converts the given value to a Tensor.
Tensor.copy Return a Tensor with same content.
Tensor.reshape Reshape the dimensions of input.
Tensor.dimshuffle Shuffle the dimensions.
Tensor.eval Run and return the computing results of this tensor.

Register

List Brief
Tensor.Variable Register as an empty variable.
Tensor.Placeholder Register as a placeholder.
Tensor.Constant Register as a variable with constant initializer.
Tensor.Uniform Register as a variable with uniform initializer.
Tensor.Normal Register as a variable with normal initializer.
Tensor.TruncatedNormal Register as a variable with truncated normal initializer.
Tensor.Gaussian Register as a variable with gaussian initializer.
Tensor.GlorotUniform Register as a variable with glorot uniform initializer.
Tensor.GlorotNormal Register as a variable with glorot normal initializer.

Override

List Brief
Tensor.__add__ x.__add__(y) <=> x + y
Tensor.__radd__ x.__radd__(y) <=> y + x
Tensor.__sub__ x.__sub__(y) <=> x - y
Tensor.__rsub__ x.__rsub__(y) <=> y - x
Tensor.__mul__ x.__mul__(y) <=> x * y
Tensor.__rmul__ x.__rmul__(y) <=> y * x
Tensor.__div__ x.__div__(y) <=> x / y
Tensor.__rdiv__ x.__rdiv__(y) <=> y / x
Tensor.__neg__ x.__neg__() <=> -x
Tensor.__gt__ x.__gt__() <=> x > y
Tensor.__ge__ x.__ge__() <=> x >= y
Tensor.__lt__ x.__lt__() <=> x < y
Tensor.__le__ x.__le__() <=> x <= y
Tensor.__eq__ x.__eq__() <=> x == y
Tensor.__repr__ Return the information(name/shape).
Tensor.__getitem__ Return the value at the specific indices.
Tensor.__setitem__ Set the value at the specific indices.

API Reference

class dragon.core.tensor.Tensor(name=None, shape=None, dtype=None)

Tensor is generally used to represent a n-dimension array, across all virtual frameworks, with a optional name and shape.

It does not have any storage, i.e., just provided to be a navigation to the real tensor in the C++ backend.

__init__(name=None, shape=None, dtype=None)

Construct a Tensor instance.

Parameters:
  • name (str, optional) – The name of Tensor.
  • shape (list, optional) – The shape of Tensor.
  • dtype (str, optional) – The type of Tensor.
Returns:

An unregistered Tensor.

Return type:

Tensor

__add__(other)

Calculate x + y.

Parameters:other (Tensor or number) – The y.
Returns:The output tensor.
Return type:Tensor
__radd__(other)

Calculate y + x.

Parameters:other (Tensor or number) – The y.
Returns:The output tensor.
Return type:Tensor
__sub__(other)

Calculate x - y.

Parameters:other (Tensor or number) – The y.
Returns:The output tensor.
Return type:Tensor
__rsub__(other)

Calculate y - x.

Parameters:other (Tensor or number) – The y.
Returns:The output tensor.
Return type:Tensor
__mul__(other)

Calculate x * y.

Parameters:other (Tensor or number) – The y.
Returns:The output tensor.
Return type:Tensor
__rmul__(other)

Calculate y * x.

Parameters:other (Tensor or number) – The y.
Returns:The output tensor.
Return type:Tensor
__div__(other)

Calculate x / y.

Parameters:other (Tensor or number) – The y.
Returns:The output tensor.
Return type:Tensor
__rdiv__(other)

Calculate y / x.

Parameters:other (Tensor or number) – The y.
Returns:The output tensor.
Return type:Tensor
__neg__()

Calculate -x.

Returns:The output tensor.
Return type:Tensor
__gt__(other)

Compute self > other element-wise.

Parameters:other (Tensor or number) – The other tensor.
Returns:The output tensor.
Return type:Tensor
__ge__(other)

Compute self > other element-wise.

Parameters:other (Tensor or number) – The other tensor.
Returns:The output tensor.
Return type:Tensor
__lt__(other)

Compute self < other element-wise.

Parameters:other (Tensor or number) – The other tensor.
Returns:The output tensor.
Return type:Tensor
__le__(other)

Compute self <= other element-wise.

Parameters:other (Tensor or number) – The other tensor.
Returns:The output tensor.
Return type:Tensor
__eq__()

Return self==value.

__eq__()

Return self==value.

__repr__()

Return a format str representing the tensor.

Returns:The format str.
Return type:str
__getitem__(item)

Return the value at the specific indices.

Parameters:item (int, slice or Tensor) – The indices.
Returns:The output tensor.
Return type:Tensor
__setitem__(key, value)

Set the value at the specific indices.

Parameters:
  • key (int, slice or Tensor) – The indices.
  • value (number, sequence or Tensor) – The value.
Returns:

Return type:

None

Variable()

Register as an empty variable.

Returns:The self.
Return type:Tensor
Placeholder()

Register as a placeholder.

Returns:The self.
Return type:Tensor
Constant(value=0)

Register as a variable with constant initializer.

Parameters:value (number, optional, default=0) – The constant value.
Returns:The self.
Return type:Tensor
Uniform(low=0, high=1)

Register as a variable with uniform initializer.

Parameters:
  • low (number, optional, default=0) – The lower bound of uniform distribution.
  • high (number, optional, default=1) – The higher bound of uniform distribution.
Returns:

The self.

Return type:

Tensor

Normal(mu=0, sigma=1)

Register as a variable with normal initializer.

Parameters:
  • mu (number, optional, default=0) – The mu of normal distribution.
  • sigma (number, optional, default=1) – The sigma of normal distribution.
Returns:

The self.

Return type:

Tensor

TruncatedNormal(mu=0, sigma=1)

Register as a variable with truncated normal initializer.

Parameters:
  • mu (number, optional, default=0) – The mu of normal distribution.
  • sigma (number, optional, default=1) – The sigma of normal distribution.
Returns:

The self.

Return type:

Tensor

Gaussian(mean=0, std=1)

Register as a variable with gaussian initializer.

Parameters:
  • mean (number, optional, default=0) – The mean(mu) of normal distribution.
  • std (number, optional, default=1) – The std(sigma) of normal distribution.
Returns:

The self.

Return type:

Tensor

GlorotUniform(scale=3.0)

Register as a variable with glorot uniform initializer.

Parameters:scale (number, optional, default=3.) – The scale factor.
Returns:The self.
Return type:Tensor
GlorotNormal(scale=2.0)

Register as a variable with glorot normal initializer.

Parameters:scale (number, optional, default=2.) – The scale factor.
Returns:The self.
Return type:Tensor
expressions

Return or Set the expressions.

Parameters:value (dict) – The expressions to be used.
Returns:The internal expressions that it has currently stored.
Return type:dict
name

Return or Set the name.

Parameters:value (str) – The name to set.
Returns:The name of this tensor.
Return type:str
set_name(name)

Set the name while getting rid of name scope.

Parameters:name (str) – The name.
Returns:
Return type:None
shape

Return or Set the shape.

Parameters:value (sequence of int) – The shape to set.
Returns:The shape of this tensor.
Return type:sequence of int
ndim

Return the ndim.

If shape is None, returns 0 instead.

Returns:The ndim.
Return type:int
dtype

Return or Set the data type.

Parameters:value (str) – The data type to set.
Returns:The data type.
Return type:str
astype(dtype, inplace=False)

Cast the data type of inputs to a specific one.

Parameters:
  • dtype (str) – The specific dtype.
  • inplace (boolean, optional, default=False) – Whether to modify the inputs.
Returns:

The output tensor.

Return type:

Tensor

extra_targets

Return or Set the extra solving targets.

Parameters:value (set) – Set the extra solving targets.
Returns:The extra solving targets.
Return type:set
clone(tensor)

Clone a existing tensor.

Parameters:tensor (Tensor) – Set the extra solving targets.
Returns:
Return type:None
set_value(new_value, **kwargs)

Feed values to the backend.

Parameters:new_value (array_like) – The values to set.
Returns:The self.
Return type:Tensor

See also

workspace.FeedTensor(*args, **kwargs) - How to feed a Tensor.

get_value()

Copy values from the backend.

Returns:The copied values.
Return type:numpy.ndarray

See also

workspace.FetchTensor(*args, **kwargs) - How to fetch a Tensor.

copy()

Return a Tensor with same content. [Theano Style]

Returns:The copy.
Return type:Tensor

See also

ops.Copy(*args, **kwargs) - How to copy A to B.

reshape(shape, **kwargs)

Reshape the dimensions of input.

Parameters:shape (tuple or list) – The new shape.
Returns:The reshaped output.
Return type:Tensor
dimshuffle(*args, **kwargs)

Shuffle the dimensions. [Theano Style]

Parameters:dimensions (list) – The desired dimensions.
Returns:The dimshuffled output.
Return type:Tensor
classmethod convert_to(value, dtype=None, name=None)

Converts the given value to a Tensor.

Parameters:
  • value (number, sequence or numpy.ndarray) – The value to convert.
  • dtype (str, optional) – The optional data type.
  • name (str, optional) – The optional name for this tensor.
Returns:

The constant contains the value.

Return type:

Tensor

get_shape()

Construct the shape descriptor. [TensorFlow Style]

Returns:The shape description.
Return type:TensorShape
eval(feed_dict=None)

Run and return the computing results of this tensor.

Parameters:feed_dict (dict) – The values to feed.
Returns:The values of this tensor in the backend.
Return type:numpy.ndarray
classmethod Ref(name, shape=None, dtype=None)

Create a reference tensor from a unknown name.

It is useful to get named Tensor navigator anywhere.

Parameters:
  • name (str) – The name of Tensor.
  • shape (None or list) – The shape of Tensor.
  • dtype (None or str) – The type of Tensor.
Returns:

The ref tensor

Return type:

Tensor

classmethod CreateOperator(
   op_type,
   inputs,
   num_outputs=1,
   existing_outputs=None,
   extra_inputs=None,
   name=None,
   **kwargs
)

Construct a new Tensor with specific operator descriptor.

Parameters:
  • op_type (str) – The type of operator.
  • inputs (sequence of Tensor) – The inputs for this operator.
  • num_outputs (int, optional, default=1) – The number of outputs to return.
  • existing_outputs (sequence of Tensor, optional) – The existing outputs for this operator.
  • extra_inputs (sequence of Tensor, optional) – The inputs that should be attached to solving targets.
  • name (str, optional) – The optional name.
Returns:

The outputs of this operator.

Return type:

sequence of Tensor

Examples

>>> import dragon as dg
>>> a = Tensor().Variable()
>>> b = Tensor().Variable()
>>> c = Tensor.CreateOperator(inputs=[a, b], op_type='Add')
>>> a = Tensor().Variable()
>>> b = Tensor().Variable()
>>> c = Tensor().Variable()
>>> c = Tensor.CreateOperator(inputs=[a, b], op_type='Add', existing_outputs=c)
>>> dynamic_shape = Tensor().Variable()
>>> dg.workspace.FeedTensor(dynamic_shape, [1, 2, 3, 4])
>>> a = dg.Fill(shape=dynamic_shape, value=5.0)
>>> print dg.function(outputs=a)
>>> [[ 5.  5.  5.]
     [ 5.  5.  5.]]
Fill(type, **kwargs)

Fill self with the specific type of filler.

Parameters:type (str) – The type of the filler.
Returns:Self, with filler registered implicitly in the backend.
Return type:Tensor