What does model.train() do in PyTorch?

后端 未结 3 879
無奈伤痛
無奈伤痛 2020-12-02 07:15

Does it call forward() in nn.Module? I thought when we call the model, forward method is being used. Why do we need to specify train()

3条回答
  •  再見小時候
    2020-12-02 08:05

    There are two ways of letting the model know your intention i.e do you want to train the model or do you want to use the model to evaluate. In case of model.train() the model knows it has to learn the layers and when we use model.eval() it indicates the model that nothing new is to be learnt and the model is used for testing. model.eval() is also necessary because in pytorch if we are using batchnorm and during test if we want to just pass a single image, pytorch throws an error if model.eval() is not specified.

提交回复
热议问题