Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS 179: Introduction to Graphical Models: Spring 2022

Homework 7

The submission for this homework should be a single PDF file containing all of the relevant code, figures, and any text explaining your results. When coding your answers, try to write functions to encapsulate and reuse code, instead of copying and pasting the same code multiple times. This will not only reduce your programming efforts, but also make it easier for us to understand and give credit for your work. Show and explain the reasoning behind your work!

In this homework, we will run a simple variational auto-encoder (VAE) model and explore its resulting representation.

To simplify the effort for this homework, a template containing much of the required code for the assignment is provided in a Jupyter            file.

Part 1: Build the VAE & load data   (30 points)

First, download the template Jupyter notebook and look over the provided code. You can run the code locally or on Google Colab, as you prefer. The VAE is defined by two quantities. First, an encoder defines the variational distribution q(ZlX = x), which we express as a Gaussian distribution,

Z o ~lZ ; u(x), y(x)I                               (u, log y) = W2 . a(W1 . x )

where a(_) is a ReLU activation function and . is the usual matrix-vector dot product. In other words, (u, log y) are expressed by a two-layer neural network with ReLU activation on the hidden layer, and linear activation on the output.

The decoder defines p(X lZ = z), which we express as a Gaussian distribution,

X o ~lX ; (z), /                                 (z) = V2 . a(V1 . z ) )

where (_) is the logistic function, so that X is modeled as a two-layer neural network transformation of z, plus a fixed amount of Gaussian noise on the pixel values.

The loss is given by the divergence between (X )q(ZlX) and p(Z )p(X lZ), where we assume p(Z ) is a basic unit Gaussian, which we estimate by sampling from q(ZlX). Given samples {(x( )i , z( )i )}, our estimated loss is

 l(z( )i ) _ x ( )i l2 +  ldet y(x( )i ) + lu(x( )i )l2 _ log det y(x( )i ) _ 1

Data   Our data consist of a small sample of hand images (mine, but inspired by Josh Tenenbaum’s IsoMap experiment), located at

https://sli.ics.uci.edu/extras/cs179/data/frames.txt

Load the data, then shift and scale the values to be between zero and one:


Part 2: Train the model   (30 points)

You have also been provided with a function             which computes the gradient of a mini-batch of data and updates the parameter values. Use this function to train your model:

1    vae  =  VAE()

This may be a bit slow. The            function also calls another provided function,                         , which plots

a scatter of images (preventing any overlapping images), so you can visualize the two-dimensional latent space Z which is being used to capture the variability in the images.

Part 3: Visualize reconstructions   (30 points)

Note: if you are short on time, or want to compare your results, you can obtain my trained VAE at

https://sli.ics.uci.edu/extras/cs179/data/vae.pkl

and load using             ,

1    with  open( 'vae .pkl ' , ' rb ' )  as  fh :  vae  =  pickle .load(fh)

However, if you have trained your own in part (2), please use yours for this question as well.

(a)  Select 6 random images from the data set. Encode and then decode each image, and show ( imshow ) the original image and reconstructed image, (u( x )). (Note: these will not look that great; the reconstruction network may be too simple to do a good job, or perhaps we just don’t have enough data in this small data set. But they should be recognizable.)

(b) Now select 10 points in a linear path across the distribution (say, from z = (_3, 0) to z = (3, 0). For each latent location, decode (z). Interpret the resulting sequence of images.

Part 4: Work on your projects!  (10 points)

Good luck with your projects, and your finals for any other classes!