Example

Basic

x=tf.Variable(2.0)
a=x/2*3 # a=x*(3/2)
 
# persisten=False일 경우 .gradient 여러번 실행시 error
with tf.GradientTape(persistent=True) as mytape:
    y=a*x**2 # y=ax^2=(3/2)x^3 
 
mytape.gradient(y,x)

<tf.Tensor: shape=(), dtype=float32, numpy=12.0>

tape.watch()

x=tf.constant(2.0)
with tf.GradientTape(persistent=True) as mytape:
    # tape.watch()를 사용하면 constant에 대해서도 미분할 수 있음.
    mytape.watch(x)
    y=a*x**2 # y=ax^2=(3/2)x^3 
 
mytape.gradient(y,x)

<tf.Tensor: shape=(), dtype=float32, numpy=18.0>

Find parameters of Normal Distribution (MLE)

tf.random.set_seed(43052)
x= tnp.random.randn(10000)*2+3
 
sigma = tf.Variable(3.0) 
mu = tf.Variable(2.0)
 
for i in range(1000):
    with tf.GradientTape() as tape: 
        pdf = 1/sigma * tnp.exp(-0.5*((x-mu)/sigma)**2)
        logL = tf.reduce_sum(tnp.log(pdf) ) 
    slope1, slope2 = tape.gradient(logL,[mu,sigma]) 
    mu.assign_add(slope1* 0.1/10000) # gradient ascent 
    sigma.assign_add(slope2* 0.1/10000) # gradient ascent

Find parameters of Binomial Distribution (MLE)

tf.random.set_seed(43052)
x= tf.constant(np.random.binomial(1,0.8,(10000,)))
 
p=tf.Variable(0.3) 
for i in range(1000):
    with tf.GradientTape() as tape: 
        pdf = p**x * (1-p)**(1-x) 
        logL = tf.reduce_sum(tnp.log(pdf)) 
    slope = tape.gradient(logL,p) 
    p.assign_add(slope* 0.1/10000) # gradient ascent