我正在考虑在程序中使用SciPy Optimizer tf.contrib.opt.ScipyOptimizerInterface(...)。一个示例用例将是

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.].


由于ScipyOptimizerInterfaceExternalOptimizerInterface的子级,因此我想知道在哪里处理数据。是在GPU还是CPU上?由于您必须在TensorFlow图中实现函数,因此我假设至少函数调用和渐变(如果可用)在GPU上完成,但是进行更新所需的计算又如何呢?我应该如何使用这类优化器来提高效率?在此先感谢您的帮助!

最佳答案

基于code on github,不,这只是一个包装,最终会调用scipy,因此更新位于CPU上,无法更改。

但是,您可以从他们的示例中在native implementation中找到一个tensorflow/probability

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)

09-04 07:17