CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics...

60
CIS 522: Lecture 5R Autoencoders 02/13/20

Transcript of CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics...

Page 1: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

CIS 522: Lecture 5R

Autoencoders02/13/20

Page 2: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3
Page 3: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Feedback / Logistics● Lecture Notes● Puppy as option instead of kitten● HW2 - Due tonight at midnight!● HW3 - Computer Vision

○ Groups of 2● Final Project Abstract Proposal (Due Tuesday 2/18)

Page 4: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Generative Models

Page 5: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Cognitive scienceImagine a kittenMake it blackMake it play with a ball

Page 6: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Like this?

Page 7: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

What is a Generative Model?Discriminative Model: Given a set of classes C, and a sample X, what is the probability X belongs to class c?

Succinctly represented as this quantity:

To generate a classification:

Examples: 1. Logistic Regression2. Decision Trees3. Support Vector Machines

(called a class probability estimate)

Page 8: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Generative Model: Given a class c from a set of classes C, and a sample X, what is the probability that X was generated from that class?

Succinctly represented as this distribution:

What is a Generative Model?

Page 9: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Generative Model: Given a class c from a set of classes C, and a sample X, what is the probability that X was generated from that class?

Succinctly represented as this distribution:

Now to generate an X, we can simply randomly sample

from this distribution!

What is a Generative Model?

Page 10: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Generative Model: Given a class c from a set of classes C, and a sample X, what is the probability that X was generated from that class?

Succinctly represented as this distribution:

Now to generate an X, we can simply randomly sample

from this distribution!Examples

● Naive Bayes● Variational Autoencoder (To be covered in this lecture)● Generative Adversarial Networks (To be covered in next lecture!)

What is a Generative Model?

Page 11: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Low-dimensional representations

Page 12: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3
Page 13: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Cute puppy… but takes ~ half a million numbers to represent

Page 14: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Artificial vs. biological representations

X 172k =

Page 15: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3
Page 16: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Autoencoders

Page 17: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Basic architecture

Page 18: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Training● “Self-supervised” training: desired output Y is just input X● e.g. MSE loss● Small bottleneck layer impedes training

y-hatX

Page 19: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

What is a linear autoencoder?● What happens if no nonlinearity?● Linear encoder and decoder● If L2 on encoder and decoder, exactly PCA (Kunin et al. 2019)

Page 20: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Convolutional Autoencoders● Decoder has to “undo” the convolution / pooling● Generally involves upsampling or “deconvolutions”● Also conv layers reducing # of features

CAE:

Source: https://stackabuse.com/autoencoders-for-image-reconstruction-in-python-and-keras/

Page 21: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Issue with Convolutional Autoencoders● Space forms clusters

● Clusters are far apart

● What if we want to

interpolate between

classes?

Source: https://towardsdatascience.com/intuitively-understanding-variational-autoencoders-1bfe67eb5daf

Page 22: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Constraining the Features● Currently no constraints

on the features.● What if we constrained the

features to the standard normal distribution?

Source: https://towardsdatascience.com/light-on-math-machine-learning-intuitive-guide-to-understanding-kl-divergence-2b382ca2b2a8

Page 23: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

KL Divergence Loss

Page 24: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Only using the KL Divergence Loss

Source: https://towardsdatascience.com/intuitively-understanding-variational-autoencoders-1bfe67eb5daf

Page 25: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Only using the KL Divergence Loss

… clearly can’t only use a KL Divergence Loss

Source: https://towardsdatascience.com/intuitively-understanding-variational-autoencoders-1bfe67eb5daf

Page 26: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

KL Divergence + Reconstruction Loss?

Source: https://towardsdatascience.com/intuitively-understanding-variational-autoencoders-1bfe67eb5daf

Page 27: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

KL Divergence + Reconstruction Loss?

Much better!

Source: https://towardsdatascience.com/intuitively-understanding-variational-autoencoders-1bfe67eb5daf

Page 28: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

KL Divergence + Reconstruction Loss?

Regularization Reconstruction loss

Page 29: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Variational autoencoders (VAEs) - motivation● Want the low-dim representation z to have independent components● Each component should contribute a similar amount● z according to unit Gaussian

Example:

● Low-dim representation of faces - eye color, hair length, etc.● Each feature should be independent● Scale so each feature has same variance

Page 30: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

VAE Process

Source: http://kvfrans.com/variational-autoencoders-explained/

Page 31: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Why sample a latent vector?

Source: https://towardsdatascience.com/intuitively-understanding-variational-autoencoders-1bfe67eb5daf

Page 32: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

VAE Process

Regularization Reconstruction loss

Source: http://kvfrans.com/variational-autoencoders-explained/

Page 33: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

VAE Computational Graph - Forward Pass

x

μ

σ

z

Page 34: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

VAE Computational Graph - Backward Pass

x

μ

σ

z

?

?

Page 35: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Reparameterization Trick

σ z

?

?

σ

ε

z

Z ~ N(μ,σ)

ε ~ N(0,1)

Z = εσ + μ

Page 36: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Where did this loss function come from?

Regularization Reconstruction loss

Page 37: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Estimating the VAE Lower Bound

VAE Objective Function:

Page 38: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Estimating the VAE Lower Bound

VAE Objective Function:

Latent Representation: (Think HMM’s)

Page 39: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Estimating the VAE Lower Bound

VAE Objective Function:

Latent Representation: (Think HMM’s)

Can compute individual terms of integral, but not the integral itself!

Page 40: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Estimating the VAE Lower Bound

Encoder Network: Decoder Network:

Source: http://kvfrans.com/variational-autoencoders-explained/

Page 41: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Estimating the VAE Lower Bound

Equations from Stanford lecture linked here: https://www.youtube.com/watch?v=5WoItGTWV54

Page 42: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Estimating the VAE Lower Bound

Equations from Stanford lecture linked here: https://www.youtube.com/watch?v=5WoItGTWV54

Page 43: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Estimating the VAE Lower Bound

Equations from Stanford lecture linked here: https://www.youtube.com/watch?v=5WoItGTWV54

Page 44: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Estimating the VAE Lower Bound

Equations from Stanford lecture linked here: https://www.youtube.com/watch?v=5WoItGTWV54

Log likelihood from our decoder network!

KL Divergence constraining z to STD Gaussian

Intractable term

Page 45: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Estimating the VAE Lower Bound

Equations from Stanford lecture linked here: https://www.youtube.com/watch?v=5WoItGTWV54

Log likelihood from our encoder network!

KL Divergence constraining z to STD Gaussian

(Since KL Div always non-negative)

Page 46: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Estimating the VAE Lower Bound

Equations from Stanford lecture linked here: https://www.youtube.com/watch?v=5WoItGTWV54

Page 47: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Transposed Convolution (“Deconvolution”)

Source: https://github.com/vdumoulin/conv_arithmetic

Page 48: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3
Page 49: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Issues with VAEs

Current fix is to use a VAE + GAN (you’ll learn about GANs in the next lecture)

Page 50: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Transposed Convolution (“Deconvolution”)

Source: https://github.com/vdumoulin/conv_arithmetic

Page 51: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Checkerboarding Artifact

Source: https://distill.pub/2016/deconv-checkerboard/

Page 52: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Resize Convolutions

Source: https://distill.pub/2016/deconv-checkerboard/

Page 53: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Better Weight Initialization

● Train unsupervised with unlabeled inputs

● Throw away decoder network

● Start training with labeled data!

Page 54: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

“Explainable” AI

What do these weights mean? What if we do a mis-prediction?

Page 55: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

“Explainable” AI

Vary each of the features in the code vector and see what the output looks like!

Page 56: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

VAE Vector Arithmetic

Face x with moustac

he

Face x without moustache

moustacheFace x’ without moustache

+ moustache = Face x’ with moustache

Page 57: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Style Disentanglement

VAE

Zartist

Ztime

Zstyle

Mona lisa painted by Dali in 2019?

Cubist “Starry Night” in 1500?

Page 58: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Style Disentanglement

Page 59: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3

Interpolation of Latent Vectors - Music VAE

Page 60: CIS 522: Lecture 5R - Penn Engineeringcis522/slides/CIS522_Lecture5R.pdf · Feedback / Logistics Lecture Notes Puppy as option instead of kitten HW2 - Due tonight at midnight! HW3