我正在考虑在程序中使用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.].
由于ScipyOptimizerInterface
是ExternalOptimizerInterface
的子代,所以我想知道在哪里处理数据。是在GPU还是CPU上?由于您必须在TensorFlow图表中实现函数,因此我假设至少函数调用和渐变在GPU上完成(如果可用),但是进行更新所需的计算又如何呢?我应该如何使用这类优化器来提高效率?预先感谢您的帮助!
答案 0 :(得分:1)
基于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)