tr_loss, nb_tr_steps, nb_tr_examples are not defined or initialized before updating them in the loop of the valid() function.