Minimizing a function - parameter history is all the same value

telias: <p>Hey everbody!</p>
<p>I’m trying to minimize a function in order to better understand the optimizer process. As an example I used the Eggholder-Function (<a href="https://www.sfu.ca/%7Essurjano/egg.html" rel="noopener nofollow ugc">https://www.sfu.ca/~ssurjano/egg.html</a>) which is 2d. My goal is to get the values of my parameters (x and y) after every optimizer iteration so that i can visualize it afterwards.</p>
<p>I wrote the following code:</p>
<pre><code class="lang-auto">def eggholder_function(x):
    return -(x[1] + 47) * torch.sin(torch.sqrt(torch.abs(x[1] + x[0]/2 + 47))) - x[0]*torch.sin(torch.sqrt(torch.abs(x[0]-(x[1]+47))))

def minimize(function, initial_parameters):
    list_params = []
    params = initial_parameters
    params.requires_grad_()
    optimizer = torch.optim.Adam([params], lr=0.1)

    for i in range(5):
        optimizer.zero_grad()
        loss = function(params)
        loss.backward()
        optimizer.step()
        list_params.append(params)
        


    return params, list_params

starting_point = torch.tensor([-30.,-10.])
minimized_params, list_of_params = minimize(eggholder_function, starting_point)
</code></pre>
<p>The output is as follows:</p>
<pre><code class="lang-auto">minimized_params: tensor([-29.4984, -10.5021], requires_grad=True)

list_of_params:


[tensor([-29.4984, -10.5021], requires_grad=True),
 tensor([-29.4984, -10.5021], requires_grad=True),
 tensor([-29.4984, -10.5021], requires_grad=True),
 tensor([-29.4984, -10.5021], requires_grad=True),
 tensor([-29.4984, -10.5021], requires_grad=True)]
</code></pre>
<p>While I understand that the minimized_params is infact the optimized minimum, why does list_of_params show the same values for every iteration?</p>
<p>Thank you and have a great day!</p>

KFrank: <p>Hi Telias!</p>
<aside class="quote no-group quote-modified" data-username="telias" data-post="1" data-topic="170139" 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/t/b19c9b/48.png" class="avatar"> telias:</div>
<blockquote>
<pre><code class="lang-auto">        list_params.append(params)
</code></pre>
<p>why does list_of_params show the same values for every iteration?</p>
</blockquote>
</aside>
<p>You are appending the same (reference to a) tensor, <code>params</code>, to<br>
<code>list_params</code> with every iteration of the loop.  You do change the<br>
contents of the tensor with every iteration, but when you display<br>
<code>list_params</code>, it’s just the same single tensor – with its final<br>
value – displayed multiple times.</p>
<p>You would need to <code>clone()</code> the tensor (or otherwise make copies<br>
of the values) to record the parameter history.</p>
<p>Consider:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.13.0'
&gt;&gt;&gt;
&gt;&gt;&gt; t = torch.tensor ([1.0])
&gt;&gt;&gt;
&gt;&gt;&gt; list_t = []
&gt;&gt;&gt; list_t_clone = []
&gt;&gt;&gt;
&gt;&gt;&gt; for  i in range (5):
...     t *= 2.0
...     list_t.append (t)
...     list_t_clone.append (t.clone())
...
&gt;&gt;&gt; list_t
[tensor([32.]), tensor([32.]), tensor([32.]), tensor([32.]), tensor([32.])]
&gt;&gt;&gt; list_t_clone
[tensor([2.]), tensor([4.]), tensor([8.]), tensor([16.]), tensor([32.])]
</code></pre>
<p>Best.</p>
<p>K. Frank</p>

telias: <p>That makes sense, thank you!</p>