KFrank:

But the details of how to train the model (e.g., what optimization
algorithm) can be separated from the infrastructure that makes a
model able to be trained (e.g., tensors that have a grad property),
and the guideline of “separation of concerns” suggests that when
things can be conceptually and straightforwardly separated, they
should be separated and placed in separate classes.


This is where I had doubts, because I thought that they are not part of the model, but at the same time, the example I gave made my feel that this is OK to do it like that. But now that I’ve heard another person’s opinion, I have to agree with you, that yes, loss and optimizer are not part of the model, and they shouldn’t be specified inside the model.
But still, it would be interesting to see what more people have to say.