problem with elementwise multiplication

36 views
Skip to first unread message

Lewis Fishgold

unread,
Mar 6, 2013, 12:55:41 PM3/6/13
to alg...@googlegroups.com
Hi,

I'm having trouble with a small modification of the getting started script, and would be grateful for any help. I'm using algopy 0.3.2, numpy 1.5.1, and python 2.7.2. 

I'm trying to compute the gradient of f(x) = 2*x_1 + 2*x_2 which should be [2, 2] when x=[3,5].
Here's the code I'm running:

import numpy, algopy
from algopy import UTPM, exp

def eval_f(x):
    y = numpy.array([2,2])
    return algopy.sum(x * y)

cg = algopy.CGraph()
x = algopy.Function(numpy.array([1,2]))
y = eval_f(x)
cg.trace_off()
cg.independentFunctionList = [x]
cg.dependentFunctionList = [y]

print 'gradient =', cg.gradient(numpy.array([3.0,5.0]))

Unexpectedly, the output is:

gradient =
Traceback (most recent call last):
  File "/home/lewfish/git/machine-learning/machine-learning/src/scratch.py", line 17, in <module>
    print 'gradient =', cg.gradient(numpy.array([3.0,5.0]))
  File "/usr/local/lib/python2.7/dist-packages/algopy/tracer/tracer.py", line 252, in gradient
    self.pullback([ybar])
  File "/usr/local/lib/python2.7/dist-packages/algopy/tracer/tracer.py", line 138, in pullback
    f.xbar_from_x()
  File "/usr/local/lib/python2.7/dist-packages/algopy/tracer/tracer.py", line 943, in xbar_from_x
    self.xbar = self.func(*args)
  File "/usr/local/lib/python2.7/dist-packages/algopy/utpm/utpm.py", line 287, in __mul__
    x_data, y_data = UTPM._broadcast_arrays(self.data, rhs.data)
AttributeError: 'NoneType' object has no attribute 'data'

Strangely, when I modify the line 
return algopy.sum(x * y) 
to be
return algopy.sum(x + y)  (i replaced the * with a +)
it returns the correct answer. I also get the same problem when dividing by y.

I'm not sure if this is a bug, or reflects a misuse of algopy.

Thanks,
Lewis




Lewis Fishgold

unread,
Mar 6, 2013, 1:03:58 PM3/6/13
to alg...@googlegroups.com
I also tried changing 

y = numpy.array([2,2])
to be 
y = algopy.Function((numpy.array([2,2])))
and also tried
y = algopy.UTPM((numpy.array([2,2])))

and neither helped. Which reminds me of another question. For matrices that you are *not* taking the gradient with respect to, should they be instances of UTPM, Function or ordinary numpy arrays?

Thanks, Lewis 

Sebastian Walter

unread,
Mar 6, 2013, 1:11:09 PM3/6/13
to alg...@googlegroups.com
Hello Lewis,

Your script seems to work here.
Can you try the bleeding edge version from github?

Install with

pip install -e git+https://github.com/b45ch1/algopy#egg=algopy

or manually

cd /your/path/
git clone https://github.com/b45ch1/algopy.git


On another note:

y = numpy.array([2,2])

is an integer array, but it should be a float array

y = numpy.array([2.,2.])


Sebastian
> --
> You received this message because you are subscribed to the Google Groups
> "algopy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to algopy+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Lewis Fishgold

unread,
Mar 6, 2013, 1:20:52 PM3/6/13
to alg...@googlegroups.com
Thanks for the quick response. I just installed using pip and now I get a different error message. Which is strange because logit is defined inside special.py.

Traceback (most recent call last):
  File "/home/lewfish/git/machine-learning/machine-learning/src/scratch.py", line 1, in <module>
    import numpy, algopy
  File "/home/lewfish/src/algopy/algopy/__init__.py", line 74, in <module>
    import special
  File "/home/lewfish/src/algopy/algopy/special/__init__.py", line 1, in <module>
    from special import *
  File "/home/lewfish/src/algopy/algopy/special/special.py", line 350, in <module>
    logit.__doc__ += scipy.special.logit.__doc__
AttributeError: 'module' object has no attribute 'logit'

Lewis Fishgold

unread,
Mar 6, 2013, 1:23:09 PM3/6/13
to alg...@googlegroups.com
Ah, I think I need a more recent version of scipy which has logit in scipy.special. Let me try that and get back to you.

Lewis Fishgold

unread,
Mar 7, 2013, 12:44:34 PM3/7/13
to alg...@googlegroups.com
After installing scipy 0.12 and the bleeding edge version of algopy, the problem is fixed. 

Thanks, 
Lewis

Sebastian Walter

unread,
Mar 7, 2013, 2:10:29 PM3/7/13
to alg...@googlegroups.com
glad to hear :)
Reply all
Reply to author
Forward
0 new messages