Homework 2
This repository cotains:
- utils/display_images.py: subroutine for displaying images
- cifar10.py: subroutines for loading and downloading cifar10 data
- omp.py: STARTER CODE for OMP whitening subroutine
- dictlearn.py: STARTER CODE for dictlearn subroutine
Instructions
1. Complete zca.py:
zca.py
contains the following subroutine
def zca_white(x):
""" perform zca whitening
inputs:
x: numpy array of images
outputs:
y: numpy array of whitened images
"""
# *** put code for zca whitening here ***
return y
which need to completed using only the numpy
package.
No other packages should be used
2. Complete ica.py
ica.py
includes the following subroutines that need to be completed. The first
subroutine sample
should sample patches from images.
def sample(x, patch_size=16, num_patches=10):
''' randomly sample patches from x
inputs:
x: numpy array of images
patch_size: patch dims for patch_size x patch_size images
(default 16)
num_patches: number of patches to sample (default 10)
outputs:
y: numpy array of patches
'''
return y
The second subroutine ica
should perform gradient descent with the backtracking
line search to adapted the learning ratefor the ICA objective function.
def ica(x, **args):
''' perform independent component analysis (ICA)
inputs:
x: numpy array of images
args:
lr: learning rate (default 1e-3)
nsteps: maximum iterations (default 1000)
k: number of latent variables (defualt 20)
returns:
L: numpy array of loss function value for all iterations
W: numpy array of ICA basis vectors
'''
# default parameters
if not len(args):
args['lr'] = 1
args['nsteps'] = 200
args['k'] = 64
lr=args['lr']
nsteps = args['nsteps']
k = args['k']
# ***initialize variables here***
'''training loop using graident descent'''
for step in range(nsteps):
# ***insert gradient descent code here***
''' use backtracking line search '''
# print loss
print('step: {} / {}, L: {}'.format(step, nsteps, L[step]))
return L, W
ica
and sample
need to completed only the following packages:
numpy
-
scipy.linalg
No other packages should be used
Parameters
ica.py
also provides a sample main that loads cifar10 using cifar10.py
,
whitens the images using zca.py
, performs ICA using the ica(x,**args)
, and displays
and displays the learned basis images W
.
Note that values of parameters such as the learning rate lr
, number of basis
images k
, and number of optimization steps nsteps
may need to be changed
Example
The following image show an example result of applying ICA to whitened cifar10 data