Misc

dragon.operators.misc.Cast(inputs, dtype='float32', inplace=False, **kwargs)

Cast the data type of inputs to a specific one.

If inplace is True, cast self instead of returning a new one.

Type Constraints: (bool, int8, uint8, int32, int64, float16, float32, float64)

Parameters:
  • inputs (Tensor) – The input tensor.
  • dtype (str) – The specific data type.
  • inplace (bool) – Whether to modify the inputs.
Returns:

The output tensor.

Return type:

Tensor

Examples

>>> x = Tensor('x', dtype='float32').Variable()
>>> y = Cast(x, 'int32')
>>> z = x.astype('int64')
>>> xx = x.astype('float64', inplace=True)
>>> print(x.name, xx.name)
dragon.operators.misc.Run(
   inputs,
   module,
   op,
   param_str='',
   num_outputs=1,
   **kwargs
)

Run a custom operator. (Without GradientFlow)

Type Constraints: None

Parameters:
  • inputs (sequence of Tensor) – The inputs.
  • module (str) – The module.
  • op (str) – The class under the module.
  • param_str (str) – The str describing parameters.
  • num_outputs (int) – The number of num_outputs.
Returns:

The outputs.

Return type:

sequence of Tensor

Notes

This operator is designed to truncate gradients flow.

If you want to flow gradients, use Template(*args, **kwargs).

References

DataProcessOp - How to custom a RunOp in Dragon.

dragon.operators.misc.Template(
   inputs,
   module,
   op,
   param_str='',
   num_outputs=1,
   **kwargs
)

Run a custom operator. (With GradientFlow)

Type Constraints: None

Parameters:
  • inputs (sequence of Tensor) – The inputs.
  • module (str) – The module.
  • op (str) – The class under the module.
  • param_str (str) – The str describing parameters.
  • num_outputs (int) – The number of num_outputs.
Returns:

The outputs.

Return type:

sequence of Tensor

References

VecMultOp - How to custom a TemplateOp in Dragon.

dragon.operators.misc.Accuracy(
   inputs,
   top_k=1,
   axis=1,
   ignore_labels=(),
   **kwargs
)

Calculate the Top-K accuracy.

Type Constraints:

  • logits (float32)
  • labels (float32, int64)
Parameters:
  • inputs (sequence of Tensor) – The inputs, represent [logits, labels].
  • top_k (int) – The top-k accuracy to calculate.
  • axis (int) – The axis of classes.
  • ignore_labels (sequence of int) – The labels to ignore.
Returns:

The top-k accuracy.

Return type:

Tensor

dragon.operators.misc.StopGradient(inputs, **kwargs)

Return the identity of input with truncated gradient flow.

The expression itself is unaffected, but gradient is stopped.

Type Constraints: None

Parameters:inputs (Tensor) – The input tensor.
Returns:A identity of input.
Return type:Tensor