代码之家  ›  专栏  ›  技术社区  ›  Vincent Stimper

在GPU上执行外部优化器

  •  1
  • Vincent Stimper  · 技术社区  · 7 年前

    我正在考虑使用SciPy优化器 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上完成的(如果可用),但是更新所需的计算又如何呢?我应该如何使用这些类型的优化器来提高效率?提前感谢您的帮助!

    1 回复  |  直到 7 年前
        1
  •  1
  •   BlackBear    7 年前

    基于 code on github ,不,这只是一个确实调用 scipy

    但是,您可以找到 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)
    
    推荐文章