I’m just generating a sine wave and trying to approximate it. However, the network literally refuses to work.
I’ve tried changing the activation functions, learning rate, nodes in a layer. But nothing seems to work.
Id really appreciate it if any changes were suggested to me.
Thanks !
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import time
import numpy as np
import random

dataset = []

for i in np.linspace(0,10,400):
    dataset.append([i,10*np.sin(i)])
shuffled = dataset.copy()
random.shuffle(shuffled)    
def criterion(out, label):
    return (label - out)**2

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(1,4)
        self.fc2 = nn.Linear(4,4)
        self.fc3 = nn.Linear(4,1)
        
    def forward(self, x):
        x = torch.tanh(self.fc1(x))
        x = torch.tanh(self.fc2(x))
        x = self.fc3(x)
        return x
err=[]
net = Net()
optimizer = optim.SGD(net.parameters(), lr= 0.00001)

for epoch in range(1600):
    for data in shuffled:
        x = torch.FloatTensor([data[0]])
        y = torch.FloatTensor([data[1]])
        optimizer.zero_grad()
        out = net(x)
        loss = criterion(out,y)
        loss.backward()
        optimizer.step()
        
    print(float(loss.data[0]),epoch)
    #random.shuffle(shuffled)
    err.append(float(loss))
    '''if float(loss.data[0]) < 0.0001:
        break'''
kek = []

#testing out the net
for i in np.linspace(0,10,550):
    val = float(net(torch.FloatTensor([i])))
    kek.append(val)