-1

I can not understand why does it need "_" before another execution within a loop. Here is the code:

for i in range(len(X_train)):
       feed = {X: [X_train[i]],y: [y_train[i]]}
        _, loss = sess.run([train_op, cost],feed_dict=feed)

I had no problem in running the code, but I have no idea about why it had to place a "_" before the next statement. Anyone knows?

Willy
  • 563
  • 1
  • 4
  • 6

1 Answers1

3

Because you have supplied two inputs, sess.run returns two outputs.

A single underscore is often used in Python as a variable name that we don't care about. _, loss just means "I don't care about the first output, give me the second one."

Phydeaux
  • 2,795
  • 3
  • 17
  • 35
  • Got it! So if I don't ignore the first output, it could be like this: for i in range(len(X_train)): feed = {X: [X_train[i]],y: [y_train[i]]} UU, loss = sess.run([train_op, cost],feed_dict=feed) – Willy Feb 27 '18 at 11:48
  • @Willy correct, then the output of `train_op` will be assigned to `UU` – Phydeaux Feb 27 '18 at 11:49