def main(): # load data _input = torch.tensor(data.training_input, dtype=torch.float) _output = torch.tensor(data.training_expected_output, dtype=torch.float) # This section is for plotting ############################## gene_array = [] loss_array = [] fig, ax = plt.subplots() ax.set(xlabel='generation', ylabel='mean sum squared error', title='Neural network, error loss after each generation') # This section is for plotting ############################## ANN = NeuralNetwork(i=45, o=10, h=5) # input,output,hidden layer size # weight training for i in range(15000): # mean sum squared error mean_error = torch.mean((_output - ANN(_input))**2).detach().item() print("Generation: " + str(i) + " error: " + str(mean_error)) gene_array.append(i) loss_array.append(mean_error) ANN.train(_input, _output) torch.save(ANN, "algo1.weights") ANN = torch.load("14_good.weights") test_trained_network(ANN) ax.plot(gene_array, loss_array) plt.show()
def __init__(self, training_mode, restore_net): # the dice self.dice = Dice() # internal board, which is the state before the current move self.board = Board() # the neural network used by the players, both players share the same net if not restore_net: self.neural_network = NeuralNetwork(input_size=198, hidden_size=40, \ output_size=2) elif restore_net: self.neural_network = NeuralNetwork(restore_from_file=True) # list of players if training_mode: self.players = [Player('white', self.neural_network, learning_mode=True), \ Player('black', self.neural_network, learning_mode=True)] # let white play against RandomPlayer black for evaluating performance elif not training_mode: self.players = [Player('white', self.neural_network, learning_mode=False), \ RandomPlayer('black')] # the current player of this instance self.current_player = None # winner of this game self.winner = None self.reset()
def make_new_generation(self): new_population = [] while len(new_population) != len(self.population): a = self.roulette_select() a_encoded = encode_floats(a.encode()) # find a unique partner b = self.roulette_select() b_encoded = encode_floats(b.encode()) while b_encoded != a_encoded: b = self.roulette_select() b_encoded = encode_floats(b.encode()) ca,cb = self.cross(a_encoded,b_encoded) ma = self.mutate(ca) mb = self.mutate(cb) decoded_a = NeuralNetwork.decode(self.n_layers, decode_floats(ma)) decoded_b = NeuralNetwork.decode(self.n_layers, decode_floats(mb)) new_population.append( decoded_a ) new_population.append( decoded_b ) self.population = new_population self.fitnesses = self.zero_fitnesses()
def test_forward_pass(self): input_dim = 2 number_labels = 2 intermediate_neurons = 3 intermediate_layers = 1 neural_net = NeuralNetwork(input_dim=input_dim, number_labels=number_labels, intermediate_neurons=intermediate_neurons, intermediate_layers=intermediate_layers) # the layers in the neural net are randomly initialized # modify them to fixed numbers in order to test first_layer = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) intermediate_layer = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) intermediate_layer = np.vstack((intermediate_layer, np.array([1, 1, 1]).T)) last_layer = np.array([[0.5, 1], [0.5, 1], [0.5, 1], [1, 1]]) neural_net.layers = [first_layer, intermediate_layer, last_layer] expected_result = [92.5, 184] actual_result = neural_net.forward_pass(np.array([1, 3])) for actual, expected in zip(actual_result, expected_result): self.assertEqual(expected, actual)
def resetParamServer(): global params, accruedGradients, history_S, history_Y, rho,batches_processed nn = NeuralNetwork(NNlayers) params = nn.get_weights() accruedGradients = np.zeros(sum(nn.sizes)) history_S = [] history_Y = [] rho = [] batches_processed = 0
def test_sigmoid_activation_fct_expected(self): test_vector = np.array([1.1, 0.4, 0.9, -0.7]) expected_vector = np.array([0.75, 0.6, 0.711, 0.33]) neural_net = NeuralNetwork(1000, 10) actual_vector = neural_net.sigmoid_activation_fct(test_vector) for expected, actual in zip(expected_vector, actual_vector): self.assertAlmostEqual(expected, actual, 2)
def __init__(self, board_size, brain=None): if brain == None: self.brain = NeuralNetwork( [NeuralLayer(16, 24), NeuralLayer(4, 16)]) else: self.brain = brain self.direction = "up" # TODO: Grow the snake by this value each time it picks up food self.growcountDefault = 3 self.growcount = self.growcountDefault self.death_cause = "None" self.board_size = board_size self.pos_x = self.board_size // 2 self.pos_y = self.board_size // 2 self.alive = True self.length = 5 self.food = Food(self.board_size) self.lifetime = 0 self.fitness = 0 self.tail = [[self.pos_x, self.pos_y - 4], [self.pos_x, self.pos_y - 3], [self.pos_x, self.pos_y - 2], [self.pos_x, self.pos_y - 1]] self.vector = [] self.left_to_live_start = 200 self.left_to_live = self.left_to_live_start #Make moves equal tail and then append current head position to it self.parts = [[self.pos_x, self.pos_y - 4], [self.pos_x, self.pos_y - 3], [self.pos_x, self.pos_y - 2], [self.pos_x, self.pos_y - 1], [self.pos_x, self.pos_y]] # Add food positions and lengths for each move self.move_history = { "fitness": self.fitness, "moves": self.parts, "food_position": [[self.food.pos_x, self.food.pos_y] for i in range(len(self.parts))], "length": [self.length for i in range(len(self.parts))] }
def FeedForward(network, input): """ Arguments: --------- network : a NeuralNetwork instance input : an Input instance Returns: -------- Nothing Description: ----------- This function propagates the inputs through the network. That is, it modifies the *raw_value* and *transformed_value* attributes of the nodes in the network, starting from the input nodes. Notes: ----- The *input* arguments is an instance of Input, and contains just one attribute, *values*, which is a list of pixel values. The list is the same length as the number of input nodes in the network. i.e: len(input.values) == len(network.inputs) This is a distributed input encoding (see lecture notes 7 for more informations on encoding) In particular, you should initialize the input nodes using these input values: network.inputs[i].raw_value = input.values[i] """ network.CheckComplete() # 1) Assign input values to input nodes for i in range(0,len(input.values)): network.inputs[i].raw_value = input.values[i] network.inputs[i].transformed_value = input.values[i] # 2) Propagates to hidden layer for node in network.hidden_nodes: node.raw_value = NeuralNetwork.ComputeRawValue(node) node.transformed_value = NeuralNetwork.Sigmoid(node.raw_value) # 3) Propagates to the output layer for node in network.outputs: node.raw_value = NeuralNetwork.ComputeRawValue(node) node.transformed_value = NeuralNetwork.Sigmoid(node.raw_value) pass
def main(): inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) labels = np.array([[0], [1], [1], [0]]) architecture = (2, 3, 1) nn = NeuralNetwork(architecture, activation="sigmoid", cost="mse") costs = nn.train(inputs, labels, alpha=1, iterations=5000) print("\nCost always decreases:", all([costs[i + 1] < costs[i] for i in range(len(costs) - 1)])) print("Lowest cost:", min(costs)) print("\nResults:", "\n", np.round(nn.evaluate(inputs), 0)) plotCosts(costs) # GRAPHING plt.style.use(["dark_background"]) #plt.rc("grid", alpha=0.25) start, end = -0.5, 1.5 fidelity = 0.01 n = int((end - start) / fidelity) points = np.meshgrid(np.linspace(start, end, n + 1), np.linspace(start, end, n + 1)) values = np.zeros((n + 1, n + 1)) for i in range(n + 1): for j in range(n + 1): values[i, j] = nn.evaluate( np.array([points[0][i, j], points[1][i, j]])) x, y = points # RdYlGn plt.contourf(x, y, values, np.linspace(0, 1, 51), cmap="jet_r") plt.colorbar() plt.contour(x, y, values, 0.5, linewidths=2, linestyles="dashed", colors="black") plt.grid(color="black", alpha=0.25) plt.axhline(y=0, color="k", lw=2) plt.axvline(x=0, color="k", lw=2) plt.title("Neural Network XOR Boundary") plt.xlabel("X Axis") plt.ylabel("Y Axis") plt.show()
def test_xor_gate(self): """Simulate XOR gate and ensure working""" inputs = [[1.0, 1.0], [1.0, 0.0], [0.0, 1.0], [0.0, 0.0]] output_vector = [[0.0], [1.0], [1.0], [0.0]] inputs = np.array(inputs, dtype='float32') output_vector = np.array(output_vector) net = NeuralNetwork(inputs, output_vector) net.train() output = net.feed(np.array([[0, 1]], dtype='float32'))[0][0] output = round(output, 3) self.assertAlmostEqual(output, 1) output = net.feed(np.array([[1, 0]], dtype='float32'))[0][0] output = round(output, 3) self.assertAlmostEqual(output, 1) output = net.feed(np.array([[0, 0]], dtype='float32'))[0][0] output = round(output, 3) self.assertAlmostEqual(output, 0) output = net.feed(np.array([[1, 1]], dtype='float32'))[0][0] output = round(output, 3) self.assertAlmostEqual(output, 0)
def __init__(self, board_size, brain=None): if brain == None: _input_nodes = 5 _output_nodes = 4 self.brain = NeuralNetwork([_input_nodes] + hidden_layers + [_output_nodes]) else: self.brain = brain self.direction = "up" self.growcount = 1 self.death_cause = "None" self.board_size = board_size self.x = self.board_size // 2 self.y = self.board_size // 2 self.alive = True self.length = 5 self.food = Food(self.board_size) self.lifetime = 0 self.fitness = 0 self.tail = [[self.x, self.y - 4], [self.x, self.y - 3], [self.x, self.y - 2], [self.x, self.y - 1]] self.vector = [] self.left_to_live_start = 200 self.left_to_live = self.left_to_live_start self.parts = [[self.x, self.y - 4], [self.x, self.y - 3], [self.x, self.y - 2], [self.x, self.y - 1], [self.x, self.y]] # Add food positions and lengths for each move self.move_history = { "fitness": self.fitness, "moves": self.parts, "food_position": [[self.food.x, self.food.y] for i in range(len(self.parts))], "length": [self.length for i in range(len(self.parts))] }
def __init__(self, brain=None): self.marker = None self.fitness = 0 if brain == None: inputNodes = 9 hiddenNodes = 7 outputNodes = 9 self.brain = NeuralNetwork([inputNodes, hiddenNodes, outputNodes]) else: self.brain = brain self.wins = 0 self.draws = 0 self.total_matches = 0
def generate_initial_population(self): new_population = [] for i in range(self.population_size): new_population.append(NeuralNetwork(self.n_layers)) return new_population
def test_forward_pass_with_weights_randomly_initialized(self): input_dim = 32 * 32 number_labels = 10 intermediate_neurons = 4000 intermediate_layers = 2 neural_net = NeuralNetwork(input_dim=input_dim, number_labels=number_labels, intermediate_neurons=intermediate_neurons, intermediate_layers=intermediate_layers) input_vector = np.random.uniform(0, 1, input_dim) forward_res = neural_net.forward_pass(input_vector) self.assertEqual(number_labels, len(forward_res))
def load_model_from_file(self, classifier_type, sentence_features, filename): """Restore classifier from file""" if classifier_type is NEURAL_NET: self.classifier = NeuralNetwork(sentence_features, filename) else: self.classifier = NBClassifier(filename) #pylint: disable = R0204
def __init__(self, directors=None): super(NeuralNetWorkflow, self).__init__(workflow_name='neural_net_workflow') self.image_path = self.config.path.get('source_data', None) self.file_name = 'neural_net_workflow_scores.csv' self.directors = directors self.encoding = None self.base_dir = str() self.file = str() self.compressed_file = str() self.set_up() self.neural_net = NeuralNetwork(num_inputs=256, num_ouputs=5, classes=5, class_lables=['christopher_nolan', 'coen_brothers', 'david_lynch', 'spike_jonze', 'wes_anderson'])
def propagate_backward(nodes): for i, node in enumerate(nodes): # node is an output node if not node.forward_neighbors: node.error = target.values[i] - node.transformed_value else: # only works if we process in topological order, which we assume node.error = sum(map( lambda (weight, child): weight.value * child.delta, zip(node.forward_weights, node.forward_neighbors) )) node.delta = node.error * NeuralNetwork.SigmoidPrime(node.raw_value)
def crossover(self): #Introduce crossover i = 0 while len(self.new_generation) < self.number_of_agents: new_agent = NeuralNetwork() #Copy half of all weights from parent i and parent i+1 new_agent.weights_left = np.concatenate((self.list_of_neural_nets[i].weights_left[:int(len(self.list_of_neural_nets[i].weights_left)/2)], \ self.list_of_neural_nets[i+1].weights_left[int(len(self.list_of_neural_nets[i+1].weights_left)/2):])) new_agent.weights_down = np.concatenate((self.list_of_neural_nets[i].weights_down[:int(len(self.list_of_neural_nets[i].weights_down)/2)], \ self.list_of_neural_nets[i+1].weights_down[int(len(self.list_of_neural_nets[i+1].weights_down)/2):])) new_agent.weights_right = np.concatenate((self.list_of_neural_nets[i].weights_right[:int(len(self.list_of_neural_nets[i].weights_right)/2)], \ self.list_of_neural_nets[i+1].weights_right[int(len(self.list_of_neural_nets[i+1].weights_right)/2):])) new_agent.weights_rotate = np.concatenate((self.list_of_neural_nets[i].weights_rotate[:int(len(self.list_of_neural_nets[i].weights_rotate)/2)], \ self.list_of_neural_nets[i+1].weights_rotate[int(len(self.list_of_neural_nets[i+1].weights_rotate)/2):])) self.new_generation.append(new_agent) i += 1 print(f"Number of agents for next generation: {len(self.list_of_neural_nets)}")
def test_errors_computed_correctly(self): # mock the layers just as in the forward pass case input_dim = 2 number_labels = 2 intermediate_neurons = 3 intermediate_layers = 1 neural_net = NeuralNetwork(input_dim=input_dim, number_labels=number_labels, intermediate_neurons=intermediate_neurons, intermediate_layers=intermediate_layers) # the layers in the neural net are randomly initialized # modify them to fixed numbers in order to test first_layer = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) intermediate_layer = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) intermediate_layer = np.vstack((intermediate_layer, np.array([1, 1, 1]).T)) last_layer = np.array([[0.5, 1], [0.5, 1], [0.5, 1], [1, 1]]) neural_net.layers = [first_layer, intermediate_layer, last_layer]
def main(mnist_path, output_path, activation, hl_sizes): f = gzip.open(mnist_path, 'rb') training_set, test_set = pickle.load(f, encoding='latin1') training_examples = transform_examples(training_set) test_examples = transform_examples(test_set) if activation == "sigmoid": activation = neural_net.sigmoid d_activation = neural_net.d_sigmoid elif activation == "relu": activation = neural_net.relu d_activation = neural_net.d_relu elif activation == "elu": activation = neural_net.elu d_activation = neural_net.d_elu network = NeuralNetwork([PIXEL_COUNT] + hl_sizes + [NUM_CHARACTERS], output_path, act_func=activation, act_func_deriv=d_activation) network.train(training_generator(training_examples), test_examples) pickle.dump(network, open(output_path, 'wb'))
def populate_missing_agents(self): #To fulfill the capacity of list, duplicate current best agent #and add as new agents while len(self.new_generation) < int(self.number_of_agents/2): #Create new agent and mutate its weights try: new_agent = copy.deepcopy(self.new_generation[0]) except Exception as exception: new_agent = NeuralNetwork() print(f"Exception occured: {exception}") print(f"New agent had to be created.") self.new_generation.append(new_agent) self.list_of_neural_nets = [] self.list_of_neural_nets = self.new_generation
def test_layer_shape_expected(self): input_dim = 32 * 32 number_labels = 10 intermediate_layers = 1 intermediate_neurons = 1000 neural_net = NeuralNetwork(input_dim=input_dim, number_labels=number_labels, intermediate_layers=intermediate_layers, intermediate_neurons=intermediate_neurons) self.assertEqual(len(neural_net.layers), 3) self.assertEqual(neural_net.layers[0].shape, (input_dim + 1, intermediate_neurons)) self.assertEqual(neural_net.layers[1].shape, (intermediate_neurons + 1, intermediate_neurons)) self.assertEqual(neural_net.layers[2].shape, (intermediate_neurons + 1, number_labels))
def main_menu(): run = True list_of_neural_nets = [] number_of_games = 2 while number_of_games > 0: win.fill((0, 0, 0)) draw_text_middle('Press whatever button', 60, (255, 255, 255), win) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: net = NeuralNetwork() net = main(win, net) list_of_neural_nets.append(net) number_of_games -= 1 return list_of_neural_nets
class ModelReplica: def __init__(self, neuralNetLayers): self.neuralNet = NeuralNetwork(neuralNetLayers) self.currentParamsStep = None self.accruedGradients = np.zeros(sum(self.neuralNet.sizes)) self.isAvailable = True def hasParametersForStep(self, step): return step == self.currentParamsStep def setParams(self, params, step): self.params = params self.currentParamsStep = step def updateAccruedGradients(self, newGrad): self.accruedGradients += newGrad def getLocalAccruedGrad(self): return self.accruedGradients def computeGradient(self, x, y): gradients = self.neuralNet.jac(self.params, x, y) return gradients
# Step-size h = 0.4 # Numerical differentiation factor epsilon = 0.0001 # Learning rate tau = 2 # Tolerance for objective function TOL = 1 # Shrinking factor rho = 0.5 # Satisfactory descent rate beta = 0.95 # Time allowed for the algorithm to run (seconds) running_time = 1.1 x = [] correct = [] for i in range(0, 10): net = NeuralNetwork(M, h, epsilon, tau, TOL, running_time, beta, rho) net.learn(500) y = net.see(1000) x.append(i) correct.append(y) plt.plot(x, correct, 'K') plt.plot([0, 9], [98, 98], 'r', label="98 % correct") plt.plot([0, 9], [99, 99], 'b', label="99 % correct") plt.legend(fontsize=13) plt.title("Correct points after 1.1 second", fontsize=15) plt.xlabel("Try nr.") plt.show()
class Backgammon(object): """ This class wraps all of the backgammon functionality. Basically, the use model for this class is to build a new Backgammon with two players, execute backgammon.run(), which runs the game, and the call backgammon.reset(), backgammon.run() if you want to play again. """ def __init__(self, training_mode, restore_net): # the dice self.dice = Dice() # internal board, which is the state before the current move self.board = Board() # the neural network used by the players, both players share the same net if not restore_net: self.neural_network = NeuralNetwork(input_size=198, hidden_size=40, \ output_size=2) elif restore_net: self.neural_network = NeuralNetwork(restore_from_file=True) # list of players if training_mode: self.players = [Player('white', self.neural_network, learning_mode=True), \ Player('black', self.neural_network, learning_mode=True)] # let white play against RandomPlayer black for evaluating performance elif not training_mode: self.players = [Player('white', self.neural_network, learning_mode=False), \ RandomPlayer('black')] # the current player of this instance self.current_player = None # winner of this game self.winner = None self.reset() def save_network(self): """ Saves the Neural Network of this Backgammon instance to a file. """ self.neural_network.save_network() def reset(self): """ Resets this backgammon instance to the initial state, with a new board and determines starting player. """ self.board.reset_board() self.dice.roll() # decide which player starts the game by rolling dice # die1 > die2 player 0 starts and vice versa # if die1==die2, roll until different if self.dice.get_die1() != self.dice.get_die2(): # determine starting player: # die1 rolls for white and die2 rolls for black self.current_player = (0 if self.dice.get_die1() > self.dice.get_die2() else 1) # make sure that dice dont show same number elif self.dice.get_die1() == self.dice.get_die2(): same = True # roll until different while same: self.dice.roll() if self.dice.get_die1() != self.dice.get_die2(): self.current_player = (0 if self.dice.get_die1() > self.dice.get_die2() else 1) same = False # if black starts game, reverse players list # because white is first in the initial list if self.current_player == 1: self.players = list(reversed(self.players)) def run(self): """ Runs a game of backgammon, and does not return until the game is over. Returns the player who won the game. """ while not self.board.is_gameover(): # request players to choose a board self.get_move(self.players[0]) if self.board.is_gameover(): break self.get_move(self.players[1]) if self.board.is_gameover(): break # check whether a player has all checkers beared off # and return it as winner. if self.board.get_off(Board.WHITE) == 15: for player in self.players: if player.color == Board.WHITE: player.won(self.board) self.winner = player else: player.lost(self.board) elif self.board.get_off(Board.BLACK) == 15: for player in self.players: if player.color == Board.BLACK: player.won(self.board) self.winner = player else: player.lost(self.board) return self.winner def get_move(self, player): """ Receives a board from the player and applies the move. """ #print player.color new_board = player.choose_move(self) self.apply_move(new_board) def apply_move(self, new_board): """ Updates the board according to chosen move and initiates the next turn. """ # update board according to chosen board self.board = new_board # roll new dice self.dice.roll() # update player self.current_player = Board.get_opponent(self.current_player) @staticmethod def progress(count, total, suffix=''): bar_len = 60 filled_len = int(round(bar_len * count / float(total))) percents = round(100.0 * count / float(total), 1) bar = '=' * filled_len + '-' * (bar_len - filled_len) sys.stdout.write("\r[%s] %s%s %s" %(bar, percents, '%', suffix)) sys.stdout.flush()
def populate_list_of_neural_nets(self): for _ in range(self.number_of_agents): self.list_of_neural_nets.append(NeuralNetwork())
def run(): df = pd.read_csv('spambase_data\\spambase.data', header=None) df = df.sample(frac=1).reset_index(drop=True) print(f'No. missing values: {df.isnull().sum().sum()}') X = df.drop(57, axis=1) y = df.loc[:, 57] # Feature selection abs_corr_w_target = X.apply( lambda col: col.corr(y)).abs().sort_values().to_frame() abs_corr = X.corr().abs() plotting_tools.plot_heatmap(abs_corr_w_target, title='Correlation with target', size=(8, 16), one_dim=True) plotting_tools.plot_heatmap(abs_corr, title='Correlation before feature selection', size=(10, 16)) to_drop = set() # Amount of variation variance = X.var(axis=0, ddof=1) to_drop.update(variance[variance < 0.01].index.values) # Correlation with target to_drop.update(abs_corr_w_target[abs_corr_w_target[0] < 0.01].index.values) # Pairwise correlation to_drop.update(preprocessing.find_correlated(abs_corr, abs_corr_w_target)) to_drop = list(to_drop) nr_dropped = len(to_drop) X.drop(to_drop, axis=1, inplace=True) abs_corr = X.corr().abs() plotting_tools.plot_heatmap(abs_corr, title='Correlation after feature selection', size=(10, 16)) print(f'Dropped features: {to_drop}') # Data standardization works better, use normalization only for tests # X = preprocessing.normalize_data(X) X = preprocessing.standardize_data(X) X = X.values y = y.values train_inputs, cv_inputs, test_inputs = np.split( X, [int(0.6 * len(df)), int(0.8 * len(df))]) train_outputs, cv_outputs, test_outputs = np.split( y, [int(0.6 * len(df)), int(0.8 * len(df))]) print(f'Training set size: {train_outputs.shape[0]}\n' f'Cross validation set size: {cv_outputs.shape[0]}\n' f'Test set size: {test_outputs.shape[0]}') model = NeuralNetwork([57 - nr_dropped, 32, 1], activation_function='sigmoid') # Only use this part for tuning hyperparameters, slows down the program significantly # lambdas = list(np.arange(0.5, 1.5, 0.1)) # model.plot_learning_curves(train_inputs, train_outputs, cv_inputs, cv_outputs, # learning_rate=1.5, epochs=500, lambda_=0.6) # model.plot_validation_curves(train_inputs, train_outputs, cv_inputs, cv_outputs, # learning_rate=1.5, epochs=1000, lambdas=lambdas) model.gradient_descent(train_inputs, train_outputs, 1.5, 4000, 0.6, gradient_check=False, plot_cost=False) train_predictions = np.where(model.predict(train_inputs) > 0.5, 1, 0) test_predictions = np.where(model.predict(test_inputs) > 0.5, 1, 0) train_columns = { 'Train predictions': train_predictions[:, 0], 'Train outputs': train_outputs } test_columns = { 'Test predictions': test_predictions[:, 0], 'Test outputs': test_outputs } train_results = pd.DataFrame(train_columns) test_results = pd.DataFrame(test_columns) train_correct = pd.value_counts(train_results['Train predictions'] == train_results['Train outputs'])[True] test_correct = pd.value_counts( test_results['Test predictions'] == test_results['Test outputs'])[True] test_positive_predictions = test_results[test_results['Test predictions'] == 1] test_negative_predictions = test_results[test_results['Test predictions'] == 0] test_is_positive_correct = pd.value_counts( test_positive_predictions['Test predictions'] == test_positive_predictions['Test outputs']) test_is_negative_correct = pd.value_counts( test_negative_predictions['Test predictions'] == test_negative_predictions['Test outputs']) test_true_positives = test_is_positive_correct[True] test_false_positives = test_is_positive_correct[False] test_true_negatives = test_is_negative_correct[True] test_false_negatives = test_is_negative_correct[False] test_precision = test_true_positives / (test_true_positives + test_false_positives) test_recall = test_true_positives / (test_true_positives + test_false_negatives) test_confusion_matrix = pd.DataFrame( [[test_true_positives, test_false_positives], [test_false_negatives, test_true_negatives]], columns=[1, 0], index=[1, 0]) train_acc = train_correct / len(train_outputs) test_acc = test_correct / len(test_outputs) print(f'train_acc = {train_acc}') print(f'test_acc = {test_acc}') print(f'test_precision = {test_precision}') print(f'test_recall = {test_recall}') plotting_tools.plot_cm(test_confusion_matrix, title='Confusion matrix')
temp_y[int(l)] = 1 # we can cast this because we know labels are ints and not a weird float ys.append(temp_y) y = np.asarray(ys) return x,y #data = np.array([[0,0,0],[0,1,1],[1,0,1],[1,1,0]],dtype=np.float64) rawData = np.loadtxt("iris.data",delimiter=",") # Labels must be floats np.random.shuffle(rawData) label_count = len(set(rawData[:,-1])) feature_count = len(rawData[0])-1 X, y = sliceData(rawData) dataSetSize = len(X) #######neural network ####################### NNlayers = [feature_count, 10, label_count] # nn = NeuralNetwork(NNlayers) # costFunction = nn.cost # ############################################# params = nn.get_weights() #weights accruedGradients = np.zeros(sum(nn.sizes)) old_gradients = None old_params = None maxHistory = 10 history_S = [] #s_k = x_kp1 - x_k history_Y = [] #y_k = gf_kp1 - gf_k rho = [] #rho_k = 1.0 / (s_k * y_k) batches_processed = 0 batch_size = 10
def __init__(self, neuralNetLayers): self.neuralNet = NeuralNetwork(neuralNetLayers) self.currentParamsStep = None self.accruedGradients = np.zeros(sum(self.neuralNet.sizes)) self.isAvailable = True
def __init__(self, config, N): # with tf.variable_scope("PathNet", reuse=tf.AUTO_REUSE) as self.var_scope: # first, we will instantiate the networks for each pathnet layer self.network_structure = [] self.x_train = None self.y_train = None self.x_test = None self.y_test = None data_dims = list(config.pop("datashape", None)) if data_dims is None: raise ValueError( "You must provide a datashape parameter to configure PathNet!") data_dims.insert(0, None) print(data_dims) self.M = 0 self.L = 0 self.N = N self.first_layer = None self.sums = [] for l, (layer_name, layer_structure) in enumerate(config.items()): new_layer = [] if 'conditioning' not in layer_structure: print(layer_name) if self.M == 0: self.M = layer_structure['num_modules'] self.first_layer = l self.L = len(config.items()) - l print("FIRST LAYER: {}".format(layer_name)) self.Pmat = tf.placeholder(tf.float32, [self.L, self.M], name="PathMatrix") print(self.Pmat.get_shape().as_list()) else: print("CONDITIONING: {}".format(layer_name)) with tf.variable_scope(layer_name): for i in range(layer_structure['num_modules']): # if it is the first module of the layer, we set the x_input to None for # assignment later; otherwise, we set it to the input of the first module temp_struct = copy.deepcopy( layer_structure['module_structure']) # for j in range(len(temp_struct)): # if 'name' in temp_struct[j]: # temp_struct[j]['name'] += "_{}_{}".format(layer_name, i+1) # # print(temp_struct[j]['name']) # We need to select the proper input for this network provided the other # networks/layers have been created. input_ref = None if i == 0 and self.first_layer is not None and l > self.first_layer: input_ref = self.sums[-1] elif i == 0 and self.first_layer is not None and l == self.first_layer: input_ref = self.network_structure[self.first_layer - 1][0].yhat elif i == 0 and self.first_layer is None and l > 0: input_ref = self.network_structure[l - 1].yhat elif i > 0 and self.first_layer is not None: input_ref = new_layer[0].x_input with tf.variable_scope("M" + str(i)): new_layer.append( NeuralNetwork( temp_struct, x_in=input_ref, x_in_shape=data_dims if l == 0 else self.network_structure[ l - 1][0].yhat.get_shape().as_list(), make_dataset=True if l == 0 else False, name="M" + str(i))) new_layer[-1].build_network() del temp_struct if 'conditioning' not in layer_structure and i == 0: sum_shape = new_layer[-1].yhat.get_shape().as_list( ) # self.sums[-1].get_shape().as_list() if l > self.first_layer else # with tf.variable_scope("sums_{}".format(layer_name), reuse=tf.AUTO_REUSE): s = tf.get_variable("sum", [*sum_shape[1:]], initializer=tf.zeros_initializer() ) #, validate_shape=False) # s = tf.get_variable("sum", initializer=tf.truncated_normal([*sum_shape[1:]], mean=0.0, stddev=0.0)) self.sums.append(s) if self.first_layer is not None: self.sums[-1] = self.sums[-1] + self.Pmat[ l - self.first_layer, i] * new_layer[-1].yhat # tf.get_variable_scope().reuse_variables() self.network_structure.append(new_layer) self.output = self.sums[ -1] # the main network output is the last sum layer self.data_layer = self.network_structure[0][ 0] # this makes it easy to access our datapipeline
import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.metrics import r2_score from sklearn.preprocessing import MinMaxScaler from neural_net import NeuralNetwork #1D artificial data X = np.arange(0, 20).reshape(20, 1) + np.random.randn(20, 1) y = (np.arange(0, 20) + np.random.randn(20)).reshape(20, 1) scaler = MinMaxScaler() X = scaler.fit_transform(X) y = scaler.fit_transform(y) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101) plt.plot(X, y, '*', label='train data') plt.xlabel('features') plt.ylabel('labels') plt.title('DNN Regressor') nn = NeuralNetwork(mode='regression') nn.train(X_train, y_train, 1000) y_pred = nn.predict(X_test) print() r2 = r2_score(y_test, y_pred) print('The R^2 score is:', r2) plt.plot(X_test, y_pred, 'r*', label='test data') plt.legend()
class NeuralNetWorkflow(BaseETLWorkflow): def __init__(self, directors=None): super(NeuralNetWorkflow, self).__init__(workflow_name='neural_net_workflow') self.image_path = self.config.path.get('source_data', None) self.file_name = 'neural_net_workflow_scores.csv' self.directors = directors self.encoding = None self.base_dir = str() self.file = str() self.compressed_file = str() self.set_up() self.neural_net = NeuralNetwork(num_inputs=256, num_ouputs=5, classes=5, class_lables=['christopher_nolan', 'coen_brothers', 'david_lynch', 'spike_jonze', 'wes_anderson']) def set_up(self): self.base_dir, self.file, self.compressed_file = self.get_file_path() command.create_directories(self.base_dir) def extract(self): for director in directors: self.encoding = directors[director] movies = [x[1] for x in os.walk( '{image_path}/{director}/'.format(image_path=self.image_path, director=director), topdown=False)][-1] for movie in movies: film_stills_path = '{image_path}/' \ '{director}/' \ '{movie}/'.format(image_path=self.image_path, director=director, movie=movie) self.extract_film_stills(film_stills_path=film_stills_path, movie=movie, director=director) def extract_film_stills(self, film_stills_path=None, movie=None, director=None): for film_still in os.listdir(film_stills_path): try: film_still_path = '{film_stills_path}' \ '{film_still}'.format(film_stills_path=film_stills_path, film_still=film_still) logger.info(film_still_path) self.add_film_still_sample(image=film_still_path, movie=movie, director=director) except Exception as error: logger.error(error) def add_film_still_sample(self, image=None, movie=None, director=None): image_ndarray = image_to_numpy_ndarray(image=image) correlogram_matrix = auto_correlogram(image_ndarray) len(correlogram_matrix) self.neural_net.add_sample(correlogram_matrix=correlogram_matrix, target=self.encoding, sample_path=image) def transform(self): self.neural_net.process() save_network_to_xml(net=None, file_name=None) def load(self): save_row(file=self.file, row=[self.neural_net.cross_validation_result, self.neural_net.test_result, self.neural_net.train_result])