提交 1e51644a authored 作者: abergeron's avatar abergeron

Merge pull request #2030 from Tanjay94/As_Op

@as_op() documentation
......@@ -415,6 +415,95 @@ efficiency over the basic solution that is asked here, the two operations would
have to be jointly optimized explicitly in the code.)
as_op
=====
- Decorator that converts a function into a basic Theano op
that will call the supplied function as its implementation.
- Takes an optional infer_shape parameter that should be a
callable with this signature:
def infer_shape(node, input_shapes):
...
return output_shapes
- `input_shapes` and `output_shapes` are lists of tuples that
represent the shape of the corresponding inputs/outputs.
.. note::
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.
.. note::
Returns FromFunctionOp(fn, itypes, otypes, infer_shape)
FromfunctionOp
==============
- Build a basic Theano Op around a function.
.. note::
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.
Op Example
==========
.. code-block:: python
import theano
import numpy
from theano.compile.ops import as_op
from theano.compile.ops import FromFunctionOp
def infer_shape_numpy_dot(node, input_shapes):
ashp, bshp = input_shapes
return [ashp[:-1] + bshp[-1:]]
@as_op(itypes=[theano.tensor.fmatrix, theano.tensor.fmatrix],
otypes=[theano.tensor.fmatrix], infer_shape=infer_shape_numpy_dot)
def numpy_dot(a, b):
return numpy.dot(a, b)
You can try it as follows:
.. code-block:: python
x = theano.tensor.fmatrix()
y = theano.tensor.fmatrix()
f = function([x, y], numpy_dot(x, y))
inp1 = numpy.random.rand(5, 4)
inp2 = numpy.random.rand(4, 7)
out = f(inp1, inp2)
Exercise
========
Run the code of the *numpy_dot* example above.
Modify and execute to compute: numpy.add and numpy.subtract.
Modify and execute the example to return two outputs: x + y
and x - y.
Random numbers in tests
=======================
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论