Additional inputs (constant matrices) in Loss function


I’m implementing a loss function in Pytorch 1.7.0 but I don’t know how I can take an auxiliary matrix D (a constant matrix) as an input in addition to the output and target tensors it gets from the model?

def my_loss(output, target):
    loss = torch.mean((output - target)**2)
    return loss
model = nn.Linear(2, 2)
x = torch.randn(1, 2)
target = torch.randn(1, 2)
output = model(x)
loss = my_loss(output, target)

Any help?

You could add another argument to my_loss and pass this matrix D to it:

def my_loss(output, target, D):

Let me know, if I misunderstood the question. :slight_smile: