def test_with_wrong_input_size(self): self.assertRaises(NeuralNet.BadArchitecture, lambda: NeuralNet(input_size=0)) self.assertRaises(NeuralNet.BadArchitecture, lambda: NeuralNet(input_size=-1)) self.assertRaises(NeuralNet.BadArchitecture, lambda: NeuralNet(input_size=-10))
def part_b(file_n): f = open(file_n, "r") ip = int(f.readline()) op = int(f.readline()) batch = int(f.readline()) n = int(f.readline()) h = (f.readline()).rstrip().split(" ") h = map(int, h) h = [ip] + h + [op] if f.readline() == "relu\n": non_lin = 1 else: non_lin = 0 if f.readline() == "fixed\n": eta = 0 else: eta = 1 print ip, op, batch, n print h print non_lin, eta start = timeit.default_timer() net = NeuralNet(h, bool(non_lin)) net.grad_des(x[:, 0:-1], x[:, -1], batch, bool(eta)) stop = timeit.default_timer() t_acc = 100 * net.score(x[:, 0:-1], x[:, -1]) ts_acc = 100 * net.score(tests[:, 0:-1], tests[:, -1]) print "Train accuracy ", t_acc print "Test accuracy ", ts_acc print "Training time ", (stop - start) conf = confusion_matrix(tests[:, -1].tolist(), net.pred(tests[:, 0:-1])) plot_confusion(conf, list(set(tests[:, -1].flatten().tolist())), "For layers " + str(h))
def cross_validation(dataset, percentage_train, iterations, iterations_per_iteration, hidden_layers_sizes, neurons_type='sigmoid', alpha=0.0001, lamb=0.0): """ :param dataset: the full dataset :param percentage_train: float, percentage of instances that needs to go to the test partition :param iterations: int, number of holdouts to execute should call holdout to generate the train and test dicts should call train_NN to train the dataset should call test_NN to get the performances :return: (average_performance, stddev_performance) """ accuracies = [] precisions = [] recalls = [] for it in range(1, iterations + 1): #print("Iteration",it) train_dataset, test_dataset = holdout(dataset, percentage_train) input_size = len(list(train_dataset.keys())[0]) output_size = len(list(train_dataset.values())[0]) nn = NeuralNet(input_size, output_size, hidden_layers_sizes, neurons_type, alpha, lamb) train_NN(nn, train_dataset, iterations_per_iteration) accuracy, precision, recall = test_NN(nn, test_dataset) accuracies.append(accuracy) precisions.append(precision) recalls.append(recall) return mean(accuracies), stdev(accuracies), mean(precisions), stdev( precisions), mean(recalls), stdev(recalls)
def main(): parser = argparse.ArgumentParser(description="Halite II training") parser.add_argument("--model_name", help="Name of the model") parser.add_argument("--minibatch_size", type=int, help="Size of the minibatch", default=100) parser.add_argument("--steps", type=int, help="Number of steps in the training", default=100) parser.add_argument("--data", help="Data directory or zip file containing uncompressed games") parser.add_argument("--cache", help="Location of the model we should continue to train") parser.add_argument("--games_limit", type=int, help="Train on up to games_limit games", default=1000) parser.add_argument("--seed", type=int, help="Random seed to make the training deterministic") parser.add_argument("--bot_to_imitate", help="Name of the bot whose strategy we want to learn") parser.add_argument("--dump_features_location", help="Location of hdf file where the features should be stored") args = parser.parse_args() # Make deterministic if needed if args.seed is not None: np.random.seed(args.seed) nn = NeuralNet(cached_model=args.cache, seed=args.seed) if args.data.endswith('.zip'): raw_data = fetch_data_zip(args.data, args.games_limit) else: raw_data = fetch_data_dir(args.data, args.games_limit) data_input, data_output = parse(raw_data, args.bot_to_imitate, args.dump_features_location) data_size = len(data_input) training_input, training_output = data_input[:int(0.85 * data_size)], data_output[:int(0.85 * data_size)] validation_input, validation_output = data_input[int(0.85 * data_size):], data_output[int(0.85 * data_size):] training_data_size = len(training_input) # randomly permute the data permutation = np.random.permutation(training_data_size) training_input, training_output = training_input[permutation], training_output[permutation] print("Initial, cross validation loss: {}".format(nn.compute_loss(validation_input, validation_output))) curves = [] for s in range(args.steps): start = (s * args.minibatch_size) % training_data_size end = start + args.minibatch_size training_loss = nn.fit(training_input[start:end], training_output[start:end]) if s % 25 == 0 or s == args.steps - 1: validation_loss = nn.compute_loss(validation_input, validation_output) print("Step: {}, cross validation loss: {}, training_loss: {}".format(s, validation_loss, training_loss)) curves.append((s, training_loss, validation_loss)) cf = pd.DataFrame(curves, columns=['step', 'training_loss', 'cv_loss']) fig = cf.plot(x='step', y=['training_loss', 'cv_loss']).get_figure() # Save the trained model, so it can be used by the bot current_directory = os.path.dirname(os.path.abspath(__file__)) model_path = os.path.join(current_directory, os.path.pardir, "models", args.model_name + ".ckpt") print("Training finished, serializing model to {}".format(model_path)) nn.save(model_path) print("Model serialized") curve_path = os.path.join(current_directory, os.path.pardir, "models", args.model_name + "_training_plot.png") fig.savefig(curve_path)
def __init__(self, batch_size, memory_capacity, num_episodes, learning_rate_drop_frame_limit, target_update_frequency, seeds=[104, 106, 108], discount=0.99, delta=1, model_name=None, visualize=False): self.env = CarEnvironment(seed=seeds) self.architecture = NeuralNet() self.explore_rate = Basic_Explore_Rate() self.learning_rate = Basic_Learning_Rate() self.model_path = os.path.dirname( os.path.realpath(__file__)) + '/models/' + model_name self.log_path = self.model_path + '/log' self.visualize = visualize self.damping_mult = 1 self.initialize_tf_variables() self.target_update_frequency = target_update_frequency self.discount = discount self.replay_memory = Replay_Memory(memory_capacity, batch_size) self.training_metadata = Training_Metadata( frame=0, frame_limit=learning_rate_drop_frame_limit, episode=0, num_episodes=num_episodes) self.delta = delta document_parameters(self)
def show_images() -> None: random.seed(10) dp = DataProvider.load_from_folder(dataset_folder) nn = NeuralNet(sizes=[784, 128, 10], epochs=10) nn.train(dp.get_train_x(), dp.get_hot_encoded_train_y(), dp.get_test_x(), dp.get_hot_encoded_test_y()) properly_classified, misclassified = nn.get_properly_classified_and_misclassified_images( dp.get_test_x(), dp.get_hot_encoded_test_y()) print('properly classified') plt.imshow(properly_classified[0].reshape(28, 28), cmap=cm.binary) plt.show() plt.imshow(properly_classified[1].reshape(28, 28), cmap=cm.binary) plt.show() plt.imshow(properly_classified[2].reshape(28, 28), cmap=cm.binary) plt.show() plt.imshow(properly_classified[3].reshape(28, 28), cmap=cm.binary) plt.show() plt.imshow(properly_classified[4].reshape(28, 28), cmap=cm.binary) plt.show() print('missclasified') plt.imshow(misclassified[0].reshape(28, 28), cmap=cm.binary) plt.show() plt.imshow(misclassified[1].reshape(28, 28), cmap=cm.binary) plt.show() plt.imshow(misclassified[2].reshape(28, 28), cmap=cm.binary) plt.show() plt.imshow(misclassified[3].reshape(28, 28), cmap=cm.binary) plt.show() plt.imshow(misclassified[4].reshape(28, 28), cmap=cm.binary) plt.show()
def test_neural_net_back_forward(self): n_in, n_out = 3, 2 weights = np.array([[0, -1, 2], [-3, 4, -5]]) bias = np.arange(n_out)[:, np.newaxis] nn = NeuralNet(MeanSquaredError(), 1e-3, layers=[Linear(n_in, 2, weights, bias), ReLU()]) x = np.array([[[0], [1], [2]]]) y = np.array([[[2], [3]]]) assert y.shape[1] == n_out # |0 -1 2| |0| |0| | 3| |0| | 3| |3| # |-3 4 -5| |1| + |1| = |-6| + |1| = |-5| -> |0| # |2| pred = nn.forward(x) assert np.array_equal(pred, [[[3], [0]]]) nn.compute_loss(pred, y) dL_dx = nn.backward() # |0 -1 2| |0 + dx1| | 3 + 0 - dx2 + 2dx3| | 3 + ...| |3 - dx2 + 2dx3| # |-3 4 -5| |1 + dx2| = |-6 - 3dx1 + 4dx2 - 5dx3| = |-5 + ...| -> |0| # |2 + dx3| The second component is ReLU'ed away # MSE loss results in 2( ... ) so dL = -2dx2 + 4dx3, dL/dx = |0, -2, 4| assert np.array_equal(dL_dx, [[[0], [-2], [4]]])
def part_d(eta_a=False, rlu=False): tt = np.zeros((5, 2)) m = 0 h = [85, 0, 0, 10] l = [5, 10, 15, 20, 25] for i in [5, 10, 15, 20, 25]: print "For 2 layer ", i, eta_a, rlu h[1] = i h[2] = i start = timeit.default_timer() net = NeuralNet(h, rlu) net.grad_des(x[:, 0:-1], x[:, -1], 100, eta_a) stop = timeit.default_timer() t_acc = 100 * net.score(x[:, 0:-1], x[:, -1]) ts_acc = 100 * net.score(tests[:, 0:-1], tests[:, -1]) f_ptr.write("\nFor double layer " + str(eta_a) + str(rlu)) f_ptr.write(str(i)) f_ptr.write("\nTraining acc ") f_ptr.write(str(t_acc)) f_ptr.write("\nTesting acc ") f_ptr.write(str(ts_acc)) f_ptr.write("\nTrainig time ") f_ptr.write(str(stop - start)) print "Train accuracy ", t_acc print "Test accuracy ", ts_acc print "Training time ", (stop - start) tt[m, 0] = t_acc tt[m, 1] = ts_acc m = m + 1 conf = confusion_matrix(tests[:, -1].tolist(), net.pred(tests[:, 0:-1])) plot_confusion(conf, list(set(tests[:, -1].flatten().tolist())), "For 2 layers " + str(h) + str(eta_a) + str(rlu)) print tt plot_metric(tt, l, "For two hidden layers " + str(eta_a) + str(rlu))
def __init__(self, model_location, name): self._name = name self._neural_net = NeuralNet(cached_model=model_location) # Run prediction on random data to make sure that code path is executed at least once before the game starts random_input_data = np.random.rand(PLANET_MAX_NUM, PER_PLANET_FEATURES) predictions = self._neural_net.predict(random_input_data) assert len(predictions) == PLANET_MAX_NUM
def test_weight_shapes(self): learning_rate = 0.8 structure = {'num_inputs': 2, 'num_outputs': 1, 'num_hidden': 5} candidate = NeuralNet(structure, learning_rate) cand_weights = candidate.get_weights() self.assertEqual(cand_weights[0].shape, (3, 5)) self.assertEqual(cand_weights[1].shape, (5, 1))
def __init__(self, listpos, direction, genome): self.listpos = listpos self.direction = direction # 0 = NORTH 1 = SOUTH 2 = EAST 3 = WEST self.genome = genome self.color = genome[0] self.neurons = self.genome del self.neurons[0] self.net = NeuralNet(self.neurons) self.energy = 40000
def example3(): """ Neural net with 1 hidden layer 100 epochs test """ dp = DataProvider.load_from_folder(dataset_folder) nn = NeuralNet(sizes=[784, 128, 10], epochs=100) nn.train(dp.get_train_x(), dp.get_hot_encoded_train_y(), dp.get_test_x(), dp.get_hot_encoded_test_y())
def __init__(self, colour): self.isPlacing = True self.board = self.initialiseBoard() self.neuralNet = NeuralNet() if colour.upper() == "BLACK": self.colour = BLACK else: self.colour = WHITE
def __init__(self, untrained_net=NeuralNet(1024), training_data=Data(TRAINING_DATASET_DIRECTORY), validation_data=Data(VALIDATION_DATASET_DIRECTORY)): self.NN = untrained_net self.network_backup = untrained_net self.training_data = training_data self.validation_data = validation_data self.m_loss = [] self.accuracy = []
def testNN(): train_x, train_y, dev_x, dev_y, test_x, test_y, x_max, y_max = util.loadLinRegData( pad=False) NN = NeuralNet(eps=10**-8, layer_sizes=[9, 7, 5, 3, 1]) NN.fit(train_x, train_y, y_max[0], test_x, test_y) preds = NN.predict(train_x) train_nn_rmse = util.findRMSE(preds, train_y) * y_max[0] preds = NN.predict(test_x) test_nn_rmse = util.findRMSE(preds, test_y) * y_max[0] print('NN RMSE:', test_nn_rmse) return train_nn_rmse, test_nn_rmse
def example5(): """ Tests number of neurons in first hidden layer influence on accuracy of classification; neural net with 1 hidden layer; 10 epochs """ neurons_number = [196, 128, 98, 64, 32, 16] dp = DataProvider.load_from_folder(dataset_folder) for n in neurons_number: nn = NeuralNet(sizes=[784, n, 10]) nn.train(dp.get_train_x(), dp.get_hot_encoded_train_y(), dp.get_test_x(), dp.get_hot_encoded_test_y())
def test_forward_propagate(self): learning_rate = 0.8 structure = {'num_inputs': 2, 'num_outputs': 1, 'num_hidden': 1} candidate = NeuralNet(structure, learning_rate) x = np.array([1, 0]) cand_out = candidate.forward_propagate(x) expected_result = .500615025728 print(cand_out) self.assertAlmostEqual(cand_out, expected_result, 4)
def main(): with gzip.open('../files/mnist.pkl.gz', 'rb') as f: train_set, valid_set, test_set = cPickle.load(f, encoding="bytes") nn = NeuralNet([784, 100, 10], [i for i in range(10)], last_activation=softmax, optimize_weights_init=True) nn.train_optimized(train_set, learning_rate=0.01, batch_size=100, iterations=200, test_data=valid_set, save=True) print(nn.test_accuracy(valid_set))
def neural_net_helper(data, label=None, eta=0.01, iterations=100, layer_structure=[], regression=False, tuning=False): if label == None: label = class_label f = 5 # fold-value tune = data['tune'] perf = [] start_time = time.time() for i in range(f): # set variable to determine if extra printing is required for network # since we're doing cross-validation, print for first fold only print_on = False all_folds = data['folds'].copy() if not tuning: holdout = all_folds[i] else: holdout = tune folds = all_folds folds.pop(i) training = pd.concat(folds) nn = NeuralNet(training, label, eta, iterations, layer_structure, regression, print_on) nn.build() result = nn.test(holdout) if print_on: nn.pretty_print() perf.append(result) elapsed = time.time() - start_time print('\n======== avg. summary of performance ========') if regression: print_helper_regressor(perf, f) else: print_helper_classifier(perf, f) print('total execution time:\t{:.2f}s'.format(elapsed))
def test_networks(filenames, xs, ys): from neural_net import NeuralNet N, D = xs.shape Ny, M = ys.shape assert N == Ny nn = NeuralNet(input_dim=D, output_dim=M) mpes = [] filenames = sorted(filenames) for filename in filenames: nn.load(filename) mpes.append(mean_percent_error(nn, xs, ys)) return mpes, filenames
def test_metrics() -> None: random.seed(10) dp = DataProvider.load_from_folder(dataset_folder) nn = NeuralNet(sizes=[784, 128, 10], epochs=10) nn.train(dp.get_train_x(), dp.get_hot_encoded_train_y(), dp.get_test_x(), dp.get_hot_encoded_test_y()) scores = nn.compute_metrics(dp.get_test_x(), dp.get_hot_encoded_test_y()) print('precyzja_makro: ', scores['precyzja_makro']) print('czulosc_makro: ', scores['czulosc_makro']) print('dokladnosc: ', scores['dokladnosc']) print('raport: ', scores['raport']) print('macierz_bledow: ', scores['macierz_bledow'])
def tet_backward_propagate(self): learning_rate = 0.8 structure = {'num_inputs': 2, 'num_outputs': 1, 'num_hidden': 1} candidate = NeuralNet(structure, learning_rate) cand_weights = candidate.get_weights() X = np.array([np.array([1, 0])]) Y = np.array([np.array([0])]) candidate.train(X, Y) cand_weights = candidate.get_weights() print(cand_weights) # You can do the math to see what the new weights should be # and assert them here. self.assertTrue(True)
def __init__(self, x_pos, y_pos, ball, game, four_player=False): self.bounds = pygame.Rect(x_pos, y_pos, 15, 100) self.ball = ball self.game = game self.four_player = four_player # self.net = NeuralNet(4, 1, 3) self.net = NeuralNet(3, 1, 3) self.generation = 0 self.score = 0 self.fitness = 0 self.contacts_ball = 0 self.name = self.random_name() self.parents = [] self.colors = None self.color_ndx = 0 self.seizure_reduction = 0 self.seize_rate = random.uniform(0, 15) self.set_colors()
def test_neural_net_tends_to_correct(self): n_in, n_out = 4, 2 np.random.seed(12) weights = np.random.normal(size=(n_out, n_in)) bias = np.zeros(n_out)[:, np.newaxis] nn = NeuralNet(MeanSquaredError(), 1e-2, layers=[Linear(n_in, 2, weights, bias)]) x = np.array([[[-1], [0.5], [-0.33], [0.75]]]) y = np.array([[[-0.5], [0.2]]]) for _ in range(1000): pred = nn.forward(x) loss = nn.compute_loss(pred, y) nn.backward() assert np.isclose(loss, 0)
def test_xor(self): learning_rate = .2 structure = {'num_inputs': 2, 'num_hidden': 2, 'num_outputs': 1} candidate = NeuralNet(structure, learning_rate) labeled_data = [(np.array([0, 0]), np.array([0])), (np.array([0, 1]), np.array([1])), (np.array([1, 0]), np.array([1])), (np.array([1, 1]), np.array([0]))] iterations = 15000 trainX, trainY = zip(*labeled_data) trainX = np.array(trainX) trainY = np.array(trainY) candidate.train(trainX, trainY, iterations) cand_error = candidate.test(trainX, trainY) print "XOR Error: ", cand_error
def test_knn_classification_k_3(self): print "Constructing " learning_rate = 0.5 structure = {'num_inputs': 784, 'num_hidden': 30, 'num_outputs': 10} candidate = NeuralNet(structure, learning_rate) #iterations = 15000 iterations = 2 trainX = np.array(self.X_train) #[:20000,:]) trainY = np.array(self.y_train) #[:20000]) candidate.train(trainX, trainY, iterations) cand_error = candidate.test(self.X_train[:100, :], self.y_train[:100]) print "Train fraction: ", cand_error cand_error = candidate.test(self.X_test[:100, :], self.y_test[:100]) print "Test fraction: ", cand_error
def test_our_net(self): np.set_printoptions(precision=3) np.set_printoptions(suppress=True) # nn = NeuralNet([2, 3, 1], [0], last_activation=softmax, dont_update_biases=True) # nn.layers_w = [ # np.array([[0, -2, -4], [4, 1, 2]], dtype=float), # np.array([[2], [4], [6]], dtype=float), # np.array([], dtype=float) # ] # nn.train([[[1, 2]], [1]], learning_rate=0.5, batch_size=1, iterations=1) # print(nn.layers_w) cost = CrossEntropyForBinary([0]) nn = NeuralNet([2, 3, 1], [0], cost=cost, dont_update_biases=True) nn.layers_w = [ np.array([[0, -2, -4], [4, 1, 2]], dtype=float), np.array([[2], [4], [6]], dtype=float), np.array([], dtype=float) ] nn.train([[[1, 2]], [0]], learning_rate=0.5, batch_size=1, iterations=1) print(nn.layers_w)
def main(): xs = np.loadtxt('TrainDigitX.csv.gz', delimiter=',') ys = np.loadtxt('TrainDigitY.csv', dtype='int8') test_xs = np.loadtxt('TestDigitX.csv.gz', delimiter=',') test_ys = np.loadtxt('TestDigitY.csv', dtype='int8') test_xs2 = np.loadtxt('TestDigitX2.csv', delimiter=',') lrs = [0.1, 0.5, 1, 2.5, 5] bs = [100, 200, 400, 1000] Ls = [3, 4, 5] Ns = [32, 64, 128, 256] nn = NeuralNet(1, 64, 5, 300, 100) errors = nn.train(xs, ys) test_err = nn.validate(test_xs, test_ys) print('Test Error for TestDigitX.csv:', test_err) ypreds = nn.predict(test_xs) ypreds2 = nn.predict(test_xs2) np.savetxt('TestDigitXPred.txt', ypreds, fmt='%d') np.savetxt('TestDigitX2Pred.txt', ypreds2, fmt='%d') plt.plot(errors) plt.show()
def trainNetwork(godmode): game = Game() replay_memory = ReplayMemory(5000, 32) neural_net = NeuralNet() r_0, x_t, terminal = game.run_action(0) s_t = np.stack((x_t, x_t), axis=2) random_action_generator = RandomActionGenerator() keyboard_action = KeyboardAction() for t in range(1, 1000): if godmode: action_index = keyboard_action.action() else: action_index = np.argmax(neural_net.predict(s_t)) action_index = random_action_generator.adapt_action(action_index) r_t, x_t1, terminal = game.run_action(action_index) print("TIMESTEP", t, "/ ACTION", action_index, "/ REWARD", r_t, neural_net.state()) x_t1 = np.reshape(x_t1, (80, 80, 1)) s_t1 = np.append(x_t1, s_t[:, :, :1], axis=2) replay_memory.append({ 'state': s_t, 'action': action_index, 'reward': r_t, 'next_state': s_t1, 'terminal': terminal }) s_t = s_t1 replay_memory.save()
def test_neural_net_works_with_batches(self): n_in, n_out = 2, 2 np.random.seed(12) weights = np.random.normal(size=(n_out, n_in)) bias = np.zeros(n_out)[:, np.newaxis] nn = NeuralNet(MeanSquaredError(), 1e-2, layers=[Linear(n_in, 2, weights, bias)]) # batch of 3 x = np.array([[[-1], [0.5]], [[1], [-0.2]], [[-0.33], [0.75]]]) y = x # Why does this take so much longer to converge than the previous one? for _ in range(10000): pred = nn.forward(x) loss = nn.compute_loss(pred, y) nn.backward() assert np.isclose(loss, 0) assert np.all( np.isclose(nn.layers[0].weights, [[1, 0], [0, 1]], atol=1e-3))