Liu Yong
Liu Yong

Reputation: 535

How to use autograd find MIN/MAX point

Say we have a simple function y=sin(x**2), how do I use autograd find all X:s with the first order derivative value of 0?

Upvotes: 3

Views: 1248

Answers (1)

Umang Gupta
Umang Gupta

Reputation: 16480

Below code can find the point where the first derivative is zero. However, depending on random initialization it will only find one point. If you want to find all the points, you can try iterating over a lot of random initialization on some desired grid.

import torch 
import numpy as np
# initialization
x = torch.tensor(np.random.rand(1)).requires_grad_(True)

while (x.grad is None or torch.abs(x.grad)>0.01):
    if (x.grad is not None):
        # zero grads
        x.grad.data.zero_()
    # compute fn
    y = torch.sin(x**2)
    # compute grads
    y.backward()
    # move in direction of / opposite to grads
    x.data = x.data - 0.01*x.grad.data
    # use below line to move uphill 
    # x.data = x.data + 0.01*x.grad.data

print(x)
print(y)
print(x.grad)

Also see how to apply gradients manually in pytorch

Upvotes: 1

Related Questions