コード例 #1
0
 def __init__(self, size, hidden_layer_size, denoise = False, denoise_percent = 0.3, contractive = False):
     thetas = neur.create_initial_thetas([size, hidden_layer_size, size], 0.1)
     self.encode_weights = thetas[0]
     self.decode_weights = thetas[1]
     self.denoise = denoise
     self.corrupt = denoise_percent
     self.contractive = contractive
コード例 #2
0
def autoencoder_example():
    mnist_train   = np.fromfile('mnist_training.csv', sep=" ")
    mnist_train   = np.array(mnist_train.reshape(256, 1000)).transpose()
    
    mnist_targets   = np.fromfile('mnist_training_targets.csv', sep=" ")
    mnist_targets   = np.array(mnist_targets.reshape(10, 1000)).transpose()
    
    X = mnist_train
    y = mnist_targets
    X, y, X_val, y_val, X_test, y_test = neur.cross_validation_sets(np.array(X), np.array(y), "digits_rbm_mnist_autoencode")
    X_val = np.vstack([X_val, X_test]) 
    y_val = np.vstack([y_val, y_test]) 

    hid_layer = 300

    autoenc = ac.Autoencoder(X.shape[1], hid_layer, denoise = True, denoise_percent = 0.5)
    costs, val_costs = autoenc.optimize(X, iters = 1500, learning_rate = 0.1, val_set = X_val)

    print "::: first encoding done :::" 
    print "training error:",   costs[-1:][0]
    print "validation error:", val_costs[-1:][0]
    print "lowest validation error:", min(val_costs)
    plt.plot(costs, label='cost')
    plt.plot(val_costs, label='val cost')
    plt.legend()
    plt.ylabel('error rate')
    plt.show()        


    thetas  = neur.create_initial_thetas([64, hid_layer, 10], 0.12)
    thetas[0] = autoenc.encode_weights

    thetas, costs, val_costs = neur.gradient_decent(X, y,
                                                    learning_rate = 0.01,
                                                    hidden_layer_sz = hid_layer,
                                                    iter = 5000,
                                                    thetas = thetas,
                                                    X_val = X_val, 
                                                    y_val = y_val,
                                                    do_dropout = True,
                                                    dropout_percentage = 0.9,
                                                    do_early_stopping = True)

    h_x, a = neur.forward_prop(X_val, thetas)
    print "percentage correct predictions: ", ut.percent_equal(ut.map_to_max_binary_result(h_x), y_val)
    print "training error:",   costs[-1:][0]
    print "validation error:", val_costs[-1:][0]
    print "lowest validation error:", min(val_costs)
    plt.plot(costs, label='cost')
    plt.plot(val_costs, label='val cost')
    plt.legend()
    plt.ylabel('error rate')
    plt.show()        
コード例 #3
0
def rbm_example():
    digits = datasets.load_digits()
    X = digits.images.reshape((digits.images.shape[0], -1))
    X = (X / 16.0)
    y = ut.all_to_sparse(digits.target, max(digits.target) + 1)
    X, y, X_val, y_val, X_test, y_test = neur.cross_validation_sets(
        np.array(X), np.array(y), "digits_rbm", True)
    X_val = np.vstack([X_val, X_test])
    y_val = np.vstack([y_val, y_test])

    hid_layer = 300

    bm = rbm.RBM(64, hid_layer)
    #exit()

    costs = bm.optimize(neur.mini_batch_generator(X), 2000, 0.08)
    print "validate squared_error", bm.validate(X_val)
    #exit()

    filename = './random_set_cache/data_rbm_run.pkl'

    first_layer_weights = np.hstack([np.zeros((hid_layer, 1)), bm.weights])
    #pickle.dump(first_layer_weights, open(filename, 'w'))

    # first_layer_weights = pickle.load(open(filename, 'r'))

    thetas = neur.create_initial_thetas([64, hid_layer, 10], 0.12)
    thetas[0] = first_layer_weights

    thetas, costs, val_costs = neur.gradient_decent_gen(
        izip(neur.mini_batch_generator(X, 10),
             neur.mini_batch_generator(y, 10)),
        learning_rate=0.05,
        hidden_layer_sz=hid_layer,
        iter=8000,
        thetas=thetas,
        X_val=X_val,
        y_val=y_val,
        do_early_stopping=True)

    h_x, a = neur.forward_prop(X_test, thetas)
    print "percentage correct predictions: ", ut.percent_equal(
        ut.map_to_max_binary_result(h_x), y_test)
    print "training error:", costs[-1:][0]
    print "validation error:", val_costs[-1:][0]
    print "lowest validation error:", min(val_costs)
    plt.plot(costs, label='cost')
    plt.plot(val_costs, label='val cost')
    plt.legend()
    plt.ylabel('error rate')
    plt.show()
コード例 #4
0
ファイル: rbm_example.py プロジェクト: bhauman/neurpy
def rbm_example():
    digits = datasets.load_digits()
    X = digits.images.reshape((digits.images.shape[0], -1))
    X = (X / 16.0)
    y = ut.all_to_sparse( digits.target, max(digits.target) + 1 )
    X, y, X_val, y_val, X_test, y_test = neur.cross_validation_sets(np.array(X), np.array(y), "digits_rbm", True)
    X_val = np.vstack([X_val, X_test]) 
    y_val = np.vstack([y_val, y_test]) 

    hid_layer = 300

    bm = rbm.RBM(64, hid_layer)
    #exit()
    
    costs = bm.optimize(neur.mini_batch_generator(X), 2000, 0.08)
    print "validate squared_error",  bm.validate(X_val)
    #exit()

    filename = './random_set_cache/data_rbm_run.pkl'

    first_layer_weights = np.hstack([np.zeros((hid_layer,1)), bm.weights])
    #pickle.dump(first_layer_weights, open(filename, 'w'))

    # first_layer_weights = pickle.load(open(filename, 'r'))

    thetas  = neur.create_initial_thetas([64, hid_layer, 10], 0.12)
    thetas[0] =  first_layer_weights

    thetas, costs, val_costs = neur.gradient_decent_gen(izip(neur.mini_batch_generator(X, 10), 
                                                             neur.mini_batch_generator(y, 10)),
                                                        learning_rate = 0.05,
                                                        hidden_layer_sz = hid_layer,
                                                        iter = 8000,
                                                        thetas = thetas, 
                                                        X_val = X_val, 
                                                        y_val = y_val,
                                                        do_early_stopping = True)

    h_x, a = neur.forward_prop(X_test, thetas)
    print "percentage correct predictions: ", ut.percent_equal(ut.map_to_max_binary_result(h_x), y_test)
    print "training error:",   costs[-1:][0]
    print "validation error:", val_costs[-1:][0]
    print "lowest validation error:", min(val_costs)
    plt.plot(costs, label='cost')
    plt.plot(val_costs, label='val cost')
    plt.legend()
    plt.ylabel('error rate')
    plt.show()        
コード例 #5
0
ファイル: rbm_mnist_example.py プロジェクト: bhauman/neurpy
def rbm_mnist_example():
    mnist_train   = np.fromfile('mnist_training.csv', sep=" ")
    mnist_train   = np.array(mnist_train.reshape(256, 1000)).transpose()
    
    mnist_targets   = np.fromfile('mnist_training_targets.csv', sep=" ")
    mnist_targets   = np.array(mnist_targets.reshape(10, 1000)).transpose()
    
    X = mnist_train
    y = mnist_targets
    X, y, X_val, y_val, X_test, y_test = neur.cross_validation_sets(np.array(X), np.array(y), "digits_rbm_mnist")
    X_val = np.vstack([X_val, X_test]) 
    y_val = np.vstack([y_val, y_test]) 

    hid_layer = 300

    bm = rbm.RBM(256, hid_layer)
    #exit()
    
    costs = bm.optimize(X, 1000, 0.2, val_set = X_val)

    X = bm.prop_up(X)
    X_val = bm.prop_up(X_val)

    bm2 = rbm.RBM(hid_layer, hid_layer + 50)
    costs = bm2.optimize(X, 600, 0.2, val_set = X_val)

    X = bm2.prop_up(X)
    X_val = bm2.prop_up(X_val)

    bm3 = rbm.RBM(hid_layer + 50, hid_layer)
    costs = bm3.optimize(X, 600, 0.2, val_set = X_val)

    # lets change X


    
    filename = './random_set_cache/data_rbm_run_without_bias.pkl'

    first_layer_weights = np.hstack([np.zeros((hid_layer,1)), bm3.weights]) # without bias
    #first_layer_weights = np.hstack([bm.hidden_bias.reshape(hid_layer, 1), bm.weights]) # with bias

    #pickle.dump(first_layer_weights, open(filename, 'w'))

    #exit()

    #first_layer_weights = pickle.load(open(filename, 'r'))
    
    thetas  = neur.create_initial_thetas([64, hid_layer, 10], 0.12)
    thetas[0] =  first_layer_weights

    thetas, costs, val_costs = neur.gradient_decent(X, y,
                                                    learning_rate = 0.1,
                                                    hidden_layer_sz = hid_layer,
                                                    iter = 3000,
                                                    thetas = thetas,
                                                    X_val = X_val, 
                                                    y_val = y_val,
                                                    do_dropout = True,
                                                    dropout_percentage = 0.7,
                                                    do_early_stopping = True)

    h_x, a = neur.forward_prop(X_val, thetas)
    print "percentage correct predictions: ", ut.percent_equal(ut.map_to_max_binary_result(h_x), y_val)
    print "training error:",   costs[-1:][0]
    print "validation error:", val_costs[-1:][0]
    print "lowest validation error:", min(val_costs)
    plt.plot(costs, label='cost')
    plt.plot(val_costs, label='val cost')
    plt.legend()
    plt.ylabel('error rate')
    plt.show()        
コード例 #6
0
ファイル: rbm_mnist_example.py プロジェクト: bhauman/neurpy
def rbm_mnist_example():
    mnist_train = np.fromfile('mnist_training.csv', sep=" ")
    mnist_train = np.array(mnist_train.reshape(256, 1000)).transpose()

    mnist_targets = np.fromfile('mnist_training_targets.csv', sep=" ")
    mnist_targets = np.array(mnist_targets.reshape(10, 1000)).transpose()

    X = mnist_train
    y = mnist_targets
    X, y, X_val, y_val, X_test, y_test = neur.cross_validation_sets(
        np.array(X), np.array(y), "digits_rbm_mnist")
    X_val = np.vstack([X_val, X_test])
    y_val = np.vstack([y_val, y_test])

    hid_layer = 300

    bm = rbm.RBM(256, hid_layer)
    #exit()

    costs = bm.optimize(X, 1000, 0.2, val_set=X_val)

    X = bm.prop_up(X)
    X_val = bm.prop_up(X_val)

    bm2 = rbm.RBM(hid_layer, hid_layer + 50)
    costs = bm2.optimize(X, 600, 0.2, val_set=X_val)

    X = bm2.prop_up(X)
    X_val = bm2.prop_up(X_val)

    bm3 = rbm.RBM(hid_layer + 50, hid_layer)
    costs = bm3.optimize(X, 600, 0.2, val_set=X_val)

    # lets change X

    filename = './random_set_cache/data_rbm_run_without_bias.pkl'

    first_layer_weights = np.hstack([np.zeros((hid_layer, 1)),
                                     bm3.weights])  # without bias
    #first_layer_weights = np.hstack([bm.hidden_bias.reshape(hid_layer, 1), bm.weights]) # with bias

    #pickle.dump(first_layer_weights, open(filename, 'w'))

    #exit()

    #first_layer_weights = pickle.load(open(filename, 'r'))

    thetas = neur.create_initial_thetas([64, hid_layer, 10], 0.12)
    thetas[0] = first_layer_weights

    thetas, costs, val_costs = neur.gradient_decent(X,
                                                    y,
                                                    learning_rate=0.1,
                                                    hidden_layer_sz=hid_layer,
                                                    iter=3000,
                                                    thetas=thetas,
                                                    X_val=X_val,
                                                    y_val=y_val,
                                                    do_dropout=True,
                                                    dropout_percentage=0.7,
                                                    do_early_stopping=True)

    h_x, a = neur.forward_prop(X_val, thetas)
    print "percentage correct predictions: ", ut.percent_equal(
        ut.map_to_max_binary_result(h_x), y_val)
    print "training error:", costs[-1:][0]
    print "validation error:", val_costs[-1:][0]
    print "lowest validation error:", min(val_costs)
    plt.plot(costs, label='cost')
    plt.plot(val_costs, label='val cost')
    plt.legend()
    plt.ylabel('error rate')
    plt.show()
コード例 #7
0
def autoencoder_example():
    mnist_train = np.fromfile('mnist_training.csv', sep=" ")
    mnist_train = np.array(mnist_train.reshape(256, 1000)).transpose()

    mnist_targets = np.fromfile('mnist_training_targets.csv', sep=" ")
    mnist_targets = np.array(mnist_targets.reshape(10, 1000)).transpose()

    X = mnist_train
    y = mnist_targets
    X, y, X_val, y_val, X_test, y_test = neur.cross_validation_sets(
        np.array(X), np.array(y), "digits_rbm_mnist_autoencode")
    X_val = np.vstack([X_val, X_test])
    y_val = np.vstack([y_val, y_test])

    hid_layer = 300

    autoenc = ac.Autoencoder(X.shape[1],
                             hid_layer,
                             denoise=True,
                             denoise_percent=0.5)
    costs, val_costs = autoenc.optimize(X,
                                        iters=1500,
                                        learning_rate=0.1,
                                        val_set=X_val)

    print "::: first encoding done :::"
    print "training error:", costs[-1:][0]
    print "validation error:", val_costs[-1:][0]
    print "lowest validation error:", min(val_costs)
    plt.plot(costs, label='cost')
    plt.plot(val_costs, label='val cost')
    plt.legend()
    plt.ylabel('error rate')
    plt.show()

    thetas = neur.create_initial_thetas([64, hid_layer, 10], 0.12)
    thetas[0] = autoenc.encode_weights

    thetas, costs, val_costs = neur.gradient_decent(X,
                                                    y,
                                                    learning_rate=0.01,
                                                    hidden_layer_sz=hid_layer,
                                                    iter=5000,
                                                    thetas=thetas,
                                                    X_val=X_val,
                                                    y_val=y_val,
                                                    do_dropout=True,
                                                    dropout_percentage=0.9,
                                                    do_early_stopping=True)

    h_x, a = neur.forward_prop(X_val, thetas)
    print "percentage correct predictions: ", ut.percent_equal(
        ut.map_to_max_binary_result(h_x), y_val)
    print "training error:", costs[-1:][0]
    print "validation error:", val_costs[-1:][0]
    print "lowest validation error:", min(val_costs)
    plt.plot(costs, label='cost')
    plt.plot(val_costs, label='val cost')
    plt.legend()
    plt.ylabel('error rate')
    plt.show()