Fitting the sine curve using pytorch optimizers

Sedir: <p>Hello</p>
<p>I want to use PyTorch to fit the sine curve.<br>
I have the points of the sine curve with given frequency <code>f_trurh</code> and a sine function with an input parameter <code>f</code>.</p>
<p>However, I am facing an issue where the fit process does not seem to work properly. The Adam optimizer gets stuck in a local minima closest to the initial frequency value <code>f_0</code>.</p>
<p>Could you please help me understand what might be causing this problem?</p>
<p>Below is the code and some figures, might be helpful to understand the problem.</p>
<details>
<summary>
Attachments</summary>
<p>the code:</p>
<pre><code class="lang-auto">import torch

def sin_function(x, f):
    return torch.sin(f*x)

x_data = torch.linspace(0, 6, 10, dtype=torch.float32)
f_truth = 1
y_data = sin_function(x_data, f_truth)

f_0 = 1.8
f = torch.tensor(f_0, requires_grad=True)

loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam([f], lr=0.2)

num_epochs = 100
for epoch in range(num_epochs):
    y_pred = sin_function(x_data, f)
    loss = loss_fn(y_pred, y_data)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/3/93beee9fc63c3f8cfafff3877f346336eadd2ce6.png" data-download-href="https://discuss.pytorch.org/uploads/default/93beee9fc63c3f8cfafff3877f346336eadd2ce6" title="combined-3"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/93beee9fc63c3f8cfafff3877f346336eadd2ce6_2_690x277.png" alt="combined-3" data-base62-sha1="l51g15BRY4XT0L1Bz5MjVkLrlGe" width="690" height="277" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/93beee9fc63c3f8cfafff3877f346336eadd2ce6_2_690x277.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/93beee9fc63c3f8cfafff3877f346336eadd2ce6_2_1035x415.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/3/93beee9fc63c3f8cfafff3877f346336eadd2ce6.png 2x" data-dominant-color="F7F7F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">combined-3</span><span class="informations">1093×439 67 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
</details>

nairbv: <p>You can plot the loss function and see that from <code>f_0 = 1.8</code> you’ll go in the wrong direction.</p>
<p>E.g.</p>
<pre><code class="lang-auto">losses = [(x/50, loss_fn(torch.sin( torch.linspace(0, 6, 20) ), torch.sin( (x/50) * torch.linspace(0, 6, 20) ) )) for x in range(0, 100) ]
plt.plot( [x[0] for x in losses], [x[1] for x in losses] )
</code></pre>
<p>shows that f_0 &gt; ~ 1.75 or &lt; about 0.3 is going to learn to move in the wrong direction away from the global optimum.</p>
<p>Looking at your own graph of the input data and predicted curve, think about how close the high and low of the predicted sin curve are from (points on) the actual sin curve, and how much farther they’d (temporarily) need to move away from optimal values before finally arriving at the optimal solution.</p>

Sedir: <p>Thanks for your answer.</p>
<p>I indeed understand that standard gradient descent would work in this case only if the initial parameter value is close enough to the global minimum. However, I believe that more complicated optimizers should handle the local minima problem.</p>
<p>For instance, there is an example of PyTorch’s built-in Adam optimizer dealing with the local minima problem: <a href="https://raw.githubusercontent.com/jettify/pytorch-optimizer/master/docs/rastrigin_Adam.png" rel="noopener nofollow ugc">(image)</a>.</p>
<p>So, I guess, in my particular case, there should also be a way to solve the problem for less strict initial conditions. And the question is how to do it.</p>

Sedir: <p>In fact, after some empirical experiments, I found that increasing the learning rate to values around 2-3 made it possible to find the global minima.</p>

nairbv: <p>That’s right, there are cases where an optimizer step can step right past a local minimum. A high learning rate can cause that, and so can a momentum factor. A high lr or momentum can also cause problems – you want to avoid local minima but not the global one. In most situations, you wouldn’t want to choose a learning rate greater than one.</p>