An encode/decode process compresses an input into a latent representation and reconstructs it as an output. Comparing input and reconstruction reveals how much information the representation preserves. section 7.1.4 section 7.2.2
Use the process to inspect representation quality:
- Plot encoded data to assess class separation, especially when labels are available for visualization. section 7.1.4
- Compare original and reconstructed inputs to expose blur, stylistic changes, or outright reconstruction failures. section 7.1.4 section 7.2.2
- Treat smoothing as useful for discovering canonical patterns, but harmful when exact reconstruction is the training goal. section 7.2.2
Digit reconstruction
Reducing 784-dimensional digit images to two dimensions makes some digits, such as 0 and 1, reconstruct reasonably, while 7 or 4 can lose essential information. An autoencoder produces crisper results but still struggles with ambiguous or constrained representations. section 7.1.4 section 7.2.2
def showEncodeDecode(encode_decode, x):
"""
encode_decode: the PyTorch Module that does the encoding and decoding
steps at once
x: the input to plot as is, and after encoding & decoding it
"""
encode_decode = encode_decode.eval() #1
encode_decode = encode_decode.cpu() #2
with torch.no_grad(): #3
x_recon = encode_decode(x.cpu())
f, axarr = plt.subplots(1,2) #4
axarr[0].imshow(x.numpy()[0,:])
axarr[1].imshow(x_recon.numpy()[0,0,:])
#1 Switches to eval mode
#2 Moves things to the CPU so we don’t have to think about what device anything is on and because this function is not performance-sensitive
#3 Always no_grad if you are not training
#4 Uses Matplotlib to create a side-by-side plot with the original on the left
Runs the model in evaluation mode without gradients, reconstructs an input on the CPU, and plots the original beside its reconstruction. section 7.1.4
