3

I am trying to do a simple thing: use autograd to get gradients and do gradient descent:

import tangent

def model(x):
    return a*x + b

def loss(x,y):
    return (y-model(x))**2.0

After getting loss for an input-output pair, I want to get gradients wrt loss:

    l = loss(1,2)
    # grad_a = gradient of loss wrt a?
    a = a - grad_a
    b = b - grad_b

But the library tutorials don't show how to do obtain gradient with respect to a or b i.e. the parameters so, neither autograd nor tangent.

Andy Markman
  • 127
  • 1
  • 12
  • What do you mean by they [don't show](https://github.com/HIPS/autograd)? – sascha Feb 02 '18 at 13:10
  • @sascha yes, i tried that first before tangent. They show an example with tanh only; 1. not a composition of function and then 2. their function doesn't have any parameters ie. its just x, so no partial derivatives. – Andy Markman Feb 02 '18 at 13:12

2 Answers2

1

You can specify this with the second argument of the grad function:

def f(x,y):
    return x*x + x*y

f_x = grad(f,0) # derivative with respect to first argument
f_y = grad(f,1) # derivative with respect to second argument

print("f(2,3)   = ", f(2.0,3.0))
print("f_x(2,3) = ", f_x(2.0,3.0)) 
print("f_y(2,3) = ", f_y(2.0,3.0))

In your case, 'a' and 'b' should be an input to the loss function, which passes them to the model in order to calculate the derivatives.

There was a similar question i just answered: Partial Derivative using Autograd

benno
  • 289
  • 2
  • 10
0

Here this may help:

import autograd.numpy as np
from autograd import grad
def tanh(x):
  y=np.exp(-x)
  return (1.0-y)/(1.0+y)

grad_tanh = grad(tanh)

print(grad_tanh(1.0))

e=0.00001
g=(tanh(1+e)-tanh(1))/e
print(g)

Output:

0.39322386648296376
0.39322295790622513

Here is what you may create:

import autograd.numpy as np
from autograd import grad  # grad(f) returns f'

def f(x): # tanh
  y = np.exp(-x)
  return  (1.0 - y) / ( 1.0 + y)

D_f   = grad(f) # Obtain gradient function
D2_f = grad(D_f)# 2nd derivative
D3_f = grad(D2_f)# 3rd derivative
D4_f = grad(D3_f)# etc.
D5_f = grad(D4_f)
D6_f = grad(D5_f)

import  matplotlib.pyplot  as plt
plt.subplots(figsize = (9,6), dpi=153 )
x = np.linspace(-7, 7, 100)
plt.plot(x, list(map(f, x)),
         x, list(map(D_f , x)),
         x, list(map(D2_f , x)),
         x, list(map(D3_f , x)),
         x, list(map(D4_f , x)),
         x, list(map(D5_f , x)),
         x, list(map(D6_f , x)))
plt.show()

Output:

enter image description here

prosti
  • 42,291
  • 14
  • 186
  • 151