def improve_solution(self, results, model): self.processor.load_state_dict(model.copy()) inputs = TorchUtils.get_tensor_from_numpy(results['inputs']) targets = TorchUtils.get_tensor_from_numpy(results['targets']) TorchUtils.init_seed(results['seed'], deterministic=True) new_results = self.task.run_task(inputs, targets, results['mask']) plt.figure() plt.plot(results['best_output']) plt.plot(new_results['best_output']) plt.show()
def search_solution(self, gap): self.init_dirs(gap) self.task.configs['ring_data']['gap'] = gap inputs, targets, mask = self.data_loader.generate_new_data( self.configs['algorithm_configs']['processor'], gap=gap) self.reset(inputs.shape[0]) for run in range(self.configs['runs']): print(f'########### RUN {run} ################') seed = TorchUtils.init_seed(None, deterministic=True) results = self.task.run_task(inputs, targets, mask) results['seed'] = seed self.update_search_stats(results, run) if self.best_run == None or results[ 'best_performance'] < self.best_run['best_performance']: self.update_best_run(results, run) self.task.plot_results(results) self.close_search()
def train_surrogate_model(configs, main_folder='training_data'): if 'seed' in configs: seed = configs['seed'] else: seed = None seed = TorchUtils.init_seed(seed, deterministic=True) configs['seed'] = seed # Get training and validation data INPUTS, TARGETS, INPUTS_VAL, TARGETS_VAL, INFO = get_training_data(configs) # Train the model model_generator = get_algorithm(configs, is_main=True) data = model_generator.optimize(INPUTS, TARGETS, validation_data=(INPUTS_VAL, TARGETS_VAL), data_info=INFO) results_dir = os.path.join(model_generator.base_dir, main_folder) create_directory(results_dir) train_targets = INFO['processor']['amplification'] * TorchUtils.get_numpy_from_tensor(TARGETS[data.results['target_indices']][:len(INPUTS_VAL)]) train_output = INFO['processor']['amplification'] * data.results['best_output_training'] plot_all(train_targets, train_output, results_dir, name='TRAINING') val_targets = INFO['processor']['amplification'] * TorchUtils.get_numpy_from_tensor(TARGETS_VAL) val_output = INFO['processor']['amplification'] * data.results['best_output'] plot_all(val_targets, val_output, results_dir, name='VALIDATION') training_profile = data.results['performance_history'] * (INFO['processor']['amplification']**2) plt.figure() plt.plot(training_profile) plt.title(f'Training profile') plt.legend(['training', 'validation']) plt.savefig(os.path.join(results_dir, 'training_profile')) model_generator.path_to_model = os.path.join(model_generator.base_dir, 'reproducibility', 'model.pt') return model_generator