def test_agent_main(agent, config, training_procedure=regular_train_agent_procedure): from configs.arguments import parse_arguments args = parse_arguments(f'{type(agent)}', config) if 'CONFIG' in args.__dict__.keys() and args.__dict__['CONFIG']: import importlib.util spec = importlib.util.spec_from_file_location('overloaded.config', args.__dict__['CONFIG']) config = importlib.util.module_from_spec(spec) spec.loader.exec_module(config) else: for key, arg in args.__dict__.items(): if key != 'CONFIG': setattr(config, key, arg) U.sprint(f'\nUsing config: {config}\n', highlight=True, color='yellow') if not args.skip_confirmation: for key, arg in U.get_upper_vars_of(config).items(): print(f'{key} = {arg}') input('\nPress Enter to begin... ') try: training_procedure(agent, config) except KeyboardInterrupt: print('Stopping') torch.cuda.empty_cache()
def add_early_stopping_key_combination(callback, key='ctrl+shift+s'): # keyboard.add_hotkey(key, callback) CALLBACKS.append(callback) U.sprint(f'\n\nPress any of:\n{COMBINATIONS}\n for early stopping\n', color='red', bold=True, highlight=True) print('') return keyboard.Listener(on_press=on_press, on_release=on_release)
def _infer_input_output_sizes(self, env, *args, **kwargs) -> None: ''' Tries to infer input and output size from env if either _input_size or _output_size, is None or -1 (int) :rtype: object ''' self._observation_space = env.observation_space self._action_space = env.action_space if self._input_size is None or self._input_size == -1: self._input_size = env.observation_space.shape U.sprint(f'\nobservation dimensions: {self._input_size}\n', color='green', bold=True, highlight=True) if self._output_size is None or self._output_size == -1: if hasattr(env.action_space, 'num_binary_actions'): self._output_size = [env.action_space.num_binary_actions] elif len(env.action_space.shape) >= 1: self._output_size = env.action_space.shape else: self._output_size = [env.action_space.n] U.sprint(f'\naction dimensions: {self._output_size}\n', color='green', bold=True, highlight=True)
def make_predictions(X, y, X_name, y_name, estimator, n_preds, n_total_samples=-1, verbose=True): # get some sample output from the model if n_total_samples > -1: X = X.sample(n_total_samples) y = y[X.index] X_test, y_test = X.iloc[:n_preds], y.iloc[:n_preds] X_train, y_train = X.iloc[n_preds:], y.iloc[n_preds:] estimator.fit(X_train, y_train) preds = estimator.predict(X_test) mae = np.mean(np.abs(y_test-preds)) out_df = pd.DataFrame({ X_name: X_test, "actual_"+y_name: y_test, "predicted_"+y_name: preds.round().astype(int), }) #fn = safe_filename(f"preds/preds-SVR-{X_name}-{y_name}", "csv") #out_df.to_csv(fn, index=False) if verbose: iprint(f"Number of pairs used for prediction: {X.shape[0]}") sprint(f"mae={mae}") print(out_df.head()) return mae
LOG_DIRECTORY=C.LOG_DIRECTORY) U.save_model(_trained_model, config) env.close() if __name__ == '__main__': import experiments.continuous.c2d_config as C from configs.arguments import parse_arguments args = parse_arguments('C2D', C) for key, arg in args.__dict__.items(): setattr(C, key, arg) U.sprint(f'\nUsing config: {C}\n', highlight=True, color='yellow') if not args.skip_confirmation: for key, arg in U.get_upper_vars_of(C).items(): print(f'{key} = {arg}') input('\nPress Enter to begin... ') _agent = C.AGENT_TYPE(C) try: train_agent(C, _agent) except KeyboardInterrupt: print('Stopping') torch.cuda.empty_cache()