2

I am trying to understand this line of code from some tensorflow code

 _, l = session.run([optimizer, loss], feed_dict=feed_dict)

The context of this line is from

with tf.Session(graph=graph) as session:
  tf.global_variables_initializer().run()
  print('Initialized')
  average_loss = 0
  for step in range(num_steps):
    batch_data, batch_labels = generate_batch(
      batch_size, num_skips, skip_window)
    feed_dict = {train_dataset : batch_data, train_labels : batch_labels}
    _, l = session.run([optimizer, loss], feed_dict=feed_dict)
    average_loss += l
    if step % 2000 == 0:
      if step > 0:
        average_loss = average_loss / 2000
      # The average loss is an estimate of the loss over the last 2000 batches.
      print('Average loss at step %d: %f' % (step, average_loss))
      average_loss = 0
    # note that this is expensive (~20% slowdown if computed every 500 steps)
    if step % 10000 == 0:
      sim = similarity.eval()
      for i in range(valid_size):
        valid_word = reverse_dictionary[valid_examples[i]]
        top_k = 8 # number of nearest neighbors
        nearest = (-sim[i, :]).argsort()[1:top_k+1]
        log = 'Nearest to %s:' % valid_word
        for k in range(top_k):
          close_word = reverse_dictionary[nearest[k]]
          log = '%s %s,' % (log, close_word)
        print(log)
  final_embeddings = normalized_embeddings.eval()

And the full code is here

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/udacity/5_word2vec.ipynb

Why is an underscore being used as a variable? Seems like such an odd choice, but this is from the official Tensorflow github, so there must be a reason.

SantoshGupta7
  • 5,607
  • 14
  • 58
  • 116
  • 2
    People often use the underscore as a variable name to indicate "we don't care about this value". The function being called returns a 2-tuple, but they only care about the second value. – larsks May 26 '18 at 02:38

1 Answers1

5

When unpacking lists/tuples, _ is typically used for values you won't need later. If you look closely at that code, the _ variable isn't actually used anywhere.

Note that in the Python REPL, _ refers to the latest result.

>>> 2+2
4
>>> _
4
arshajii
  • 127,459
  • 24
  • 238
  • 287