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.

Elementwise Hessian

See original GitHub issue

I’m super impressed with the progress you’ve been making! Autograd has the potential to save me and many others a lot of grief in terms of automatically differentiating scalar-valued functions while retaining broadcasting support. So right now I can do something like

from autograd import elementwise_grad
import numpy as np
def wrapper(args):
    return energy(*args)
grad = elementwise_grad(wrapper)
inp = [1000*np.random.rand(10,1), np.random.rand(1,10), np.random.rand(1,10), np.random.rand(1,10), np.random.rand(1,10), np.random.rand(1,10)]
inp = np.broadcast_arrays(*inp)
%time gres = grad(inp)
print([i.shape for i in gres])

CPU times: user 301 ms, sys: 18 ms, total: 319 ms
Wall time: 300 ms
[(10, 10), (10, 10), (10, 10), (10, 10), (10, 10), (10, 10)]

And so each element of the returned list is the gradient of the objective function, with respect to the variable at that argument index, for all 100 combinations of input variables. So what I want now is the Hessian of my objective function, elementwise, meaning if my gradient is a (6, 10, 10) array, I want back a (6, 6, 10, 10) array with all the second derivatives including cross terms. Is this possible to do with Autograd?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
mattjjcommented, Nov 5, 2015

I think it works. Here’s a cleaner elementwise_hess function for you, as well as a better test. (Of course, don’t trust me, please test it yourself!)

from autograd import elementwise_grad
import autograd.numpy as np

def energy(args):
    return 8.314*args[0]*(args[1]*np.log(args[1]) + args[2]*np.log(args[2]))

gradfun = elementwise_grad(energy)
N = 100
M = 50

inp = [1000*np.random.rand(N,1), np.random.rand(1,M), np.random.rand(1,M)]
inp = np.broadcast_arrays(*inp)


gres = gradfun(inp)
print([i.shape for i in gres])

### here's the new stuff!!

from autograd import jacobian

def elementwise_hess(fun, argnum=0):
    sum_latter_dims = lambda x: np.sum(x.reshape(x.shape[0], -1), 1)
    def sum_grad_output(*args, **kwargs):
        return sum_latter_dims(elementwise_grad(fun)(*args, **kwargs))
    return jacobian(sum_grad_output, argnum)

# make inp an array so that jacobian() can handle it
array_inp = np.concatenate([arr[None,...] for arr in inp])

# check shapes on the given energy function
hessfun = elementwise_hess(energy)
print(hessfun(array_inp).shape)

# test on some scalar elements
from autograd import grad
hessval = hessfun(array_inp)
print grad(lambda x: grad(energy)(x)[1])(array_inp[:,8,42])
print hessval[:,1,8,42]

It prints something like this (different every time due to the pseudorandom numbers generated):

[(100, 50), (100, 50), (100, 50)]
(3, 3, 100, 50)
[   7.10510658  920.2143044     0.        ]
[   7.10510658  920.2143044     0.        ]

Please reopen the issue if it’s not right!

0reactions
richardotiscommented, Nov 5, 2015

Actually the above turned out not to be entirely true. There’s still a broadcasting hangup but it’s pretty easily solved. I’m working on a code writeup which I’ll share after I’ve kicked the tires a bit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Online Newton Step Based on Pseudo-Inverse and ...
algorithms. Keywords Online learning · Elementwise multiplication · Pseudo inverse ·. Hessian matrix · Online convex optimization. 1 Introduction.
Read more >
Modified online Newton step based on element wise ... - arXiv
The large data is a challenge in Newton method to store second order matrices as hessian. In this paper, we have proposed an...
Read more >
Gradient and Hessian of - ( - 1 - 2 - x - T - Math Stack Exchange
This includes the Kronecker, Hadamard/elementwise, Frobenius/trace and dyadic/tensor products, as well as the Matrix/dot product. IFF the ...
Read more >
Modified online Newton step based on elementwise ...
The large data are a challenge in the Newton method to store second-order matrices such as the hessian. In this article, we have...
Read more >
Matrix Reference Manual: Matrix Calculus
... Differentials of Inverses, Trace and Determinant; Hessian matrices ... A • B the Hadamard or elementwise product; matrices and vectors A, B, ......
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