Created
September 26, 2018 23:04
-
-
Save ahwillia/2e4404321d9be3628d0cf9fa868c10db to your computer and use it in GitHub Desktop.
Computing hessian-vector products in tensorflow
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
""" | |
Computing hessian-vector products in tensorflow. | |
For simplicity, we demonstrate the idea on a Poisson regression model. | |
""" | |
import tensorflow as tf | |
import numpy as np | |
from scipy.optimize import minimize | |
n = 1000 # number of datapoints | |
p = 5 # number of features | |
# Create data. | |
# `X` is a matrix of independent/predictor variables. | |
# `true_b` are the ground-truth log inputs to the Poisson distribution. | |
# `y` is are the dependent/predicted variables (count data). | |
X = .3 * np.random.randn(n, p) | |
true_b = np.random.randn(p,) | |
y = np.random.poisson(np.exp(np.dot(X, true_b))).astype(float) | |
# Create placeholder for model coefficients. | |
b = tf.placeholder(shape=(p,), dtype=tf.float64) | |
# Compute loss and gradient. | |
Xb = tf.squeeze(tf.matmul(tf.constant(X), b[:, None])) | |
loss = tf.reduce_mean(tf.nn.log_poisson_loss(tf.constant(y), Xb)) | |
grad = tf.gradients(loss, b) | |
# Create new placeholder for vector multiplied with Hessian. | |
z = tf.placeholder(shape=(p,), dtype=tf.float64) | |
hess_vec = tf.gradients(tf.reduce_sum(grad * z), b) | |
# Loss function at estimate b_ | |
def f(b_): | |
return sess.run(loss, feed_dict={b: b_}) | |
# Gradient at estimate b_ | |
def g(b_): | |
return sess.run(grad, feed_dict={b: b_})[0] | |
# Hessian vector product at estimate b_ | |
def hp(b_, z_): | |
return sess.run(hess_vec, feed_dict={b: b_, z: z_})[0] | |
# Optimize | |
sess = tf.Session() | |
result = minimize(f, np.zeros(p), jac=g, hessp=hp, method='newton-cg') | |
print('True regression coeffs: {}'.format(true_b)) | |
print('Estimated regression coeffs: {}'.format(result.x)) |
Author
ahwillia
commented
Sep 26, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment