Can't Backpropagate in Pytorch module with .backwards(); weights are not updated
I'm willing to create a network estimating the next sample of the signal. I've statred with a simple sin signal. But when I run the code I got noise as an output. Then checked the layer weights and figured aout that they are not updating. I can't find the mistake here.

class Model(nn.Module):
def __init__(self,in_dim,hidden_dim,num_classes):
    super(Model, self).__init__()
    self.layer1 = nn.Linear(in_dim,hidden_dim)
    self.layer2 = nn.Linear(hidden_dim,hidden_dim)
    self.layer3 = nn.Linear(hidden_dim,num_classes)
    self.relu = nn.ReLU()

def forward(self,x):
    a = self.relu(self.layer1(x))
    a = self.relu(self.layer2(a))
    return self.relu(self.layer3(a))  


train: 

def train(epoch,L,depth):
    criteria = nn.MSELoss()
    learning_rate = 1e-3
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

    t = np.linspace(0,2,L+2)       
    fs = L+2

    trn_loss = list()

   for f in range(0,epoch):
       phase = f/np.pi      
       x = np.sin(2*np.pi*t*fs+phase)   
       x = torch.from_numpy(x).detach().float()

       optimizer.zero_grad()

       x_hat = model(x[:-2])

       currentCost = criteria(x_hat,x[-2])
       trn_loss.append(currentCost.item())
       print(model.layer1.weight.data.clone())
       currentCost.backward()
       optimizer.step()
       print(model.layer1.weight.data.clone())
       sys.exit('DEBUG')


output:

tensor([[-0.1715, -0.1696,  0.0424,  ...,  0.0154,  0.1450, -0.0544],
    [ 0.0368,  0.1427, -0.1419,  ...,  0.0966,  0.0298, -0.0659],
    [-0.1641, -0.1551,  0.0570,  ..., -0.0227, -0.1426, -0.0648],
    ...,
    [-0.0684, -0.1707, -0.0711,  ...,  0.0788,  0.1386,  0.1546],
    [ 0.1401, -0.0922, -0.0104,  ..., -0.0490,  0.0404,  0.1038],
    [-0.0604, -0.0517,  0.0715,  ..., -0.1200,  0.0014,  0.0215]])
tensor([[-0.1715, -0.1696,  0.0424,  ...,  0.0154,  0.1450, -0.0544],
    [ 0.0368,  0.1427, -0.1419,  ...,  0.0966,  0.0298, -0.0659],
    [-0.1641, -0.1551,  0.0570,  ..., -0.0227, -0.1426, -0.0648],
    ...,
    [-0.0684, -0.1707, -0.0711,  ...,  0.0788,  0.1386,  0.1546],
    [ 0.1401, -0.0922, -0.0104,  ..., -0.0490,  0.0404,  0.1038],
    [-0.0604, -0.0517,  0.0715,  ..., -0.1200,  0.0014,  0.0215]])


Your final layer in forward call uses ReLU activation. This limits outputs of the network to [0, +inf) range.

Please notice your target is in the [-1, 1] range, so the network cannot output half (negative) of the values (and for the positive part it has to crunch +inf possible values into [0, 1] space).

You should change return self.relu(self.layer3(a)) to return self.layer3(a) in forward.

Better yet, in order to help your network accommodate to [-1, 1] range, use torch.tanh activation, so return torch.tanh(self.layer3(a)) should work best.
