-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
133 lines (118 loc) · 4.66 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""Training script for interfacing with library. This script
can be used from the commandline/yaml to run any of the
algorithms in squiRL
Attributes:
args (argparse.Namespace): Parsed config arguments for running script
parser (argparse.ArgumentParser): Argument parser
"""
import os
import json
import argparse
from pytorch_lightning.loggers import WandbLogger
from pytorch_lightning.utilities.seed import seed_everything
import pytorch_lightning as pl
import squiRL
import gym
from pytorch_lightning.profiler import AdvancedProfiler
def train(hparams) -> None:
"""Runs algorithm
Args:
hparams (argparse.Namespace): Stores all passed args
"""
if hparams.debug:
hparams.logger = None
profiler = True
else:
hparams.logger = WandbLogger(project=hparams.project)
hparams.logger.experiment
profiler = None
cwd = os.getcwd()
path = os.path.join(cwd, 'models')
if not os.path.exists(path):
os.mkdir(path)
path = os.path.join(path, hparams.logger.version)
if not os.path.exists(path):
os.mkdir(path)
path = os.path.join(path, hparams.logger.version)
if hparams.save_config:
with open(path + '.json', 'wt') as f:
config = vars(hparams).copy()
config.pop("logger")
config.pop("gpus")
config.pop("tpu_cores")
json.dump(config, f, indent=4)
seed_everything(hparams.seed)
algorithm = squiRL.reg_algorithms[hparams.algorithm](hparams)
trainer = pl.Trainer.from_argparse_args(hparams, profiler=profiler)
trainer.fit(algorithm)
if __name__ == '__main__':
__spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)"
parser = argparse.ArgumentParser(add_help=False)
group_prog = parser.add_argument_group("program_args")
group_env = parser.add_argument_group("environment_args")
# add PROGRAM level args
parser.add_argument(
'--save_config',
type=bool,
default=True,
help='Save settings to file in json format. Ignored in json file')
parser.add_argument('--load_config',
type=str,
help='Load from json file. Command line override.')
group_prog.add_argument('--seed',
type=int,
default=42,
help="experiment seed")
group_prog.add_argument(
'--debug',
type=bool,
default=False,
help="stops logging to wandb, turns on profiler, sets num_workers "
"to None, to allow debugging on a single thread")
group_prog.add_argument('--algorithm',
type=str,
default='VPG',
help="DRL algorithm")
args, _ = parser.parse_known_args()
group_alg = parser.add_argument_group(args.algorithm + "_args")
group_prog.add_argument('--project',
type=str,
default=args.algorithm,
help="project name for wandb logs")
# add environment specific args
group_env.add_argument("--env",
type=str,
default="CartPole-v0",
help="gym environment tag")
args, remaining_args = parser.parse_known_args()
env = gym.make(args.env)
parser.add_argument('--observation_space',
type=int,
default=env.observation_space.shape[0],
help='env state space')
parser.add_argument('--action_space',
type=int,
default=env.action_space.n,
help='env action space')
parser.add_argument('--max_reward',
type=int,
default=env.spec.reward_threshold,
help='Max reward allowed')
env.close()
# add algorithm specific args
group_alg = squiRL.reg_algorithms[args.algorithm].add_model_specific_args(
group_alg)
# add all the available trainer options to argparse
parser = pl.Trainer.add_argparse_args(parser)
# this is done to add all args to help
parser = argparse.ArgumentParser(
parents=[parser],
epilog="Trainer args docs can be found at PyTorch Lightning.")
args = parser.parse_args()
if args.load_config:
with open(args.load_config, 'rt') as f:
t_args = argparse.Namespace()
t_args.__dict__.update(json.load(f))
args = parser.parse_args(namespace=t_args)
print(args)
train(args)