Actually I meant to say that you must I have created a function to train your model. So outside that function create a object of class nn.CrossEntropyLoss and inside the function use the same object to calculate the loss