Train/test mode is something like this:
net.train()
#train loop/function
for (images, labels) in train_loader:
    # train code


net.eval()
#test loop or function
for (images, labels) in test_loader
#    test code eg. outputs = net(images)

So, you set the flags before you iterate over the corresponding data loader. You can wrap them in functions which can enable you measure your performance on the validation set after every n training iterations.