<s> import numpy as np <EOL> import sparse_autoencoder <EOL> import gradient <EOL> import scipy . io <EOL> import display_network <EOL> import scipy . optimize <EOL> import cPickle <EOL> image_channels = <NUM_LIT:3> <EOL> patch_dim = <NUM_LIT:8> <EOL> num_patches = <NUM_LIT> <EOL> visible_size = patch_dim * patch_dim * image_channels <EOL> output_size = visible_size <EOL> hidden_size = <NUM_LIT> <EOL> sparsity_param = <NUM_LIT> <EOL> lambda_ = <NUM_LIT> <EOL> beta = <NUM_LIT:5> <EOL> epsilon = <NUM_LIT:0.1> <EOL> debug_hidden_size = <NUM_LIT:5> <EOL> debug_visible_size = <NUM_LIT:8> <EOL> patches = np . random . rand ( <NUM_LIT:8> , <NUM_LIT:10> ) <EOL> theta = sparse_autoencoder . initialize ( debug_hidden_size , debug_visible_size ) <EOL> cost , grad = sparse_autoencoder . sparse_autoencoder_linear_cost ( theta , debug_visible_size , debug_hidden_size , <EOL> lambda_ , sparsity_param , beta , patches ) <EOL> J = lambda x : sparse_autoencoder . sparse_autoencoder_linear_cost ( x , debug_visible_size , debug_hidden_size , <EOL> lambda_ , sparsity_param , beta , patches ) <EOL> num_grad = gradient . compute_gradient ( J , theta ) <EOL> print grad , num_grad <EOL> diff = np . linalg . norm ( num_grad - grad ) / np . linalg . norm ( num_grad + grad ) <EOL> print diff <EOL> print "<STR_LIT>" <EOL> patches = scipy . io . loadmat ( '<STR_LIT>' ) [ '<STR_LIT>' ] <EOL> display_network . display_color_network ( patches [ : , <NUM_LIT:0> : <NUM_LIT:100> ] , filename = '<STR_LIT>' ) <EOL> patch_mean = np . mean ( patches , <NUM_LIT:1> ) <EOL> patches = patches - np . tile ( patch_mean , ( patches . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> ) ) . transpose ( ) <EOL> sigma = patches . dot ( patches . transpose ( ) ) / patches . shape [ <NUM_LIT:1> ] <EOL> ( u , s , v ) = np . linalg . svd ( sigma ) <EOL> zca_white = u . dot ( np . diag ( <NUM_LIT:1> / ( s + epsilon ) ) ) . dot ( u . transpose ( ) ) <EOL> patches_zca = zca_white . dot ( patches ) <EOL> display_network . display_color_network ( patches_zca [ : , <NUM_LIT:0> : <NUM_LIT:100> ] , filename = '<STR_LIT>' ) <EOL> theta = sparse_autoencoder . initialize ( hidden_size , visible_size ) <EOL> options_ = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : True } <EOL> J = lambda x : sparse_autoencoder . sparse_autoencoder_linear_cost ( x , visible_size , hidden_size , <EOL> lambda_ , sparsity_param , beta , patches_zca ) <EOL> result = scipy . optimize . minimize ( J , theta , method = '<STR_LIT>' , jac = True , options = options_ ) <EOL> opt_theta = result . x <EOL> print result <EOL> print ( '<STR_LIT>' ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:wb>' ) as f : <EOL> cPickle . dump ( opt_theta , f ) <EOL> cPickle . dump ( zca_white , f ) <EOL> cPickle . dump ( patch_mean , f ) <EOL> print ( '<STR_LIT>' ) <EOL> W = opt_theta [ <NUM_LIT:0> : hidden_size * visible_size ] . reshape ( hidden_size , visible_size ) <EOL> b = opt_theta [ <NUM_LIT:2> * hidden_size * visible_size : <NUM_LIT:2> * hidden_size * visible_size + hidden_size ] <EOL> display_network . display_color_network ( W . dot ( zca_white ) . transpose ( ) , '<STR_LIT>' ) </s>
