Accessing nn.Module mode

Hello,

While making a very custom NN, I had to take into account in the forward the fact of being in train mode or not.
I discovered that I could use the function model.train(True) , but I don’t know how to access this mode.

I need to get boolean tensor in order to make an if statement in the forward:

def forward(self, x):
is_train = self.mystery_fn()
if (is_train):

else:

so far I have build my own is_train parameter, in the init and the functions to change it, but I guess using the proper Pytorch approved one would be better.

Best regards

Barth

You can access this flag via self.training.

1 Like

Thank you very much !