*Hello, I am trying to create a custom operation that would round a tensor
variable to n-bits. (e.g. f(0.5238472) returns .5 for 2bit rounding)
I was able to do this but I want to define the gradient of this operation to be
the same as the un-rounded expression.
Below is the code and test that I performed. I get an attributeError when I try
to compute dy = T.grad(y,x) (please see the full error below).
*
import theano
import theano.tensor as T
import numpy
#theano.config.exception_verbosity = 'high'
class Quantize(theano.Op):
# Properties attribute
__props__ = ()
#itypes and otypes attributes are
#compulsory if make_node method is not defined.
#They're the type of input and output respectively
# itypes = [theano.tensor.scalar, theano.tensor.iscalar]
# otypes = [theano.tensor.scalar]
#Compulsory if itypes and otypes are not defined
def make_node(self, x, n):
x = T.as_tensor_variable(x)
return theano.Apply(self, [x, n], [x.type.make_variable()])
# Python implementation:
def perform(self, node, inputs_storage, output_storage):
x, n = inputs_storage
x_quantized, = output_storage
x_quantized[0] = numpy.round(x*2**n)/2**n
def grad(self, inputs, g):
return [g, 0]
def infer_shape(self, node, input_shapes):
return [input_shapes[0]]
if __name__ == "__main__":
x = T.dscalar()
n = T.iscalar()
y = Quantize()(x, n)
f = theano.function([x, n], y)
a = numpy.random.rand()
print a
print f(a, 4)
dy = T.grad(y, x)
df = theano.function([x], dy)
print df(a)
error below
0.0151228405317
0.0
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition
2016.1.4\helpers\pydev\pydevd.py", line 1531, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition
2016.1.4\helpers\pydev\pydevd.py", line 938, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:/Users/Coor3427_5JYQJB2/Dropbox
(ASU)/MachineLearning/Pseudo-Ensembles/quantize.py", line 49, in <module>
dy = T.grad(y, x)
File "C:\Anaconda2\lib\site-packages\theano\gradient.py", line 561, in grad
grad_dict, wrt, cost_name)
File "C:\Anaconda2\lib\site-packages\theano\gradient.py", line 1324, in
_populate_grad_dict
rval = [access_grad_cache(elem) for elem in wrt]
File "C:\Anaconda2\lib\site-packages\theano\gradient.py", line 1279, in
access_grad_cache
term = access_term_cache(node)[idx]
File "C:\Anaconda2\lib\site-packages\theano\gradient.py", line 1190, in
access_term_cache
if not isinstance(term.type,
AttributeError: 'list' object has no attribute 'type'
Thank you in advance,
Kan Kawabata
--
---
You received this message because you are subscribed to the Google Groups
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.