Created
April 15, 2021 14:32
-
-
Save AndBondStyle/4cf03e25aa1cac73ddaccb32e117e12d to your computer and use it in GitHub Desktop.
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
import matplotlib.pyplot as plt | |
from matplotlib.lines import Line2D | |
import numpy as np | |
def plot_grad_flow(model): | |
ave_grads = [] | |
max_grads = [] | |
layers = [] | |
for name, param in model.named_parameters(): | |
if param.requires_grad and "bias" not in name: | |
if param.grad is None: | |
print("[!] No grad:", name) | |
continue | |
layers.append(name) | |
ave_grads.append(param.grad.abs().mean()) | |
max_grads.append(param.grad.abs().max()) | |
plt.bar(np.arange(len(max_grads)), max_grads, alpha=0.1, lw=1, color="c") | |
plt.bar(np.arange(len(max_grads)), ave_grads, alpha=0.1, lw=1, color="b") | |
plt.hlines(0, 0, len(ave_grads) + 1, lw=2, color="k") | |
plt.xticks(range(0, len(ave_grads), 1), layers, rotation="vertical") | |
plt.xlim(left=0, right=len(ave_grads)) | |
plt.ylim(bottom=-0.001, top=0.02) | |
plt.xlabel("Layers") | |
plt.ylabel("average gradient") | |
plt.title("Gradient flow") | |
plt.grid(True) | |
plt.legend( | |
[Line2D([0], [0], color=x, lw=4) for x in "cbk"], | |
["max-gradient", "mean-gradient", "zero-gradient"], | |
) | |
if __name__ == "__main__": | |
model = ... | |
inputs = ... | |
labels = ... | |
criterion = ... | |
outputs = model.forward(inputs) | |
loss = criterion(outputs, labels) | |
loss.backward() | |
plot_grad_flow(model) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment