google / tangent

Source-to-Source Debuggable Derivatives in Pure Python
Apache License 2.0
2.31k stars 434 forks source link

Maybe a bug #31

Closed jkitchin closed 6 years ago

jkitchin commented 6 years ago

This seems like a possible bug in tangent.

import numpy as np

def f(x):
    """Sum the upper triangle of the array x."""
    sum = 0
    rows, cols = x.shape
    for i in np.arange(rows):
        for j in np.arange(i, cols):
            sum = sum + x[i, j]
    return sum

df = tangent.grad(f)

This raises:


ValueErrorTraceback (most recent call last)
<ipython-input-40-2b69c132cb2e> in <module>()
     10     return sum
     11 
---> 12 df = tangent.grad(f)

/usr/local/google/home/kitchin/.local/lib/python2.7/site-packages/tangent/grad_util.pyc in grad(func, wrt, optimized, motion, mode, preserve_result, verbose)
    176 
    177   # Take the gradient
--> 178   node, namespace = grad_tree(func, wrt, motion, mode, preserve_result, verbose)
    179 
    180   if mode == 'reverse' and motion == 'joint':

/usr/local/google/home/kitchin/.local/lib/python2.7/site-packages/tangent/grad_util.pyc in grad_tree(func, wrt, motion, mode, preserve_result, verbose)
     97   namespace.update(six.get_function_globals(func))
     98 
---> 99   node, required = grad_ast(func, wrt, motion, mode, preserve_result, verbose)
    100   final.body.extend(node.body)
    101 

/usr/local/google/home/kitchin/.local/lib/python2.7/site-packages/tangent/grad_util.pyc in grad_ast(func, wrt, motion, mode, preserve_result, verbose)
     53   if mode == 'reverse':
     54     node, required, stack = reverse_ad.reverse_ad(node.body[0], wrt,
---> 55                                                   preserve_result)
     56     if verbose >= 2:
     57       print('RAW')

/usr/local/google/home/kitchin/.local/lib/python2.7/site-packages/tangent/reverse_ad.pyc in reverse_ad(node, wrt, preserve_result)
    831 
    832   ad = ReverseAD(wrt, preserve_result)
--> 833   pri, adj = ad.visit(node)
    834   mod = gast.Module(body=[pri, adj])
    835   mod = annotate.find_stacks(mod)

/usr/local/google/home/kitchin/.local/lib/python2.7/site-packages/tangent/reverse_ad.pyc in visit(self, node)
    146     if anno.hasanno(node, 'active_in'):
    147       self.active_variables = anno.getanno(node, 'active_in')
--> 148     pri, adj = visitor(node)
    149 
    150     # Annotate primal and adjoint statements

/usr/local/google/home/kitchin/.local/lib/python2.7/site-packages/tangent/reverse_ad.pyc in visit_FunctionDef(self, node)
    209 
    210     # Perform AD on the function body
--> 211     body, adjoint_body = self.visit_statements(node.body[:-1])
    212 
    213     # Annotate the first statement of the primal and adjoint as such

/usr/local/google/home/kitchin/.local/lib/python2.7/site-packages/tangent/reverse_ad.pyc in visit_statements(self, nodes)
    270     primals, adjoints = [], collections.deque()
    271     for node in nodes:
--> 272       primal, adjoint = self.visit(node)
    273       if not isinstance(primal, list):
    274         primal = [primal]

/usr/local/google/home/kitchin/.local/lib/python2.7/site-packages/tangent/reverse_ad.pyc in visit(self, node)
    146     if anno.hasanno(node, 'active_in'):
    147       self.active_variables = anno.getanno(node, 'active_in')
--> 148     pri, adj = visitor(node)
    149 
    150     # Annotate primal and adjoint statements

/usr/local/google/home/kitchin/.local/lib/python2.7/site-packages/tangent/reverse_ad.pyc in visit_Assign(self, node)
    487       context = [t.id if hasattr(t, 'id') else t for t in node.targets]
    488       raise ValueError(
--> 489           'Failed to process assignment to: %s. Error: %s' % (context, e))
    490     if not isinstance(adjoint_rhs, list):
    491       adjoint_rhs = [adjoint_rhs]

ValueError: Failed to process assignment to: ['t']. Error: Unknown node type: Attribute
mdanatg commented 6 years ago

Indeed, this is a known issue - we don't support attributes or member functions yet. The workaround is to use np.shape(x) instead of x.shape.

We'll add a friendlier error message shortly.

mdanatg commented 6 years ago

Duplicate of #18