ops – Some Common Ops and extra Ops stuff
This file contains auxiliary Ops, used during the compilation phase and Opsbuilding class (FromFunctionOp
) and decorator (as_op()
) thathelp 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 theoptional functionalities, some optimizations may not apply. If youwant to help, you can supply an infer_shape function that computesthe shapes of the output given the shapes of the inputs.
Also the gradient is undefined in the resulting op and Theano willraise an error if you attempt to get the gradient of a graphcontaining 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 outputvariables of a Theano function.
There is a mechanism in Theano that should prevent this, but the useof OutputGuard adds a safeguard: it may be possible for some optimizationrun before the add_destroy_handler phase to bypass this mechanism, bymaking 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 inaxis 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.
Shapei
(_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 notthe 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.
asop
(_itypes, otypes, infer_shape=None)[source]- Decorator that converts a function into a basic Theano op that will callthe supplied function as its implementation.
It takes an optional infer_shape parameter that should be a callable withthis signature:
- def infer_shape(node, input_shapes):
- … return output_shapes
Here input_shapes and output_shapes are lists of tuples that representthe shape of the corresponding inputs/outputs.
This should not be used when performance is a concern since the very basicnature 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.
registerdeep_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 theinput and output C variable names respectively.
- version – A number indicating the version of the code, for cache.
theano.compile.ops.
registerrebroadcast_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 variablenames 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.
registershape_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 namesrespectively.
- version – A number indicating the version of the code, for cache.
theano.compile.ops.
registershape_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 namesrespectively.
- version – A number indicating the version of the code, for cache.
theano.compile.ops.
registerspecify_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 namesrespectively. %(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.
registerview_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)sfor the input and output C variable names respectively.
- version – A number indicating the version of the code, for cache.
theano.compile.ops.
shapei
(_var, i, fgraph=None)[source]- Equivalent of var.shape[i], but apply if possible the shape featureoptimization.
This is useful in optimization that need to get the shape. Thisremove the need of the following shape_feature optimization thatconvert it. So this speed up optimization and remove Equilibriummax 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 tointroduce var in to get the optimized shape.