history_performance = [] training_data = tuple(load_mnist.load_training(output_nodes)) test_data = tuple(load_mnist.load_test()) for i in range(0, 100): scorecard = [] network = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate) for row in training_data: network.train(*row) for row in test_data: correct_label = row[1] outputs = network.query(row[0]) label = numpy.argmax(outputs) if label == correct_label: scorecard.append(1) else: scorecard.append(0) performance = sum(scorecard) / len(scorecard) print("ШАГ:", i, "/,", "Эффективность =", performance) history_performance.append(performance) print("Средняя эффективность =", sum(history_performance) / len(history_performance)) if __name__ == "__main__": pass
step_width = 0.01 x1_test = [] x2_test = [] for i in range(int(1.0 / step_width) + 1): x1_test.append(i * step_width) x2_test.append(i * step_width) y_test = [] # go through all records in the test data set for i in range(len(x1_test)): temp = [] for j in range(len(x2_test)): input_list = [x1_test[i], x2_test[j]] # query the network output = neuralNetwork.query(input_list) # append output value temp.append(output[0, 0]) # append temp to y y_test.append(temp) # Visualize test result x1_test, x2_test = np.meshgrid(x1_test, x2_test) y_test = np.array(y_test) fig = plt.figure() ax = fig.gca(projection='3d') surf = ax.plot_surface(x1_test, x2_test, y_test) plt.title("Network output") ax.set_xlabel('x1')
# test the neural network # scorecard for how well the network performs, initially empty scorecard = [] # go through all the records in the test data set for record in test_data_list: # split the record by the ',' commas all_values = record.split(',') # correct answer is first value correct_label = int(all_values[0]) # scale and shift the inputs inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01 # query the network outputs = n.query(inputs) # the index of the highest value corresponds to the label label = np.argmax(outputs) # append correct or incorrect to list if (label == correct_label): # network's answer matches correct answer, add 1 to scorecard scorecard.append(1) else: # network's answer doesn't match correct answer, add 0 to scorecard scorecard.append(0) pass pass # calculate the performance score, the fraction of correct answers scorecard_array = np.asarray(scorecard)