Created
June 2, 2011 18:19
-
-
Save apatil/1004949 to your computer and use it in GitHub Desktop.
Attempts to produce second derivative vector (diagonal of Hessian) in Theano
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from theano import tensor as T | |
import theano | |
x = T.dvector('x') | |
z = T.dscalar('z') | |
y = T.sum(T.cos(x*z)) | |
x_test = np.random.normal(size=10) | |
grad1 = T.grad(y,x) | |
# Causes an error | |
grad2, _ = theano.scan(fn=lambda i, x, grad1, z: T.grad(grad1[i], x)[i], | |
sequences=T.arange(x.shape[0]), non_sequences=[x, grad1, z]) | |
f = theano.function([x,z], grad2) | |
print 'Right answer: %s\n computed answer: %s'%(-4*np.cos(x_test*2), f(x_test,2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't this : T.arange(x.shape[0])
be : np.arange(x_test.shape[0])
?