Last active
August 7, 2018 23:04
-
-
Save lxuechen/3a0e8ba237ef389a4712d818b902f273 to your computer and use it in GitHub Desktop.
fast prototyping with eager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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