Skip to content

Instantly share code, notes, and snippets.

@benanne
Created February 25, 2014 16:12
Show Gist options
  • Save benanne/9212037 to your computer and use it in GitHub Desktop.
Save benanne/9212037 to your computer and use it in GitHub Desktop.
Theano consider constant op
import theano
import theano.tensor as T
from theano.tensor.opt import register_canonicalize
class ConsiderConstant(theano.compile.ViewOp):
def grad(self, args, g_outs):
return [T.zeros_like(g_out) for g_out in g_outs]
consider_constant = ConsiderConstant()
register_canonicalize(theano.gof.OpRemove(consider_constant), name='remove_consider_constant')
# TODO: implement w.r.t.?
@benanne
Copy link
Author

benanne commented Feb 25, 2014

Example:

g = T.grad((x * consider_constant(x)).sum(), x)
=> gradient is x, not 2*x, as the second x is treated as a constant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment