# Plot train loss (per batch)x_epochs = np.arange(len(metrics.train_batch_loss)) /len(model.train_dataloader)ax = sns.scatterplot(x=x_epochs, y=metrics.train_batch_loss, s=2, label="Training loss", legend="auto")# sns.scatterplot(x = x_epochs, y=moving_average(train_loss_metrics, n=len(model.train_dataloader)))ax.set_xlabel("Epoch")ax.set_ylabel("MSE Loss")ax.set_title("Training loss calculated for each mini batch")plt.show()
# Plot training and validation accuracy per epochax = sns.lineplot(metrics.valid_precision_epoch, label="Validation Accuracy")sns.lineplot(metrics.train_precision_epoch, label="Training Accuracy")ax.set_ylim(None, 1)ax.set_xlabel("Epoch")ax.set_ylabel("Accuracy")ax.set_title("Validation set accuracy every epoch")plt.show()
# Visualise the weights in the first layerdef weights(mdl: DigitClassifier, layer:int=0):"""Return the weights in the layers 0th->layer""" img_size = (28,28) return mdl.linears[0].weight[layer].detach().reshape(*img_size).cpu().numpy()digit =0fig, ax = plt.subplots()ax.set_title(f"0th layer weights for digit: {digit}")im = ax.imshow(weights(model, digit), cmap=mpl.colormaps['seismic'])fig.colorbar(im, ax=ax)plt.show()
## Setup a color map renormaliser such that w=0 is in the middle# Print out some bounds to see what works best# Max value:bound =abs(model.linears[0].weight.detach()).max().numpy()print(f"Max value: {bound}")# 3 std from the meanbound =3*abs(model.linears[0].weight.detach()).std().numpy()print(f"3 std: {bound}")# Set bound to 0.5 (this seemed sensible)bound =0.5print(f"Setting bound to: {bound}")norm = cm.colors.Normalize(vmax=bound, vmin=-bound)
Max value: 1.9481966495513916
3 std: 0.5106891989707947
Setting bound to: 0.5
fig, ax = plt.subplots()im = ax.imshow(weights(model, 0), cmap=mpl.colormaps['seismic'], norm=norm, # interpolation='kaiser' )# TODO: Try contour map# cs = ax.contour(weights, colors='k')# ax.contour(weights, origin="upper") # Set origin if not using imshowfig.colorbar(im, ax=ax)plt.show()
WARNING:matplotlib.animation:MovieWriter ffmpeg unavailable; using Pillow instead.
INFO:matplotlib.animation:Animation.save using <class 'matplotlib.animation.PillowWriter'>