def calculate_average_cost(network, examples, reg_lambda=0): """ Calculate the network's average cost, J, over the given examples """ cost_vector = [] for example in examples: input_vector = example[0] target_vector = example[1] network.forward(input_vector) cost = network.cost(target_vector, reg_lambda) cost_vector.append(cost) avg_cost = sum(cost_vector) / len(cost_vector) return avg_cost
def calculate_average_cost(network, examples, reg_lambda=0): """ Calculate the network's average cost, J, over the given examples """ cost_vector = [] for example in examples: input_vector = example[0] target_vector = example[1] network.forward(input_vector) cost = network.cost(target_vector, reg_lambda) cost_vector.append(cost) avg_cost = sum(cost_vector)/len(cost_vector) return avg_cost
def print_network_outputs(network, testing_data): """Print the given network's outputs on the test data""" for i in range(len(testing_data)): outputs = network.forward(testing_data[i][1]) print('Input: {0}, Target Output: {1}, Actual Output: {2}'. format(testing_data[i][1], testing_data[i][0], outputs))
def test_gradients(self): layout = [3,5,2] network = pine.util.create_network(layout, ['logistic']*2) input_vector = [-2.3,3.1,-5.8] target_output_vector = [0.4,1] network.forward(input_vector) cost_gradient_vec = network.cost_gradient(target_output_vector) network.backward(cost_gradient_vec) for layer in network.layers: for neuron in layer.neurons: # weight gradients check: for i in range(len(neuron.weights)): epsilon = 0.0001 old_theta = neuron.weights[i] neuron.weights[i] = neuron.weights[i] + epsilon network.forward(input_vector) J1 = network.cost(target_output_vector) neuron.weights[i] = old_theta - epsilon network.forward(input_vector) J2 = network.cost(target_output_vector) estimated_gradient = (J1 - J2) / (2*epsilon) diff = abs(neuron.weight_gradients[i] - estimated_gradient) assert diff < 0.0001, "w difference: {}".format(diff) # print("w difference: {}".format(diff)) # print("weight_gradient[i]: {}".format(neuron.weight_gradients[i])) # print("estimated_gradient: {}".format(estimated_gradient)) neuron.weights[i] = old_theta # threshold gradient check: epsilon = 0.0001 old_theta = neuron.threshold neuron.threshold = neuron.threshold + epsilon network.forward(input_vector) J1 = network.cost(target_output_vector) neuron.threshold = old_theta - epsilon network.forward(input_vector) J2 = network.cost(target_output_vector) estimated_gradient = (J1 - J2) / (2*epsilon) diff = abs(neuron.threshold_gradient - estimated_gradient) assert diff < 0.0001, "t difference: {}".format(diff) # print("t difference: {}".format(diff)) neuron.threshold = old_theta
def test_gradients(self): layout = [3, 5, 2] network = pine.util.create_network(layout, ['logistic'] * 2) input_vector = [-2.3, 3.1, -5.8] target_output_vector = [0.4, 1] network.forward(input_vector) cost_gradient_vec = network.cost_gradient(target_output_vector) network.backward(cost_gradient_vec) for layer in network.layers: for neuron in layer.neurons: # weight gradients check: for i in range(len(neuron.weights)): epsilon = 0.0001 old_theta = neuron.weights[i] neuron.weights[i] = neuron.weights[i] + epsilon network.forward(input_vector) J1 = network.cost(target_output_vector) neuron.weights[i] = old_theta - epsilon network.forward(input_vector) J2 = network.cost(target_output_vector) estimated_gradient = (J1 - J2) / (2 * epsilon) diff = abs(neuron.weight_gradients[i] - estimated_gradient) assert diff < 0.0001, "w difference: {}".format(diff) # print("w difference: {}".format(diff)) # print("weight_gradient[i]: {}".format(neuron.weight_gradients[i])) # print("estimated_gradient: {}".format(estimated_gradient)) neuron.weights[i] = old_theta # threshold gradient check: epsilon = 0.0001 old_theta = neuron.threshold neuron.threshold = neuron.threshold + epsilon network.forward(input_vector) J1 = network.cost(target_output_vector) neuron.threshold = old_theta - epsilon network.forward(input_vector) J2 = network.cost(target_output_vector) estimated_gradient = (J1 - J2) / (2 * epsilon) diff = abs(neuron.threshold_gradient - estimated_gradient) assert diff < 0.0001, "t difference: {}".format(diff) # print("t difference: {}".format(diff)) neuron.threshold = old_theta
def test_network_forward(self): network = pine.network.Network() network.layers.append(self.layer) new_neuron = pine.network.Neuron(2,self.act_func) new_neuron.weights = [1,-2] new_neuron.threshold = 4 new_layer = pine.network.Layer() new_layer.neurons = [self.neuron] network.layers.append(new_layer) local_output = sum([x*y for x,y in zip([self.output, self.output], self.neuron.weights)]) + self.neuron.threshold out = [1.0 / (1 + math.exp(-1.0*local_output))] self.assertEqual(network.forward(self.input_vector), out) #0.9525741275104728
def test_network_forward(self): network = pine.network.Network() network.layers.append(self.layer) new_neuron = pine.network.Neuron(2, self.act_func) new_neuron.weights = [1, -2] new_neuron.threshold = 4 new_layer = pine.network.Layer() new_layer.neurons = [self.neuron] network.layers.append(new_layer) local_output = sum([ x * y for x, y in zip([self.output, self.output], self.neuron.weights) ]) + self.neuron.threshold out = [1.0 / (1 + math.exp(-1.0 * local_output))] self.assertEqual(network.forward(self.input_vector), out) #0.9525741275104728
print('Number of input neurons: {0}'.format( len(network.layers[0].neurons[0].weights))) for i in range(len(network.layers) - 1): print('Number of hidden neurons: {0}'.format( len(network.layers[i].neurons))) print('Number of output neurons: {0}'.format( len(network.layers[-1].neurons))) print('Number of total layers: {0}'.format(len(network.layers) + 1)) # now determine what needs to be done if args.only_predict: # just predict if args.predictions_file: writer = csv.writer(args.predictions_file) for example in examples: hypothesis_vector = network.forward(example[0]) if args.verbose: print('Input: {0}, Target Output: {1}, Actual Output: {2}'.format( example[0], example[1], hypothesis_vector)) if args.predictions_file: writer.writerow(hypothesis_vector) elif args.testing: # testing if args.predictions_file: writer = csv.writer(args.predictions_file) for example in examples: hypothesis_vector = network.forward(example[0]) if args.verbose: print('Input: {0}, Target Output: {1}, Actual Output: {2}'.format( example[0], example[1], hypothesis_vector))
def print_network_outputs(network, testing_data): """Print the given network's outputs on the test data""" for i in range(len(testing_data)): outputs = network.forward(testing_data[i][1]) print('Input: {0}, Target Output: {1}, Actual Output: {2}'.format( testing_data[i][1], testing_data[i][0], outputs))