def basic_iris(): iris = datasets.load_iris() scaler = pre.Scaler() X = scaler.fit_transform(iris.data) y = ut.all_to_sparse( iris.target, max(iris.target) + 1 ) X, y, X_val, y_val, X_test, y_test = neur.cross_validation_sets(np.array(X), np.array(y), "iris") X_val = np.vstack([X_val, X_test]) y_val = np.vstack([y_val, y_test]) thetas, costs, val_costs = neur.gradient_decent(np.array(X), np.array(y), #hidden_layer_sz = 11, hidden_layer_sz = 20, iter = 8000, wd_coef = 0.0, learning_rate = 0.07, momentum_multiplier = 0.3, rand_init_epsilon = 0.12, do_early_stopping = True, #do_dropout = True, dropout_percentage = 0.9, do_learning_adapt = True, X_val = np.array(X_val), y_val = np.array(y_val)) 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()
def est_gradient_decsent(self): iris = datasets.load_iris() X = iris.data scaler = pre.Scaler() X = scaler.fit_transform(X) y = self.all_to_sparse(iris.target, max(iris.target) + 1) X, y, X_val, y_val, X_test, y_test = neur.cross_validation_sets(np.array(X), np.array(y)) thetas, costs, val_costs = neur.gradient_decent(np.array(X), np.array(y), np.array(X_val), np.array(y_val))
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()
def est_gradient_decsent(self): iris = datasets.load_iris() X = iris.data scaler = pre.Scaler() X = scaler.fit_transform(X) y = self.all_to_sparse(iris.target, max(iris.target) + 1) X, y, X_val, y_val, X_test, y_test = neur.cross_validation_sets( np.array(X), np.array(y)) thetas, costs, val_costs = neur.gradient_decent( np.array(X), np.array(y), np.array(X_val), np.array(y_val))
def basic_iris(): iris = datasets.load_iris() scaler = pre.Scaler() X = scaler.fit_transform(iris.data) y = ut.all_to_sparse(iris.target, max(iris.target) + 1) X, y, X_val, y_val, X_test, y_test = neur.cross_validation_sets( np.array(X), np.array(y), "iris") X_val = np.vstack([X_val, X_test]) y_val = np.vstack([y_val, y_test]) thetas, costs, val_costs = neur.gradient_decent( np.array(X), np.array(y), #hidden_layer_sz = 11, hidden_layer_sz=20, iter=8000, wd_coef=0.0, learning_rate=0.07, momentum_multiplier=0.3, rand_init_epsilon=0.12, do_early_stopping=True, #do_dropout = True, dropout_percentage=0.9, do_learning_adapt=True, X_val=np.array(X_val), y_val=np.array(y_val)) 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()
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()
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()
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()