Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AI and Deep Learning by (50.2k points)

I need to get the loss history over time to plot it in the 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 decreases 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?

1 Answer

0 votes
by (108k points)

The variables are not modified until the end of the optimization (instead of being fed to session.run calls) and evaluating a "back channel" Tensor gets the un-modified variables. Alternatively, 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())

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]

Browse Categories

...