-
Notifications
You must be signed in to change notification settings - Fork 30
/
local.py
163 lines (137 loc) · 5.56 KB
/
local.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
# Copyright (C) 2018 Heron Systems, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
__ __
____ _____/ /__ ____ / /_
/ __ `/ __ / _ \/ __ \/ __/
/ /_/ / /_/ / __/ /_/ / /_
\__,_/\__,_/\___/ .___/\__/
/_/
Local Mode
Train an agent with a single GPU.
Usage:
local [options]
local --resume <path>
local (-h | --help)
Agent Options:
--agent <str> Name of agent class [default: ActorCritic]
Environment Options:
--env <str> Environment name [default: PongNoFrameskip-v4]
--rwd-norm <str> Reward normalizer name [default: Clip]
--manager <str> Manager to use [default: SubProcEnvManager]
Script Options:
--gpu-id <int> CUDA device ID of GPU [default: 0]
--nb-env <int> Number of parallel env [default: 64]
--seed <int> Seed for random variables [default: 0]
--nb-step <int> Number of steps to train for [default: 10e6]
--load-network <path> Path to network file (for pretrained weights)
--load-optim <path> Path to optimizer file
--resume <path> Resume training from log ID .../<logdir>/<env>/<log-id>/
--config <path> Use a JSON config file for arguments
--eval Run an evaluation after training
--prompt Prompt to modify arguments
Network Options:
--net1d <str> Network to use for 1d input [default: Identity1D]
--net2d <str> Network to use for 2d input [default: Identity2D]
--net3d <str> Network to use for 3d input [default: FourConv]
--net4d <str> Network to use for 4d input [default: Identity4D]
--netbody <str> Network to use on merged inputs [default: LSTM]
--head1d <str> Network to use for 1d output [default: Identity1D]
--head2d <str> Network to use for 2d output [default: Identity2D]
--head3d <str> Network to use for 3d output [default: Identity3D]
--head4d <str> Network to use for 4d output [default: Identity4D]
--custom-network <str> Name of custom network class
Optimizer Options:
--optim <str> Name of optimizer [default: RMSprop]
--lr <float> Learning rate [default: 0.0007]
--grad-norm-clip <float> Clip gradient norms [default: 0.5]
--warmup <int> Number of steps to warm up for [default: 100]
Logging Options:
--tag <str> Name your run [default: None]
--logdir <path> Path to logging directory [default: /tmp/adept_logs/]
--epoch-len <int> Save a model every <int> frames [default: 1e6]
--nb-eval-env <int> Evaluate agent in a separate thread [default: 0]
--summary-freq <int> Tensorboard summary frequency [default: 10]
Troubleshooting Options:
--profile Profile this script
"""
from adept.container import Init, Local
from adept.registry import REGISTRY as R
from adept.utils.script_helpers import parse_none, parse_path
from adept.utils.util import DotDict
MODE = "Local"
def parse_args():
from docopt import docopt
args = docopt(__doc__)
args = {k.strip("--").replace("-", "_"): v for k, v in args.items()}
del args["h"]
del args["help"]
args = DotDict(args)
# Ignore other args if resuming
if args.resume:
args.resume = parse_path(args.resume)
return args
if args.config:
args.config = parse_path(args.config)
args.logdir = parse_path(args.logdir)
args.gpu_id = int(args.gpu_id)
args.nb_env = int(args.nb_env)
args.seed = int(args.seed)
args.nb_step = int(float(args.nb_step))
args.tag = parse_none(args.tag)
args.nb_eval_env = int(args.nb_eval_env)
args.summary_freq = int(args.summary_freq)
args.lr = float(args.lr)
args.warmup = int(float(args.warmup))
args.epoch_len = int(float(args.epoch_len))
args.profile = bool(args.profile)
return args
def main(args):
"""
Run local training.
:param args: Dict[str, Any]
:return:
"""
args, log_id_dir, initial_step, logger = Init.main(MODE, args)
R.save_extern_classes(log_id_dir)
container = Local(args, logger, log_id_dir, initial_step)
if args.profile:
try:
from pyinstrument import Profiler
except:
raise ImportError("You must install pyinstrument to use profiling.")
container.nb_step = 10e3
profiler = Profiler()
profiler.start()
try:
container.run()
finally:
if args.profile:
profiler.stop()
print(profiler.output_text(unicode=True, color=True))
container.close()
if args.eval:
from adept.scripts.evaluate import main
eval_args = {
"log_id_dir": log_id_dir,
"gpu_id": 0,
"nb_episode": 30,
}
if args.custom_network:
eval_args["custom_network"] = args.custom_network
main(eval_args)
if __name__ == "__main__":
main(parse_args())