Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'ArrayBox' object has no attribute 'exp' (very simple code example) #436

Closed
Duncanswilson opened this issue Oct 12, 2018 · 2 comments

Comments

@Duncanswilson
Copy link

I'm having trouble getting anything to work the way I'm defining my functions, it's probably user error, but my example is terse.

I'm just trying to define a simple neural network and keep running into issues using np.tanh() or np.exp() and the only similar errors I could find are from having nested data structures, which I don't have.

Here's my full code snippet:

import autograd.numpy as np
from autograd import grad

action_dim = 2
feature_dim = 6
layer_dim = 4 
w1 = np.random.rand(feature_dim, layer_dim)
b1 = np.random.rand(layer_dim)
w2 = np.random.rand(layer_dim, action_dim)
b2 = np.random.rand(action_dim)

def continuous_policy_function(w1, b1, w2, b2):
    x = observation.dot(w1) + b1
    x = 1.0 / (1.0 + np.exp(-x))  # sigmoid "squashing" function to interval [0,1]
    out = x.dot(w2) + b2
    return out 

grad_function = grad(continuous_policy_function)
observation = np.random.rand(feature_dim)
action = continuous_policy_function(w1, b1, w2, b2)
print(grad_function(w1, b1, w2, b2))

and the full trace:

Traceback (most recent call last):
  File "annoying_autograd.py", line 55, in <module>
    print(grad_function(w1, b1, w2, b2))
  File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/autograd/wrap_util.py", line 20, in nary_f
    return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
  File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/autograd/differential_operators.py", line 24, in grad
    vjp, ans = _make_vjp(fun, x)
  File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/autograd/core.py", line 10, in make_vjp
    end_value, end_node =  trace(start_node, fun, x)
  File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/autograd/tracer.py", line 10, in trace
    end_box = fun(start_box)
  File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/autograd/wrap_util.py", line 15, in unary_f
    return fun(*subargs, **kwargs)
  File "annoying_autograd.py", line 29, in continuous_policy_function
    x = 1.0 / (1.0 + np.exp(-x))  # sigmoid "squashing" function to interval [0,1]
  File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/autograd/tracer.py", line 48, in f_wrapped
    return f_raw(*args, **kwargs)
AttributeError: 'ArrayBox' object has no attribute 'exp'

I'm using Python 3.6.4 and and autograd 1.2 which I pulled from pip this morning.

Please let me know what I'm doing wrong here! :)

@Duncanswilson
Copy link
Author

Duncanswilson commented Oct 12, 2018

So I managed to fix this on my end but with little explanation of what I was doing wrong.

One thing that is clear is that I was using the wrong grad function, but I also had to replace all uses of dot with the @ operator. note: (this also works if you replace calls like observation.dot(w1) with np.dot(observation, w1 )

... ¯_(ツ)_/¯ I'd love to know if there is something deeper going on here!

import autograd.numpy as np
from autograd import elementwise_grad as egrad  # for functions that vectorize over inputs

action_dim = 2
feature_dim = 6
layer_dim = 4 
w1 = np.random.rand(feature_dim, layer_dim)
b1 = np.random.rand(layer_dim)
w2 = np.random.rand(layer_dim, action_dim)
b2 = np.random.rand(action_dim)

def continuous_policy_function(w1, b1, w2, b2):
    x = observation @ w1 + b1
    x = 1.0 / (1.0 + np.exp(-x))  # sigmoid "squashing" function to interval [0,1]
    out = x @ w2 + b2
    return out 

grad_function = egrad(continuous_policy_function)
observation = np.random.rand(feature_dim)
action = continuous_policy_function(w1, b1, w2, b2)
print(grad_function(w1, b1, w2, b2))

@j-towns
Copy link
Collaborator

j-towns commented Oct 12, 2018

Hey there, have a read of the tutorial, there's more detail in there, including

we don't support the syntax A.dot(B); use the equivalent np.dot(A, B) instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants