Following are 2 code patterns for the training part of Tensorflow NN.
I find it logical to use the model 1. But I am seeing model 2 in multiple places frequently. I feel Model 2 is wrong. Wont model run the graph in session twice for every iteration for the same data? Is there something I am missing and people do it for any other reasons?
Model 1
for epoch in range(epochs):
for iteration in range(num_tr_iter):
_, loss, accuracy = sess.run([optimizer, loss, accuracy], feed_dict)
Model 2
for epoch in range(epochs):
for iteration in range(num_tr_iter):
sess.run(optimizer, feed_dict)
loss, accuracy = sess.run([loss, accuracy],feed_dict)
EDIT : Am expanding the question for more clarity
If the below sess.run() execute optimizer node, it will execute all its dependent node. It will run the underlying convnet and loss function as well.
sess.run(optimizer, feed_dict)
Next if the below sess.run() execute the loss node, why doesnt it execute the convnet with its current weights. I am not inferring that it will run the optimization again. Even to arrive at the current loss, wouldnt tensorflow execute the convnet and calculate the loss?
loss, accuracy = sess.run([loss, accuracy],feed_dict)