@rasbt

I guess, because it would require that forward also accepts a second array, targets.

My code was just meant to be simple (and thus non functional), in order to illustrate my question. In my case, the target variable is non changing, and it setup durring a setup phase as self.target.
For my code example, I am working with VGG/sequential models, and the forward function for my loss module would be inside a class (which I add to the sequential model).

The “typical” use of the nn modules is that you return the predictions (e.g., logits, softmax outputs etc) from forward and compute the cost outside the loop. E.g., sth like

I am collecting the self.loss value from each custom nn module, for use in computations. But I don’t need to change the output of a layer in my hierarchical model.