Solving equation with two unknowns

username: Samuel_Bachorik
cooked: <p>Hi this is my code for solving quadratic equation. This is working fine but problem is when I try to add one more unknown.</p>
<pre><code class="lang-auto">x = torch.nn.Parameter(torch.zeros(1), requires_grad=True)

def model(x):
    b=3
    a=2
    c=1
    global y
    y = a*x ** 2 + b*x + c
    return y

optimizer = torch.optim.Adam([x], lr=0.1)

for i in range(150):
    a = model(x)

    loss = (0 - a) ** 2

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    print(loss)
</code></pre>
<p>I am computing loss from x. and my optimizer is optimizing x after every loop. 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 ?<br>
For example equation like this<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/8/f/8fa73ec89f92ad59f52d00ea269a8561803a7083.png" alt="image" data-base62-sha1="kuOBaJDy2BmMlM0S5NgF6SL8dGP" width="151" height="83"></p>

username: KFrank
cooked: <p>Hi Samuel!</p>
<aside class="quote no-group" data-username="Samuel_Bachorik" data-post="1" data-topic="127330" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/samuel_bachorik/48/56232_2.png" class="avatar"> Samuel_Bachorik:</div>
<blockquote>
<p>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 ?</p>
</blockquote>
</aside>
<p>Let’s say you have <code>nUnknowns</code> unknowns in your system and<br>
also <code>nUnknowns</code> equations.  Make the variable you optimize a<br>
1d tensor of length <code>nUnknowns</code>:</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.nn.Parameter (torch.zeros (nUnknowns))
</code></pre>
<p>Then have your model return the left-hand sides of your <code>nUnknowns</code><br>
equations as a tensor of shape <code>[nUnknowns]</code> and supply the<br>
right-hand sides of your equations as a <code>target</code> vector, also of shape<br>
<code>[nUnknowns]</code>.  You could calculate the loss “by hand” as you do in<br>
your example, or you could use <code>MSELoss</code>:</p>
<pre data-code-wrap="python"><code class="lang-python">loss = torch.nn.MSELoss() (model (x), target)
</code></pre>
<p>(You could also have your model return a vector of differences<br>
between the left-hand and right-hand sides of your equations,<br>
and use its sum of squares as your loss.)</p>
<p>Two side comments:</p>
<p><code>torch.nn.Parameter</code> has <code>requires_grad = True)</code> by default,<br>
so you don’t need to specify this explicitly.  (But it doesn’t hurt to<br>
do so.)</p>
<p>As a pure python comment, not specific to pytorch. in the context<br>
of just the code you posted, <code>global y</code> doesn’t do anything.  But, in<br>
general, you should try to avoid <code>global</code>; there are usually cleaner,<br>
“safer” ways of accomplishing whatever it is you might be trying to<br>
do with <code>global</code>.</p>
<p>Best.</p>
<p>K. Frank</p>

username: Samuel_Bachorik
cooked: <aside class="quote no-group" data-username="KFrank" data-post="2" data-topic="127330" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/48.png" class="avatar"> KFrank:</div>
<blockquote>
<p>(You could also have your model return a vector of differences<br>
between the left-hand and right-hand sides of your equations,<br>
and use its sum of squares as your loss.)</p>
</blockquote>
</aside>
<p>To this sentence I want to ask you one more thing. “return a vector of differences” Please please, can you give me example how can I write this in python ? I understand what you mean but how to write vector like this in python. Thank you.</p>

username: KFrank
cooked: <p>Hi Samuel!</p>
<aside class="quote no-group" data-username="Samuel_Bachorik" data-post="3" data-topic="127330" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/samuel_bachorik/48/56232_2.png" class="avatar"> Samuel_Bachorik:</div>
<blockquote>
<p>how to write vector like this in python.</p>
</blockquote>
</aside>
<p>Consider the following:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.9.0'
&gt;&gt;&gt;
&gt;&gt;&gt; # your scalar x = z[0] and y = z[1]
&gt;&gt;&gt; z = torch.nn.Parameter (torch.tensor ([3.0, 7.0]))   # just to make it more interesting than 0.0
&gt;&gt;&gt; equationCoefficients = torch.tensor ([[1.0, 1.0], [1.0, -1.0]])
&gt;&gt;&gt; rhs = torch.tensor ([5.0, 1.0])
&gt;&gt;&gt; lhs = equationCoefficients @ z
&gt;&gt;&gt; lhs
tensor([10., -4.], grad_fn=&lt;MvBackward&gt;)
&gt;&gt;&gt; vectorOfDifferences = lhs - rhs
&gt;&gt;&gt; vectorOfDifferences
tensor([ 5., -5.], grad_fn=&lt;SubBackward0&gt;)
&gt;&gt;&gt; loss = (vectorOfDifferences**2).sum()
&gt;&gt;&gt; loss
tensor(50., grad_fn=&lt;SumBackward0&gt;)
</code></pre>
<p>Best.</p>
<p>K. Frank</p>