Why model.training=True and model.train() give different behavior?

I have a neural network called model. I expected that model.training=True would have the same effect as model.train(). However, the behaviors are different, at least for dropout. In the former, dropout seemed to be disabled (different runs model(x) give the same output), while in the latter, dropout is activated (different runs model(x) give different outputs). Thanks.

.training is an attribute of the current module.
If you call model.train() or model.eval(), this attribute will be changed for all child modules inside the parent class as can be seen here.

Thus, if you would like to change the .training attribute manually, you would have to change it for each module manually.

3 Likes