question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError: 'ArrayBox' object has no attribute 'exp'

See original GitHub issue

Hi everyone,

I’m implementing an alpha-divergence minimisation algorithm with a pretty simple loss function that I need to differentiate. The problem, it seems, is that the function contains exp and log, which should be totally fine.

Moreover, if I isolate the exponential or the logarithm function I can compute its gradient normally, so I guess the problem may be in the context of my loss function.

The code looks like this:

def wake_sleep(inputs, inputs_f, S, theta, alpha):
    nn_params = [1e-1, [1, 10, 10, 1]]
    z = [init_random_params(nn_params) for _ in range(S)]
    init_theta = theta

    def bb_alpha_energy(theta, t):
        print(theta)
        m_a, s_a, sigma = theta
        a = np.random.normal(size=(S, S)) * s_a + m_a
        m_qgp = 1. / S * np.sum([g_nn(inputs, z_i) for z_i in z], axis=0)

        mc_q = []
        for a_k in a:
            m_k = []
            for z_s, a_s in zip(z, a_k):
                m_k.append((g_nn(inputs_f, z_s) - m_qgp) * a_s)
            m = (1. / np.sqrt(S - 1) * np.sum(m_k, axis = 0))
            mc_q.append(gaussian_pdf(inputs_f, m, sigma)**alpha)
        mc_q = np.mean(mc_q, axis = 0)

        alpha_energy = 1. / alpha * np.sum(np.log(mc_q)) - kl(m_a, s_a, 0., 1.)
        return alpha_energy

    grad_loss = grad(bb_alpha_energy)
    opt_theta = adam(grad_loss, init_theta, step_size=0.01, num_iters=500)
    return opt_theta

The failure occurs when the tracer goes through mc_q.append(gaussian_pdf(inputs_f, m, sigma)**alpha).

May it be because I’m mixing vector-output functions (such as gaussian_pdf) with my scalar-output function? How should I go about this?

Thank you, Alex

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
j-townscommented, Jul 5, 2018

Are you using import numpy as np? If so change to import autograd.numpy as np.

1reaction
jennyJRwongcommented, Oct 31, 2019

我也遇到了类似的问题,使用了几行简单得多的代码。下列:

def function(self, variable=None): result = np.exp(variable) return result 生成错误消息:

result = np.exp(variable)

AttributeError:“ ArrayBox”对象没有属性“ exp”

但是,只需将对np.exp的调用替换为

def function(self, variable=None): from math import e result = e**(variable) return result 运行良好。

how about the log() the same issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Examples of the supported features in Autograd
... f9(a): # Assume a is an array with 2 elements 2 b = np.array([1.0,2.0]) ----> 3 return a.dot(b) AttributeError: 'ArrayBox' object has...
Read more >
Numpy AttributeError: 'float' object has no attribute 'exp'
Probably there's something wrong with the input values for X and/or T. The function from the question works ok: import numpy as np...
Read more >
AttributeError: 'ArrayBox' object has no attribute 'exp' -
Hi everyone,. I'm implementing an alpha-divergence minimisation algorithm with a pretty simple loss function that I need to differentiate.
Read more >
Autograd ArrayBox type intermediate output from optimizer
Hi not sure if this has been addressed before but when I try to run qml.GradientDescentOptimizer on a cost function with intermediate costs, ......
Read more >
How to Fix: TypeError: 'numpy.float64' object is not callable
How to Fix: TypeError: 'numpy.float64' object is not callable ... How to Fix: 'numpy.ndarray' object has no attribute 'append'
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found