Question Details

No question body available.

Tags

python pytorch

Answers (1)

January 12, 2026 Score: 1 Rep: 35,417 Quality: Medium Completeness: 100%

This should really be a comment but it's too long.

I'm not sure you fully see the chain where references can be passed on, so here goes.

The relevant lines, out of context:

class SoftmaxRegission(torch.nn.Module):
    linear: torch.nn.Linear
    ...

model = SoftmaxRegission(numfeatures=numfeatures, numclasses=numclasses) logits, probas = model(features) cost = F.crossentropy(logits, targets) cost.backward()

Along the above lines:

  1. model is the object we're talking about (I assume), if you have a reference to model you have a reference to its attributes.
  2. logits is returned by model(features), which is implemented via model.call(), which is inherited from torch.nn.Module.call().
  3. cost is returned by crossentropy(), but it takes logits as input.

So what must be going on is that torch.nn.Model.call() returns something that is given a reference to self, i.e. model in your code. So logits can have a reference to the model it came from, and this reference can be used by cross_entropy() to give it to cost as well. Then cost.backward() can use that reference, stored in some attribute of the object named cost.

Whether the "use the reference in cost to update the model" logic is in Python or C depends on the implementation. You'll want to start with the implementation of type(cost).backward, because that's where this is triggered. Follow execution in that method, wherever it goes, until you find the model being updated.

And just to have a more robust version of my recommendation I already gave you in a comment: please read Ned Batchelder's post on Python names and references. It's necessary for you to be able to understand this. And it's necessary for anyone developing Python to understand this.