Pytorch achieves different outputs for training and testing

Pytroch achieves different outputs for training and testing

We have been saying before that model.train()it model.eval()is conducive to the realization of BN and Dropout. Why is this?

Because BN and Dropout have different operations for training and testing! So we need to distinguish between training and testing.

Before training or testing the code, we stipulate:

  • Training to addmodel.train()
  • Test to addmodel.eval()

This is because such a model may trainingattribute is set trueor false. In this way, we will know if the model has been trained.

class Model(nn.Module):
    def __init__(self):
        pass
    def isTraining(self):
        if self.training:
            return True
        else:
            return False
        
model = Model()
model.train()
print(model.training)
model.isTraining()

model.eval()
print(model.training)
model.isTraining()
result:
True
True
False
False

Guess you like

Origin blog.csdn.net/qq_43477218/article/details/115307042