def train_nn(self, plot, epochs): total_acc = np.zeros(epochs) # Run for multiple epochs to improve accuracy for m in range(epochs): # Pass data through the network to get predictions self.nn.feed_forward(self.train_x) # Update the weights based on the data error self.nn.back_prop(self.train_x, self.train_y.reshape(self.train_y.shape[0], 1)) # Get accuracy based on correctly classified samples acc = self.nn.get_accuracy( self.train_y.reshape(self.train_y.shape[0], 1)) total_acc[m] = acc # Print out every 10 epochs #if (m + 1) % 10 == 0: #print(f'Epoch: {m + 1}, Accuracy: {total_acc[m]:.2f}') if plot: plt = Plot() plt.nn_acc_plot(total_acc) # Return the training accuracy of the model return total_acc[epochs - 1]
from plotting import FakeDuplicate, AggregateBins, Plot, PlotGroup, Plotter, AlgoOpt import validation _maxEff = [0.2, 0.5, 0.8, 1.025] _maxFake = [0.2, 0.5, 0.8, 1.025] _effandfake1 = PlotGroup("effandfake1", [ Plot("effic", xtitle="#eta", ytitle="efficiency vs #eta", ymax=_maxEff), Plot(FakeDuplicate("fakeduprate_vs_eta", assoc="num_assoc(recoToSim)_eta", dup="num_duplicate_eta", reco="num_reco_eta", title="fake+duplicates vs #eta"), xtitle="#eta", ytitle="fake+duplicates vs #eta", ymax=_maxFake), Plot("efficPt", title="", xtitle="p_{t}", ytitle="efficiency vs p_{t}", xmax=300, xlog=True), Plot(FakeDuplicate("fakeduprate_vs_pT", assoc="num_assoc(recoToSim)_pT", dup="num_duplicate_pT", reco="num_reco_pT", title=""), xtitle="p_{t}", ytitle="fake+duplicates rate vs p_{t}", ymax=_maxFake, xmin=0.2, xmax=300, xlog=True), Plot("effic_vs_hit", xtitle="hits", ytitle="efficiency vs hits"), Plot(FakeDuplicate("fakeduprate_vs_hit", assoc="num_assoc(recoToSim)_hit", dup="num_duplicate_hit", reco="num_reco_hit", title="fake+duplicates vs hit"), xtitle="hits", ytitle="fake+duplicates rate vs hits", ymax=_maxFake), ]) _effandfake2 = PlotGroup("effandfake2", [ Plot("effic_vs_phi", xtitle="#phi", ytitle="efficiency vs #phi", ymax=_maxEff), Plot(FakeDuplicate("fakeduprate_vs_phi", assoc="num_assoc(recoToSim)_phi", dup="num_duplicate_phi", reco="num_reco_phi", title="fake+duplicates vs #phi"), xtitle="#phi", ytitle="fake+duplicates rate vs #phi", ymax=_maxFake), Plot("effic_vs_dxy", title="", xtitle="dxy", ytitle="efficiency vs dxy", ymax=_maxEff), Plot(FakeDuplicate("fakeduprate_vs_dxy", assoc="num_assoc(recoToSim)_dxy", dup="num_duplicate_dxy", reco="num_reco_dxy", title=""), xtitle="dxy", ytitle="fake+duplicates rate vs dxy", ymax=_maxFake), Plot("effic_vs_dz", xtitle="dz", ytitle="efficiency vs dz", ymax=_maxEff), Plot(FakeDuplicate("fakeduprate_vs_dz", assoc="num_assoc(recoToSim)_dz", dup="num_duplicate_dz", reco="num_reco_dz", title="fake+duplicates vs dz"), xtitle="dz", ytitle="fake+duplicates rate vs dz", ymax=_maxFake), ]) _dupandfake1 = PlotGroup("dupandfake1", [ Plot("fakerate", xtitle="#eta", ytitle="fakerate vs #eta", ymax=_maxFake),
# # Per track collection plots # ######################################## _maxEff = [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 0.8, 1.025] _maxFake = [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 0.8, 1.025] #_minMaxResol = [1e-5, 2e-5, 5e-5, 1e-4, 2e-4, 5e-4, 1e-3, 2e-3, 5e-3, 1e-2, 2e-2, 5e-2, 0.1, 0.2, 0.5, 1] _minMaxResol = [1e-5, 4e-5, 1e-4, 4e-4, 1e-3, 4e-3, 1e-2, 4e-2, 0.1, 0.4, 1] _minMaxN = [5e-1, 5, 5e1, 5e2, 5e3, 5e4, 5e5, 5e6, 5e7, 5e8, 5e9] _effandfake1 = PlotGroup("effandfake1", [ Plot("efficPt", title="Efficiency vs p_{T}", xtitle="TP p_{T} (GeV)", ytitle="efficiency vs p_{T}", xlog=True, ymax=_maxEff), Plot(FakeDuplicate("fakeduprate_vs_pT", assoc="num_assoc(recoToSim)_pT", dup="num_duplicate_pT", reco="num_reco_pT", title="fake+duplicates vs p_{T}"), xtitle="track p_{T} (GeV)", ytitle="fake+duplicates rate vs p_{T}", ymax=_maxFake, xlog=True), Plot("effic", xtitle="TP #eta", ytitle="efficiency vs #eta", title="",
verbose = True fesolver = CooFESolver(verbose=verbose) optimizer = Topopt(fesolver, young, poisson, verbose=verbose) # compute history = False x = optimizer.init(load, density_constraint) x, x_more = optimizer.layout(load, density_constraint, x, penal, rmin, delta, loopy, history) print('Elapsed time is: ', time.time() - t, 'seconds.') if history: x_history = x_more loop = len(x_history) else: loop = x_more x_history = None # save if x_history: import imageio imageio.mimsave('topopt.gif', x_history) # plot pl = Plot(x, load, nelx, nely) pl.figure(title='loop ' + str(loop)) pl.boundary() pl.loading() pl.show()
X_dimensions = np.array([(0, 100), (0, 100)]) # dimensions of Search Space # obstacles Obstacles = np.array([(20, 20, 40, 40), (20, 60, 40, 80), (60, 20, 80, 40), (60, 60, 80, 80)]) x_init = (0, 0) # starting location x_goal = (100, 100) # goal location Q = np.array([(2, 1)]) # length of tree edges r = 1 # length of smallest edge to check for intersection with obstacles max_samples = 1024 # max number of samples to take before timing out rewire_count = 32 # optional, number of nearby branches to rewire prc = 0.1 # probability of checking for a connection to goal # create Search Space X = SearchSpace(X_dimensions, Obstacles) # create rrt_search rrt = RRTStar(X, Q, x_init, x_goal, max_samples, r, prc, rewire_count) path = rrt.rrt_star() # plot plot = Plot("rrt_star_2d") plot.plot_tree(X, rrt.trees) if path is not None: plot.plot_path(X, path) plot.plot_obstacles(X, Obstacles) plot.plot_start(X, x_init) plot.plot_goal(X, x_goal) plot.draw(auto_open=True)
fesolver = CvxFEA(verbose=verbose) # create optimizer object and initialise the problem optimizer = Topopt(den_con, load, fesolver, verbose=verbose, history=history) # compute t = time.time() x, x_history, ki = optimizer.layout(penal, rmin, delta, loopy, filt) print('Elapsed time is: ', time.time() - t, 'seconds.') # plotting pl = Plot(load, directory) pl.loading(load) pl.boundary(load) if history: for i in x_history: pl.add(i, animated=True) pl.save('video') pl.add(x, animated=False) if save_plot: pl.save('figure') if plotting: pl.show()
# loading case object, other classes can be selected and created load = Canti(nelx, nely, young, Emin, poisson) # FEA object is generated, other solvers can be selected and created fesolver = CvxFEA(verbose=verbose) # create optimizer object and initialise the problem optimizer = Topopt(den_con, load, fesolver, verbose=verbose) # execute the optimization t = time.time() x, x_history = optimizer.layout(penal, rmin, delta, loopy, filt, history) print('Elapsed time is: ', time.time() - t, 'seconds.') # plotting pl = Plot(load, title='Cantilever beam example 1600x300 elementen') pl.boundary(load) pl.loading(load) if history: for i in x_history: pl.add(i, animated=True) pl.save('video') pl.add(x, animated=False) if save_plot: pl.save('figure') if plotting: pl.show()
# bus_loc = (40,80) # bus_ori = 170 lresolution = 10 presolution = 1 # initialize obstacles obstacles = Obs.random_obstacle_generate(dim_range, start, end, N_obs) # initialize search space Space = Xspace(dim_range, obstacles) Car = lidar_car(car_loc, car_ori) car_lidar_data = Car.lidar_check(Space, lresolution, presolution) # plot part handle_plot = Plotter(Space) # get current axes ax = plt.gca() # add search space to axes ax = handle_plot.plot_2D(ax, obstacles, start, end) # add agents to space ax = handle_plot.plot_2D_vehicle(ax, Car) ax = handle_plot.plot_2D_vehicle(ax, Bus) ax.axis('equal') ax.grid() plt.xlim([0, dim_range[0]]) plt.ylim([0, dim_range[1]]) plt.xlabel('x (m)') plt.ylabel('y (m)')
# loading case object, other classes can be selected and created load = Inverter(nelx, nely, young, Emin, poisson, ext_stiff) # FEA object is generated, other solvers can be selected and created fesolver = CvxFEA(verbose=verbose) # create optimizer object and initialise the problem optimizer = Topopt(den_con, load, fesolver, verbose=verbose) # compute t = time.time() x, x_history = optimizer.layout(penal, rmin, delta, loopy, filt, history) print('Elapsed time is: ', time.time() - t, 'seconds.') # plotting pl = Plot(load, title='Inverter example') pl.boundary(load) pl.loading(load) if history: for i in x_history: pl.add(i, animated=True) pl.save('video') pl.add(x, animated=False) if save_plot: pl.save('figure') if plotting: pl.show()