我们如何在 TensorFlow 2.0 中使用 lbfgs_minimize
How can we use lbfgs_minimize in TensorFlow 2.0
我无法从安装了 tensorflow 2.0 的 tensorflow 重现 this example。
这是原始片段:
# A high-dimensional quadratic bowl.
ndims = 60
minimum = np.ones([ndims], dtype='float64')
scales = np.arange(ndims, dtype='float64') + 1.0
# The objective function and the gradient.
def quadratic(x):
value = tf.reduce_sum(scales * (x - minimum) ** 2)
return value, tf.gradients(value, x)[0]
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)
with tf.Session() as session:
results = session.run(optim_results)
# Check that the search converged
assert(results.converged)
# Check that the argmin is close to the actual value.
np.testing.assert_allclose(results.position, minimum)
以下错误不起作用:
RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.
如果我更改代码并改用渐变胶带,如下所示:
def quadratic(x):
x = tf.Variable(x, dtype='float64')
with tf.GradientTape() as t:
value = tf.reduce_sum(scales * (x - minimum) ** 2)
grad = t.gradient(value, x)
return value, grad
我也收到以下错误:
TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.
总的来说,我尝试过的任何东西都不起作用,而且我不知道如何在 tensorflow 2.0 中使用 lbfgs。
使用tf.function
in your objective function so it is executed as a graph, then you will be able to use tf.gradients
:
import tensorflow as tf
import tensorflow_probability as tfp
import numpy as np
# A high-dimensional quadratic bowl.
ndims = 60
minimum = tf.ones([ndims], dtype='float64')
scales = tf.range(ndims, dtype='float64') + 1.0
# The objective function and the gradient.
@tf.function
def quadratic(x):
value = tf.reduce_sum(scales * (x - minimum) ** 2)
return value, tf.gradients(value, x)[0]
start = tf.range(ndims, 0, -1, dtype='float64')
optim_results = tfp.optimizer.lbfgs_minimize(
quadratic, initial_position=start, num_correction_pairs=10,
tolerance=1e-8)
# Check that the search converged
print(optim_results.converged.numpy())
# True
# Check that the argmin is close to the actual value.
print(np.allclose(optim_results.position.numpy(), minimum.numpy()))
# True
只是为@jdehesa 的答案添加一点 - 在这种情况下使用 tfp.math.value_and_gradient 也很有用,如果您使用急切模式,它将为您创建渐变带。例如:
import tensorflow as tf
import tensorflow_probability as tfp
ndims = 60
minimum = tf.ones([ndims], dtype="float64")
scales = tf.range(ndims, dtype="float64") + 1.0
def quadratic(x):
value = tf.reduce_sum(scales * (x - minimum) ** 2)
return value
start = tf.range(ndims, 0, -1, dtype="float64")
optim_results = tfp.optimizer.lbfgs_minimize(
lambda x: tfp.math.value_and_gradient(quadratic, x),
initial_position=start,
num_correction_pairs=10,
tolerance=1e-8,
)
我无法从安装了 tensorflow 2.0 的 tensorflow 重现 this example。
这是原始片段:
# A high-dimensional quadratic bowl.
ndims = 60
minimum = np.ones([ndims], dtype='float64')
scales = np.arange(ndims, dtype='float64') + 1.0
# The objective function and the gradient.
def quadratic(x):
value = tf.reduce_sum(scales * (x - minimum) ** 2)
return value, tf.gradients(value, x)[0]
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)
with tf.Session() as session:
results = session.run(optim_results)
# Check that the search converged
assert(results.converged)
# Check that the argmin is close to the actual value.
np.testing.assert_allclose(results.position, minimum)
以下错误不起作用:
RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.
如果我更改代码并改用渐变胶带,如下所示:
def quadratic(x):
x = tf.Variable(x, dtype='float64')
with tf.GradientTape() as t:
value = tf.reduce_sum(scales * (x - minimum) ** 2)
grad = t.gradient(value, x)
return value, grad
我也收到以下错误:
TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.
总的来说,我尝试过的任何东西都不起作用,而且我不知道如何在 tensorflow 2.0 中使用 lbfgs。
使用tf.function
in your objective function so it is executed as a graph, then you will be able to use tf.gradients
:
import tensorflow as tf
import tensorflow_probability as tfp
import numpy as np
# A high-dimensional quadratic bowl.
ndims = 60
minimum = tf.ones([ndims], dtype='float64')
scales = tf.range(ndims, dtype='float64') + 1.0
# The objective function and the gradient.
@tf.function
def quadratic(x):
value = tf.reduce_sum(scales * (x - minimum) ** 2)
return value, tf.gradients(value, x)[0]
start = tf.range(ndims, 0, -1, dtype='float64')
optim_results = tfp.optimizer.lbfgs_minimize(
quadratic, initial_position=start, num_correction_pairs=10,
tolerance=1e-8)
# Check that the search converged
print(optim_results.converged.numpy())
# True
# Check that the argmin is close to the actual value.
print(np.allclose(optim_results.position.numpy(), minimum.numpy()))
# True
只是为@jdehesa 的答案添加一点 - 在这种情况下使用 tfp.math.value_and_gradient 也很有用,如果您使用急切模式,它将为您创建渐变带。例如:
import tensorflow as tf
import tensorflow_probability as tfp
ndims = 60
minimum = tf.ones([ndims], dtype="float64")
scales = tf.range(ndims, dtype="float64") + 1.0
def quadratic(x):
value = tf.reduce_sum(scales * (x - minimum) ** 2)
return value
start = tf.range(ndims, 0, -1, dtype="float64")
optim_results = tfp.optimizer.lbfgs_minimize(
lambda x: tfp.math.value_and_gradient(quadratic, x),
initial_position=start,
num_correction_pairs=10,
tolerance=1e-8,
)