I'm using TensorFlow 1.14.0 with Python(3.6.8). I'm trying to use tensorflow_probability's lbfgs optimizer implementation( documentation/example ).
If I run the example code provided in the documentation it works fine. I tried to follow the same procedure for my own code which uses the tf.GradientTape()
approach for computing the objective function. When doing it that way, the gradients come back as None
type.
I'm not seeing why one is working, but the other is not.
Edit: I realized that running eager execution using the gradients wouldn't work, so I adjusted the example to be able to be run with eager execution.
Non-working example(using GradientTape) with eager execution
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
tf.enable_eager_execution()
# A high-dimensional quadratic bowl.
ndims = 3
minimum = np.ones([ndims], dtype='float64')
scales = np.arange(ndims, dtype='float64') + 1.0
# The objective function and the gradient.
def quadratic(x):
with tf.GradientTape() as g:
value = tf.reduce_sum(scales * (x - minimum) ** 2)
grads = g.gradient(value, x)
print('Gradients: ')
print(grads)
return value, grads
start = np.arange(ndims, 0, -1, dtype='float64')
optim_results = tfp.optimizer.lbfgs_minimize(quadratic, initial_position=start, num_correction_pairs=10,tolerance=1e-8)
print('results')
print(optim_results)
# Check that the search converged
assert(optim_results.converged)
# Check that the argmin is close to the actual value.
np.testing.assert_allclose(optim_results.position, minimum)
You need to watch x. For the operations inside this context manager, it's required at least one of their inputs is being watched.
with tf.GradientTape() as g:
g.watch(x)
value = tf.reduce_sum(scales * (x - minimum) ** 2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.