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
class Main: def __init__(self): self.neural_net = NeuralNet() self.connection = pika.BlockingConnection( pika.ConnectionParameters("localhost")) def connect(self): channel = self.connection.channel() channel.queue_declare(queue="neural_net") channel.basic_consume(self.callback, queue="neural_net", no_ack=True) print("[*] Waiting for messages. To exit press CTRL+C") channel.start_consuming() def callback(self, channel, method, properties, body): print("[x] Received %r" % body) parsed_body = json.loads(body) if isinstance(parsed_body, list): self.neural_net.update_training_set(body) self.neural_net.train() else: request = parsed_body['request'] result = self.neural_net.predict(request) self.send_prediction(result) def send_prediction(self, result): print("Send %r" % result) channel = self.connection.channel() channel.queue_declare(queue="results") channel.basic_publish(exchange='', routing_key='', body=result)
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()
) '''Play with Neural Net hyperparams''' layer_architectures = [[15, 3, 1], [19, 7, 3, 1], [9, 7, 5, 3, 1]] results = [] for arch in layer_architectures: cur_result = [] cur_result.append(arch) print("Fitting NN with architecture", arch) NN = NeuralNet(layer_sizes=arch) NN.fit(train_x, train_y, y_max[0]) preds = NN.predict(train_x) rmse = util.findRMSE(preds, train_y) * y_max[0] cur_result.append(rmse) preds = NN.predict(dev_x) rmse = util.findRMSE(preds, dev_y) * y_max[0] cur_result.append(rmse) results.append(cur_result) print('Train rmse', cur_result[1], 'Dev rmse', cur_result[2]) results.sort(key=lambda x: x[2]) for result in results: print('Architecture: \t', result[0], 'Train RMSE: \t', result[1], 'Dev RMSE: \t', result[2]) np.savetxt('Output/nn_rmse_various_architectures.csv', results)
Xtest, ytest = test_set print(X.shape) binarizer = LabelBinarizer() Y = binarizer.fit_transform(y) hidden_layer_sizes = [50] model = NeuralNet(hidden_layer_sizes, sgd=0) t = time.time() model.fit(X, Y) print("Fitting took %d seconds" % (time.time() - t)) # Comput training error yhat = model.predict(X) trainError = np.mean(yhat != y) print("Training error = ", trainError) # Compute test error yhat = model.predict(Xtest) testError = np.mean(yhat != ytest) print("Test error = ", testError) elif task == "2.1": W = np.array([[-2, 2, -1], [1, -2, 0]]) x = np.array([-3, 2, 2]) v = np.array([[3], [1]]) z = x @ W.T hz = 1 / (1 + np.exp(-1 * z))
# Results matrix for all parameter combinations cv_results = grid_search.cv_results_ test_score = cv_results['mean_test_score'] test_score = test_score.reshape(n_params) print('Mean fit time = %.2e s' % np.mean(cv_results['mean_fit_time'])) print('### GRID SEARCH RESULTS ###') print('Best params:') pprint.pprint(grid_search.best_params_) print('Test accuracy = %.3f' % grid_search.best_score_) # Accuracy using our splitted dataset and not CV results best_estimator.fit(X_train, y_train) y_fit = best_estimator.predict(X_train) y_pred = best_estimator.predict(X_test) y_proba = best_estimator.predict_proba(X_test) print('\n### NORMAL DATA TRAIN TEST SPLIT ###') print('Train accuracy = %.3f' % best_estimator.score(X_train, y_train)) print('Test accuracy = %.3f' % best_estimator.score(X_test, y_test)) # Creating y-arrays with labels due to oddities related to scikitplot version that was installed y_1 = ['cat' if i == 0 else 'dog' for i in y_test] y_2 = ['cat' if i == 0 else 'dog' for i in y_pred] ######################################################################################################## # Plotting save = 'nbins%d_pca%d_seed%d_ts%.2f' % (n_bins, n_pca, seed, test_size) fs = 12
class Bot: 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 play(self): """ Play a game using stdin/stdout. """ # Initialize the game. game = hlt.Game(self._name) while True: # Update the game map. game_map = game.update_map() start_time = time.time() # Produce features for each planet. features = self.produce_features(game_map) # Find predictions which planets we should send ships to. predictions = self._neural_net.predict(features) # Use simple greedy algorithm to assign closest ships to each planet according to predictions. ships_to_planets_assignment = self.produce_ships_to_planets_assignment( game_map, predictions) # Produce halite instruction for each ship. instructions = self.produce_instructions( game_map, ships_to_planets_assignment, start_time) # Send the command. game.send_command_queue(instructions) def produce_features(self, game_map): """ For each planet produce a set of features that we will feed to the neural net. We always return an array with PLANET_MAX_NUM rows - if planet is not present in the game, we set all featurse to 0. :param game_map: game map :return: 2-D array where i-th row represents set of features of the i-th planet """ feature_matrix = [[0 for _ in range(PER_PLANET_FEATURES)] for _ in range(PLANET_MAX_NUM)] for planet in game_map.all_planets(): # Compute "ownership" feature - 0 if planet is not occupied, 1 if occupied by us, -1 if occupied by enemy. if planet.owner == game_map.get_me(): ownership = 1 elif planet.owner is None: ownership = 0 else: # owned by enemy ownership = -1 my_best_distance = 10000 enemy_best_distance = 10000 gravity = 0 health_weighted_ship_distance = 0 sum_of_health = 0 for player in game_map.all_players(): for ship in player.all_ships(): d = ship.calculate_distance_between(planet) if player == game_map.get_me(): my_best_distance = min(my_best_distance, d) sum_of_health += ship.health health_weighted_ship_distance += d * ship.health gravity += ship.health / (d * d) else: enemy_best_distance = min(enemy_best_distance, d) gravity -= ship.health / (d * d) distance_from_center = distance(planet.x, planet.y, game_map.width / 2, game_map.height / 2) health_weighted_ship_distance = health_weighted_ship_distance / sum_of_health remaining_docking_spots = planet.num_docking_spots - len( planet.all_docked_ships()) signed_current_production = planet.current_production * ownership is_active = remaining_docking_spots > 0 or ownership != 1 feature_matrix[planet.id] = [ planet.health, remaining_docking_spots, planet.remaining_resources, signed_current_production, gravity, my_best_distance, enemy_best_distance, ownership, distance_from_center, health_weighted_ship_distance, is_active ] return feature_matrix def produce_ships_to_planets_assignment(self, game_map, predictions): """ Given the predictions from the neural net, create assignment (undocked ship -> planet) deciding which planet each ship should go to. Note that we already know how many ships is going to each planet (from the neural net), we just don't know which ones. :param game_map: game map :param predictions: probability distribution describing where the ships should be sent :return: list of pairs (ship, planet) """ undocked_ships = [ ship for ship in game_map.get_me().all_ships() if ship.docking_status == ship.DockingStatus.UNDOCKED ] # greedy assignment assignment = [] number_of_ships_to_assign = len(undocked_ships) if number_of_ships_to_assign == 0: return [] planet_heap = [] ship_heaps = [[] for _ in range(PLANET_MAX_NUM)] # Create heaps for greedy ship assignment. for planet in game_map.all_planets(): # We insert negative number of ships as a key, since we want max heap here. heapq.heappush(planet_heap, (-predictions[planet.id] * number_of_ships_to_assign, planet.id)) h = [] for ship in undocked_ships: d = ship.calculate_distance_between(planet) heapq.heappush(h, (d, ship.id)) ship_heaps[planet.id] = h # Create greedy assignment already_assigned_ships = set() while number_of_ships_to_assign > len(already_assigned_ships): # Remove the best planet from the heap and put it back in with adjustment. # (Account for the fact the distribution values are stored as negative numbers on the heap.) ships_to_send, best_planet_id = heapq.heappop(planet_heap) ships_to_send = -(-ships_to_send - 1) heapq.heappush(planet_heap, (ships_to_send, best_planet_id)) # Find the closest unused ship to the best planet. _, best_ship_id = heapq.heappop(ship_heaps[best_planet_id]) while best_ship_id in already_assigned_ships: _, best_ship_id = heapq.heappop(ship_heaps[best_planet_id]) # Assign the best ship to the best planet. assignment.append((game_map.get_me().get_ship(best_ship_id), game_map.get_planet(best_planet_id))) already_assigned_ships.add(best_ship_id) return assignment def produce_instructions(self, game_map, ships_to_planets_assignment, round_start_time): """ Given list of pairs (ship, planet) produce instructions for every ship to go to its respective planet. If the planet belongs to the enemy, we go to the weakest docked ship. If it's ours or is unoccupied, we try to dock. :param game_map: game map :param ships_to_planets_assignment: list of tuples (ship, planet) :param round_start_time: time (in seconds) between the Epoch and the start of this round :return: list of instructions to send to the Halite engine """ command_queue = [] # Send each ship to its planet for ship, planet in ships_to_planets_assignment: speed = hlt.constants.MAX_SPEED is_planet_friendly = not planet.is_owned( ) or planet.owner == game_map.get_me() if is_planet_friendly: if ship.can_dock(planet): command_queue.append(ship.dock(planet)) else: command_queue.append( self.navigate(game_map, round_start_time, ship, ship.closest_point_to(planet), speed)) else: docked_ships = planet.all_docked_ships() assert len(docked_ships) > 0 weakest_ship = None for s in docked_ships: if weakest_ship is None or weakest_ship.health > s.health: weakest_ship = s command_queue.append( self.navigate(game_map, round_start_time, ship, ship.closest_point_to(weakest_ship), speed)) return command_queue def navigate(self, game_map, start_of_round, ship, destination, speed): """ Send a ship to its destination. Because "navigate" method in Halite API is expensive, we use that method only if we haven't used too much time yet. :param game_map: game map :param start_of_round: time (in seconds) between the Epoch and the start of this round :param ship: ship we want to send :param destination: destination to which we want to send the ship to :param speed: speed with which we would like to send the ship to its destination :return: """ current_time = time.time() have_time = current_time - start_of_round < 1.2 navigate_command = None if have_time: navigate_command = ship.navigate(destination, game_map, speed=speed, max_corrections=180) if navigate_command is None: # ship.navigate may return None if it cannot find a path. In such a case we just thrust. dist = ship.calculate_distance_between(destination) speed = speed if (dist >= speed) else dist navigate_command = ship.thrust( speed, ship.calculate_angle_between(destination)) return navigate_command
# Fit classifier on training data start = time.time() network.fit(x_train, y_train, pred_epochs=pred_epochs, max_epoch=max_epoch) output_file.write("Training time:" + str(np.around((time.time() - start) / 60., 1)) + "minutes\n") output_file.write(dash * "-" + "\n") # Plot training error and error rate plt.plot(network.pred_errors, '-g', label='Error Rate') plt.plot(network.costs, '-r', label='Training Error') plt.legend() plt.xlabel('Epoch') plt.ylabel('Value') plt.savefig(img_file) # Test on validation set pred_labels = network.predict(x_test) accuracy = 100 * np.sum(pred_labels == np.argmax(y_test, axis=1)) / float(len(y_test)) output_file.write("Validation set accuracy: {} \n".format(accuracy)) output_file.write(dash * "-" + "\n") output_file.close() # Get Kaggle predictions indices = np.array(range(1, len(test_features) + 1)) pred_labels = network.predict(test_features) kaggle_format = np.vstack(((indices), pred_labels)).T np.savetxt("./results/digits_" + cost + ".csv", kaggle_format, delimiter=",", fmt='%d,%d', header='Id,Category', comments='')
def test_predict(): nn = NeuralNet() #loads nn again nn.predict(np.zeros((80, 80, 4)))
from pathlib import Path import numpy as np from neural_net import NeuralNet from data_prep import read_mnist train_labels, train_features = read_mnist( './mnist_dataset/mnist_train_100.csv') test_labels, test_features = read_mnist('./mnist_dataset/mnist_test_10.csv') nn = NeuralNet((784, 100, 10)) for i in range(len(train_labels)): nn.train(train_features[i], train_labels[i]) results = [nn.predict(i) for i in test_features] print(results) print([np.argmax(i) for i in test_labels])
X_train, Y_train, X_val, Y_val = getdata() X_train, X_val = standardise(X_train, X_val) # print X_train[1].reshape(28,28) # plt.imshow(X_train[1].reshape(28,28)) # plt.show() # exit() model = NeuralNet(784, 1000, 10) loss_history, train_acc_history = model.train(X_train, Y_train, reg=0, num_iters=2000, batch_size=32, learning_rate=0.1) plt.subplot(2, 1, 1) plt.plot(loss_history) plt.title('Loss History') plt.ylabel('Loss') plt.subplot(2, 1, 2) plt.plot(train_acc_history) plt.title('Train Accuracy History') plt.ylabel('Train Accuracy') plt.show() print "Train Accuracy:", (model.predict(X_train) == Y_train).mean() print "Validation Accuracy:", (model.predict(X_val) == Y_val).mean()
from neural_net import NeuralNet from utils import * def pre_process(x): return add_column_of_ones(normalize(x)) if __name__ == "__main__": X_train, labels_train, X_test, labels_test = load_dataset_and_trim() print("Loaded dataset") train_data = pre_process(X_train) test_data = pre_process(X_test) print("Pre-processed data") neural_net = NeuralNet() neural_net.train(train_data, labels_train) print("Trained model") predictions_train = neural_net.predict(train_data) predictions_test = neural_net.predict(test_data) training_accuracy = get_frac_equal(predictions_train, labels_train) test_accuracy = get_frac_equal(predictions_test, labels_test) report_accuracies(training_accuracy, test_accuracy)
# select 2 non-agent vehicles elif x < len(X_test_dataframes) and other_car_limit_test > 0: x_positions_test = np.append( x_positions_test, X_test_dataframes[x].iloc[:, 6 * i + 2 + 2].to_numpy()) y_positions_test = np.append( y_positions_test, X_test_dataframes[x].iloc[:, 6 * i + 2 + 3].to_numpy()) other_car_limit_test -= 1 elif x < len( X_test_dataframes ) and other_car_limit_test == 0 and agent_found_flag_test == True: X_test_x[x] = x_positions_test X_test_y[x] = y_positions_test model1 = NeuralNet([50], max_iter=10000) model1.fit(X_x, y_x) y_hat_x = model1.predict(X_test_x).flatten() model2 = NeuralNet([30], max_iter=10000) model2.fit(X_y, y_y) y_hat_y = model2.predict(X_test_y).flatten() y_hat = np.insert(y_hat_y, np.arange(len(y_hat_x)), y_hat_x) pd.DataFrame(y_hat).to_csv("output.csv")
############################################################################################################### # ----------------------------------------------- 1.4 MLP --------------------------------------------------- # ############################################################################################################### elif model == "MLP": hidden_layer_sizes = [50] mlp_model = NeuralNet(hidden_layer_sizes, learning_rate_decay=False, max_iter=500) t = time.time() # mlp_model.fit(X, Y) mlp_model.fitWithSGD(X, Y, epoch=40, minibatch_size=2500) print("Fitting took %d seconds" % (time.time() - t)) # Compute training error yhat = mlp_model.predict(X) tr_error = np.mean(yhat != y) print("Training error = ", tr_error) # Compute test error yhat = mlp_model.predict(Xtest) test_error = np.mean(yhat != ytest) print("Test error = ", test_error) ############################################################################################################### # ----------------------------------------------- 1.5 CNN --------------------------------------------------- # ############################################################################################################### # CITATION: The code for CNN is adapted from Mahan Fathi's Stanford's CS231 Assignment 2 repo # https://github.com/MahanFathi/CS231/blob/master/assignment2/cs231n/