108. Creating a train/test loop - Why model.train() command was not included while functionalizing the training loop? #592
-
def train_step(model: torch.nn.Module,
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
If you are in the training loop and include model.eval() is because you also include model.train(), in this case is not necessary because you are not entering into evaluation mode, but if you enter to evaluation mode you must come back to train mode, and you add in that case the model.eval() to the loop, let me know if make sense fella. If the question is answered please let me know, otherwise will try again, cheers my fella. |
Beta Was this translation helpful? Give feedback.
-
Good question! Perhaps I can provide some clarity on this. A model's default state is However, when performing evaluation/testing, it's best to call So if I've made a mistake in the materials somewhere, the best practice is as follows: When training (e.g. inside a training loop) Call For example: def train_step(model, ...):
model.train()
# Perform training steps... When testing/evaluating/making predictions (e.g. inside a testing loop) Call For example: def test_step(model, ...):
model.eval()
# Perform testing steps... -- For more, see this resource: https://stackoverflow.com/questions/51433378/what-does-model-train-do-in-pytorch |
Beta Was this translation helpful? Give feedback.
If you include model.eval() is just to ensure that the model is training, also is not mandatory to include model.eval() if you include model.train(), model.train() is like to make 100% sure that you are training and avoid anomalies from your model.
If model.eval() is not included the model will remain in the previous state, so add model.eval() is a good practice. for example if the model was in test mode before the loop and the loop does not explicitly change the mode to training maybe will lead to undesirably behavior and affect training.
If model.train() was not included while functionalizing the training loop maybe the professor was 100% sure that there were not another state before, b…