# Fitting the sine curve using pytorch optimizers

Hello

I want to use PyTorch to fit the sine curve.
I have the points of the sine curve with given frequency `f_trurh` and a sine function with an input parameter `f`.

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 `f_0`.

Below is the code and some figures, might be helpful to understand the problem.

Attachments

the code:

``````import torch

def sin_function(x, f):

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

loss_fn = torch.nn.MSELoss()

num_epochs = 100
for epoch in range(num_epochs):
y_pred = sin_function(x_data, f)
loss = loss_fn(y_pred, y_data)
loss.backward()
optimizer.step()
``````

You can plot the loss function and see that from `f_0 = 1.8` you’ll go in the wrong direction.

E.g.

``````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] )
``````

shows that f_0 > ~ 1.75 or < about 0.3 is going to learn to move in the wrong direction away from the global optimum.

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.