I am beginner in PyTorch and want to create something like:
class net()
…
…
…
def forward(self, x):
out = self.layerA(x)
out = self.layerB(out)
pLoss = calcPLoss(out)
out = self.layerC(out)
return out, pLoss
train()
…
out, pLoss = net(input)
loss = criterion(out, input)
loss = loss + pLoss
loss.backward()
Here, I have two question:

I am not able to return multiple variables from forward function. Can somebody show me correct way to do this?
If I would be able to do this, does PyTorch is able to manage the gradients accordingly? Because my pLoss is calculated in the intermediate layer and I have directly summing up this with the final layer loss. But I want my gradients from pLoss to backpropogate only before intermediate layer.

Thanks!