Search code examples
python-3.xpytorchgradient-descentbackpropagation

Finding parameters with backpropagation and gradient descent in PyTorch


I am experimenting with PyTorch and autodifferentiation and gradient descent

To that end I would like to estimate the parameters that would produce a certain value to an arbitrary linear in the parameters function.

My code is here:

import torch

X = X.astype(float)

X = np.array([[3.], [4.], [5.]])

X = torch.from_numpy(X)

X.requires_grad = True

W = np.random.randn(3,3)

W = np.triu(W, k=0)

W = torch.from_numpy(W)

W.requires_grad = True

out = 10 - (X@torch.transpose(X, 1,0) * W).sum()

out is :

enter image description here

My objective is to make out close to 0 (within an interval of [-.00001 , 0.0001]) by adjusting W using the gradient of W.

How should I proceed from here to achieve this end with pytorch?

Update

@Umang: this is what I get when I run the code you propose:

enter image description here

In fact the algorithm diverges.


Solution

  • # your code as it is
    import torch
    import numpy as np 
    
    X = np.array([[3.], [4.], [5.]])
    X = torch.from_numpy(X)
    X.requires_grad = True
    W = np.random.randn(3,3)
    W = np.triu(W, k=0)
    W = torch.from_numpy(W)
    W.requires_grad = True
    
    # define parameters for gradient descent
    max_iter=100
    lr_rate = 1e-3
    
    # we will do gradient descent for max_iter iteration, or convergence till the criteria is met.
    i=0
    out = compute_out(X,W)
    while (i<max_iter) and (torch.abs(out)>0.01):
        loss = (out-0)**2
        W = W - lr_rate*torch.autograd.grad(loss, W)[0]
        i+=1
        print(f"{i}: {out}")
        out = compute_out(X,W)
    
    print(W)
    

    We define a loss function such that its minima is at the desired point and run gradient descent. Here, I have used squared-error but you may use other loss functions too with desired minima.