Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get loss function history using tf.contrib.opt.ScipyOptimizerInterface

I need to get the loss history over time to plot it in graph. Here is my skeleton of code:

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)

With append_loss_history definition:

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

When I see the verbose output of ScipyOptimizerInterface, the loss is actually decrease over time. But when I print loss_history, the losses are nearly the same over time.

Refer to the doc: "Variables subject to optimization are updated in-place AT THE END OF OPTIMIZATION" https://www.tensorflow.org/api_docs/python/tf/contrib/opt/ScipyOptimizerInterface. Is that the reason for the being unchanged of the loss?

like image 400
Nguyễn Bảo Sinh Avatar asked Jun 21 '17 19:06

Nguyễn Bảo Sinh


1 Answers

I think you have the problem down; the variables themselves are not modified until the end of the optimization (instead being fed to session.run calls), and evaluating a "back channel" Tensor gets the un-modified variables. Instead, use the fetches argument to optimizer.minimize to piggyback on the session.run calls which have the feeds specified:

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())

(Modified from the example in the documentation). This prints Tensors with the updated values:

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]
like image 105
Allen Lavoie Avatar answered Oct 30 '22 22:10

Allen Lavoie