Dear all,
I’m fairly new to PyTorch and neural networks but out of curiosity, I have started experiencing with PyTorch.
This might be a dummy question given my level of understanding but I’m trying (and failing miserably) to use PyTorch for a simple matter of sine wave phase and amplitude estimation. I feed the network with a sine wave over one period only so estimating its amplitude is simply the max of the input value whereas its phase can be derived from the max found and the first value feeded.
I started with the sine example of PyTorch 1.
What I was hoping was that once the network is trained, it would manage to readily estimate amplitude and phase of a sine wave given a sine wave output.
Here is the code:

import torch
import math
import matplotlib.pyplot as plt
x = torch.linspace(-math.pi, math.pi, 2000)
model = torch.nn.Sequential(
torch.nn.Linear(2000, 15),
torch.nn.Linear(15, 2),
)
loss_fn = torch.nn.MSELoss(reduction=‘sum’)
amp = 2;
phase = 23.14torch.rand(1)
y = amp * torch.sin(x + phase)
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
line1, = ax.plot(x, y)
line2, = ax.plot(x, y, ‘r’)
fig.canvas.draw()
fig.canvas.flush_events()
learning_rate = 1e-3
optimizer = torch.optim.SGD(model.parameters(), lr=5.5e-8, momentum=0.95)
for t in range(200000):
if t%50 == 0:
    amp = 2*torch.rand(1);
    phase = 2*3.14*torch.rand(1)
    y = amp * torch.sin(x + phase)
    line1.set_ydata(y.detach().numpy())

amp_phase = model(y)

y_pred = amp_phase[0] * torch.sin(x + amp_phase[1])

# Compute and print loss.
#loss = loss_fn(y_pred, y)
loss = torch.sum((y_pred - y) * (y_pred - y))
if t % 100 == 99:
    print(t, loss.item())
    print('true',amp,phase)
    print('estimated',amp_phase)
    line2.set_ydata(y_pred.detach().numpy())
    fig.canvas.draw()
    fig.canvas.flush_events()


optimizer.zero_grad()
loss.backward()
optimizer.step()


Is it a correct approach ?