Hi Samuel!



 Samuel_Bachorik:

When I change my equation to System of linear equations and  I have one more unknown how could I calculate loss and update both unknowns ?


Let’s say you have nUnknowns unknowns in your system and
also nUnknowns equations.  Make the variable you optimize a
1d tensor of length nUnknowns:
x = torch.nn.Parameter (torch.zeros (nUnknowns))

Then have your model return the left-hand sides of your nUnknowns
equations as a tensor of shape [nUnknowns] and supply the
right-hand sides of your equations as a target vector, also of shape
[nUnknowns].  You could calculate the loss “by hand” as you do in
your example, or you could use MSELoss:
loss = torch.nn.MSELoss() (model (x), target)

(You could also have your model return a vector of differences
between the left-hand and right-hand sides of your equations,
and use its sum of squares as your loss.)
Two side comments:
torch.nn.Parameter has requires_grad = True) by default,
so you don’t need to specify this explicitly.  (But it doesn’t hurt to
do so.)
As a pure python comment, not specific to pytorch. in the context
of just the code you posted, global y doesn’t do anything.  But, in
general, you should try to avoid global; there are usually cleaner,
“safer” ways of accomplishing whatever it is you might be trying to
do with global.
Best.
K. Frank