In [1]:
# this part imports libs and load data from csv file
import csv
from dateutil import parser
from datetime import timedelta
from sklearn import svm
import numpy as np
import pandas as pd
import pdb
import pickle
from sklearn.cross_validation import train_test_split
from sklearn import preprocessing
import sklearn
import scipy.stats as ss
import cPickle
import gzip
import os
import sys
import time
import numpy
import theano
import theano.tensor as T
from theano.tensor.shared_randomstreams import RandomStreams
from DL_libs import *
%pylab inline


Populating the interactive namespace from numpy and matplotlib
C:\Users\u\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\lib\_util.py:35: DeprecationWarning: Module scipy.linalg.blas.fblas is deprecated, use scipy.linalg.blas instead
  DeprecationWarning)
C:\Users\u\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\lib\_util.py:35: DeprecationWarning: Module scipy.linalg.blas.fblas is deprecated, use scipy.linalg.blas instead
  DeprecationWarning)
C:\Users\u\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\lib\_util.py:35: DeprecationWarning: Module scipy.linalg.blas.fblas is deprecated, use scipy.linalg.blas instead
  DeprecationWarning)
C:\Users\u\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\lib\_util.py:35: DeprecationWarning: Module scipy.linalg.blas.fblas is deprecated, use scipy.linalg.blas instead
  DeprecationWarning)
C:\Users\u\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\lib\_util.py:35: DeprecationWarning: Module scipy.linalg.blas.fblas is deprecated, use scipy.linalg.blas instead
  DeprecationWarning)
C:\Users\u\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\lib\_util.py:35: DeprecationWarning: Module scipy.linalg.blas.fblas is deprecated, use scipy.linalg.blas instead
  DeprecationWarning)
C:\Users\u\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\lib\_util.py:35: DeprecationWarning: Module scipy.linalg.blas.fblas is deprecated, use scipy.linalg.blas instead
  DeprecationWarning)
C:\Users\u\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\lib\_util.py:35: DeprecationWarning: Module scipy.linalg.blas.fblas is deprecated, use scipy.linalg.blas instead
  DeprecationWarning)

In [4]:
f = gzip.open('mnist.pkl.gz', 'rb')
train_set, valid_set, test_set = cPickle.load(f)
X_train,y_train = train_set

X_valid,y_valid = valid_set
X_total=np.vstack((X_train, X_valid))
X_total = np.array(X_total, dtype= theano.config.floatX)
print'sample size', X_total.shape
y_total = np.concatenate([y_train, y_valid])
#print y_total.shape
array_A =[]
array_B =[]
for i in range(100000):
    array_A.append(np.random.random_integers(0, 59999))
    array_B.append(np.random.random_integers(0, 59999))
pos_index = []
neg_index = []
for index in xrange(100000):
    if y_total[array_A[index]] - y_total[array_B[index]] == 1:
        pos_index.append(index)
    else:
        neg_index.append(index)
print 'number of positive examples', len(pos_index)
selected_neg_index= neg_index[ : len(pos_index)]


sample size (60000L, 784L)
number of positive examples 8982

In [5]:
# example input
from utils import tile_raster_images

import PIL.Image

image = PIL.Image.fromarray(tile_raster_images(X = X_train[:100,:],
             img_shape=(28, 28), tile_shape=(10, 10),
             tile_spacing=(1, 1)))
image.show()
pylab.imshow(X_train[0].reshape(28, 28), cmap="Greys")


Out[5]:
<matplotlib.image.AxesImage at 0x10feda58>

In [6]:
import pandas as pd
array_A = np.array(array_A)
array_B = np.array(array_B)
index_for_positive_image_A = array_A[pos_index]
index_for_positive_image_B = array_B[pos_index]
index_for_neg_image_A = array_A[selected_neg_index]
index_for_neg_image_B = array_B[selected_neg_index]

X_pos_A = X_total[index_for_positive_image_A]
X_pos_B = X_total[index_for_positive_image_B]
X_pos_whole = np.hstack((X_pos_A,X_pos_B))
X_neg_A = X_total[index_for_neg_image_A]
X_neg_B = X_total[index_for_neg_image_B]
X_neg_whole = np.hstack((X_neg_A, X_neg_B))
print X_pos_A.shape,  X_pos_B.shape, X_pos_whole.shape
print X_neg_A.shape,  X_neg_B.shape, X_neg_whole.shape

X_whole = np.vstack((X_pos_whole, X_neg_whole))
print X_whole.shape
y_pos = np.ones(X_pos_whole.shape[0])
y_neg = np.zeros(X_neg_whole.shape[0])
y_whole = np.concatenate([y_pos,y_neg])
print y_whole


(8982L, 784L) (8982L, 784L) (8982L, 1568L)
(8982L, 784L) (8982L, 784L) (8982L, 1568L)
(17964L, 1568L)
[ 1.  1.  1. ...,  0.  0.  0.]

In [7]:
x_train_pre_validation_minmax, x_test_minmax, y_train_pre_validation_minmax, y_test_minmax = train_test_split(X_whole,y_whole,\
                                                            test_size=0.2, random_state=211)
x_train_minmax, x_validation_minmax, y_train_minmax, y_validation_minmax = train_test_split(x_train_pre_validation_minmax,
                                                                                            y_train_pre_validation_minmax,\
                                                            test_size=0.2, random_state=21)
print x_train_minmax.shape, y_train_minmax.shape, x_validation_minmax.shape, \
y_validation_minmax.shape, x_test_minmax.shape, y_test_minmax.shape


(11496L, 1568L) (11496L,) (2875L, 1568L) (2875L,) (3593L, 1568L) (3593L,)

In [40]:
print "This is a example of the two digit to check whether they are a same digit:"
print "The label is %d" % y_train_minmax[0]
pylab.imshow(x_train_minmax[0, :784].reshape(28, 28), cmap="Greys")


This is a example of the two digit to check whether they are a same digit:
The label is 0
Out[40]:
<matplotlib.image.AxesImage at 0x6dfdc90>

In [37]:
pylab.imshow(x_train_minmax[0, 784:].reshape(28, 28), cmap="Greys")


Out[37]:
<matplotlib.image.AxesImage at 0x6c61650>

In [8]:
# this is a use case.
pretraining_epochs=1
pretrain_lr=0.001
batch_size=30
hidden_layers_sizes =[100, 100]
corruption_levels=[0, 0]
x =x_train_minmax
print "original shape", x.shape
a_MAE = train_a_MultipleAEs(x, pretraining_epochs=pretraining_epochs, pretrain_lr=pretrain_lr, batch_size=batch_size, 
                        hidden_layers_sizes =hidden_layers_sizes, corruption_levels=corruption_levels)
print a_MAE.transform(x).shape


original shape (12904, 1568)
... building the model
... getting the pretraining functions
... pre-training the model
Pre-training layer 0, epoch 0, cost  648.42200876
Pre-training layer 1, epoch 0, cost  70.5771415353
(12904, 100)

In [16]:
new_x_train_minmax = a_MAE.transform(x_train_minmax)
new_x_test_minmax = a_MAE.transform(x_test_minmax)

In [ ]:
import numpy as np
from scipy import linalg
from sklearn.datasets import make_sparse_spd_matrix
from sklearn.covariance import GraphLassoCV, ledoit_wolf
import matplotlib.pyplot as plt
X = x_train_minmax
n_samples, n_features = x_train_minmax.shape
X -= X.mean(axis=0)
X /= X.std(axis=0)

##############################################################################
# Estimate the covariance
emp_cov = np.dot(X.T, X) / n_samples

model = GraphLassoCV()
model.fit(X)
cov_ = model.covariance_
prec_ = model.precision_

lw_cov_, _ = ledoit_wolf(X)
lw_prec_ = linalg.inv(lw_cov_)

##############################################################################
# Plot the results
plt.figure(figsize=(10, 6))
plt.subplots_adjust(left=0.02, right=0.98)

# plot the covariances
covs = [('Empirical', emp_cov), ('Ledoit-Wolf', lw_cov_),
        ('GraphLasso', cov_), ('True', cov)]
vmax = cov_.max()
for i, (name, this_cov) in enumerate(covs):
    plt.subplot(2, 4, i + 1)
    plt.imshow(this_cov, interpolation='nearest', vmin=-vmax, vmax=vmax,
               cmap=plt.cm.RdBu_r)
    plt.xticks(())
    plt.yticks(())
    plt.title('%s covariance' % name)

In [ ]:
from numpy import corrcoef, sum, log, arange
from numpy.random import rand
from pylab import pcolor, show, colorbar, xticks, yticks
import sklearn.covariance 
X = x_train_minmax
n_samples, n_features = x_train_minmax.shape
X -= X.mean(axis=0)
X /= X.std(axis=0)
R = sklearn.covariance.empirical_covariance(X , assume_centered=False)
pcolor(R)
colorbar()
#yticks(arange(0.5,10.5),range(0,10))
#xticks(arange(0.5,10.5),range(0,10))
show()

In [8]:
import numpy
numpy.version.full_version


Out[8]:
'1.8.2'

In [18]:
pylab.imshow(R, cmap="Greys")


Out[18]:
(12611, 12611)

In [8]:
# get the new representation for A set. first 784-D
pretraining_epochs=15
pretrain_lr=0.001
batch_size=30
hidden_layers_sizes =[100, 100]
corruption_levels=[0, 0]
x = x_train_minmax[:, :x_train_minmax.shape[1]/2]
print "original shape", x.shape
a_MAE_A = train_a_MultipleAEs(x, pretraining_epochs=pretraining_epochs, pretrain_lr=pretrain_lr, batch_size=batch_size, 
                        hidden_layers_sizes =hidden_layers_sizes, corruption_levels=corruption_levels)
new_x_train_minmax_A =  a_MAE_A.transform(x_train_minmax[:, :x_train_minmax.shape[1]/2])


original shape (11496L, 784L)
... building the model
... getting the pretraining functions
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
... pre-training the model
Pre-training layer 0, epoch 0, cost  346.412293713
Pre-training layer 0, epoch 1, cost  223.633915136
Pre-training layer 0, epoch 2, cost  200.773793171
Pre-training layer 0, epoch 3, cost  187.685809111
Pre-training layer 0, epoch 4, cost  178.260302295
Pre-training layer 0, epoch 5, cost  170.923861029
Pre-training layer 0, epoch 6, cost  164.961292259
Pre-training layer 0, epoch 7, cost  160.073162105
Pre-training layer 0, epoch 8, cost  155.967431557
Pre-training layer 0, epoch 9, cost  152.442808308
Pre-training layer 0, epoch 10, cost  149.403436396
Pre-training layer 0, epoch 11, cost  146.745100358
Pre-training layer 0, epoch 12, cost  144.384362643
Pre-training layer 0, epoch 13, cost  142.288324298
Pre-training layer 0, epoch 14, cost  140.394283707
Pre-training layer 1, epoch 0, cost  68.8486104485
Pre-training layer 1, epoch 1, cost  59.0348931681
Pre-training layer 1, epoch 2, cost  57.283454055
Pre-training layer 1, epoch 3, cost  55.8174529949
Pre-training layer 1, epoch 4, cost  54.5755321895
Pre-training layer 1, epoch 5, cost  53.4465015961
Pre-training layer 1, epoch 6, cost  52.5028828338
Pre-training layer 1, epoch 7, cost  51.6316607877
Pre-training layer 1, epoch 8, cost  50.8494747929
Pre-training layer 1, epoch 9, cost  50.2181426242
Pre-training layer 1, epoch 10, cost  49.6159746138
Pre-training layer 1, epoch 11, cost  49.0762398087
Pre-training layer 1, epoch 12, cost  48.6404534787
Pre-training layer 1, epoch 13, cost  48.2142917062
Pre-training layer 1, epoch 14, cost  47.8374454039
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.

In [9]:
# get the new representation for B set. first 784-D
pretraining_epochs=15
pretrain_lr=0.001
batch_size=30
hidden_layers_sizes =[100, 100]
corruption_levels=[0, 0]
x = x_train_minmax[:, x_train_minmax.shape[1]/2:]
print "original shape", x.shape
a_MAE_B = train_a_MultipleAEs(x, pretraining_epochs=pretraining_epochs, pretrain_lr=pretrain_lr, batch_size=batch_size, 
                        hidden_layers_sizes =hidden_layers_sizes, corruption_levels=corruption_levels)
new_x_train_minmax_B =  a_MAE_B.transform(x_train_minmax[:, x_train_minmax.shape[1]/2:])


original shape (11496L, 784L)
... building the model
... getting the pretraining functions
... pre-training the model
Pre-training layer 0, epoch 0, cost  349.129511531
Pre-training layer 0, epoch 1, cost  226.94319863
Pre-training layer 0, epoch 2, cost  203.627585707
Pre-training layer 0, epoch 3, cost  190.220203221
Pre-training layer 0, epoch 4, cost  180.50820274
Pre-training layer 0, epoch 5, cost  172.980472013
Pre-training layer 0, epoch 6, cost  166.926397778
Pre-training layer 0, epoch 7, cost  161.933313192
Pre-training layer 0, epoch 8, cost  157.751923269
Pre-training layer 0, epoch 9, cost  154.167129443
Pre-training layer 0, epoch 10, cost  151.114810011
Pre-training layer 0, epoch 11, cost  148.405242745
Pre-training layer 0, epoch 12, cost  146.026729473
Pre-training layer 0, epoch 13, cost  143.923327549
Pre-training layer 0, epoch 14, cost  141.990527125
Pre-training layer 1, epoch 0, cost  68.4416070958
Pre-training layer 1, epoch 1, cost  58.4317386106
Pre-training layer 1, epoch 2, cost  56.6261931233
Pre-training layer 1, epoch 3, cost  55.1918037926
Pre-training layer 1, epoch 4, cost  53.8913107351
Pre-training layer 1, epoch 5, cost  52.7589751516
Pre-training layer 1, epoch 6, cost  51.7341569201
Pre-training layer 1, epoch 7, cost  50.8811003109
Pre-training layer 1, epoch 8, cost  50.0645828707
Pre-training layer 1, epoch 9, cost  49.4361171504
Pre-training layer 1, epoch 10, cost  48.8376318898
Pre-training layer 1, epoch 11, cost  48.2815674303
Pre-training layer 1, epoch 12, cost  47.8472076637
Pre-training layer 1, epoch 13, cost  47.4090705925
Pre-training layer 1, epoch 14, cost  47.0423730837

In [10]:
new_x_test_minmax_A = a_MAE_A.transform(x_test_minmax[:, :x_test_minmax.shape[1]/2])
new_x_test_minmax_B = a_MAE_B.transform(x_test_minmax[:, x_test_minmax.shape[1]/2:])
new_x_validation_minmax_A = a_MAE_A.transform(x_validation_minmax[:, :x_validation_minmax.shape[1]/2])
new_x_validation_minmax_B = a_MAE_B.transform(x_validation_minmax[:, x_validation_minmax.shape[1]/2:])
new_x_train_minmax_whole = np.hstack((new_x_train_minmax_A, new_x_train_minmax_B))
new_x_test_minmax_whole = np.hstack((new_x_test_minmax_A, new_x_test_minmax_B))
new_x_validationt_minmax_whole = np.hstack((new_x_validation_minmax_A, new_x_validation_minmax_B))

In [13]:
new_x_train_minmax_whole.shape


Out[13]:
(11496L, 200L)

In [11]:
#### L1-based SVM original dataset
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.linear_model import RidgeClassifier
from sklearn.svm import LinearSVC, SVC
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import Perceptron
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.naive_bayes import BernoulliNB, MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import NearestCentroid
from sklearn.utils.extmath import density
from sklearn import metrics
x_train_pre_validation_scaled = x_train_minmax
X_test_scaled = x_test_minmax
print 'SVM result on original dataset'
L1_SVC_Selector= LinearSVC(C=1, penalty="l2", dual=False)
L1_SVC_X = L1_SVC_Selector.fit_transform(x_train_pre_validation_scaled, y_train_minmax)
traning_accuracy = L1_SVC_Selector.score(x_train_pre_validation_scaled, y_train_minmax)
print 'training accuracy', traning_accuracy
testing_accuracy = L1_SVC_Selector.score(X_test_scaled, y_test_minmax)
print 'testing accuracy', testing_accuracy
print L1_SVC_X.shape
predicted = L1_SVC_Selector.predict(X_test_scaled)
print 'testing precision', sklearn.metrics.precision_score(y_test_minmax, predicted, pos_label=1)
print 'testing recall', sklearn.metrics.recall_score(y_test_minmax, predicted, pos_label=1)
print sklearn.metrics.recall_score(y_test_minmax, predicted, pos_label=1)


SVM result on original dataset
training accuracy 0.663361169102
testing accuracy 0.571388811578
(11496L, 478L)
testing precision 0.57358490566
testing recall 0.586872586873
0.586872586873

In [45]:
L1_SVC_POLY_Selector= SVC(kernel='poly').fit(x_train_pre_validation_scaled, y_train_minmax)
print L1_SVC_POLY_Selector.score(x_train_pre_validation_scaled, y_train_minmax)
print L1_SVC_POLY_Selector.score(X_test_scaled, y_test_minmax)
predicted = L1_SVC_POLY_Selector.predict(X_test_scaled)
print sklearn.metrics.precision_score(y_test_minmax, predicted, pos_label=1)
print sklearn.metrics.recall_score(y_test_minmax, predicted, pos_label=1)


0.500077351485
0.50198019802
0.0
0.0

In [46]:
#### RBF SVM original dataset
L1_SVC_RBF_Selector= SVC(kernel='rbf').fit(x_train_pre_validation_scaled, y_train_minmax)
print L1_SVC_RBF_Selector.score(x_train_pre_validation_scaled, y_train_minmax)
print L1_SVC_RBF_Selector.score(X_test_scaled, y_test_minmax)
predicted = L1_SVC_RBF_Selector.predict(X_test_scaled)
print sklearn.metrics.precision_score(y_test_minmax, predicted, pos_label=1)
print sklearn.metrics.recall_score(y_test_minmax, predicted, pos_label=1)


0.645266089109
0.599504950495
0.594985535198
0.613320079523

In [14]:
#### L1-based SVM on trasformed original dataset
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.linear_model import RidgeClassifier
from sklearn.svm import LinearSVC
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import Perceptron
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.naive_bayes import BernoulliNB, MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import NearestCentroid
from sklearn.utils.extmath import density
from sklearn import metrics
x_train_pre_validation_scaled = new_x_train_minmax
X_test_scaled = new_x_test_minmax

L1_SVC_Selector= LinearSVC(C=1, penalty="l1", dual=False)
L1_SVC_X = L1_SVC_Selector.fit_transform(x_train_pre_validation_scaled, y_train_minmax)
traning_accuracy = L1_SVC_Selector.score(x_train_pre_validation_scaled, y_train_minmax)
print 'training accuracy', traning_accuracy
testing_accuracy = L1_SVC_Selector.score(X_test_scaled, y_test_minmax)
print 'testing accuracy', testing_accuracy
print L1_SVC_X.shape
predicted = L1_SVC_Selector.predict(X_test_scaled)
print 'testing precision', sklearn.metrics.precision_score(y_test_minmax, predicted, pos_label=1)
print 'testing recall', sklearn.metrics.recall_score(y_test_minmax, predicted, pos_label=1)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-14-efac07132f4f> in <module>()
     11 from sklearn.utils.extmath import density
     12 from sklearn import metrics
---> 13 x_train_pre_validation_scaled = new_x_train_minmax
     14 X_test_scaled = new_x_test_minmax
     15 

NameError: name 'new_x_train_minmax' is not defined

In [23]:
#### L1-based SVM on seperately trasformed original dataset
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.linear_model import RidgeClassifier
from sklearn.svm import LinearSVC
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import Perceptron
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.naive_bayes import BernoulliNB, MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import NearestCentroid
from sklearn.utils.extmath import density
from sklearn import metrics
x_train_pre_validation_scaled = new_x_train_minmax_whole
X_test_scaled = new_x_test_minmax_whole

L1_SVC_Selector= LinearSVC(C=1, penalty="l1", dual=False)
L1_SVC_X = L1_SVC_Selector.fit_transform(x_train_pre_validation_scaled, y_train_minmax)
traning_accuracy = L1_SVC_Selector.score(x_train_pre_validation_scaled, y_train_minmax)
print 'training accuracy', traning_accuracy
testing_accuracy = L1_SVC_Selector.score(X_test_scaled, y_test_minmax)
print 'testing accuracy', testing_accuracy
print L1_SVC_X.shape
predicted = L1_SVC_Selector.predict(X_test_scaled)
print 'testing precision', sklearn.metrics.precision_score(y_test_minmax, predicted, pos_label=1)
print 'testing recall', sklearn.metrics.recall_score(y_test_minmax, predicted, pos_label=1)


training accuracy 0.55670750864
testing accuracy 0.519095477387
(12732L, 179L)
testing precision 0.520963855422
testing recall 0.540229885057

In [16]:
# get the new representation for sperated vectors
pretraining_epochs=15
pretrain_lr=0.001
batch_size=30
hidden_layers_sizes =[100, 100]
corruption_levels=[0, 0]
x = new_x_train_minmax_whole
print "original shape", x.shape
a_MAE_new_set = train_a_MultipleAEs(x, pretraining_epochs=pretraining_epochs, pretrain_lr=pretrain_lr, batch_size=batch_size, 
                        hidden_layers_sizes =hidden_layers_sizes, corruption_levels=corruption_levels)
new_x_train_minmax_new_set =  a_MAE_new_set.transform(new_x_train_minmax_whole)
new_x_test_minmax_new_set = a_MAE_new_set.transform(new_x_test_minmax_whole)


original shape (11496L, 200L)
... building the model
... getting the pretraining functions
... pre-training the model
Pre-training layer 0, epoch 0, cost  118.177096162
Pre-training layer 0, epoch 1, cost  97.7526594947
Pre-training layer 0, epoch 2, cost  95.8107967655
Pre-training layer 0, epoch 3, cost  94.3870854405
Pre-training layer 0, epoch 4, cost  93.0674232211
Pre-training layer 0, epoch 5, cost  91.8169773198
Pre-training layer 0, epoch 6, cost  90.6762560709
Pre-training layer 0, epoch 7, cost  89.5957194412
Pre-training layer 0, epoch 8, cost  88.6158297961
Pre-training layer 0, epoch 9, cost  87.7273767409
Pre-training layer 0, epoch 10, cost  86.9481322481
Pre-training layer 0, epoch 11, cost  86.1908101443
Pre-training layer 0, epoch 12, cost  85.5006395465
Pre-training layer 0, epoch 13, cost  84.8962040084
Pre-training layer 0, epoch 14, cost  84.3446187521
Pre-training layer 1, epoch 0, cost  69.0075137848
Pre-training layer 1, epoch 1, cost  57.8697038244
Pre-training layer 1, epoch 2, cost  56.7365472834
Pre-training layer 1, epoch 3, cost  55.929914859
Pre-training layer 1, epoch 4, cost  55.2160968987
Pre-training layer 1, epoch 5, cost  54.5584755942
Pre-training layer 1, epoch 6, cost  53.9750626535
Pre-training layer 1, epoch 7, cost  53.4056634012
Pre-training layer 1, epoch 8, cost  52.8792666753
Pre-training layer 1, epoch 9, cost  52.4516524239
Pre-training layer 1, epoch 10, cost  52.0314312994
Pre-training layer 1, epoch 11, cost  51.6219030442
Pre-training layer 1, epoch 12, cost  51.2731153826
Pre-training layer 1, epoch 13, cost  50.9333671794
Pre-training layer 1, epoch 14, cost  50.6723605118

In [18]:
#### L1-based SVM on seperately trasformed original dataset
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.linear_model import RidgeClassifier
from sklearn.svm import LinearSVC
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import Perceptron
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.naive_bayes import BernoulliNB, MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import NearestCentroid
from sklearn.utils.extmath import density
from sklearn import metrics
x_train_pre_validation_scaled = new_x_train_minmax_new_set
X_test_scaled = new_x_test_minmax_new_set

L1_SVC_Selector= LinearSVC(C=1, penalty="l1", dual=False)
L1_SVC_X = L1_SVC_Selector.fit_transform(x_train_pre_validation_scaled, y_train_minmax)
traning_accuracy = L1_SVC_Selector.score(x_train_pre_validation_scaled, y_train_minmax)
print 'training accuracy', traning_accuracy
testing_accuracy = L1_SVC_Selector.score(X_test_scaled, y_test_minmax)
print 'testing accuracy', testing_accuracy
print L1_SVC_X.shape
predicted = L1_SVC_Selector.predict(X_test_scaled)
print 'testing precision', sklearn.metrics.precision_score(y_test_minmax, predicted, pos_label=1)
print 'testing recall', sklearn.metrics.recall_score(y_test_minmax, predicted, pos_label=1)


training accuracy 0.680149617258
testing accuracy 0.686334539382
(11496L, 96L)
testing precision 0.688668866887
testing recall 0.69056811914

In [17]:
# feature log selection based on L2
log_clf_l2 = sklearn.linear_model.LogisticRegression(C=1, penalty='l2' )
log_clf_l2.fit(x_train_pre_validation_scaled, y_train_pre_validation)
predicted = log_clf_l2.predict(X_test_scaled)
print log_clf_l2.score(x_train_pre_validation_scaled, y_train_pre_validation)
print log_clf_l2.score(X_test_scaled, y_test)
print 'precision', sklearn.metrics.precision_score(y_test, predicted, pos_label=1)
print 'recall', sklearn.metrics.recall_score(y_test, predicted, pos_label=1)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-17-8893d6e6ceee> in <module>()
      1 # feature log selection based on L2
      2 log_clf_l2 = sklearn.linear_model.LogisticRegression(C=1, penalty='l2' )
----> 3 log_clf_l2.fit(x_train_pre_validation_scaled, y_train_pre_validation)
      4 predicted = log_clf_l2.predict(X_test_scaled)
      5 print log_clf_l2.score(x_train_pre_validation_scaled, y_train_pre_validation)

NameError: name 'y_train_pre_validation' is not defined

In [ ]:


In [28]:
# feature log selection based on L2 on original dataset
X_test = x_test_minmax
y_test = y_test_minmax
X_train = x_train_minmax
y_train = y_train_minmax
print 'performance for logisitc regression'
log_clf = sklearn.linear_model.LogisticRegression(C=1, penalty='l2' )
log_clf.fit(X_train, y_train)
predicted = log_clf.predict(X_test)
print 'training accuracy', log_clf.score(X_train, y_train)
print 'testing accuracy', log_clf.score(X_test, y_test)
print 'precision', sklearn.metrics.precision_score(y_test, predicted, pos_label=1)
print 'recall', sklearn.metrics.recall_score(y_test, predicted, pos_label=1)


performance for logisitc regression
training accuracy 0.619776939994
testing accuracy 0.507286432161
precision 0.509832841691
recall 0.51824087956

In [29]:
# feature log selection based on L2 on transformed original
X_test = new_x_test_minmax
y_test = y_test_minmax
X_train = new_x_train_minmax
y_train = y_train_minmax
print 'performance for logisitc regression'
log_clf = sklearn.linear_model.LogisticRegression(C=1, penalty='l2' )
log_clf.fit(X_train, y_train)
predicted = log_clf.predict(X_test)
print 'training accuracy', log_clf.score(X_train, y_train)
print 'testing accuracy', log_clf.score(X_test, y_test)
print 'precision', sklearn.metrics.precision_score(y_test, predicted, pos_label=1)
print 'recall', sklearn.metrics.recall_score(y_test, predicted, pos_label=1)


performance for logisitc regression
training accuracy 0.665724159598
testing accuracy 0.665075376884
precision 0.661665053243
recall 0.68315842079

In [30]:
# feature log selection based on L2 on seperately transformed dataset
X_test = new_x_test_minmax_new_set
y_test = y_test_minmax
X_train = new_x_train_minmax_new_set
y_train = y_train_minmax
print 'performance for logisitc regression'
log_clf = sklearn.linear_model.LogisticRegression(C=1, penalty='l2' )
log_clf.fit(X_train, y_train)
predicted = log_clf.predict(X_test)
print 'training accuracy', log_clf.score(X_train, y_train)
print 'testing accuracy', log_clf.score(X_test, y_test)
print 'precision', sklearn.metrics.precision_score(y_test, predicted, pos_label=1)
print 'recall', sklearn.metrics.recall_score(y_test, predicted, pos_label=1)


performance for logisitc regression
training accuracy 0.72808671065
testing accuracy 0.733417085427
precision 0.727492739593
recall 0.751124437781

In [8]:
# build a sda, pretraining plus fine tuning on original data.
# set up paramaters
from DL_libs import *

#finetune_lr=0.1
finetune_lr = 0.1
pretraining_epochs = 30
#pretrain_lr=0.001
pretrain_lr = 0.001
training_epochs = 300
batch_size = 30


hidden_layers_sizes= [100, 100]
corruption_levels = [0, 0]

sda = trainSda(x_train_minmax, y_train_minmax,
             x_validation_minmax, y_validation_minmax , 
             x_test_minmax, y_test_minmax,
             hidden_layers_sizes = hidden_layers_sizes, corruption_levels = corruption_levels, batch_size = batch_size , \
             training_epochs = training_epochs, pretraining_epochs = pretraining_epochs, 
             pretrain_lr = pretrain_lr, finetune_lr=finetune_lr
             )


... building the model
... getting the pretraining functions
... pre-training the model
Pre-training layer 0, epoch 0, cost  647.922785805
Pre-training layer 0, epoch 1, cost  459.647282329
Pre-training layer 0, epoch 2, cost  423.468142598
Pre-training layer 0, epoch 3, cost  400.399108653
Pre-training layer 0, epoch 4, cost  383.07459036
Pre-training layer 0, epoch 5, cost  369.358344102
Pre-training layer 0, epoch 6, cost  358.181938709
Pre-training layer 0, epoch 7, cost  348.88467612
Pre-training layer 0, epoch 8, cost  341.018234687
Pre-training layer 0, epoch 9, cost  334.262997635
Pre-training layer 0, epoch 10, cost  328.384889748
Pre-training layer 0, epoch 11, cost  323.209703586
Pre-training layer 0, epoch 12, cost  318.606135178
Pre-training layer 0, epoch 13, cost  314.473944912
Pre-training layer 0, epoch 14, cost  310.735538637
Pre-training layer 0, epoch 15, cost  307.32996513
Pre-training layer 0, epoch 16, cost  304.208641381
Pre-training layer 0, epoch 17, cost  301.332304955
Pre-training layer 0, epoch 18, cost  298.668828399
Pre-training layer 0, epoch 19, cost  296.191634689
Pre-training layer 0, epoch 20, cost  293.878531229
Pre-training layer 0, epoch 21, cost  291.710836811
Pre-training layer 0, epoch 22, cost  289.672715785
Pre-training layer 0, epoch 23, cost  287.750660896
Pre-training layer 0, epoch 24, cost  285.933084835
Pre-training layer 0, epoch 25, cost  284.209993109
Pre-training layer 0, epoch 26, cost  282.572719318
Pre-training layer 0, epoch 27, cost  281.013709474
Pre-training layer 0, epoch 28, cost  279.526345517
Pre-training layer 0, epoch 29, cost  278.104800501
Pre-training layer 1, epoch 0, cost  58.0747334321
Pre-training layer 1, epoch 1, cost  47.3869523522
Pre-training layer 1, epoch 2, cost  45.6167354622
Pre-training layer 1, epoch 3, cost  44.1142099382
Pre-training layer 1, epoch 4, cost  42.780782391
Pre-training layer 1, epoch 5, cost  41.605622552
Pre-training layer 1, epoch 6, cost  40.5720918767
Pre-training layer 1, epoch 7, cost  39.6616534643
Pre-training layer 1, epoch 8, cost  38.8568531552
Pre-training layer 1, epoch 9, cost  38.142348447
Pre-training layer 1, epoch 10, cost  37.5050541242
Pre-training layer 1, epoch 11, cost  36.9339620018
Pre-training layer 1, epoch 12, cost  36.4198631944
Pre-training layer 1, epoch 13, cost  35.9550605955
Pre-training layer 1, epoch 14, cost  35.5331053845
Pre-training layer 1, epoch 15, cost  35.1485703809
Pre-training layer 1, epoch 16, cost  34.7968631751
Pre-training layer 1, epoch 17, cost  34.4740761866
Pre-training layer 1, epoch 18, cost  34.1768676831
Pre-training layer 1, epoch 19, cost  33.9023670417
Pre-training layer 1, epoch 20, cost  33.6480984098
Pre-training layer 1, epoch 21, cost  33.4119183963
Pre-training layer 1, epoch 22, cost  33.1919647235
Pre-training layer 1, epoch 23, cost  32.9866136623
Pre-training layer 1, epoch 24, cost  32.7944446165
Pre-training layer 1, epoch 25, cost  32.614210567
Pre-training layer 1, epoch 26, cost  32.4448133265
Pre-training layer 1, epoch 27, cost  32.2852827466
Pre-training layer 1, epoch 28, cost  32.1347591762
Pre-training layer 1, epoch 29, cost  31.9924786036
... getting the finetuning functions
... finetunning the model
epoch 1, minibatch 420/420, validation error 36.126984 %
 epoch 1, minibatch 420/420, test error of best model 37.760814 %
epoch 2, minibatch 420/420, validation error 29.873016 %
 epoch 2, minibatch 420/420, test error of best model 31.424936 %
epoch 3, minibatch 420/420, validation error 26.317460 %
 epoch 3, minibatch 420/420, test error of best model 27.277354 %
epoch 4, minibatch 420/420, validation error 24.507937 %
 epoch 4, minibatch 420/420, test error of best model 24.783715 %
epoch 5, minibatch 420/420, validation error 23.015873 %
 epoch 5, minibatch 420/420, test error of best model 23.282443 %
epoch 6, minibatch 420/420, validation error 21.809524 %
 epoch 6, minibatch 420/420, test error of best model 21.984733 %
epoch 7, minibatch 420/420, validation error 20.888889 %
 epoch 7, minibatch 420/420, test error of best model 21.246819 %
epoch 8, minibatch 420/420, validation error 20.158730 %
 epoch 8, minibatch 420/420, test error of best model 20.687023 %
epoch 9, minibatch 420/420, validation error 19.587302 %
 epoch 9, minibatch 420/420, test error of best model 20.101781 %
epoch 10, minibatch 420/420, validation error 19.396825 %
 epoch 10, minibatch 420/420, test error of best model 19.669211 %
epoch 11, minibatch 420/420, validation error 19.047619 %
 epoch 11, minibatch 420/420, test error of best model 19.465649 %
epoch 12, minibatch 420/420, validation error 18.698413 %
 epoch 12, minibatch 420/420, test error of best model 19.134860 %
epoch 13, minibatch 420/420, validation error 18.412698 %
 epoch 13, minibatch 420/420, test error of best model 18.753181 %
epoch 14, minibatch 420/420, validation error 18.095238 %
 epoch 14, minibatch 420/420, test error of best model 18.524173 %
epoch 15, minibatch 420/420, validation error 17.650794 %
 epoch 15, minibatch 420/420, test error of best model 18.269720 %
epoch 16, minibatch 420/420, validation error 17.333333 %
 epoch 16, minibatch 420/420, test error of best model 18.269720 %
epoch 17, minibatch 420/420, validation error 17.238095 %
 epoch 17, minibatch 420/420, test error of best model 18.040712 %
epoch 18, minibatch 420/420, validation error 17.111111 %
 epoch 18, minibatch 420/420, test error of best model 17.709924 %
epoch 19, minibatch 420/420, validation error 16.825397 %
 epoch 19, minibatch 420/420, test error of best model 17.557252 %
epoch 20, minibatch 420/420, validation error 16.793651 %
 epoch 20, minibatch 420/420, test error of best model 17.251908 %
epoch 21, minibatch 420/420, validation error 16.571429 %
 epoch 21, minibatch 420/420, test error of best model 17.175573 %
epoch 22, minibatch 420/420, validation error 16.603175 %
epoch 23, minibatch 420/420, validation error 16.507937 %
 epoch 23, minibatch 420/420, test error of best model 16.844784 %
epoch 24, minibatch 420/420, validation error 16.412698 %
 epoch 24, minibatch 420/420, test error of best model 16.793893 %
epoch 25, minibatch 420/420, validation error 16.444444 %
epoch 26, minibatch 420/420, validation error 16.476190 %
epoch 27, minibatch 420/420, validation error 16.476190 %
epoch 28, minibatch 420/420, validation error 16.571429 %
epoch 29, minibatch 420/420, validation error 16.634921 %
epoch 30, minibatch 420/420, validation error 16.571429 %
epoch 31, minibatch 420/420, validation error 16.603175 %
epoch 32, minibatch 420/420, validation error 16.476190 %
epoch 33, minibatch 420/420, validation error 16.476190 %
epoch 34, minibatch 420/420, validation error 16.444444 %
epoch 35, minibatch 420/420, validation error 16.539683 %
epoch 36, minibatch 420/420, validation error 16.666667 %
epoch 37, minibatch 420/420, validation error 16.698413 %
epoch 38, minibatch 420/420, validation error 16.539683 %
epoch 39, minibatch 420/420, validation error 16.476190 %
epoch 40, minibatch 420/420, validation error 16.444444 %
epoch 41, minibatch 420/420, validation error 16.444444 %
epoch 42, minibatch 420/420, validation error 16.412698 %
 epoch 42, minibatch 420/420, test error of best model 16.692112 %
epoch 43, minibatch 420/420, validation error 16.317460 %
 epoch 43, minibatch 420/420, test error of best model 16.564885 %
epoch 44, minibatch 420/420, validation error 16.222222 %
 epoch 44, minibatch 420/420, test error of best model 16.615776 %
epoch 45, minibatch 420/420, validation error 16.158730 %
 epoch 45, minibatch 420/420, test error of best model 16.692112 %
epoch 46, minibatch 420/420, validation error 16.126984 %
 epoch 46, minibatch 420/420, test error of best model 16.743003 %
epoch 47, minibatch 420/420, validation error 16.063492 %
 epoch 47, minibatch 420/420, test error of best model 16.793893 %
epoch 48, minibatch 420/420, validation error 16.031746 %
 epoch 48, minibatch 420/420, test error of best model 16.692112 %
epoch 49, minibatch 420/420, validation error 16.126984 %
epoch 50, minibatch 420/420, validation error 16.126984 %
epoch 51, minibatch 420/420, validation error 16.158730 %
epoch 52, minibatch 420/420, validation error 15.968254 %
 epoch 52, minibatch 420/420, test error of best model 16.666667 %
epoch 53, minibatch 420/420, validation error 16.000000 %
epoch 54, minibatch 420/420, validation error 16.095238 %
epoch 55, minibatch 420/420, validation error 16.063492 %
epoch 56, minibatch 420/420, validation error 16.000000 %
epoch 57, minibatch 420/420, validation error 16.000000 %
epoch 58, minibatch 420/420, validation error 16.031746 %
epoch 59, minibatch 420/420, validation error 16.031746 %
epoch 60, minibatch 420/420, validation error 16.031746 %
epoch 61, minibatch 420/420, validation error 16.031746 %
epoch 62, minibatch 420/420, validation error 16.031746 %
epoch 63, minibatch 420/420, validation error 16.126984 %
epoch 64, minibatch 420/420, validation error 16.222222 %
epoch 65, minibatch 420/420, validation error 16.253968 %
epoch 66, minibatch 420/420, validation error 16.285714 %
epoch 67, minibatch 420/420, validation error 16.317460 %
epoch 68, minibatch 420/420, validation error 16.317460 %
epoch 69, minibatch 420/420, validation error 16.317460 %
epoch 70, minibatch 420/420, validation error 16.380952 %
epoch 71, minibatch 420/420, validation error 16.476190 %
epoch 72, minibatch 420/420, validation error 16.507937 %
epoch 73, minibatch 420/420, validation error 16.571429 %
epoch 74, minibatch 420/420, validation error 16.507937 %
epoch 75, minibatch 420/420, validation error 16.476190 %
epoch 76, minibatch 420/420, validation error 16.476190 %
epoch 77, minibatch 420/420, validation error 16.476190 %
epoch 78, minibatch 420/420, validation error 16.444444 %
epoch 79, minibatch 420/420, validation error 16.476190 %
epoch 80, minibatch 420/420, validation error 16.412698 %
epoch 81, minibatch 420/420, validation error 16.380952 %
epoch 82, minibatch 420/420, validation error 16.349206 %
epoch 83, minibatch 420/420, validation error 16.349206 %
epoch 84, minibatch 420/420, validation error 16.317460 %
epoch 85, minibatch 420/420, validation error 16.285714 %
epoch 86, minibatch 420/420, validation error 16.222222 %
epoch 87, minibatch 420/420, validation error 16.253968 %
The pretraining code ran for 3.55m

In [78]:
import DL_libs

In [9]:
print 'hidden_layers_sizes:', hidden_layers_sizes
print 'corruption_levels:', corruption_levels

training_predicted = sda.predict(x_train_minmax)
y_train = y_train_minmax
print 'train accuracy: ', '{percent:.1%}'.format(percent=sklearn.metrics.accuracy_score(y_train, training_predicted)) 
print 'precision: ', '{percent:.1%}'.format(percent=sklearn.metrics.precision_score(y_train, training_predicted, pos_label=1)) 
print 'recall: ', '{percent:.1%}'.format( percent= sklearn.metrics.recall_score(y_train, training_predicted, pos_label=1))


test_predicted = sda.predict(x_test_minmax)
y_test = y_test_minmax
print 'testing accuracy: ', '{percent:.1%}'.format(percent=sklearn.metrics.accuracy_score(y_test, test_predicted)) 
print 'precision: ', '{percent:.1%}'.format(percent=sklearn.metrics.precision_score(y_test, test_predicted, pos_label=1)) 
print 'recall: ', '{percent:.1%}'.format( percent= sklearn.metrics.recall_score(y_test, test_predicted, pos_label=1))


hidden_layers_sizes: [100, 100]
corruption_levels: [0, 0]
train accuracy:  100.0%
precision:  100.0%
recall:  100.0%
testing accuracy:  83.6%
precision:  84.1%
recall:  82.8%

In [19]:
# build a sda, pretraining plus fine tuning on seperately transformed data
# set up paramaters

#finetune_lr=0.1
finetune_lr = 0.1
pretraining_epochs = 30
#pretrain_lr=0.001
pretrain_lr = 0.001
training_epochs = 300
batch_size = 30


hidden_layers_sizes= [100, 100, 100, 100, 100, 100, 100]
corruption_levels = [0, 0,0,0,0,0,0]

sda_transformed = trainSda(new_x_train_minmax_whole, y_train_minmax,
             new_x_validationt_minmax_whole, y_validation_minmax , 
             new_x_test_minmax_whole, y_test_minmax,
             hidden_layers_sizes = hidden_layers_sizes, corruption_levels = corruption_levels, batch_size = batch_size , \
             training_epochs = training_epochs, pretraining_epochs = pretraining_epochs, 
             pretrain_lr = pretrain_lr, finetune_lr=finetune_lr
             )


... building the model
... getting the pretraining functions
... pre-training the model
Pre-training layer 0, epoch 0, cost  114.256938138
Pre-training layer 0, epoch 1, cost  93.9030509266
Pre-training layer 0, epoch 2, cost  91.9026493191
Pre-training layer 0, epoch 3, cost  90.4216274721
Pre-training layer 0, epoch 4, cost  89.0833314506
Pre-training layer 0, epoch 5, cost  87.8559026421
Pre-training layer 0, epoch 6, cost  86.7326903062
Pre-training layer 0, epoch 7, cost  85.7092163413
Pre-training layer 0, epoch 8, cost  84.7805045526
Pre-training layer 0, epoch 9, cost  83.9406079567
Pre-training layer 0, epoch 10, cost  83.1827745405
Pre-training layer 0, epoch 11, cost  82.4998118929
Pre-training layer 0, epoch 12, cost  81.8844535795
Pre-training layer 0, epoch 13, cost  81.3296517669
Pre-training layer 0, epoch 14, cost  80.8287788804
Pre-training layer 0, epoch 15, cost  80.3757466419
Pre-training layer 0, epoch 16, cost  79.9650599517
Pre-training layer 0, epoch 17, cost  79.5918239973
Pre-training layer 0, epoch 18, cost  79.2517202103
Pre-training layer 0, epoch 19, cost  78.9409629298
Pre-training layer 0, epoch 20, cost  78.6562454559
Pre-training layer 0, epoch 21, cost  78.394682192
Pre-training layer 0, epoch 22, cost  78.1537523696
Pre-training layer 0, epoch 23, cost  77.9312494346
Pre-training layer 0, epoch 24, cost  77.7252380692
Pre-training layer 0, epoch 25, cost  77.5340186086
Pre-training layer 0, epoch 26, cost  77.356097283
Pre-training layer 0, epoch 27, cost  77.190160574
Pre-training layer 0, epoch 28, cost  77.0350525423
Pre-training layer 0, epoch 29, cost  76.8897545957
Pre-training layer 1, epoch 0, cost  65.289325397
Pre-training layer 1, epoch 1, cost  55.8385680867
Pre-training layer 1, epoch 2, cost  54.7632418679
Pre-training layer 1, epoch 3, cost  53.9280922759
Pre-training layer 1, epoch 4, cost  53.165253243
Pre-training layer 1, epoch 5, cost  52.4690417037
Pre-training layer 1, epoch 6, cost  51.8381835994
Pre-training layer 1, epoch 7, cost  51.2699796165
Pre-training layer 1, epoch 8, cost  50.7605411479
Pre-training layer 1, epoch 9, cost  50.3052705513
Pre-training layer 1, epoch 10, cost  49.8992545631
Pre-training layer 1, epoch 11, cost  49.537553657
Pre-training layer 1, epoch 12, cost  49.2154006068
Pre-training layer 1, epoch 13, cost  48.9283258803
Pre-training layer 1, epoch 14, cost  48.6722269922
Pre-training layer 1, epoch 15, cost  48.4433971186
Pre-training layer 1, epoch 16, cost  48.2385258493
Pre-training layer 1, epoch 17, cost  48.0546822563
Pre-training layer 1, epoch 18, cost  47.8892878249
Pre-training layer 1, epoch 19, cost  47.7400845094
Pre-training layer 1, epoch 20, cost  47.6051013681
Pre-training layer 1, epoch 21, cost  47.4826219129
Pre-training layer 1, epoch 22, cost  47.3711533921
Pre-training layer 1, epoch 23, cost  47.2693986267
Pre-training layer 1, epoch 24, cost  47.1762306421
Pre-training layer 1, epoch 25, cost  47.0906701056
Pre-training layer 1, epoch 26, cost  47.0118654491
Pre-training layer 1, epoch 27, cost  46.9390754831
Pre-training layer 1, epoch 28, cost  46.8716542738
Pre-training layer 1, epoch 29, cost  46.809038047
Pre-training layer 2, epoch 0, cost  64.6849856472
Pre-training layer 2, epoch 1, cost  54.5781199269
Pre-training layer 2, epoch 2, cost  53.6689762541
Pre-training layer 2, epoch 3, cost  53.0094906298
Pre-training layer 2, epoch 4, cost  52.4144570274
Pre-training layer 2, epoch 5, cost  51.8735284116
Pre-training layer 2, epoch 6, cost  51.384534679
Pre-training layer 2, epoch 7, cost  50.9450784784
Pre-training layer 2, epoch 8, cost  50.5519613972
Pre-training layer 2, epoch 9, cost  50.2014049073
Pre-training layer 2, epoch 10, cost  49.8893636369
Pre-training layer 2, epoch 11, cost  49.6117901952
Pre-training layer 2, epoch 12, cost  49.3648199478
Pre-training layer 2, epoch 13, cost  49.1448801064
Pre-training layer 2, epoch 14, cost  48.948740031
Pre-training layer 2, epoch 15, cost  48.7735215023
Pre-training layer 2, epoch 16, cost  48.6166848138
Pre-training layer 2, epoch 17, cost  48.4760022154
Pre-training layer 2, epoch 18, cost  48.3495262322
Pre-training layer 2, epoch 19, cost  48.2355573107
Pre-training layer 2, epoch 20, cost  48.1326131595
Pre-training layer 2, epoch 21, cost  48.0394008588
Pre-training layer 2, epoch 22, cost  47.9547920761
Pre-training layer 2, epoch 23, cost  47.8778013347
Pre-training layer 2, epoch 24, cost  47.8075671029
Pre-training layer 2, epoch 25, cost  47.7433354052
Pre-training layer 2, epoch 26, cost  47.6844456454
Pre-training layer 2, epoch 27, cost  47.6303183554
Pre-training layer 2, epoch 28, cost  47.5804446066
Pre-training layer 2, epoch 29, cost  47.5343768573
Pre-training layer 3, epoch 0, cost  68.8535163073
Pre-training layer 3, epoch 1, cost  55.9492523839
Pre-training layer 3, epoch 2, cost  55.1384725388
Pre-training layer 3, epoch 3, cost  54.5695368616
Pre-training layer 3, epoch 4, cost  54.0519231171
Pre-training layer 3, epoch 5, cost  53.5769882187
Pre-training layer 3, epoch 6, cost  53.1443141238
Pre-training layer 3, epoch 7, cost  52.7527714366
Pre-training layer 3, epoch 8, cost  52.4001161652
Pre-training layer 3, epoch 9, cost  52.0834202318
Pre-training layer 3, epoch 10, cost  51.799480133
Pre-training layer 3, epoch 11, cost  51.5450873317
Pre-training layer 3, epoch 12, cost  51.3171806996
Pre-training layer 3, epoch 13, cost  51.1129210419
Pre-training layer 3, epoch 14, cost  50.929719686
Pre-training layer 3, epoch 15, cost  50.7652417373
Pre-training layer 3, epoch 16, cost  50.6173960595
Pre-training layer 3, epoch 17, cost  50.4843187506
Pre-training layer 3, epoch 18, cost  50.364353875
Pre-training layer 3, epoch 19, cost  50.2560335453
Pre-training layer 3, epoch 20, cost  50.1580585159
Pre-training layer 3, epoch 21, cost  50.0692799136
Pre-training layer 3, epoch 22, cost  49.9886824085
Pre-training layer 3, epoch 23, cost  49.9153689323
Pre-training layer 3, epoch 24, cost  49.8485469409
Pre-training layer 3, epoch 25, cost  49.7875161447
Pre-training layer 3, epoch 26, cost  49.7316576043
Pre-training layer 3, epoch 27, cost  49.6804240684
Pre-training layer 3, epoch 28, cost  49.6333314331
Pre-training layer 3, epoch 29, cost  49.5899512054
Pre-training layer 4, epoch 0, cost  70.3362343373
Pre-training layer 4, epoch 1, cost  55.8685531931
Pre-training layer 4, epoch 2, cost  55.0566019141
Pre-training layer 4, epoch 3, cost  54.5244688385
Pre-training layer 4, epoch 4, cost  54.0661524412
Pre-training layer 4, epoch 5, cost  53.6589801789
Pre-training layer 4, epoch 6, cost  53.2946883105
Pre-training layer 4, epoch 7, cost  52.968282658
Pre-training layer 4, epoch 8, cost  52.6757943263
Pre-training layer 4, epoch 9, cost  52.4137106467
Pre-training layer 4, epoch 10, cost  52.1788445945
Pre-training layer 4, epoch 11, cost  51.9683067529
Pre-training layer 4, epoch 12, cost  51.7794908477
Pre-training layer 4, epoch 13, cost  51.6100551708
Pre-training layer 4, epoch 14, cost  51.4579004168
Pre-training layer 4, epoch 15, cost  51.3211470222
Pre-training layer 4, epoch 16, cost  51.1981139274
Pre-training layer 4, epoch 17, cost  51.0872993651
Pre-training layer 4, epoch 18, cost  50.9873635629
Pre-training layer 4, epoch 19, cost  50.8971130381
Pre-training layer 4, epoch 20, cost  50.8154861958
Pre-training layer 4, epoch 21, cost  50.7415400466
Pre-training layer 4, epoch 22, cost  50.6744379486
Pre-training layer 4, epoch 23, cost  50.6134383262
Pre-training layer 4, epoch 24, cost  50.5578843405
Pre-training layer 4, epoch 25, cost  50.5071944824
Pre-training layer 4, epoch 26, cost  50.4608540553
Pre-training layer 4, epoch 27, cost  50.4184075031
Pre-training layer 4, epoch 28, cost  50.3794515334
Pre-training layer 4, epoch 29, cost  50.3436289777
Pre-training layer 5, epoch 0, cost  68.6841517877
Pre-training layer 5, epoch 1, cost  55.6241892069
Pre-training layer 5, epoch 2, cost  55.0516752408
Pre-training layer 5, epoch 3, cost  54.6770543092
Pre-training layer 5, epoch 4, cost  54.3413509836
Pre-training layer 5, epoch 5, cost  54.0343402641
Pre-training layer 5, epoch 6, cost  53.753334173
Pre-training layer 5, epoch 7, cost  53.4963671941
Pre-training layer 5, epoch 8, cost  53.2615864296
Pre-training layer 5, epoch 9, cost  53.0472151951
Pre-training layer 5, epoch 10, cost  52.8515767794
Pre-training layer 5, epoch 11, cost  52.6731098207
Pre-training layer 5, epoch 12, cost  52.5103705018
Pre-training layer 5, epoch 13, cost  52.3620268371
Pre-training layer 5, epoch 14, cost  52.2268498424
Pre-training layer 5, epoch 15, cost  52.1037043399
Pre-training layer 5, epoch 16, cost  51.9915406545
Pre-training layer 5, epoch 17, cost  51.8893876285
Pre-training layer 5, epoch 18, cost  51.7963469541
Pre-training layer 5, epoch 19, cost  51.7115886116
Pre-training layer 5, epoch 20, cost  51.6343471119
Pre-training layer 5, epoch 21, cost  51.5639182366
Pre-training layer 5, epoch 22, cost  51.4996560196
Pre-training layer 5, epoch 23, cost  51.4409697849
Pre-training layer 5, epoch 24, cost  51.3873211321
Pre-training layer 5, epoch 25, cost  51.3382208268
Pre-training layer 5, epoch 26, cost  51.2932255982
Pre-training layer 5, epoch 27, cost  51.2519348773
Pre-training layer 5, epoch 28, cost  51.2139875223
Pre-training layer 5, epoch 29, cost  51.1790585836
Pre-training layer 6, epoch 0, cost  67.2315211572
Pre-training layer 6, epoch 1, cost  57.2281465077
Pre-training layer 6, epoch 2, cost  56.7116294616
Pre-training layer 6, epoch 3, cost  56.3597467389
Pre-training layer 6, epoch 4, cost  56.0328891856
Pre-training layer 6, epoch 5, cost  55.7258735232
Pre-training layer 6, epoch 6, cost  55.4393326141
Pre-training layer 6, epoch 7, cost  55.1736778838
Pre-training layer 6, epoch 8, cost  54.9286711197
Pre-training layer 6, epoch 9, cost  54.7035804192
Pre-training layer 6, epoch 10, cost  54.4973741498
Pre-training layer 6, epoch 11, cost  54.3088658456
Pre-training layer 6, epoch 12, cost  54.1368086043
Pre-training layer 6, epoch 13, cost  53.9799528714
Pre-training layer 6, epoch 14, cost  53.8370805565
Pre-training layer 6, epoch 15, cost  53.7070246597
Pre-training layer 6, epoch 16, cost  53.5886802332
Pre-training layer 6, epoch 17, cost  53.4810101391
Pre-training layer 6, epoch 18, cost  53.3830475788
Pre-training layer 6, epoch 19, cost  53.2938965184
Pre-training layer 6, epoch 20, cost  53.2127306751
Pre-training layer 6, epoch 21, cost  53.1387914876
Pre-training layer 6, epoch 22, cost  53.0713853659
Pre-training layer 6, epoch 23, cost  53.0098804341
Pre-training layer 6, epoch 24, cost  52.9537029307
Pre-training layer 6, epoch 25, cost  52.9023333955
Pre-training layer 6, epoch 26, cost  52.8553027489
Pre-training layer 6, epoch 27, cost  52.8121883516
Pre-training layer 6, epoch 28, cost  52.7726101166
Pre-training layer 6, epoch 29, cost  52.7362267368
... getting the finetuning functions
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
Your Python version is from Canopy. You need to install the package 'mingw 4.5.2' from Canopy package manager.
... finetunning the model
epoch 1, minibatch 383/383, validation error 51.122807 %
 epoch 1, minibatch 383/383, test error of best model 50.448179 %
epoch 2, minibatch 383/383, validation error 50.947368 %
 epoch 2, minibatch 383/383, test error of best model 50.420168 %
epoch 3, minibatch 383/383, validation error 43.719298 %
 epoch 3, minibatch 383/383, test error of best model 44.033613 %
epoch 4, minibatch 383/383, validation error 39.228070 %
 epoch 4, minibatch 383/383, test error of best model 39.803922 %
epoch 5, minibatch 383/383, validation error 37.122807 %
 epoch 5, minibatch 383/383, test error of best model 36.974790 %
epoch 6, minibatch 383/383, validation error 35.087719 %
 epoch 6, minibatch 383/383, test error of best model 35.322129 %
epoch 7, minibatch 383/383, validation error 33.298246 %
 epoch 7, minibatch 383/383, test error of best model 33.893557 %
epoch 8, minibatch 383/383, validation error 31.649123 %
 epoch 8, minibatch 383/383, test error of best model 32.268908 %
epoch 9, minibatch 383/383, validation error 30.526316 %
 epoch 9, minibatch 383/383, test error of best model 30.952381 %
epoch 10, minibatch 383/383, validation error 29.192982 %
 epoch 10, minibatch 383/383, test error of best model 30.084034 %
epoch 11, minibatch 383/383, validation error 28.280702 %
 epoch 11, minibatch 383/383, test error of best model 29.019608 %
epoch 12, minibatch 383/383, validation error 27.614035 %
 epoch 12, minibatch 383/383, test error of best model 28.179272 %
epoch 13, minibatch 383/383, validation error 26.982456 %
 epoch 13, minibatch 383/383, test error of best model 27.647059 %
epoch 14, minibatch 383/383, validation error 26.175439 %
 epoch 14, minibatch 383/383, test error of best model 27.086835 %
epoch 15, minibatch 383/383, validation error 25.157895 %
 epoch 15, minibatch 383/383, test error of best model 25.966387 %
epoch 16, minibatch 383/383, validation error 24.596491 %
 epoch 16, minibatch 383/383, test error of best model 25.602241 %
epoch 17, minibatch 383/383, validation error 24.280702 %
 epoch 17, minibatch 383/383, test error of best model 24.733894 %
epoch 18, minibatch 383/383, validation error 23.684211 %
 epoch 18, minibatch 383/383, test error of best model 24.033613 %
epoch 19, minibatch 383/383, validation error 23.087719 %
 epoch 19, minibatch 383/383, test error of best model 23.053221 %
epoch 20, minibatch 383/383, validation error 22.385965 %
 epoch 20, minibatch 383/383, test error of best model 22.436975 %
epoch 21, minibatch 383/383, validation error 21.614035 %
 epoch 21, minibatch 383/383, test error of best model 21.400560 %
epoch 22, minibatch 383/383, validation error 21.052632 %
 epoch 22, minibatch 383/383, test error of best model 20.644258 %
epoch 23, minibatch 383/383, validation error 20.631579 %
 epoch 23, minibatch 383/383, test error of best model 19.915966 %
epoch 24, minibatch 383/383, validation error 20.140351 %
 epoch 24, minibatch 383/383, test error of best model 19.439776 %
epoch 25, minibatch 383/383, validation error 19.403509 %
 epoch 25, minibatch 383/383, test error of best model 19.075630 %
epoch 26, minibatch 383/383, validation error 18.877193 %
 epoch 26, minibatch 383/383, test error of best model 18.571429 %
epoch 27, minibatch 383/383, validation error 18.280702 %
 epoch 27, minibatch 383/383, test error of best model 18.375350 %
epoch 28, minibatch 383/383, validation error 17.824561 %
 epoch 28, minibatch 383/383, test error of best model 17.731092 %
epoch 29, minibatch 383/383, validation error 17.473684 %
 epoch 29, minibatch 383/383, test error of best model 17.338936 %
epoch 30, minibatch 383/383, validation error 17.263158 %
 epoch 30, minibatch 383/383, test error of best model 16.834734 %
epoch 31, minibatch 383/383, validation error 16.947368 %
 epoch 31, minibatch 383/383, test error of best model 16.666667 %
epoch 32, minibatch 383/383, validation error 16.596491 %
 epoch 32, minibatch 383/383, test error of best model 16.414566 %
epoch 33, minibatch 383/383, validation error 16.210526 %
 epoch 33, minibatch 383/383, test error of best model 16.022409 %
epoch 34, minibatch 383/383, validation error 16.000000 %
 epoch 34, minibatch 383/383, test error of best model 15.770308 %
epoch 35, minibatch 383/383, validation error 15.824561 %
 epoch 35, minibatch 383/383, test error of best model 15.546218 %
epoch 36, minibatch 383/383, validation error 15.859649 %
epoch 37, minibatch 383/383, validation error 15.719298 %
 epoch 37, minibatch 383/383, test error of best model 15.154062 %
epoch 38, minibatch 383/383, validation error 15.614035 %
 epoch 38, minibatch 383/383, test error of best model 14.901961 %
epoch 39, minibatch 383/383, validation error 15.508772 %
 epoch 39, minibatch 383/383, test error of best model 14.761905 %
epoch 40, minibatch 383/383, validation error 15.228070 %
 epoch 40, minibatch 383/383, test error of best model 14.509804 %
epoch 41, minibatch 383/383, validation error 15.228070 %
epoch 42, minibatch 383/383, validation error 15.157895 %
 epoch 42, minibatch 383/383, test error of best model 14.341737 %
epoch 43, minibatch 383/383, validation error 14.982456 %
 epoch 43, minibatch 383/383, test error of best model 14.369748 %
epoch 44, minibatch 383/383, validation error 14.842105 %
 epoch 44, minibatch 383/383, test error of best model 14.341737 %
epoch 45, minibatch 383/383, validation error 14.842105 %
epoch 46, minibatch 383/383, validation error 14.666667 %
 epoch 46, minibatch 383/383, test error of best model 14.117647 %
epoch 47, minibatch 383/383, validation error 14.561404 %
 epoch 47, minibatch 383/383, test error of best model 14.117647 %
epoch 48, minibatch 383/383, validation error 14.561404 %
epoch 49, minibatch 383/383, validation error 14.526316 %
 epoch 49, minibatch 383/383, test error of best model 13.977591 %
epoch 50, minibatch 383/383, validation error 14.280702 %
 epoch 50, minibatch 383/383, test error of best model 13.921569 %
epoch 51, minibatch 383/383, validation error 14.140351 %
 epoch 51, minibatch 383/383, test error of best model 13.921569 %
epoch 52, minibatch 383/383, validation error 14.070175 %
 epoch 52, minibatch 383/383, test error of best model 13.977591 %
epoch 53, minibatch 383/383, validation error 13.964912 %
 epoch 53, minibatch 383/383, test error of best model 13.837535 %
epoch 54, minibatch 383/383, validation error 14.245614 %
epoch 55, minibatch 383/383, validation error 14.421053 %
epoch 56, minibatch 383/383, validation error 14.701754 %
epoch 57, minibatch 383/383, validation error 14.982456 %
epoch 58, minibatch 383/383, validation error 14.982456 %
epoch 59, minibatch 383/383, validation error 15.052632 %
epoch 60, minibatch 383/383, validation error 14.315789 %
epoch 61, minibatch 383/383, validation error 14.210526 %
epoch 62, minibatch 383/383, validation error 14.210526 %
epoch 63, minibatch 383/383, validation error 14.140351 %
epoch 64, minibatch 383/383, validation error 14.210526 %
epoch 65, minibatch 383/383, validation error 14.350877 %
epoch 66, minibatch 383/383, validation error 14.035088 %
epoch 67, minibatch 383/383, validation error 14.526316 %
epoch 68, minibatch 383/383, validation error 13.964912 %
epoch 69, minibatch 383/383, validation error 14.315789 %
epoch 70, minibatch 383/383, validation error 13.719298 %
 epoch 70, minibatch 383/383, test error of best model 12.941176 %
epoch 71, minibatch 383/383, validation error 13.754386 %
epoch 72, minibatch 383/383, validation error 13.614035 %
 epoch 72, minibatch 383/383, test error of best model 13.081232 %
epoch 73, minibatch 383/383, validation error 13.368421 %
 epoch 73, minibatch 383/383, test error of best model 12.689076 %
epoch 74, minibatch 383/383, validation error 13.754386 %
epoch 75, minibatch 383/383, validation error 14.456140 %
epoch 76, minibatch 383/383, validation error 14.280702 %
epoch 77, minibatch 383/383, validation error 15.964912 %
epoch 78, minibatch 383/383, validation error 14.000000 %
epoch 79, minibatch 383/383, validation error 13.614035 %
epoch 80, minibatch 383/383, validation error 13.578947 %
epoch 81, minibatch 383/383, validation error 14.035088 %
epoch 82, minibatch 383/383, validation error 14.631579 %
epoch 83, minibatch 383/383, validation error 14.807018 %
epoch 84, minibatch 383/383, validation error 14.315789 %
epoch 85, minibatch 383/383, validation error 14.877193 %
epoch 86, minibatch 383/383, validation error 13.192982 %
 epoch 86, minibatch 383/383, test error of best model 12.717087 %
epoch 87, minibatch 383/383, validation error 13.438596 %
epoch 88, minibatch 383/383, validation error 12.912281 %
 epoch 88, minibatch 383/383, test error of best model 12.801120 %
epoch 89, minibatch 383/383, validation error 14.456140 %
epoch 90, minibatch 383/383, validation error 14.210526 %
epoch 91, minibatch 383/383, validation error 14.421053 %
epoch 92, minibatch 383/383, validation error 13.719298 %
epoch 93, minibatch 383/383, validation error 12.982456 %
epoch 94, minibatch 383/383, validation error 14.035088 %
epoch 95, minibatch 383/383, validation error 13.473684 %
epoch 96, minibatch 383/383, validation error 12.807018 %
 epoch 96, minibatch 383/383, test error of best model 12.296919 %
epoch 97, minibatch 383/383, validation error 14.280702 %
epoch 98, minibatch 383/383, validation error 15.052632 %
epoch 99, minibatch 383/383, validation error 13.649123 %
epoch 100, minibatch 383/383, validation error 13.228070 %
epoch 101, minibatch 383/383, validation error 14.280702 %
epoch 102, minibatch 383/383, validation error 14.245614 %
epoch 103, minibatch 383/383, validation error 15.508772 %
epoch 104, minibatch 383/383, validation error 14.912281 %
epoch 105, minibatch 383/383, validation error 12.982456 %
epoch 106, minibatch 383/383, validation error 12.807018 %
 epoch 106, minibatch 383/383, test error of best model 11.932773 %
epoch 107, minibatch 383/383, validation error 13.228070 %
epoch 108, minibatch 383/383, validation error 12.947368 %
epoch 109, minibatch 383/383, validation error 13.403509 %
epoch 110, minibatch 383/383, validation error 14.912281 %
epoch 111, minibatch 383/383, validation error 13.122807 %
epoch 112, minibatch 383/383, validation error 13.298246 %
epoch 113, minibatch 383/383, validation error 14.666667 %
epoch 114, minibatch 383/383, validation error 12.842105 %
epoch 115, minibatch 383/383, validation error 14.070175 %
epoch 116, minibatch 383/383, validation error 13.929825 %
epoch 117, minibatch 383/383, validation error 13.052632 %
epoch 118, minibatch 383/383, validation error 13.368421 %
epoch 119, minibatch 383/383, validation error 14.175439 %
epoch 120, minibatch 383/383, validation error 13.052632 %
epoch 121, minibatch 383/383, validation error 13.122807 %
epoch 122, minibatch 383/383, validation error 12.982456 %
epoch 123, minibatch 383/383, validation error 13.122807 %
epoch 124, minibatch 383/383, validation error 13.368421 %
epoch 125, minibatch 383/383, validation error 13.157895 %
epoch 126, minibatch 383/383, validation error 13.157895 %
epoch 127, minibatch 383/383, validation error 13.263158 %
epoch 128, minibatch 383/383, validation error 13.087719 %
epoch 129, minibatch 383/383, validation error 13.263158 %
epoch 130, minibatch 383/383, validation error 14.210526 %
epoch 131, minibatch 383/383, validation error 13.228070 %
epoch 132, minibatch 383/383, validation error 13.157895 %
epoch 133, minibatch 383/383, validation error 13.052632 %
epoch 134, minibatch 383/383, validation error 13.298246 %
epoch 135, minibatch 383/383, validation error 13.122807 %
epoch 136, minibatch 383/383, validation error 13.087719 %
epoch 137, minibatch 383/383, validation error 13.333333 %
epoch 138, minibatch 383/383, validation error 13.087719 %
epoch 139, minibatch 383/383, validation error 13.333333 %
epoch 140, minibatch 383/383, validation error 13.403509 %
epoch 141, minibatch 383/383, validation error 13.263158 %
epoch 142, minibatch 383/383, validation error 13.333333 %
epoch 143, minibatch 383/383, validation error 13.228070 %
epoch 144, minibatch 383/383, validation error 13.263158 %
epoch 145, minibatch 383/383, validation error 13.263158 %
epoch 146, minibatch 383/383, validation error 13.438596 %
epoch 147, minibatch 383/383, validation error 13.438596 %
epoch 148, minibatch 383/383, validation error 13.508772 %
epoch 149, minibatch 383/383, validation error 13.298246 %
epoch 150, minibatch 383/383, validation error 13.333333 %
epoch 151, minibatch 383/383, validation error 12.912281 %
epoch 152, minibatch 383/383, validation error 13.017544 %
epoch 153, minibatch 383/383, validation error 13.017544 %
epoch 154, minibatch 383/383, validation error 13.087719 %
epoch 155, minibatch 383/383, validation error 12.912281 %
epoch 156, minibatch 383/383, validation error 12.982456 %
epoch 157, minibatch 383/383, validation error 12.912281 %
epoch 158, minibatch 383/383, validation error 12.912281 %
epoch 159, minibatch 383/383, validation error 12.982456 %
epoch 160, minibatch 383/383, validation error 13.017544 %
epoch 161, minibatch 383/383, validation error 13.052632 %
epoch 162, minibatch 383/383, validation error 13.052632 %
epoch 163, minibatch 383/383, validation error 13.052632 %
epoch 164, minibatch 383/383, validation error 12.982456 %
epoch 165, minibatch 383/383, validation error 12.982456 %
epoch 166, minibatch 383/383, validation error 12.982456 %
epoch 167, minibatch 383/383, validation error 13.157895 %
epoch 168, minibatch 383/383, validation error 13.157895 %
epoch 169, minibatch 383/383, validation error 13.087719 %
epoch 170, minibatch 383/383, validation error 13.087719 %
epoch 171, minibatch 383/383, validation error 13.052632 %
epoch 172, minibatch 383/383, validation error 13.017544 %
epoch 173, minibatch 383/383, validation error 13.087719 %
epoch 174, minibatch 383/383, validation error 13.087719 %
epoch 175, minibatch 383/383, validation error 13.122807 %
epoch 176, minibatch 383/383, validation error 13.052632 %
epoch 177, minibatch 383/383, validation error 13.052632 %
epoch 178, minibatch 383/383, validation error 13.122807 %
epoch 179, minibatch 383/383, validation error 13.087719 %
epoch 180, minibatch 383/383, validation error 13.052632 %
epoch 181, minibatch 383/383, validation error 13.052632 %
epoch 182, minibatch 383/383, validation error 13.017544 %
epoch 183, minibatch 383/383, validation error 13.017544 %
epoch 184, minibatch 383/383, validation error 13.017544 %
epoch 185, minibatch 383/383, validation error 12.982456 %
epoch 186, minibatch 383/383, validation error 12.982456 %
epoch 187, minibatch 383/383, validation error 12.912281 %
epoch 188, minibatch 383/383, validation error 12.912281 %
epoch 189, minibatch 383/383, validation error 12.947368 %
epoch 190, minibatch 383/383, validation error 12.947368 %
epoch 191, minibatch 383/383, validation error 12.947368 %
The pretraining code ran for 7.04m

In [20]:
print 'hidden_layers_sizes:', hidden_layers_sizes
print 'corruption_levels:', corruption_levels

training_predicted = sda_transformed.predict(new_x_train_minmax_whole)
y_train = y_train_minmax
print 'train accuracy: ', '{percent:.1%}'.format(percent=sklearn.metrics.accuracy_score(y_train, training_predicted)) 
print 'precision: ', '{percent:.1%}'.format(percent=sklearn.metrics.precision_score(y_train, training_predicted, pos_label=1)) 
print 'recall: ', '{percent:.1%}'.format( percent= sklearn.metrics.recall_score(y_train, training_predicted, pos_label=1))


test_predicted = sda_transformed.predict(new_x_test_minmax_whole)
y_test = y_test_minmax
print 'testing accuracy: ', '{percent:.1%}'.format(percent=sklearn.metrics.accuracy_score(y_test, test_predicted)) 
print 'precision: ', '{percent:.1%}'.format(percent=sklearn.metrics.precision_score(y_test, test_predicted, pos_label=1)) 
print 'recall: ', '{percent:.1%}'.format( percent= sklearn.metrics.recall_score(y_test, test_predicted, pos_label=1))


hidden_layers_sizes: [100, 100, 100, 100, 100, 100, 100]
corruption_levels: [0, 0, 0, 0, 0, 0, 0]
train accuracy:  100.0%
precision:  100.0%
recall:  100.0%
testing accuracy:  87.9%
precision:  87.7%
recall:  88.4%

In [47]:
import pickle
with open('array_A.pickle', 'wb') as handle:
  pickle.dump(array_A, handle)
with open('array_B.pickle', 'wb') as handle:
  pickle.dump(array_B, handle)
with open('pos_index.pickle', 'wb') as handle:
  pickle.dump(pos_index, handle)
with open('neg_index.pickle', 'wb') as handle:
  pickle.dump(neg_index, handle)

In [58]:
import cPickle
import gzip
import os
import sys
import time

import numpy

import theano
import theano.tensor as T
from theano.tensor.shared_randomstreams import RandomStreams

from logistic_sgd import LogisticRegression, load_data
from mlp import HiddenLayer
from dA import dA
import warnings
warnings.filterwarnings("ignore")

def shared_dataset(data_xy, borrow=True):
    """ Function that loads the dataset into shared variables

    The reason we store our dataset in shared variables is to allow
    Theano to copy it into the GPU memory (when code is run on GPU).
    Since copying data into the GPU is slow, copying a minibatch everytime
    is needed (the default behaviour if the data is not in a shared
    variable) would lead to a large decrease in performance.
    """
    data_x, data_y = data_xy
    shared_x = theano.shared(numpy.asarray(data_x,
                                           dtype=theano.config.floatX),
                             borrow=borrow)
    shared_y = theano.shared(numpy.asarray(data_y,
                                           dtype=theano.config.floatX),
                             borrow=borrow)
    # When storing data on the GPU it has to be stored as floats
    # therefore we will store the labels as ``floatX`` as well
    # (``shared_y`` does exactly that). But during our computations
    # we need them as ints (we use labels as index, and if they are
    # floats it doesn't make sense) therefore instead of returning
    # ``shared_y`` we will have to cast it to int. This little hack
    # lets ous get around this issue
    return shared_x, T.cast(shared_y, 'int32')
def shared_dataset_X(data_x, borrow=True):
    shared_x = theano.shared(numpy.asarray(data_x,
                                           dtype=theano.config.floatX),
                             borrow=borrow)
    return shared_x
class MultipleAEs(object):
    """Stacked denoising auto-encoder class that extract hi level features.
    get the last hidden layer activation

    """

    def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
                 hidden_layers_sizes=[500, 500],
                 corruption_levels=[0.1, 0.1]):
        """ This class is made to support a variable number of layers.

        :type numpy_rng: numpy.random.RandomState
        :param numpy_rng: numpy random number generator used to draw initial
                    weights

        :type theano_rng: theano.tensor.shared_randomstreams.RandomStreams
        :param theano_rng: Theano random generator; if None is given one is
                           generated based on a seed drawn from `rng`

        :type n_ins: int
        :param n_ins: dimension of the input to the sdA

        :type n_layers_sizes: list of ints
        :param n_layers_sizes: intermediate layers size, must contain
        :type corruption_levels: list of float
        :param corruption_levels: amount of corruption to use for each
                                  layer
        """

        self.sigmoid_layers = []
        self.dA_layers = []
        self.params = []
        self.n_layers = len(hidden_layers_sizes)

        assert self.n_layers > 0

        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
        # allocate symbolic variables for the data
        self.x = T.matrix('x')  # the data is presented as rasterized images
        self.y = T.ivector('y')  # the labels are presented as 1D vector of
                                 # [int] labels

        # The SdA is an MLP, for which all weights of intermediate layers
        # are shared with a different denoising autoencoders
        # We will first construct the SdA as a deep multilayer perceptron,
        # and when constructing each sigmoidal layer we also construct a
        # denoising autoencoder that shares weights with that layer
        # During pretraining we will train these autoencoders (which will
        # lead to chainging the weights of the MLP as well)
        # During finetunining we will finish training the SdA by doing
        # stochastich gradient descent on the MLP

        for i in xrange(self.n_layers):
            # construct the sigmoidal layer

            # the size of the input is either the number of hidden units of
            # the layer below or the input size if we are on the first layer
            if i == 0:
                input_size = n_ins
            else:
                input_size = hidden_layers_sizes[i - 1]

            # the input to this layer is either the activation of the hidden
            # layer below or the input of the SdA if you are on the first
            # layer
            if i == 0:
                layer_input = self.x
            else:
                layer_input = self.sigmoid_layers[-1].output

            sigmoid_layer = HiddenLayer(rng=numpy_rng,
                                        input=layer_input,
                                        n_in=input_size,
                                        n_out=hidden_layers_sizes[i],
                                        activation=T.nnet.sigmoid)
            # add the layer to our list of layers
            self.sigmoid_layers.append(sigmoid_layer)
            # its arguably a philosophical question...
            # but we are going to only declare that the parameters of the
            # sigmoid_layers are parameters of the StackedDAA
            # the visible biases in the dA are parameters of those
            # dA, but not the SdA
            self.params.extend(sigmoid_layer.params)

            # Construct a denoising autoencoder that shared weights with this
            # layer
            dA_layer = dA(numpy_rng=numpy_rng,
                          theano_rng=theano_rng,
                          input=layer_input,
                          n_visible=input_size,
                          n_hidden=hidden_layers_sizes[i],
                          W=sigmoid_layer.W,
                          bhid=sigmoid_layer.b)
            self.dA_layers.append(dA_layer)
        '''
        we don't need this layer since is AE
        # We now need to add a logistic layer on top of the MLP
        self.logLayer = LogisticRegression(
                         input=self.sigmoid_layers[-1].output,
                         n_in=hidden_layers_sizes[-1], n_out=n_outs)
        
        self.params.extend(self.logLayer.params)
        # construct a function that implements one step of finetunining
        
        # compute the cost for second phase of training,
        # defined as the negative log likelihood
        self.finetune_cost = self.logLayer.negative_log_likelihood(self.y)
        # compute the gradients with respect to the model parameters
        # symbolic variable that points to the number of errors made on the
        # minibatch given by self.x and self.y
        self.errors = self.logLayer.errors(self.y)
        '''
    def transform(self,data_x): # get the last layaer activations to transform data.
        last_layer_activations = self.sigmoid_layers[-1].output
        theano_fn = theano.function(inputs=[],
                                 outputs=last_layer_activations,

                                 givens={self.x: data_x})
        newFeatures=theano_fn()
        return newFeatures
    def pretraining_functions(self, train_set_x, batch_size):
        ''' Generates a list of functions, each of them implementing one
        step in trainnig the dA corresponding to the layer with same index.
        The function will require as input the minibatch index, and to train
        a dA you just need to iterate, calling the corresponding function on
        all minibatch indexes.

        :type train_set_x: theano.tensor.TensorType
        :param train_set_x: Shared variable that contains all datapoints used
                            for training the dA

        :type batch_size: int
        :param batch_size: size of a [mini]batch

        :type learning_rate: float
        :param learning_rate: learning rate used during training for any of
                              the dA layers
        '''

        # index to a [mini]batch
        index = T.lscalar('index')  # index to a minibatch
        corruption_level = T.scalar('corruption')  # % of corruption to use
        learning_rate = T.scalar('lr')  # learning rate to use
        # number of batches
        n_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
        # begining of a batch, given `index`
        batch_begin = index * batch_size
        # ending of a batch given `index`
        batch_end = batch_begin + batch_size

        pretrain_fns = []
        for dA in self.dA_layers:
            # get the cost and the updates list
            cost, updates = dA.get_cost_updates(corruption_level,
                                                learning_rate)
            # compile the theano function
            fn = theano.function(inputs=[index,
                              theano.Param(corruption_level, default=0.2),
                              theano.Param(learning_rate, default=0.1)],
                                 outputs=cost,
                                 updates=updates,
                                 givens={self.x: train_set_x[batch_begin:
                                                             batch_end]})
            # append `fn` to the list of functions
            pretrain_fns.append(fn)

        return pretrain_fns

    def build_finetune_functions(self, datasets, batch_size, learning_rate):
        '''Generates a function `train` that implements one step of
        finetuning, a function `validate` that computes the error on
        a batch from the validation set, and a function `test` that
        computes the error on a batch from the testing set

        :type datasets: list of pairs of theano.tensor.TensorType
        :param datasets: It is a list that contain all the datasets;
                         the has to contain three pairs, `train`,
                         `valid`, `test` in this order, where each pair
                         is formed of two Theano variables, one for the
                         datapoints, the other for the labels

        :type batch_size: int
        :param batch_size: size of a minibatch

        :type learning_rate: float
        :param learning_rate: learning rate used during finetune stage
        '''

        (train_set_x, train_set_y) = datasets[0]
        (valid_set_x, valid_set_y) = datasets[1]
        (test_set_x, test_set_y) = datasets[2]

        # compute number of minibatches for training, validation and testing
        n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
        n_valid_batches /= batch_size
        n_test_batches = test_set_x.get_value(borrow=True).shape[0]
        n_test_batches /= batch_size

        index = T.lscalar('index')  # index to a [mini]batch

        # compute the gradients with respect to the model parameters
        gparams = T.grad(self.finetune_cost, self.params)

        # compute list of fine-tuning updates
        updates = []
        for param, gparam in zip(self.params, gparams):
            updates.append((param, param - gparam * learning_rate))

        train_fn = theano.function(inputs=[index],
              outputs=self.finetune_cost,
              updates=updates,
              givens={
                self.x: train_set_x[index * batch_size:
                                    (index + 1) * batch_size],
                self.y: train_set_y[index * batch_size:
                                    (index + 1) * batch_size]},
              name='train')

        test_score_i = theano.function([index], self.errors,
                 givens={
                   self.x: test_set_x[index * batch_size:
                                      (index + 1) * batch_size],
                   self.y: test_set_y[index * batch_size:
                                      (index + 1) * batch_size]},
                      name='test')

        valid_score_i = theano.function([index], self.errors,
              givens={
                 self.x: valid_set_x[index * batch_size:
                                     (index + 1) * batch_size],
                 self.y: valid_set_y[index * batch_size:
                                     (index + 1) * batch_size]},
                      name='valid')

        # Create a function that scans the entire validation set
        def valid_score():
            return [valid_score_i(i) for i in xrange(n_valid_batches)]

        # Create a function that scans the entire test set
        def test_score():
            return [test_score_i(i) for i in xrange(n_test_batches)]

        return train_fn, valid_score, test_score
def train_a_MultipleAEs(X, pretraining_epochs=10, pretrain_lr=0.001, batch_size=30,
                        hidden_layers_sizes=[100, 100], corruption_levels=[0, 0]):
    
    # get a shared copy of X
    train_set_x = shared_dataset_X(X)
    # compute number of minibatches for training, validation and testing
    n_train_batches = train_set_x.get_value(borrow=True).shape[0]
    n_train_batches /= batch_size

    # numpy random generator
    numpy_rng = numpy.random.RandomState(89677)
    print '... building the model'
    # construct the stacked denoising autoencoder class
    sda = MultipleAEs(numpy_rng=numpy_rng, n_ins=train_set_x.get_value(borrow=True).shape[1],
              hidden_layers_sizes=hidden_layers_sizes, corruption_levels = corruption_levels)

    #########################
    # PRETRAINING THE MODEL #
    #########################
    print '... getting the pretraining functions'
    pretraining_fns = sda.pretraining_functions(train_set_x=train_set_x,
                                                batch_size=batch_size)

    print '... pre-training the model'
    start_time = time.clock()
    ## Pre-train layer-wise
    corruption_levels = [.1, .2, .3]
    for i in xrange(sda.n_layers):
        # go through pretraining epochs
        for epoch in xrange(pretraining_epochs):
            # go through the training set
            c = []
            for batch_index in xrange(n_train_batches):
                c.append(pretraining_fns[i](index=batch_index,
                         corruption=corruption_levels[i],
                         lr=pretrain_lr))
            print 'Pre-training layer %i, epoch %d, cost ' % (i, epoch),
            print numpy.mean(c)

    end_time = time.clock()
    return sda
class SdA(object):
    """Stacked denoising auto-encoder class (SdA)

    A stacked denoising autoencoder model is obtained by stacking several
    dAs. The hidden layer of the dA at layer `i` becomes the input of
    the dA at layer `i+1`. The first layer dA gets as input the input of
    the SdA, and the hidden layer of the last dA represents the output.
    Note that after pretraining, the SdA is dealt with as a normal MLP,
    the dAs are only used to initialize the weights.
    """

    def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
                 hidden_layers_sizes=[500, 500], n_outs=10,
                 corruption_levels=[0.1, 0.1]):
        """ This class is made to support a variable number of layers.

        :type numpy_rng: numpy.random.RandomState
        :param numpy_rng: numpy random number generator used to draw initial
                    weights

        :type theano_rng: theano.tensor.shared_randomstreams.RandomStreams
        :param theano_rng: Theano random generator; if None is given one is
                           generated based on a seed drawn from `rng`

        :type n_ins: int
        :param n_ins: dimension of the input to the sdA

        :type n_layers_sizes: list of ints
        :param n_layers_sizes: intermediate layers size, must contain
                               at least one value

        :type n_outs: int
        :param n_outs: dimension of the output of the network

        :type corruption_levels: list of float
        :param corruption_levels: amount of corruption to use for each
                                  layer
        """

        self.sigmoid_layers = []
        self.dA_layers = []
        self.params = []
        self.n_layers = len(hidden_layers_sizes)

        assert self.n_layers > 0

        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
        # allocate symbolic variables for the data
        self.x = T.matrix('x')  # the data is presented as rasterized images
        self.y = T.ivector('y')  # the labels are presented as 1D vector of
                                 # [int] labels

        # The SdA is an MLP, for which all weights of intermediate layers
        # are shared with a different denoising autoencoders
        # We will first construct the SdA as a deep multilayer perceptron,
        # and when constructing each sigmoidal layer we also construct a
        # denoising autoencoder that shares weights with that layer
        # During pretraining we will train these autoencoders (which will
        # lead to chainging the weights of the MLP as well)
        # During finetunining we will finish training the SdA by doing
        # stochastich gradient descent on the MLP

        for i in xrange(self.n_layers):
            # construct the sigmoidal layer

            # the size of the input is either the number of hidden units of
            # the layer below or the input size if we are on the first layer
            if i == 0:
                input_size = n_ins
            else:
                input_size = hidden_layers_sizes[i - 1]

            # the input to this layer is either the activation of the hidden
            # layer below or the input of the SdA if you are on the first
            # layer
            if i == 0:
                layer_input = self.x
            else:
                layer_input = self.sigmoid_layers[-1].output

            sigmoid_layer = HiddenLayer(rng=numpy_rng,
                                        input=layer_input,
                                        n_in=input_size,
                                        n_out=hidden_layers_sizes[i],
                                        activation=T.nnet.sigmoid)
            # add the layer to our list of layers
            self.sigmoid_layers.append(sigmoid_layer)
            # its arguably a philosophical question...
            # but we are going to only declare that the parameters of the
            # sigmoid_layers are parameters of the StackedDAA
            # the visible biases in the dA are parameters of those
            # dA, but not the SdA
            self.params.extend(sigmoid_layer.params)

            # Construct a denoising autoencoder that shared weights with this
            # layer
            dA_layer = dA(numpy_rng=numpy_rng,
                          theano_rng=theano_rng,
                          input=layer_input,
                          n_visible=input_size,
                          n_hidden=hidden_layers_sizes[i],
                          W=sigmoid_layer.W,
                          bhid=sigmoid_layer.b)
            self.dA_layers.append(dA_layer)

        # We now need to add a logistic layer on top of the MLP
        self.logLayer = LogisticRegression(
                         input=self.sigmoid_layers[-1].output,
                         n_in=hidden_layers_sizes[-1], n_out=n_outs)

        self.params.extend(self.logLayer.params)
        # construct a function that implements one step of finetunining

        # compute the cost for second phase of training,
        # defined as the negative log likelihood
        self.finetune_cost = self.logLayer.negative_log_likelihood(self.y)
        # compute the gradients with respect to the model parameters
        # symbolic variable that points to the number of errors made on the
        # minibatch given by self.x and self.y
        self.errors = self.logLayer.errors(self.y)

    def pretraining_functions(self, train_set_x, batch_size):
        ''' Generates a list of functions, each of them implementing one
        step in trainnig the dA corresponding to the layer with same index.
        The function will require as input the minibatch index, and to train
        a dA you just need to iterate, calling the corresponding function on
        all minibatch indexes.

        :type train_set_x: theano.tensor.TensorType
        :param train_set_x: Shared variable that contains all datapoints used
                            for training the dA

        :type batch_size: int
        :param batch_size: size of a [mini]batch

        :type learning_rate: float
        :param learning_rate: learning rate used during training for any of
                              the dA layers
        '''

        # index to a [mini]batch
        index = T.lscalar('index')  # index to a minibatch
        corruption_level = T.scalar('corruption')  # % of corruption to use
        learning_rate = T.scalar('lr')  # learning rate to use
        # number of batches
        n_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
        # begining of a batch, given `index`
        batch_begin = index * batch_size
        # ending of a batch given `index`
        batch_end = batch_begin + batch_size

        pretrain_fns = []
        for dA in self.dA_layers:
            # get the cost and the updates list
            cost, updates = dA.get_cost_updates(corruption_level,
                                                learning_rate)
            # compile the theano function
            fn = theano.function(inputs=[index,
                              theano.Param(corruption_level, default=0.2),
                              theano.Param(learning_rate, default=0.1)],
                                 outputs=cost,
                                 updates=updates,
                                 givens={self.x: train_set_x[batch_begin:
                                                             batch_end]})
            # append `fn` to the list of functions
            pretrain_fns.append(fn)

        return pretrain_fns
    def predict(self, x_dataset):
        predict_fn = theano.function([], sda.logLayer.y_pred,
                   givens={sda.x: x_dataset})
        predicted = predict_fn()
        return predicted
    def predict_p(self, x_dataset):
        predict_p_fn = theano.function([], sda.logLayer.p_y_given_x,
                   givens={sda.x: x_dataset})
        predicted_p = predict_p_fn()
        return predicted_p
    def build_finetune_functions(self, datasets, batch_size, learning_rate):
        '''Generates a function `train` that implements one step of
        finetuning, a function `validate` that computes the error on
        a batch from the validation set, and a function `test` that
        computes the error on a batch from the testing set

        :type datasets: list of pairs of theano.tensor.TensorType
        :param datasets: It is a list that contain all the datasets;
                         the has to contain three pairs, `train`,
                         `valid`, `test` in this order, where each pair
                         is formed of two Theano variables, one for the
                         datapoints, the other for the labels

        :type batch_size: int
        :param batch_size: size of a minibatch

        :type learning_rate: float
        :param learning_rate: learning rate used during finetune stage
        '''

        (train_set_x, train_set_y) = datasets[0]
        (valid_set_x, valid_set_y) = datasets[1]
        (test_set_x, test_set_y) = datasets[2]

        # compute number of minibatches for training, validation and testing
        n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
        n_valid_batches /= batch_size
        n_test_batches = test_set_x.get_value(borrow=True).shape[0]
        n_test_batches /= batch_size

        index = T.lscalar('index')  # index to a [mini]batch

        # compute the gradients with respect to the model parameters
        gparams = T.grad(self.finetune_cost, self.params)

        # compute list of fine-tuning updates
        updates = []
        for param, gparam in zip(self.params, gparams):
            updates.append((param, param - gparam * learning_rate))

        train_fn = theano.function(inputs=[index],
              outputs=self.finetune_cost,
              updates=updates,
              givens={
                self.x: train_set_x[index * batch_size:
                                    (index + 1) * batch_size],
                self.y: train_set_y[index * batch_size:
                                    (index + 1) * batch_size]},
              name='train')

        test_score_i = theano.function([index], self.errors,
                 givens={
                   self.x: test_set_x[index * batch_size:
                                      (index + 1) * batch_size],
                   self.y: test_set_y[index * batch_size:
                                      (index + 1) * batch_size]},
                      name='test')

        valid_score_i = theano.function([index], self.errors,
              givens={
                 self.x: valid_set_x[index * batch_size:
                                     (index + 1) * batch_size],
                 self.y: valid_set_y[index * batch_size:
                                     (index + 1) * batch_size]},
                      name='valid')

        # Create a function that scans the entire validation set
        def valid_score():
            return [valid_score_i(i) for i in xrange(n_valid_batches)]

        # Create a function that scans the entire test set
        def test_score():
            return [test_score_i(i) for i in xrange(n_test_batches)]

        return train_fn, valid_score, test_score

##### create a function to train an Sda and return it.
def trainSda(X_train_minmax, y_train,
             X_validation_minmax, y_validation , 
             X_test_minmax, y_test,
             hidden_layers_sizes = [100, 100, 100], corruption_levels = [0, 0, 0], batch_size = 30 , \
             training_epochs = 100, pretraining_epochs = 100, pretrain_lr = 0.001, finetune_lr=0.1
             ):
    n_visible = X_train_minmax.shape[1]
    # compute number of minibatches for training, validation and testing

    train_set_x, train_set_y = shared_dataset( (X_train_minmax,  y_train), borrow=True)
    valid_set_x, valid_set_y = shared_dataset( (X_validation_minmax,  y_validation), borrow=True)
    test_set_x, test_set_y = shared_dataset( (X_test_minmax,  y_test), borrow=True)
    n_train_batches = train_set_x.get_value(borrow=True).shape[0]
    n_train_batches /= batch_size
    # numpy random generator
    numpy_rng = numpy.random.RandomState(89677)
    print '... building the model'
    # construct the stacked denoising autoencoder class
    sda = SdA(numpy_rng=numpy_rng, n_ins=n_visible,
              hidden_layers_sizes= hidden_layers_sizes,
              n_outs=2)
    #########################
    # PRETRAINING THE MODEL #
    #########################
    print '... getting the pretraining functions'
    pretraining_fns = sda.pretraining_functions(train_set_x=train_set_x,
                                                batch_size=batch_size)

    print '... pre-training the model'
    start_time = time.clock()
    ## Pre-train layer-wise

    for i in xrange(sda.n_layers):
        # go through pretraining epochs
        for epoch in xrange(pretraining_epochs):
            # go through the training set
            c = []
            for batch_index in xrange(n_train_batches):
                c.append(pretraining_fns[i](index=batch_index,
                         corruption=corruption_levels[i],
                         lr=pretrain_lr))
            print 'Pre-training layer %i, epoch %d, cost ' % (i, epoch),
            print numpy.mean(c)

    end_time = time.clock()

    print >> sys.stderr, ('The pretraining code ran for %.2fm' % ((end_time - start_time) / 60.))

    ########################
    # FINETUNING THE MODEL #
    ########################

    # get the training, validation and testing function for the model
    print '... getting the finetuning functions'
    datasets = [(train_set_x, train_set_y) , (valid_set_x, valid_set_y), (test_set_x, test_set_y)]
    train_fn, validate_model, test_model = sda.build_finetune_functions(
                datasets=datasets, batch_size=batch_size,
                learning_rate=finetune_lr)

    print '... finetunning the model'
    # early-stopping parameters
    patience = 10 * n_train_batches  # look as this many examples regardless
    patience_increase = 2.  # wait this much longer when a new best is
                            # found
    improvement_threshold = 0.995  # a relative improvement of this much is
                                   # considered significant
    validation_frequency = min(n_train_batches, patience / 2)
                                  # go through this many
                                  # minibatche before checking the network
                                  # on the validation set; in this case we
                                  # check every epoch

    best_params = None
    best_validation_loss = numpy.inf
    test_score = 0.
    start_time = time.clock()

    done_looping = False
    epoch = 0

    while (epoch < training_epochs) and (not done_looping):
        epoch = epoch + 1
        for minibatch_index in xrange(n_train_batches):
            minibatch_avg_cost = train_fn(minibatch_index)
            iter = (epoch - 1) * n_train_batches + minibatch_index

            if (iter + 1) % validation_frequency == 0:
                validation_losses = validate_model()
                this_validation_loss = numpy.mean(validation_losses)
                print('epoch %i, minibatch %i/%i, validation error %f %%' %
                      (epoch, minibatch_index + 1, n_train_batches,
                       this_validation_loss * 100.))

                # if we got the best validation score until now
                if this_validation_loss < best_validation_loss:

                    #improve patience if loss improvement is good enough
                    if (this_validation_loss < best_validation_loss *
                        improvement_threshold):
                        patience = max(patience, iter * patience_increase)

                    # save best validation score and iteration number
                    best_validation_loss = this_validation_loss
                    best_iter = iter

                    # test it on the test set
                    test_losses = test_model()
                    test_score = numpy.mean(test_losses)
                    print((' epoch %i, minibatch %i/%i, test error of '
                           'best model %f %%') %
                          (epoch, minibatch_index + 1, n_train_batches,
                           test_score * 100.))

            if patience <= iter:
                done_looping = True
                break
    return sda
# the following is a split stacked auto encoder

class Split_SdA(object):
    """Split Stacked denoising auto-encoder class (SdA)

    """

    def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
                 hidden_layers_sizes_A=[100, 100],
                 hidden_layers_sizes_B=[100, 100],
                 n_outs=2,
                 corruption_levels_A=[0, 0],
                 corruption_levels_B=[0, 0]):
        """ This class is made to support a variable number of layers.

        :type numpy_rng: numpy.random.RandomState
        :param numpy_rng: numpy random number generator used to draw initial
                    weights

        :type theano_rng: theano.tensor.shared_randomstreams.RandomStreams
        :param theano_rng: Theano random generator; if None is given one is
                           generated based on a seed drawn from `rng`

        :type n_ins: int
        :param n_ins: dimension of the input to the sdA

        :type n_layers_sizes: list of ints
        :param n_layers_sizes: intermediate layers size, must contain
                               at least one value

        :type n_outs: int
        :param n_outs: dimension of the output of the network

        :type corruption_levels: list of float
        :param corruption_levels: amount of corruption to use for each
                                  layer
        """

        self.sigmoid_layers = []
        self.dA_layers = []
        self.params = []
        self.n_layers = len(hidden_layers_sizes)

        assert self.n_layers > 0

        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
        # allocate symbolic variables for the data
        self.x = T.matrix('x')  # the data is presented as rasterized images
        self.y = T.ivector('y')  # the labels are presented as 1D vector of
                                 # [int] labels

        # The SdA is an MLP, for which all weights of intermediate layers
        # are shared with a different denoising autoencoders
        # We will first construct the SdA as a deep multilayer perceptron,
        # and when constructing each sigmoidal layer we also construct a
        # denoising autoencoder that shares weights with that layer
        # During pretraining we will train these autoencoders (which will
        # lead to chainging the weights of the MLP as well)
        # During finetunining we will finish training the SdA by doing
        # stochastich gradient descent on the MLP

        for i in xrange(self.n_layers):
            # construct the sigmoidal layer

            # the size of the input is either the number of hidden units of
            # the layer below or the input size if we are on the first layer
            if i == 0:
                input_size = n_ins
            else:
                input_size = hidden_layers_sizes[i - 1]

            # the input to this layer is either the activation of the hidden
            # layer below or the input of the SdA if you are on the first
            # layer
            if i == 0:
                layer_input = self.x
            else:
                layer_input = self.sigmoid_layers[-1].output

            sigmoid_layer = HiddenLayer(rng=numpy_rng,
                                        input=layer_input,
                                        n_in=input_size,
                                        n_out=hidden_layers_sizes[i],
                                        activation=T.nnet.sigmoid)
            # add the layer to our list of layers
            self.sigmoid_layers.append(sigmoid_layer)
            # its arguably a philosophical question...
            # but we are going to only declare that the parameters of the
            # sigmoid_layers are parameters of the StackedDAA
            # the visible biases in the dA are parameters of those
            # dA, but not the SdA
            self.params.extend(sigmoid_layer.params)

            # Construct a denoising autoencoder that shared weights with this
            # layer
            dA_layer = dA(numpy_rng=numpy_rng,
                          theano_rng=theano_rng,
                          input=layer_input,
                          n_visible=input_size,
                          n_hidden=hidden_layers_sizes[i],
                          W=sigmoid_layer.W,
                          bhid=sigmoid_layer.b)
            self.dA_layers.append(dA_layer)

        # We now need to add a logistic layer on top of the MLP
        self.logLayer = LogisticRegression(
                         input=self.sigmoid_layers[-1].output,
                         n_in=hidden_layers_sizes[-1], n_out=n_outs)

        self.params.extend(self.logLayer.params)
        # construct a function that implements one step of finetunining

        # compute the cost for second phase of training,
        # defined as the negative log likelihood
        self.finetune_cost = self.logLayer.negative_log_likelihood(self.y)
        # compute the gradients with respect to the model parameters
        # symbolic variable that points to the number of errors made on the
        # minibatch given by self.x and self.y
        self.errors = self.logLayer.errors(self.y)