In [11]:
import tangent
In [2]:
import tensorflow as tf
In [3]:
def f(W,x):
h1 = tf.matmul(x,W)
h2 = tf.tanh(h1)
out = tf.reduce_sum(h2)
return out
In [4]:
dfdW = tangent.grad(f)
In [5]:
dfdW??
output:
def dfdW(W, x, bout=1.0):
h1 = tf.matmul(x, W)
h2 = tf.tanh(h1)
# Grad of: out = tf.reduce_sum(h2)
_bh2 = tangent.unreduce(bout, tangent.shape_as_list(h2), None, None)
bh2 = _bh2
# Grad of: h2 = tf.tanh(h1)
_h2 = h2
_bh1 = bh2 * (1 - _h2 * _h2)
bh1 = _bh1
# Grad of: h1 = tf.matmul(x, W)
_bW = tangent.matmul_adjoint_y(bh1, x, W, False, False)
bW = _bW
return bW
okay this is pretty cool
In [6]:
import numpy as np
In [7]:
# See here:
# https://github.com/google/tangent/blob/6f19d40f333c9b73a9cfd6a9bce78d7fb5dfd035/tangent/grads.py#L212-L298
def np_f(W,x):
h1 = np.multiply(x,W)
h2 = np.tanh(h1)
out = np.sum(h2)
return out
In [9]:
np_dfdW = tangent.grad(np_f)
In [10]:
np_dfdW??
def dnp_fdW(W, x, bout=1.0):
h1 = np.multiply(x, W)
h2 = np.tanh(h1)
# Grad of: out = np.sum(h2)
_bh2 = tangent.astype(tangent.unreduce(bout, numpy.shape(h2), None, np.
_NoValue), h2)
bh2 = _bh2
# Grad of: h2 = np.tanh(h1)
_h2 = h2
_bh1 = bh2 * (1.0 - _h2 * _h2)
bh1 = _bh1
# Grad of: h1 = np.multiply(x, W)
_bW = x * bh1
bW = _bW
return bW
:tada:
In [ ]: