Tensor

Basic

List Brief
grad Compute the gradients for variables with respect to the cost.
disconnected_grad Return the input with truncated gradient flow.

Variable

List Brief
scalar Return a scalar variable.
iscalar Return a int32 scalar variable.

Initializer

List Brief
constant Initialize a tensor with constant value.
zeros Initialize a tensor with zeros.
zeros_like Initialize a tensor with zeros, refer the shape of another tensor.
ones Initialize a tensor with ones.
ones_like Initialize a tensor with ones, refer the shape of another tensor.

Operator

List Brief
cast Cast input to the tensor of specific data type.
dot Calculate A dot B.
transpose Transpose the input according to the given permutations.
sum Compute the sum along the given axis.
mean Compute the mean along the given axis.
prod Compute the product along the given axis.
argmax Compute the indices of maximum elements along the given axis.
argmin Compute the indices of minimum elements along the given axis.
square Calculate the square of input.
sqrt Calculate the sqrt of input.
pow Calculate the power of input.
exp Calculate the exponential of input.
log Calculate the logarithm of input.
clip Clip the input to be between min and max.
join Convenience function to concatenate along the given axis.
stack Stack the inputs along the given axis.
concatenate Concatenate the inputs along the given axis.
reshape Reshape the dimensions of input.
flatten Flatten the input by keeping the specific dimensions.
repeat Repeat the input along the given axis.
tile Tile the input according to the given multiples.
arange Return a vector of elements by arange.
cumsum Compute the cumulative sum along the given axis.
cumprod Compute the cumulative product along the given axis.
to_one_hot Generate a matrix where each row corresponds to the one hot encoding.

NNet

List Brief
batch_normalization Batch Normalization, introduced by [Ioffe & Szegedy, 2015]
relu Rectified Linear Unit function, introduces by [Nair & Hinton, 2010].
softmax Softmax function.
categorical_crossentropy Compute the categorical cross-entropy between input and target distribution.
sigmoid Sigmoid function.
tanh TanH function.
binary_crossentropy Compute the binary cross-entropy between input and target distribution.

API Reference

dragon.vm.theano.gradient.grad(cost, wrt, **kwargs)

Compute the gradients for variables with respect to the cost.

Parameters:
  • cost (Tensor) – The cost.
  • wrt (Tensor or list of Tensor) – The variables w.r.t the cost.
Returns:

The gradients of variables.

Return type:

Tensor or list of Tensor

Examples

>>> import dragon as dg
>>> x = dg.Tensor('x').Variable()
>>> y = x * 2
>>> dx = grad(y, x)
>>> z = dg.Tensor('z').Variable()
>>> y = x + z
>>> dx, dz = grad(y, [x, z])
dragon.vm.theano.gradient.disconnected_grad(x)

Return the identity of input with truncated gradient flow.

The expression itself is unaffected, but the gradient is stopped.

Parameters:x (Tensor) – The input tensor.
Returns:The identity of input.
Return type:Tensor
dragon.vm.theano.tensor.basic.scalar(name=None, dtype=None)

Return a scalar variable.

If dtype is None, use config.floatX.

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

The scalar variable.

Return type:

Tensor

dragon.vm.theano.tensor.basic.iscalar(name=None)

Return a int32 scalar variable.

Parameters:name (str, optional) – The name of Tensor.
Returns:The scalar variable.
Return type:Tensor
dragon.vm.theano.tensor.basic.zeros(shape, dtype=None)

Initialize a tensor with zeros.

If dtype is None, use config.floatX.

Parameters:
  • shape (sequence of int) – The shape of Tensor.
  • dtype (str, optional) – The data type of Tensor.
Returns:

The initialized tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.zeros_like(model, dtype=None, **kwargs)

Initialize a tensor with zeros, refer the shape of another tensor.

The values can be access only after the run of graph.

If dtype is None, use config.floatX.

Parameters:
  • model (Tensor) – The tensor to refer shape.
  • dtype (str, optional) – The data type of Tensor.
Returns:

The initialized tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.ones(shape, dtype=None)

Initialize a tensor with ones.

If dtype is None, use config.floatX.

Parameters:
  • shape (sequence of int) – The shape of Tensor.
  • dtype (str, optional) – The data type of Tensor.
Returns:

The initialized tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.ones_like(model, dtype=None, **kwargs)

Initialize a tensor with ones, refer the shape of another tensor.

The values can be access only after the run of graph.

If dtype is None, use config.floatX.

Parameters:
  • model (Tensor) – The tensor to refer shape.
  • dtype (str) – The data type of Tensor.
Returns:

The initialized tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.cast(x, dtype)

Cast input to the tensor of specific data type.

If dtype is None, use config.floatX.

Parameters:
  • x (Tensor) – The input tensor.
  • dtype (str) – The data type.
Returns:

The output tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.dot(a, b)

Calculate A dot B.

This operator can trigger Matrix Multiplication or Matrix Vector Multiplication also.

Parameters:
  • a (Tensor) – The input, represents as A.
  • b (Tensor) – The input, represents as B.
Returns:

The output tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.transpose(x, axes=None)

Transpose the input according to the given permutations.

Parameters:
  • x (Tensor) – The input tensor.
  • axes (sequence of int, optional) – The permutations. Default is None (Reverse Dimensions).
Returns:

The output tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.max(x, axis=None, keepdims=False)

Compute the values of maximum elements along the given axis.

Parameters:
  • x (Tensor) – The input tensor.
  • axis (int) – The axis to compute. Default is None (Along all axes).
  • keep_dims (boolean) – Whether to keep dims after computing.
Returns:

The values.

Return type:

Tensor

dragon.vm.theano.tensor.basic.min(x, axis=None, keepdims=False)

Compute the values of minimum elements along the given axis.

Parameters:
  • x (Tensor) – The input tensor.
  • axis (int) – The axis to compute. Default is None (Along all axes).
  • keep_dims (boolean) – Whether to keep dims after computing.
Returns:

The values.

Return type:

Tensor

dragon.vm.theano.tensor.basic.sum(input, axis=None, keepdims=False, **kwargs)

Compute the sum along the given axis.

Parameters:
  • input (Tensor) – The input tensor.
  • axis (int) – The axis to compute. Default is None (Along all axes).
  • keep_dims (boolean) – Whether to keep dims after computing.
Returns:

The sum result.

Return type:

Tensor

dragon.vm.theano.tensor.basic.mean(input, axis=None, keepdims=False, **kwargs)

Compute the mean along the given axis.

Parameters:
  • input (Tensor) – The input tensor.
  • axis (int) – The axis to compute. Default is None (Along all axes).
  • keep_dims (boolean) – Whether to keep dims after computing.
Returns:

The mean result.

Return type:

Tensor

dragon.vm.theano.tensor.basic.prod(input, axis=None, keepdims=False, **kwargs)

Compute the product along the given axis.

Parameters:
  • input (Tensor) – The input tensor.
  • axis (int) – The axis to compute. Default is None (Along all axes).
  • keep_dims (boolean) – Whether to keep dims after computing.
Returns:

The product result.

Return type:

Tensor

dragon.vm.theano.tensor.basic.argmax(x, axis=None, keepdims=False)

Compute the indices of maximum elements along the given axis.

Parameters:
  • x (Tensor) – The input tensor.
  • axis (int) – The axis to compute. Default is None (Along all axes).
  • keep_dims (boolean) – Whether to keep dims after computing.
Returns:

The indices.

Return type:

Tensor

dragon.vm.theano.tensor.basic.argmin(x, axis=None, keepdims=False)

Compute the indices of minimum elements along the given axis.

Parameters:
  • x (Tensor) – The input tensor.
  • axis (int) – The axis to compute. Default is None (Along all axes).
  • keep_dims (boolean) – Whether to keep dims after computing.
Returns:

The indices.

Return type:

Tensor

dragon.vm.theano.tensor.basic.square(a)

Calculate the square of input.

Parameters:a (Tensor) – The input tensor.
Returns:The square result.
Return type:Tensor
dragon.vm.theano.tensor.basic.sqrt(a)

Calculate the sqrt of input.

Parameters:a (Tensor) – The input tensor.
Returns:The sqrt result.
Return type:Tensor
dragon.vm.theano.tensor.basic.pow(a, power)

Calculate the power of input.

Parameters:a (Tensor) – The input tensor.
Returns:The pow result.
Return type:Tensor
dragon.vm.theano.tensor.basic.exp(a)

Calculate the exponential of input.

Parameters:a (Tensor) – The input tensor.
Returns:The exponential result.
Return type:Tensor
dragon.vm.theano.tensor.basic.log(a)

Calculate the logarithm of input.

Parameters:a (Tensor) – The input tensor.
Returns:The logarithm result.
Return type:Tensor
dragon.vm.theano.tensor.basic.clip(x, min=None, max=None)

Clip the input to be between min and max.

Parameters:
  • x (Tensor) – The input tensor.
  • min (number, optional) – The min bound. Default is None (Ignore).
  • max (number, optional) – The max bound. Default is None (Ignore).
Returns:

The clip result.

Return type:

Tensor

dragon.vm.theano.tensor.basic.join(axis, *tensors_list)

Convenience function to concatenate along the given axis.

Parameters:
  • axis (int) – The axis to concatenate.
  • tensor_list (list of Tensor) – The inputs to concatenate.
Returns:

The output tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.stack(*tensors, **kwargs)

Stack the inputs along the given axis.

All dimensions of inputs should be same.

The axis can be negative.

Parameters:
  • tensors (list of Tensor) – The inputs.
  • axis (int) – The axis to stack.
Returns:

The output tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.concatenate(tensor_list, axis=0)

Concatenate the inputs along the given axis.

All dimensions except specific axis should be same.

Parameters:
  • tensor_list (list of Tensor) – The inputs to concatenate.
  • axis (int) – The axis to concatenate.
Returns:

The output tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.reshape(x, newshape, **kwargs)

Reshape the dimensions of input.

Parameters:
  • x (Tensor) – The input tensor.
  • newshape (list or tuple) – The new shape.
Returns:

The output tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.flatten(x, outdim=1)

Flatten the input by keeping the specific dimensions.

Parameters:
  • x (Tensor) – The input tensor.
  • outdim (int) – The number of dimensions to keep.
Returns:

The output tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.repeat(x, repeats, axis=None)

Repeat the input along the given axis.

Parameters:
  • x (Tensor) – The input tensor.
  • repeats (int) – The magnitude of repeating.
  • axis (int) – The axis to repeat. Defaults is -1 (Repeat as Scalar).
Returns:

The output tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.tile(x, reps, **kwargs)

Tile the input according to the given multiples.

Parameters:
  • x (Tensor) – The input tensor.
  • reps (list of int) – The multiple of each axis.
Returns:

The output tensor.

Return type:

Tensor

dragon.vm.theano.tensor.basic.arange(start, stop=None, step=1, dtype=None)

Return a vector of elements by arange.

If stop is None, use the range: [0, start).

Parameters:
  • start (int or Tensor) – The start of the range.
  • stop (int or Tensor) – The stop of range.
  • step (int or Tensor) – The interval between two elements.
  • dtype (str) – The data type. float32 or int32.
Returns:

The vector.

Return type:

Tensor

dragon.vm.theano.tensor.extra_ops.cumsum(x, axis=None)

Compute the cumulative sum along the given axis.

Parameters:
  • x (Tensor) – The input tensor.
  • axis (int, optional) – The axis to sum.
dragon.vm.theano.tensor.extra_ops.cumprod(x, axis=None)

Compute the cumulative product along the given axis.

Parameters:
  • x (Tensor) – The input tensor.
  • axis (int, optional) – The axis to sum.
dragon.vm.theano.tensor.extra_ops.to_one_hot(y, nb_class, **kwargs)

Generate a matrix where each row corresponds to the one hot encoding.

The y should be a 1d vector.

Parameters:
  • y (Tensor) – The input tensor.
  • nb_class (int) – The number of classes.
Returns:

The one hot matrix.

Return type:

Tensor

dragon.vm.theano.tensor.nnet.batch_normalization(
   inputs,
   gamma,
   beta,
   mean,
   var,
   **kwargs
)

Batch Normalization, introduced by [Ioffe & Szegedy, 2015]

Parameters:
  • inputs (Tensor) – The input tensor.
  • gamma (Tensor) – The scale parameter.
  • beta (Tensor) – The shift parameter.
  • mean (Tensor) – The moving average of mean.
  • var (Tensor) – The moving average of variance.
Returns:

The output tensor.

Return type:

Tensor

dragon.vm.theano.tensor.nnet.relu(x, alpha=0)

Rectified Linear Unit function, introduces by [Nair & Hinton, 2010].

Parameters:
  • x (Tensor) – The input tensor.
  • alpha (float) – The slope of negative side.
Returns:

The output tensor.

Return type:

Tensor

dragon.vm.theano.tensor.nnet.softmax(c)

Softmax function.

The c should be a matrix, without any spatial axes.

Parameters:c (Tensor) – The input tensor.
Returns:The output tensor.
Return type:Tensor
dragon.vm.theano.tensor.nnet.categorical_crossentropy(coding_dist, true_dist, axis=1)

Compute the categorical cross-entropy between input and target distribution.

Parameters:
  • coding_dist (Tensor) – The distribution of input.
  • true_dist (Tensor) – The distribution of target.
  • axis (int) – The axis of category.
Returns:

The categorical cross-entropy.

Return type:

Tensor

dragon.vm.theano.tensor.nnet.sigmoid(x)

Sigmoid function.

Parameters:inputs (Tensor) – The input tensor.
Returns:The output tensor.
Return type:Tensor
dragon.vm.theano.tensor.nnet.tanh(x)

TanH function.

Parameters:inputs (Tensor) – The input tensor.
Returns:The output tensor.
Return type:Tensor
dragon.vm.theano.tensor.nnet.binary_crossentropy(output, target)

Compute the binary cross-entropy between input and target distribution.

Parameters:
  • output (Tensor) – The distribution of input.
  • target (Tensor) – The distribution of target.
Returns:

The binary cross-entropy.

Return type:

Tensor