I’d like to run scipy implementation of BFGS optimization algorithm on GPU and scipy seems not to support GPUs. The target function which I want to run on GPU is the following one which is part of the implementation of this repository:
//here the variable initializations
opts = {'gtol': effective_gamma, 'norm': 2}
result = minimize(private_loss, x0, (x, y), method='BFGS', jac=private_gradient, options=opts,
callback=cb)
I know there is Tensorflow Probablity implementation of BFGS, but I couldn’t find out how I can convert this scipy function into Tensordlow Probablity. Any Idea how I could to run the following function on GPU with minimum code change?
Advertisement
Answer
Here are two suggestions of mine:
jax.scipy: Jax contains the implementation of scipy and also supports GPUs and TPUs. You can theoretically install it, convert your numpy variables to jax.numpy and call jax.scipy.optimize.minimize(params):
JavaScript114141import jax.numpy as jnp
2from jax.scipy.optimize import minimize
3
4// here x0,x,y initialization and private_loss and private_gradient functions definition
5
6x0 = jax.numpy.asarray(x0)
7x = jax.numpy.asarray(x)
8y = jax.numpy.asarray(y)
9
10opts = {'gtol': effective_gamma, 'norm': 2}
11result = minimize(private_loss, x0, (x, y), method='BFGS', jac=private_gradient, options=opts, callback=cb)
12
13// here rest of the code
14
Dont foget to also take care of converting the variables which will be used in private_loss and private_gradient function to jax.numpy.
Tensorflow Probability: As you already mentioned, you can also use bfgs_minimize implementation by tensorflow. based on the colab here, your code will be something like this:
JavaScript130301def make_val_and_grad_fn(value_fn):
2@functools.wraps(value_fn)
3def val_and_grad(param):
4return tfp.math.value_and_gradient(value_fn, param)
5return val_and_grad
6
7def run(optimizer):
8result2 = optimizer()
9return np_value(result2)
10
11@make_val_and_grad_fn
12def private_loss(param):
13// here private_loss
14
15x_var = tf.Variable(x)
16y_var = tf.Variable(y, dtype = tf.float64)
17x0 = tf.Variable(x0)
18
19tolerance = effective_gamma
20
21@tf.function
22def minimize_with_bfgs():
23result1 = tfp.optimizer.bfgs_minimize(
24private_loss,
25initial_position=tf.constant(x0),
26tolerance=tolerance)
27return result1
28
29results = run(minimize_with_bfgs)
30