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 add
model.train()
- Test to add
model.eval()
This is because such a model may training
attribute is set true
or 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