Ejemplo n.º 1
0
def demo(structure = [25**2, 23**2, 21**2,19**2,16**2, 15**2]):
    # Getting the data
    X,y = utils.load_mnist()
    
    
    autoencoder = StackedDA([100], alpha=0.01)
    autoencoder.pre_train(X[:1000], 10)
    
    y = utils.makeMultiClass(y)
    autoencoder.fine_tune(X[:1000], y[:1000], learning_layer=200, n_iters=20, alpha=0.01)

    W = autoencoder.W[0].T[:, 1:]
    W = utils.saveTiles(W, img_shape= (28,28), tile_shape=(10,10), filename="Results/res_dA.png")
Ejemplo n.º 2
0
def demo():
    X,y = utils.load_mnist()
    y = utils.makeMultiClass(y)
    
    # building the SDA
    sDA = StackedDA([100])

    # pre-trainning the SDA
    sDA.pre_train(X[:100], noise_rate=0.3, epochs=1)

    # saving a PNG representation of the first layer
    W = sDA.Layers[0].W.T[:, 1:]
    utils.saveTiles(W, img_shape= (28,28), tile_shape=(10,10), filename="results/res_dA.png")

    # adding the final layer
    sDA.finalLayer(X[:37500], y[:37500], epochs=2)

    # trainning the whole network
    sDA.fine_tune(X[:37500], y[:37500], epochs=2)

    # predicting using the SDA
    pred = sDA.predict(X[37500:]).argmax(1)

    # let's see how the network did
    y = y[37500:].argmax(1)
    e = 0.0
    for i in range(len(y)):
        e += y[i]==pred[i]

    # printing the result, this structure should result in 80% accuracy
    print "accuracy: %2.2f%%"%(100*e/len(y))

    return sDA
Ejemplo n.º 3
0
def useLayers():
    X, y = utils.load_mnist()
    y = utils.makeMultiClass(y)

    # Layers
    sDA = StackedDA([100])
    sDA.pre_train(X[:1000], rate=0.5, n_iters=500)
    sDA.finalLayer(y[:1000], learner_size=200, n_iters=1)
    sDA.fine_tune(X[:1000], y[:1000], n_iters=1)
    pred = sDA.predict(X)

    W = sDA.Layers[0].W.T[:, 1:]
    W = utils.saveTiles(W,
                        img_shape=(28, 28),
                        tile_shape=(10, 10),
                        filename="Results/res_dA.png")
    return pred, y
Ejemplo n.º 4
0
def useLayers():
    X,y = utils.load_mnist()
    y = utils.makeMultiClass(y)
    
    # Layers
    sDA = StackedDA([100])
    sDA.pre_train(X[:1000], rate=0.5, n_iters=500)
    sDA.finalLayer(y[:1000], learner_size=200, n_iters=1)
    sDA.fine_tune(X[:1000], y[:1000], n_iters=1)
    pred = sDA.predict(X)
    
    W = sDA.Layers[0].W.T[:, 1:]
    W = utils.saveTiles(W, img_shape= (28,28), tile_shape=(10,10), filename="Results/res_dA.png")
    return pred, y
Ejemplo n.º 5
0
def demo(structure=[25**2, 23**2, 21**2, 19**2, 16**2, 15**2]):
    # Getting the data
    X, y = utils.load_mnist()

    autoencoder = StackedDA([100], alpha=0.01)
    autoencoder.pre_train(X[:1000], 10)

    y = utils.makeMultiClass(y)
    autoencoder.fine_tune(X[:1000],
                          y[:1000],
                          learning_layer=200,
                          n_iters=20,
                          alpha=0.01)

    W = autoencoder.W[0].T[:, 1:]
    W = utils.saveTiles(W,
                        img_shape=(28, 28),
                        tile_shape=(10, 10),
                        filename="Results/res_dA.png")
Ejemplo n.º 6
0
def SdA_gridsearch():
    funcs = DLFuncs_SdA()
    traindata_path='Z://Cristina//Section3//SdA_experiments//allLpatches.pklz'
    trainUdata_path='Z://Cristina//Section3//SdA_experiments//allUpatches.pklz'
    labeldata_path='Z://Cristina//Section3//SdA_experiments//allLabels.pklz'
    
    datasets = funcs.load_wUdata(traindata_path, labeldata_path, trainUdata_path)
 
    ############
    ### plotting labels 
    ############
    dftrain = pd.DataFrame();   dfvalid = pd.DataFrame();   dftest = pd.DataFrame();   
    # get training data in numpy format   
    X,y = datasets[3]
    Xtrain = np.asarray(X)
    # extract one img
    Xtrain = Xtrain.reshape(Xtrain.shape[0], 4, 900)
    Xtrain = Xtrain[:,0,:]
    ytrain = utils2.makeMultiClass(y)
    dftrain['y'] = pd.Series(['y0' if y[yi]==0 else 'y1' for yi in range(len(ytrain))])
    dftrain['group'] = pd.Series(np.repeat('train',len(y)))
    
    # get valid data in numpy format   
    X,y = datasets[4]
    Xvalid = np.asarray(X)
    # extract one img
    Xvalid = Xvalid.reshape(Xvalid.shape[0], 4, 900)
    Xvalid = Xvalid[:,0,:]
    yvalid = utils2.makeMultiClass(y)
    dfvalid['y'] = pd.Series(['y0' if y[yi]==0 else 'y1' for yi in range(len(yvalid))])
    dfvalid['group'] = pd.Series(np.repeat('valid',len(y)))
    
    # get training data in numpy format   
    X,y = datasets[5]
    Xtest = np.asarray(X)
    # extract one img
    Xtest = Xtest.reshape(Xtest.shape[0], 4, 900)
    Xtest = Xtest[:,0,:]
    ytest = utils2.makeMultiClass(y)
    dftest['y'] = pd.Series(['y0' if y[yi]==0 else 'y1' for yi in range(len(ytest))])
    dftest['group'] = pd.Series(np.repeat('test',len(y)))
    
    # concat and plot
    df = pd.concat([dftrain, dfvalid, dftest], axis=0)
    sns.set(style="whitegrid")
    # Draw a nested barplot to show survival for class and sex
    g = sns.countplot(x="group", hue="y", palette="muted", data=df)
    for p in g.patches:
        height = p.get_height()
        g.text(p.get_x(), height+ 3, '%1.2f'%(height))
    fig = g.get_figure()
    fig.savefig('grid_searchResults/datasets.png')

    ############
    ### Define grid search parameters
    ############
    nlayers = [1,2,3,4]
    nhiddens = [100,225,400,900]
    hidden_layers_sidelen = [10,15,20,30] 
    nnoise_rate = [0.35]
    nalpha = [0.01,0.1,0.5] 
    batch_size = [500,100,10]
    # note batch_size is fixed to 1 and epochs is fixed to 25
    k=0
    BestAveAccuracy = 0
    #dfresults = pd.DataFrame()# when first time
    
    ###########
    ## Process Resuts
    ###########
    pkl_filegridS = open('grid_searchResults/gridSearch_results2.pkl','rb')
    dfresults = pickle.load(pkl_filegridS)
    print dfresults
    #items = itertools.product(nlayers, nhiddens, nnoise_rate, nalpha, batch_size)
    #item=items.next()
    
    for item in itertools.product(nlayers, nhiddens, nnoise_rate, nalpha, batch_size): 
        k+=1
        print(k,item)
        
        if(k>90):
            # setup the training functions
            nlayer = item[0]
            nhidden = item[1]
            sidelen = hidden_layers_sidelen[ nhiddens.index(nhidden) ]
            noiseRate = item[2]
            alpha = item[3]
            batchS = item[4]
            if(nlayer == 1):
                StackedDA_layers = [nhidden]
            if(nlayer == 2):
                StackedDA_layers = [nhidden,nhidden]
            if(nlayer == 3):
                StackedDA_layers = [nhidden,nhidden,nhidden]
                
            # building the SDA
            sDA = StackedDA(StackedDA_layers, alpha, item)
            
            # pre-trainning the SDA
            sDA.pre_train(Xtrain, noise_rate=noiseRate, epochs=200, batchsize=batchS)
            
            #####################################
            # saving a PNG representation of the first layer
            #####################################
            # Plot images in 2D       
            W0 = sDA.Layers[0].W.T[:, 1:]
            imageW0 = Image.fromarray(
                utils.tile_raster_images(X=W0 , img_shape=(30, 30), 
                                   tile_shape=(10, 10),
                                   tile_spacing=(1, 1)))
        
            #show and save                     
            imageW0.save('grid_searchResults/filters_hidden_1stlayer_'+str(item)+'.png')
            # prepare display    
            fig, ax = plt.subplots()  
            ax.imshow(imageW0,  cmap="Greys_r")
            ax.axes.get_xaxis().set_visible(False)
            ax.axes.get_yaxis().set_visible(False)
            
           # W = sDA.Layers[0].W.T[:, 1:]
            #utils2.saveTiles(W, img_shape= (30,30), tile_shape=(20,10), filename='grid_searchResults/'+'.png")
        
            # adding the final layer
            sDA.finalLayer(Xtrain, ytrain, epochs=20)
        
            # trainning the whole network
            sDA.fine_tune(Xtrain, ytrain, epochs=20)
        
            # predicting using the SDA
            pred = sDA.predict(Xvalid).argmax(1)
            
            # let's see how the network did
            y = yvalid.argmax(1)
            e0 = 0.0; y0 = len([0 for yi in range(len(y)) if y[yi]==0])
            e1 = 0.0; y1 = len([1 for yi in range(len(y)) if y[yi]==1])
            for i in range(len(y)):
                if(y[i] == 1):
                    #print(y[i]==pred[i], y[i])
                    e1 += y[i]==pred[i]
                if(y[i] == 0):
                    #print(y[i]==pred[i], y[i])
                    e0 += y[i]==pred[i]
        
            # printing the result, this structure should result in 80% accuracy
            print "accuracy for class 0: %2.2f%%"%(100*e0/y0)
            print "accuracy for class 1: %2.2f%%"%(100*e1/y1)
            
            # append results
            accuracy0 = 100*e0/y0
            accuracy1 = 100*e1/y1
            itemresults = item + (accuracy0, accuracy1)
            dSresultsiter =  pd.DataFrame(data=np.array(itemresults)).T
            dSresultsiter.columns=['nlayers', 'nhiddens', 'nnoise_rate', 'nalpha', 'batchsize', 'accuracy0','accuracy1']
                  
            dfresults = dfresults.append(dSresultsiter)      
            AveAccuracy = (accuracy0 + accuracy1)/2
            # find best model so far
            if( AveAccuracy > BestAveAccuracy):
                bestsDA = sDA
                BestAveAccuracy = AveAccuracy
                print("best Accuracy = %d, for SdA:" % AveAccuracy)
                print(bestsDA)
                
            # save the best model
            with open('grid_searchResults/gridSearch_results2.pkl', 'wb') as f:
                pickle.dump(dfresults, f)
            
        ### continue
        print(dfresults)

    return 
Ejemplo n.º 7
0
def main():

    # open and load csv files
    time_load_start = time.clock()
    X_train, y_train = fipr.load_csv("train_file.csv", True)
    X_test, y_test = fipr.load_csv("test_file.csv", True)
    #y_train = y_train.flatten()
    #y_test = y_test.flatten()
    time_load_end = time.clock()
    print("Loading finished, loading time: %g seconds" %
          (time_load_end - time_load_start))

    X_test_even, y_test_even = fipr.load_csv("test_file_even.csv", True)

    training_data = X_train
    training_labels = y_train

    test_data = X_test
    test_labels = y_test

    test_data_even = X_test_even
    test_labels_even = y_test_even

    # building the SDA
    sDA = StackedDA([100])

    # start counting time for training
    time_train_start = time.clock()
    print('Pre-training...')

    # pre-trainning the SDA
    sDA.pre_train(training_data[:1000], noise_rate=0.3, epochs=100)
    print('Training Network...')

    # adding the final layer
    sDA.finalLayer(training_data, training_labels, epochs=500)

    # trainning the whole network
    sDA.fine_tune(training_data, training_labels, epochs=500)

    # print training time
    time_train_end = time.clock()
    print("Training finished, training time: %g seconds \n" %
          (time_train_end - time_train_start))

    # start counting time for testing
    time_test_start = time.clock()

    print('Testing performance...')
    # predicting using the SDA
    y_pred = sDA.predict(test_data).argmax(1)

    # print simple precision metric to the console
    print('Accuracy:  ' + str(fipr.compute_accuracy(y_test, y_pred)))

    # print testing time
    time_test_end = time.clock()
    print("Testing finished, testing time: %g seconds  \n" %
          (time_test_end - time_test_start))

    # Even set test
    y_pred_even = sDA.predict(test_data_even).argmax(1)

    # print simple precision metric to the console
    print('Accuracy on EVEN set:  ' +
          str(fipr.compute_accuracy(y_test_even, y_pred_even)))

    return sDA
Ejemplo n.º 8
0
    g = sns.countplot(x="group", hue="y", palette="muted", data=df)
    
    

a = ["1"]
b = ["0"]
c = ["a","b","c"]
d = ["d","e","f"]

for item in itertools.product(a, b, c, d): 
    print(item)
    

    
    # building the SDA
    sDA = StackedDA([400, 400])

    # pre-trainning the SDA
    sDA.pre_train(Xvalid, noise_rate=0.3, epochs=1)

    # saving a PNG representation of the first layer
    W = sDA.Layers[0].W.T[:, 1:]
    utils2.saveTiles(W, img_shape= (30,30), tile_shape=(20,10), filename="res_dA.png")

    # adding the final layer
    sDA.finalLayer(Xtrain, ytrain, epochs=1)

    # trainning the whole network
    sDA.fine_tune(Xtrain, ytrain, epochs=1)

    # predicting using the SDA