Python 在GPU上执行外部优化器
我正在考虑在我的程序中使用SciPy优化器。下面是一个示例用例Python 在GPU上执行外部优化器,python,tensorflow,gpu,Python,Tensorflow,Gpu,我正在考虑在我的程序中使用SciPy优化器。下面是一个示例用例 vector = tf.Variable([7., 7.], 'vector') # Make vector norm as small as possible. loss = tf.reduce_sum(tf.square(vector)) optimizer = ScipyOptimizerInterface(loss, options={'maxiter': 100}) with tf.Session() as sess
vector = tf.Variable([7., 7.], 'vector')
# Make vector norm as small as possible.
loss = tf.reduce_sum(tf.square(vector))
optimizer = ScipyOptimizerInterface(loss, options={'maxiter': 100})
with tf.Session() as session:
optimizer.minimize(session)
# The value of vector should now be [0., 0.].
由于ScipyOptimizerInterface
是ExternalOptimizerInterface
的子级,我想知道数据处理在哪里完成。它是在GPU上还是在CPU上?由于您必须在TensorFlow图中实现该函数,我假设至少函数调用和梯度是在GPU上完成的(如果可用),但是更新所需的计算又如何呢?我应该如何使用这些类型的优化器来提高效率?提前感谢您的帮助 根据,不,这只是一个最终调用scipy
的包装器,因此更新在CPU上,无法更改
但是,您可以从他们的示例中找到:
minimum = np.array([1.0, 1.0]) # The center of the quadratic bowl.
scales = np.array([2.0, 3.0]) # The scales along the two axes.
# 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 = tf.constant([0.6, 0.8]) # Starting point for the search.
optim_results = tfp.optimizer.bfgs_minimize(
quadratic, initial_position=start, 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)
# Print out the total number of function evaluations it took. Should be 6.
print ("Function evaluations: %d" % results.num_objective_evaluations)
BFGS优化器只是scipy优化器中实现的众多优化器之一。但这仍然是我一直在寻找的答案。谢谢