Question Details

No question body available.

Tags

python pytorch

Answers (2)

February 13, 2026 Score: 2 Rep: 7,173 Quality: Low Completeness: 100%

As already noted in the comments, the two versions of your code do two different things:

Version 1:

loss = nn.CrossEntropyLoss()
loss(torch.tensor((.1, .2)), torch.tensor((.3, .4)))

Here, you first create an instance loss of the class CrossEntropyLoss with its default initialization arguments, then you call the instance with the tensors of interest.

Version 2:

nn.CrossEntropyLoss(torch.tensor((.1, .2)), torch.tensor((.3, .4)))

Here, you directly pass the tensors at instantiation time of the class CrossEntropyLoss, meaning they are provided as initialization arguments to the class. This fails, because the initialization arguments of CrossEntropyLoss, if provided at all, need to be different ones.

A fix to this version would be:

nn.CrossEntropyLoss()(torch.tensor((.1, .2)), torch.tensor((.3, .4)))

Here, we first initialize an instance again (CrossEntropyLoss()), then we call the instance, rather than the class, with the tensors – note the extra pair of brackets ().

As to your final comment, it seems that loss(input, target) is equivalent to torch.nn.functional.crossentropy(input, target):

This is essentially correct: As far as I know, the classes in torch.nn are mostly wrappers around the functions in torch.nn.functional. A fundamental difference between the two:

  • The functions in the latter can directly be called with the tensors of interest (and, potentially, additional arguments to control the functions' behavior).
  • The classes in the former first have to be instantiated (potentially with custom initialization arguments to control their behavior), then (and only then) the instances can be called with the tensors of interest (and only them).
February 13, 2026 Score: 1 Rep: 2,366 Quality: Low Completeness: 80%

You’re mixing up the loss module instance with the loss class. nn.CrossEntropyLoss() returns a callable module (its forward() takes (input, target)), so loss(input, target) is valid.

import torch
import torch.nn as nn

loss = nn.CrossEntropyLoss() #