ScipyOptimizerInterface

ScipyOptimizerInterface

我需要获取一段时间内的损失历史记录,然后将其绘制在图中。
这是我的代码框架:

optimizer = tf.contrib.opt.ScipyOptimizerInterface(loss, method='L-BFGS-B',
options={'maxiter': args.max_iterations, 'disp': print_iterations})
optimizer.minimize(sess, loss_callback=append_loss_history)


使用append_loss_history定义:

def append_loss_history(**kwargs):
    global step
    if step % 50 == 0:
        loss_history.append(loss.eval())
    step += 1


当我看到ScipyOptimizerInterface的详细输出时,损耗实际上会随着时间而减少。
但是,当我打印loss_history时,随着时间的流逝损失几乎是相同的。

参考文档:
“在优化结束时,将对要优化的变量进行就地更新”
https://www.tensorflow.org/api_docs/python/tf/contrib/opt/ScipyOptimizerInterface。这是亏损不变的原因吗?

最佳答案

我认为你有问题了。变量本身直到优化结束(而不是being fed to session.run calls)才被修改,并且评估“反向通道” Tensor会得到未修改的变量。而是使用fetchesoptimizer.minimize参数在指定了feed的session.run调用上搭载:

import tensorflow as tf

def print_loss(loss_evaled, vector_evaled):
  print(loss_evaled, vector_evaled)

vector = tf.Variable([7., 7.], 'vector')
loss = tf.reduce_sum(tf.square(vector))

optimizer = tf.contrib.opt.ScipyOptimizerInterface(
    loss, method='L-BFGS-B',
    options={'maxiter': 100})

with tf.Session() as session:
  tf.global_variables_initializer().run()
  optimizer.minimize(session,
                     loss_callback=print_loss,
                     fetches=[loss, vector])
  print(vector.eval())


(从example in the documentation修改)。这会打印具有更新值的张量:

98.0 [ 7.  7.]
79.201 [ 6.29289341  6.29289341]
7.14396e-12 [ -1.88996808e-06  -1.88996808e-06]
[ -1.88996808e-06  -1.88996808e-06]

08-24 20:56