# Setup the PSO algorithm and run # list of lengthh 10000, 1 or 0. More 1s towards end of list. # to graph, look at average over some increment. # for example, graph the average over the first 1000 episodes, then the next. # avg will be number in [0, 1]. This corresponds to "fitness" IR = [q.run_session(random.random(), random.random()) for _ in range(30)] IRAvg = sum([sum(i[-1000:])/1000 for i in IR])/30 InitialRewardPerEpisode = IR print ("Before tuning, QLearner achieves averages fitness of ", IRAvg, "(S.D. = ", statistics.stdev([sum(i[-1000:])/1000 for i in IR]), ") in 30 runs of 10,000 episodes.") dataplot = DataPlotter() # Eval function/fitness is average of the last 1000 episodes rewards. # Should converge to ~0.7. In other words, gets a reward 70% of the time. pso = PSO.ParticleSwarm((lambda a, b : sum(q.run_session(a, b)[-1000:])/1000), 5, 100, 0.00, 1.00, dataplot) pso.minV = -1 pso.maxV = 1 particles = [PSO.Particle(0.00, 1.00, dataplot) for _ in range(pso.numParticles)] pso.particles = particles pso.display_particles() alpha, gamma = pso.algorithm() FR = [q.run_session(alpha, gamma) for _ in range(30)] FRAvg = sum([sum(i[-1000:])/1000 for i in FR])/30 FinalRewardPerEpisode = FR print ("After tuning, QLearner achieves average fitness of ", FRAvg, "(S.D. = ", statistics.stdev([sum(i[-1000:])/1000 for i in FR]), ")in 30 runs of 10,000 episodes.") dataplot.appendqlRVals(IR, FR) dataplot.outputGraphs()
def run_test(test_number, hidden_layers, Export=False): # Start timing time_started = time.strftime("%Y-%m-%d-%H-%M-%S") num_inputs = X.shape[1] # Variables no_iterations = 4000 shape = [num_inputs] + hidden_layers + [10] no_particles = 25 swarm_inertia = 0.9 swarm_phi_p = 1 swarm_phi_g = 3 swarm_v_max = 3 accuracies = [0.1] measured_times = [ time_delta(time_started, time.strftime("%Y-%m-%d-%H-%M-%S")) ] # Initialize swarm cost_func = functools.partial(eval_neural_network, shape=shape, X=X, y=train_labels_one_hot) swarm = PSO.ParticleSwarm( cost_func, num_dimensions=dim_weights(shape), num_particles=no_particles, inertia=swarm_inertia, phi_p=swarm_phi_p, phi_g=swarm_phi_g, v_max=swarm_v_max, ) # Train... i = 0 best_scores = [(i, swarm.best_score)] while swarm.best_score > 1e-6 and i < no_iterations: swarm.update(num_iterations=no_iterations, current_iter=i + 1) i = i + 1 # if swarm.best_score < best_scores[-1][1]: if i % 10 == 0: best_scores.append((i, swarm.best_score)) corrects, wrongs, predictions = eval_accuracy( vector_to_weights(swarm.g, shape), shape, X_test, y_test) accuracy = corrects / (corrects + wrongs) current_time = time_delta(time_started, time.strftime("%Y-%m-%d-%H-%M-%S")) accuracies.append(accuracy) measured_times.append(current_time) print( best_scores[-1][0], "- Time(s):", current_time, "Error:", best_scores[-1][1], "Accuracy:", accuracy, ) # End time time_ended = time.strftime("%Y-%m-%d-%H-%M-%S") time_elapsed = time_delta(time_started, time_ended) # Plot iters = [tup[0] for tup in best_scores] errors = [tup[1] for tup in best_scores] """ figure = plt.figure() errorplot = plt.subplot(2, 1, 1) errorplot.plot(iters, errors) plt.title("PSO") plt.ylabel("Mean squared error") accuracyplot = plt.subplot(2, 1, 2) accuracyplot.plot(iters, accuracies) plt.xlabel("Iterations") plt.ylabel("Accuracy") """ # Test... best_weights = vector_to_weights(swarm.g, shape) best_nn = MultiLayerPerceptron(shape, weights=best_weights) y_test_pred = np.round(best_nn.run(X_test)) print( sklearn.metrics.classification_report(test_labels_one_hot, y_test_pred.T)) print("Accuracy:", accuracy) print("Time(s):", time_elapsed) # Output results report = sklearn.metrics.classification_report(test_labels_one_hot, y_test_pred.T, output_dict=True) report = DataFrame(report).transpose().values.tolist() # start CSV file ExportToFile = ("results/PSO/" + str(len(train_imgs)) + str(hidden_layers) + "/" + str(test_number) + "-accuracy-" + str(accuracy) + "-time-" + time_started + ".csv") if Export == True: # figure.savefig("results/PSO/figures/"+str(len(train_imgs))+str(hidden_layers)+"/"+str(test_number)+"-"+"-time-"+time_started+".png") # plt.clf() with open(ExportToFile, "a", newline="\n") as out: writer = csv.writer(out, delimiter=",") writer.writerow(["Accuracies"] + accuracies) writer.writerow(["Errors"] + errors) writer.writerow(["Iters"] + iters) writer.writerow(["Time(s)"] + measured_times) writer.writerow([]) header = [ "#Particles", "#Iterations", "Inertia", "Phi_p", "Phi_g", "V_max", "Accuracy", "Time Elapsed (s)", "Inputs", "Hidden nodes", "Outputs", "Time started", "Time ended", ] writer.writerow(header) line = [ no_particles, no_iterations, swarm_inertia, swarm_phi_p, swarm_phi_g, swarm_v_max, accuracy, time_elapsed, shape[0], shape[1], shape[2], time_started, time_ended, ] writer.writerow(line) writer.writerow("\n") writer.writerow( ["#", "Precision", "Recall", "f1-score", "support"]) for x in range(10): writer.writerow([ x, report[x][0], report[x][1], report[x][2], report[x][3] ]) writer.writerow([ "Micro avg", report[10][0], report[10][1], report[10][2], report[10][3], ]) writer.writerow([ "Macro avg", report[11][0], report[11][1], report[11][2], report[11][3], ]) writer.writerow([ "Weighted avg", report[12][0], report[12][1], report[12][2], report[12][3], ]) writer.writerow([ "Samples avg", report[13][0], report[13][1], report[13][2], report[13][3], ]) out.close()