Skip to content

Instantly share code, notes, and snippets.

@lxuechen
Last active August 7, 2018 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lxuechen/3a0e8ba237ef389a4712d818b902f273 to your computer and use it in GitHub Desktop.
Save lxuechen/3a0e8ba237ef389a4712d818b902f273 to your computer and use it in GitHub Desktop.
fast prototyping with eager
block = Residual()
x = tf.random_normal(shape=(N, C, H, W))
dy = tf.random_normal(shape=(N, C, H, W))
with tf.GradientTape() as tape:
tape.watch(x)
y = block(x)
# Compute true grads
dx_true = tape.gradient(y, x, output_gradients=dy)
# Compute grads from reconstruction
dx, _ = block.backward_grads(x, y, dy)
# Check whether the difference is below a certain threshold
thres = 1e-6
diff_abs = tf.reshape(abs(dx - dx_true), [-1])
assert all(diff_abs < thres)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment