def infer(path, label): """ Trains an RL model. First initializes environment, logging, and machine learning model. Then iterates through epochs of infering and prints score intermittently. """ util.set_xd() infer = {} infer['env'] = gym.make(params['env_name']) infer['env'].init(params) infer['model'] = PPO(params, infer['env'].observation_space.shape[0]).to( params['device']) infer['model'].load_state_dict(torch.load(path)) x = transform_state(infer['env'].reset(), 0) for i in range(1, params['nt']): prob, m, u = run_network(infer, x) state, _ = infer['env'].step(u) x_prime = transform_state(state, i) x = x_prime x = np.array(infer['env'].get_x()) plot.plot_SS(x, params['T'], title=f"State Space after {label}") plot.plot_error(x, params['T'], title=f"Errors after {label}")
def predict(): valid_data = _load('valid') valid_labels = valid_data.pop(score) model = keras.models.load_model('model.h5') prediction = model.predict(valid_data).flatten() plot.plot_prediction(valid_labels, prediction) plot.plot_error(valid_labels, prediction)
def predict(file_path): valid_data = _load(file_path) valid_labels = valid_data.pop(score) model = keras.models.load_model('model.h5') prediction = model.predict(valid_data).flatten() print('Prediction Output:', prediction) plot.plot_prediction(valid_labels, prediction) plot.plot_error(valid_labels, prediction)
def plot_output(dirname, input_beam, input_pulse, fwhm, amp, fluences, exact_density_out=None, exact_population_final=None): filename = lambda name: os.path.join(dirname, name) density = amp.density population = amp.population upper = population[0] lower = population[1] inversion = upper - lower Z = amp.Z T = amp.T if params.output_rel_time: T = T / fwhm TZ, ZT = np.meshgrid(T, Z) zlim = (Z[0], Z[-1]) tlim = (T[0], T[-1]) ref_density = input_pulse.ref_density ref_inversion = amp.active_medium.initial_inversion.ref_inversion out_t_label = norm_t_label if params.output_rel_time else t_amp_label stride_z = max(len(amp.Z) // params.out_count_z, 1) stride_t = max(len(amp.T) // params.out_count_t, 1) plot.plot_data(filename("density_in"), "Input Photon Density", (T, None, tlim, out_t_label), (density[0]/ref_density, None, None, density_rel_label)) plot.plot_data(filename("density_out"), "Output Photon Density", (T, None, tlim, out_t_label), (density[-1]/ref_density, None, None, density_rel_label)) plot.plot_data(filename("densities"), "Input and Output Photon Density", ((T, ) * 2, None, tlim, out_t_label), ((density[0]/ref_density, density[-1]/ref_density), None, None, density_rel_label), ("input pulse", "output pulse")) plot.plot_data(filename("densities_norm"), "Normalized Input and Output Photon Density", ((T, ) * 2, None, tlim, out_t_label), ((density[0]/ref_density, density[-1]/np.amax(density[-1])), None, None, density_norm_rel_label), ("input pulse", "output pulse")) plot.plot_data(filename("upper_init"), "Initial Upper State Population", (Z, None, zlim, z_label), (upper.T[0]/ref_inversion, None, None, upper_rel_label)) plot.plot_data(filename("upper_final"), "Final Upper State Population", (Z, None, zlim, z_label), (upper.T[-1]/ref_inversion, None, None, upper_rel_label)) plot.plot_data(filename("lower_init"), "Initial Lower State Population", (Z, None, zlim, z_label), (lower.T[0]/ref_inversion, None, None, lower_rel_label)) plot.plot_data(filename("lower_final"), "Final Lower State Population", (Z, None, zlim, z_label), (lower.T[-1]/ref_inversion, None, None, lower_rel_label)) plot.plot_data(filename("inversion_init"), "Initial Population Inversion", (Z, None, zlim, z_label), (inversion.T[0]/ref_inversion, None, None, inversion_rel_label)) plot.plot_data(filename("inversion_final"), "Final Population Inversion", (Z, None, zlim, z_label), (inversion.T[-1]/ref_inversion, None, None, inversion_rel_label)) plot.plot_projection(filename("density_evo"), "Photon Density Evolution", (ZT, None, z_label), (TZ, None, out_t_label), (density/ref_density, None, density_rel_label), (30, -30), (stride_z, stride_t)) plot.plot_projection(filename("upper_evo"), "Upper State Population Evolution", (ZT, None, z_label), (TZ, None, out_t_label), (upper/ref_inversion, None, upper_rel_label), (30, 30), (stride_z, stride_t)) plot.plot_projection(filename("lower_evo"), "Lower State Population Evolution", (ZT, None, z_label), (TZ, None, out_t_label), (lower/ref_inversion, None, lower_rel_label), (30, 30), (stride_z, stride_t)) plot.plot_projection(filename("inversion_evo"), "Population Inversion Evolution", (ZT, None, z_label), (TZ, None, out_t_label), (inversion/ref_inversion, None, inversion_rel_label), (30, 30), (stride_z, stride_t)) if exact_density_out is not None: plot.plot_error(filename("density_err"), "Photon Density Relative Error", (T, None, tlim, out_t_label), ((exact_density_out, density[-1]), None, None, error_label)) if exact_population_final is not None: plot.plot_error(filename("inversion_err"), "Population Inversion Relative Error", (Z, None, zlim, z_label), ((exact_population_final[0] - exact_population_final[1], inversion.T[-1]), None, None, error_label)) if amp.active_medium.doping_agent.lower_lifetime != 0.0: plot.plot_error(filename("upper_err"), "Upper State Population Relative Error", (Z, None, zlim, z_label), ((exact_population_final[0], upper.T[-1]), None, None, error_label)) plot.plot_error(filename("lower_err"), "Lower State Population Relative Error", (Z, None, zlim, z_label), ((exact_population_final[1], lower.T[-1]), None, None, error_label)) norm_fluences = fluences / input_beam.ref_fluence plot.plot_data(filename("fluence"), "Fluence Evolution", (Z, None, zlim, z_label), (norm_fluences, None, None, fluence_rel_label))
def startCalc(self): self.get_nodes() self.get_chebyshev_poly() self.coeff_chebyshef() self.approximation() self.get_error() print("Maximum error: ", self.error, " Degree: ", self.deg) self.errors.append(self.error) if hasattr(self, 'eps') and self.error > self.eps: self.deg += 1 self.reset_vars() self.startCalc() return X = np.arange(self.a, self.b, 0.01) Y = self.getVal(X) plot((X, Y), (self.app_x, self.app_y), self.getEquation()) if len(self.errors) > 1: plot_error(self.errors, self.getEquation())
with open(f) as json_data: d = json.load(json_data) distributions.append(d) print len(distributions) # Plot if args.save_plot or args.show_plot: print args.type if args.type is None or args.type == 'discrete': plot.plot_discrete(distributions, labels, args.show_plot, args.save_plot) elif args.type == 'continuous': plot.plot_continuous(distributions, labels, args.show_plot, args.save_plot) elif args.type == 'continuous2': plot.plot_continuous2(distributions, labels, args.show_plot, args.save_plot) elif args.type == 'error': plot.plot_error(distributions, labels, args.show_plot, args.save_plot) elif args.type == 'error_all_pairs': plot.plot_error_all_pairs(distributions, labels, args.show_plot, args.save_plot) elif args.type == 'error_consecutive_pairs': plot.plot_error_consecutive_pairs(distributions, labels, args.show_plot, args.save_plot) elif args.type == 'error_fraction': plot.plot_error_fraction(distributions, labels, args.show_plot, args.save_plot) elif args.type == 'relative_error': plot.plot_relative_error(distributions, labels, args.show_plot, args.save_plot) elif args.type == 'guess': plot.plot_guess(distributions, labels, args.show_plot, args.save_plot) # Delete the files if keep=True if not args.keep: for f in files:
# print some metrics train_samples_size = len(train_loader) * BATCH_SIZE valid_samples_size = len(valid_loader) * BATCH_SIZE loss_train_epoch = loss_train / train_samples_size loss_valid_epoch = loss_valid / valid_samples_size error_train_epoch = 100 - 100 * (acc_train / train_samples_size) error_valid_epoch = 100 - 100 * (acc_valid / valid_samples_size) error_history.append((error_train_epoch, error_valid_epoch)) loss_history.append((loss_train_epoch, loss_valid_epoch)) print( 'Epoch: {} train loss: {:.5f} valid loss: {:.5f} train error: {:.2f} % valid error: {:.2f} %' .format(epoch, loss_train_epoch, loss_valid_epoch, error_train_epoch, error_valid_epoch)) # check if model is better if error_valid_epoch < best_error[1]: best_error = (epoch, error_valid_epoch) snapshot(SAVED_MODELS_DIR, RUN_TIME, RUN_NAME, True, epoch, error_valid_epoch, model.state_dict(), model.optimizer.state_dict()) # check that the model is not doing worst over the time if best_error[0] + PATIENCE < epoch: print('Overfitting. Stopped at epoch {}.'.format(epoch)) break epoch += 1 plot_loss(RUN_TIME, RUN_NAME, loss_history) plot_error(RUN_TIME, RUN_NAME, error_history)
def ProcessRunData(errorType="Self", makePlot=False, perfRun=False, plotOrders=[], plotGrid=0): time = [] error = [] order = [] coreCount = [] runId = [] grid = [] timeTaken = 0 consecutivePlotTime = [] consecutivePlotKEDR = [] consecutivePlotDKE = [] consecutivePlotError = [] currentRun = RunData() with open(arguments.dataLog, 'r') as f: data_list = list(csv.reader(f)) # Initialise first run of data for i in range(10): currentRun.args.append(float(data_list[1][i])) # Iterate through all rows excluding headers for i in range(1, len(data_list)+1): rowSettings = [] if i < len(data_list): for j in range(10): rowSettings.append(float(data_list[i][j])) # print(f"Current = {currentRun.args}") # print(f"New = {rowSettings}") # Check if still reading data from the same run if currentRun.args == rowSettings and i != len(data_list): # print(f"raw time ={data_list[i][10]}") currentRun.t.append(float(data_list[i][-4])) currentRun.Enstrophy.append(float(data_list[i][-3])) currentRun.KE.append(float(data_list[i][-2])) currentRun.KEDR.append(float(data_list[i][-1])) # print(f"Stored time = {currentRun.t}") else: print("new run") # Open file with timing data with open(arguments.timingLog, 'r') as tf: print(f"Opening timing file {arguments.timingLog}") reader = csv.reader(tf) # Iterate through rows for k, row in enumerate(reader): # Ignore headers if k > 0: # Decide whether current run coincides with # row in the timing log equal = True for j in range(len(currentRun.args)): if float(row[j]) != currentRun.args[j]: equal = False break # If it is the same run then store its walltime if equal: print(f"Wall Time is {row[-1]}s") currentRun.wallTime = float(row[-1]) grid.append(int(row[9])) coreCount.append(int(row[-4])) runId.append(int(row[0])) timeTaken += float(row[-1]) # Update list of wall times time.append(currentRun.wallTime) # If it was a run to measure performance then error is not needed if not perfRun: # Obtain data from benchmark run enstBench, KEDRBench, dKE_dtBench = CollectBenchmarkData() # Calculate derivative of kinetic energy dKE_dt = plot.DerKE(currentRun.t, currentRun.KE) # Calculate the error for the given metric (errorType) errorsum, errorList = plot.calc_error( currentRun.t, dKE_dt, currentRun.KEDR, currentRun.Enstrophy, dKE_dtBench, KEDRBench, enstBench, errorType) error.append(errorsum) order.append(int(currentRun.args[8])) print(f"Order = {currentRun.args[8]}") # Plot error if errorType == "Self" and makePlot: plot.plot_error(currentRun.t, errorList, currentRun.KEDR, dKE_dt) # check whether data is wanted to plot side-by-side orders if order[-1] in plotOrders and grid[-1] == plotGrid: consecutivePlotTime.append(currentRun.t[:]) consecutivePlotKEDR.append(currentRun.KEDR[:]) consecutivePlotDKE.append(dKE_dt[:]) consecutivePlotError.append(errorList[:]) print(consecutivePlotTime == currentRun.t) currentRun.args = rowSettings del currentRun.t[:] del currentRun.Enstrophy[:] del currentRun.KE[:] del currentRun.KEDR[:] # print(currentRun.t) # print(f"Iterator = {i}") # print(f"new time ={data_list[i][10]}") if i < len(data_list): currentRun.t.append(float(data_list[i][-4])) currentRun.Enstrophy.append(float(data_list[i][-3])) currentRun.KE.append(float(data_list[i][-2])) currentRun.KEDR.append(float(data_list[i][-1])) # Check if data has been stored to generate plots if consecutivePlotTime: plot.plot_consecutive_error(consecutivePlotTime, consecutivePlotError, consecutivePlotKEDR, consecutivePlotDKE, plotOrders) formattedTime = plot.FormatTime(timeTaken) print(f"Total wall clock time for this batch was {timeTaken}s") print(f"{int(formattedTime[0])} days, ", end="") print(f"{int(formattedTime[1])} hours, ", end="") print(f"{int(formattedTime[2])} minutes, ", end="") print(f"and {formattedTime[3]: .2f} seconds") return time, error, order, coreCount, runId, grid