ops – Some Common Ops and extra Ops stuff

This file contains auxiliary Ops, used during the compilation phase and Ops building class (FromFunctionOp) and decorator (as_op()) that help make new Ops more rapidly.

class theano.compile.ops.FromFunctionOp(fn, itypes, otypes, infer_shape)[source]

Build a basic Theano Op around a function.

Since the resulting Op is very basic and is missing most of the optional functionalities, some optimizations may not apply. If you want to help, you can supply an infer_shape function that computes the shapes of the output given the shapes of the inputs.

Also the gradient is undefined in the resulting op and Theano will raise an error if you attempt to get the gradient of a graph containing this op.

class theano.compile.ops.OutputGuard[source]

This op is used only internally by Theano.

Only the AddDestroyHandler optimizer tries to insert them in the graph.

This Op is declared as destructive while it is not destroying anything. It returns a view. This is used to prevent destruction of the output variables of a Theano function.

There is a mechanism in Theano that should prevent this, but the use of OutputGuard adds a safeguard: it may be possible for some optimization run before the add_destroy_handler phase to bypass this mechanism, by making in-place optimizations.

TODO: find a current full explanation.

class theano.compile.ops.Rebroadcast(*axis)[source]

Change the input’s broadcastable fields in some predetermined way.

See also

unbroadcast, addbroadcast, patternbroadcast

Notes

Works inplace and works for CudaNdarrayType.

Example

Rebroadcast((0, True), (1, False))(x) would make x broadcastable in axis 0 and not broadcastable in axis 1.

class theano.compile.ops.Shape[source]

L{Op} to return the shape of a matrix.

Notes

Non-differentiable.

class theano.compile.ops.Shape_i(i)[source]

L{Op} to return the shape of a matrix.

Notes

Non-differentiable.

class theano.compile.ops.SpecifyShape[source]

L{Op} that puts into the graph the user-provided shape.

In the case where this op stays in the final graph, we assert the shape. For this the output of this op must be used in the graph. This is not the case most of the time if we only take the shape of the output. Maybe there are other optimizations that will mess with this.

Notes

Maybe in the future we will never do the assert!

We currently don’t support specifying partial shape information.

TODO : test this op with sparse. Do C code for them too.

class theano.compile.ops.ViewOp[source]

Returns an inplace view of the input. Used internally by Theano.

theano.compile.ops.as_op(itypes, otypes, infer_shape=None)[source]

Decorator that converts a function into a basic Theano op that will call the supplied function as its implementation.

It takes an optional infer_shape parameter that should be a callable with this signature:

def infer_shape(node, input_shapes):
... return output_shapes

Here input_shapes and output_shapes are lists of tuples that represent the shape of the corresponding inputs/outputs.

This should not be used when performance is a concern since the very basic nature of the resulting Op may interfere with certain graph optimizations.

Examples

@as_op(itypes=[theano.tensor.fmatrix, theano.tensor.fmatrix],
otypes=[theano.tensor.fmatrix])
def numpy_dot(a, b):
return numpy.dot(a, b)
theano.compile.ops.register_deep_copy_op_c_code(typ, code, version=())[source]

Tell DeepCopyOp how to generate C code for a Theano Type.

Parameters:
  • typ (Theano type) – It must be the Theano class itself and not an instance of the class.
  • code (C code) – Deep copies the Theano type ‘typ’. Use %(iname)s and %(oname)s for the input and output C variable names respectively.
  • version – A number indicating the version of the code, for cache.
theano.compile.ops.register_rebroadcast_c_code(typ, code, version=())[source]

Tell Rebroadcast how to generate C code for a Theano Type.

typ
: Theano type
It must be the Theano class itself and not an instance of the class.
code
: C code
That checks if the dimension %(axis)s is of shape 1 for the Theano type ‘typ’. Use %(iname)s and %(oname)s for the input and output C variable names respectively, and %(axis)s for the axis that we need to check. This code is put in a loop for all axes.
version
A number indicating the version of the code, for cache.
theano.compile.ops.register_shape_c_code(type, code, version=())[source]

Tell Shape Op how to generate C code for a Theano Type.

Parameters:
  • typ (Theano type) – It must be the Theano class itself and not an instance of the class.
  • code (C code) – Returns a vector representing the shape for the Theano type ‘typ’. Use %(iname)s and %(oname)s for the input and output C variable names respectively.
  • version – A number indicating the version of the code, for cache.
theano.compile.ops.register_shape_i_c_code(typ, code, check_input, version=())[source]

Tell Shape_i how to generate C code for a Theano Type.

Parameters:
  • typ (Theano type) – It must be the Theano class itself and not an instance of the class.
  • code (C code) – Gets the shape of dimensions %(i)s for the Theano type ‘typ’. Use %(iname)s and %(oname)s for the input and output C variable names respectively.
  • version – A number indicating the version of the code, for cache.
theano.compile.ops.register_specify_shape_c_code(typ, code, version=(), c_support_code_apply=None)[source]

Tell SpecifyShape how to generate C code for a Theano Type.

Parameters:
  • typ (Theano type) – It must be the Theano class itself and not an instance of the class.
  • code (C code) – Checks the shape and returns a view for the Theano type ‘typ’. Use %(iname)s and %(oname)s for the input and output C variable names respectively. %(shape)s is the vector of shape of %(iname)s. Check that its length is good.
  • version – A number indicating the version of the code, for cache.
  • c_support_code_apply – Extra code.
theano.compile.ops.register_view_op_c_code(type, code, version=())[source]

Tell ViewOp how to generate C code for a Theano Type.

Parameters:
  • type (Theano type) – It must be the Theano class itself and not an instance of the class.
  • code (C code) – Returns a view for the Theano type ‘type’. Use %(iname)s and %(oname)s for the input and output C variable names respectively.
  • version – A number indicating the version of the code, for cache.
theano.compile.ops.shape_i(var, i, fgraph=None)[source]

Equivalent of var.shape[i], but apply if possible the shape feature optimization.

This is useful in optimization that need to get the shape. This remove the need of the following shape_feature optimization that convert it. So this speed up optimization and remove Equilibrium max iteration problems.

Parameters:
  • var – The variable we want to take the shape of.
  • i – The shape dimensions we want
  • fgraph (optional) – If var.fgraph do not exist, the fgraph that have the shape_feature to introduce var in to get the optimized shape.