提交 993df98e authored 作者: Pierre Luc Carrier's avatar Pierre Luc Carrier 提交者: --global

Add breakpoint Op

上级 1bf7ea39
import pdb
import theano
import theano.tensor as T
from theano.gof import Op, Apply
class PdbBreakpoint(Op):
"""
This is an identity-like op with the side effect of enforcing a
conditional breakpoint, inside a theano function, based on a symbolic
scalar condition.
@type name: String
@param name: name of the conditional breakpoint. To be printed when the
breakpoint is activated.
:note: WARNING. At least one of the outputs of the op must be used
otherwise the op will be removed from the Theano graph
due to its outputs being unused
:note: WARNING. Employing the function inside a theano graph can prevent
Theano from applying certain optimizations to improve
performance, reduce memory consumption and/or reduce
numerical instability.
Detailed explanation:
As of 2014-12-01 the PdbBreakpoint op is not known by any
optimization. Setting a PdbBreakpoint op in the middle of a
pattern that is usually optimized out will block the optimization.
Example:
.. code-block:: python
import theano
import theano.tensor as T
from theano.tests.breakpoint import PdbBreakpoint
input = T.fvector()
target = T.fvector()
# Mean squared error between input and target
mse = (input - target) ** 2
# Conditional breakpoint to be activated if the total MSE is higher
# than 100. The breakpoint will monitor the inputs, targets as well
# as the individual error values
breakpointOp = PdbBreakpoint("MSE too high")
condition = T.gt(mse.sum(), 100)
mse, _, _ = breakpointOp(condition, mse, input, target)
# Compile the theano function
fct = theano.function([input, target], mse)
# Use the function
print fct([10, 0], [10, 5]) # Will NOT activate the breakpoint
print fct([0, 0], [10, 5]) # Will activate the breakpoint
"""
__props__ = ()
def __init__(self, name):
self.name = name
def make_node(self, condition, *monitored_vars):
# Validate that the condition is a scalar (else it is not obvious how
# is should be evaluated)
assert (condition.ndim == 0)
# Build the Apply node
inputs = [condition] + list(monitored_vars)
outputs = [inp.type.make_variable() for inp in monitored_vars]
return Apply(op=self, inputs=inputs, outputs=outputs)
def perform(self, node, inputs, output_storage):
condition = inputs[0]
monitored = inputs[1:]
if condition:
print "-------------------------------------------------"
print "Conditional breakpoint %s activated" % self.name
print "The monitored variables are stored in 'monitored'"
print "-------------------------------------------------"
pdb.set_trace()
for i in range(len(output_storage)):
output_storage[i][0] = monitored[i]
def grad(self, inputs, output_gradients):
return ([inputs[0].zeros_like().astype(theano.config.floatX)] +
output_gradients)
def infer_shape(self, inputs, input_shapes):
# Return the shape of every input but the condition
return input_shapes[1:]
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论