-
Notifications
You must be signed in to change notification settings - Fork 119
/
main.py
executable file
·202 lines (196 loc) · 5.43 KB
/
main.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from __future__ import print_function, division
import os
os.environ["OMP_NUM_THREADS"] = "1"
import argparse
import torch
import torch.multiprocessing as mp
from environment import atari_env
from utils import read_config
from model import A3Clstm
from train import train
from test import test
from shared_optim import SharedRMSprop, SharedAdam
#from gym.configuration import undo_logger_setup
import time
#undo_logger_setup()"
parser = argparse.ArgumentParser(description="A3C")
parser.add_argument(
"-l", "--lr", type=float, default=0.0001, help="learning rate (default: 0.0001)"
)
parser.add_argument(
"-ec",
"--entropy-coef",
type=float,
default=0.01,
help="entropy loss coefficient (default: 0.01)",
)
parser.add_argument(
"-vc",
"--value-coef",
type=float,
default=0.5,
help="value coefficient (default: 0.5)",
)
parser.add_argument(
"-g",
"--gamma",
type=float,
default=0.99,
help="discount factor for rewards (default: 0.99)",
)
parser.add_argument(
"-t", "--tau", type=float, default=1.00, help="parameter for GAE (default: 1.00)"
)
parser.add_argument(
"-s", "--seed", type=int, default=1, help="random seed (default: 1)"
)
parser.add_argument(
"-w",
"--workers",
type=int,
default=32,
help="how many training processes to use (default: 32)",
)
parser.add_argument(
"-ns",
"--num-steps",
type=int,
default=20,
help="number of forward steps in A3C (default: 20)",
)
parser.add_argument(
"-mel",
"--max-episode-length",
type=int,
default=10000,
help="maximum length of an episode (default: 10000)",
)
parser.add_argument(
"-ev",
"--env",
default="PongNoFrameSkip-v4",
help="environment to train on (default: PongNoFrameSkip-v4)",
)
parser.add_argument(
"-so",
"--shared-optimizer",
default=True,
help="use an optimizer with shared statistics.",
)
parser.add_argument("-ld", "--load", action="store_true", help="load a trained model")
parser.add_argument(
"-sm",
"--save-max",
action="store_true",
help="Save model on every test run high score matched or bested",
)
parser.add_argument(
"-o",
"--optimizer",
default="Adam",
choices=["Adam", "RMSprop"],
help="optimizer choice of Adam or RMSprop",
)
parser.add_argument(
"-lmd",
"--load-model-dir",
default="trained_models/",
help="folder to load trained models from",
)
parser.add_argument(
"-smd",
"--save-model-dir",
default="trained_models/",
help="folder to save trained models",
)
parser.add_argument("-lg", "--log-dir", default="logs/", help="folder to save logs")
parser.add_argument(
"-gp",
"--gpu-ids",
type=int,
default=[-1],
nargs="+",
help="GPUs to use [-1 CPU only] (default: -1)",
)
parser.add_argument(
"-a", "--amsgrad", action="store_true", help="Adam optimizer amsgrad parameter"
)
parser.add_argument(
"--skip-rate",
type=int,
default=4,
metavar='SR',
help="frame skip rate (default: 4)")
parser.add_argument(
"-hs",
"--hidden-size",
type=int,
default=512,
help="LSTM Cell number of features in the hidden state h",
)
parser.add_argument(
"-tl",
"--tensorboard-logger",
action="store_true",
help="Creates tensorboard logger to see graph of model, view model weights and biases, and monitor test agent reward progress",
)
parser.add_argument(
"-evc", "--env-config",
default="config.json",
help="environment to crop and resize info (default: config.json)")
parser.add_argument(
"-dss",
"--distributed-step-size",
type=int,
default=[],
nargs="+",
help="use different step size among workers by using a list of step sizes to distributed among workers to use (default: [])",
)
# Based on
# https://github.com/pytorch/examples/tree/master/mnist_hogwild
# Training settings
# Implemented multiprocessing using locks but was not beneficial. Hogwild
# training was far superior
if __name__ == '__main__':
args = parser.parse_args()
torch.manual_seed(args.seed)
if args.gpu_ids != [-1]:
torch.cuda.manual_seed(args.seed)
mp.set_start_method("spawn")
setup_json = read_config(args.env_config)
env_conf = setup_json["Default"]
for i in setup_json.keys():
if i in args.env:
env_conf = setup_json[i]
env = atari_env(args.env, env_conf, args)
shared_model = A3Clstm(env.observation_space.shape[0], env.action_space, args)
if args.load:
saved_state = torch.load(
f"{args.load_model_dir}{args.env}.dat",
map_location=lambda storage, loc: storage,
)
shared_model.load_state_dict(saved_state)
shared_model.share_memory()
if args.shared_optimizer:
if args.optimizer == 'RMSprop':
optimizer = SharedRMSprop(shared_model.parameters(), lr=args.lr)
if args.optimizer == 'Adam':
optimizer = SharedAdam(
shared_model.parameters(), lr=args.lr, amsgrad=args.amsgrad)
optimizer.share_memory()
else:
optimizer = None
processes = []
p = mp.Process(target=test, args=(args, shared_model, env_conf))
p.start()
processes.append(p)
time.sleep(0.001)
for rank in range(0, args.workers):
p = mp.Process(
target=train, args=(rank, args, shared_model, optimizer, env_conf))
p.start()
processes.append(p)
time.sleep(0.001)
for p in processes:
time.sleep(0.001)
p.join()