Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix example python numerical_verification_eprop_hardcoded_vs_autodiff.py in tf1.0 #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions numerical_verification_eprop_hardcoded_vs_autodiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
cell.compute_loss_gradient(learning_signals, pre_synpatic_spike_one_step_before, spikes, voltages,
thr_variations, decay_out, True)

# 6. Compute the gradients with BPTT as a ground truth
gradients_BPTT = tf.gradients(loss, cell.w_rec_var)[0]
# 6. Compute the gradients with auto-diff as a ground truth (stop_z_gradients=True)
gradients_autodiff = tf.gradients(loss, cell.w_rec_var)[0]

# 7. Start the tensorflow session to run the computation
# (until now we only built a computational graph, no simulation has been performed)
Expand All @@ -124,16 +124,16 @@

# Compute the relative error:
g_e_prop = np_tensors['gradients_eprop']
g_bptt = np_tensors['gradients_autodiff']
M = np.max(np.abs(g_bptt))
g_autodiff = np_tensors['gradients_autodiff']
M = np.max(np.abs(g_autodiff))

print("Max abs value of the true gradient: ", M)
assert (not np.any(np.isnan(g_bptt)), "The auto-diff has NaN coeffs, this not a very interesting verification.")
assert (not np.any(np.isnan(g_autodiff)), "The auto-diff has NaN coeffs, this not a very interesting verification.")
assert M != 0, "The auto-diff gradient is zero, this not a very interesting verification."
g_e_prop /= M
g_bptt /= M
g_autodiff /= M

gradient_errors = (g_e_prop - g_bptt) ** 2
gradient_errors = (g_e_prop - g_autodiff) ** 2
max_gradient_errors = np.max(gradient_errors)
print("Gradients computed with symmetric e-prop:")
print(np.array_str(np_tensors['gradients_eprop'], precision=5, suppress_small=True))
Expand Down