-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
283 lines (234 loc) · 11.5 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import numpy as np
from collections import namedtuple
import logging
import sys
import argparse
import os
import gym
from gym import wrappers
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from replay_memory import ExpReplay
from dqn_model import DQN
from dqn_learn import dqn_train
from dqn_eval import dqn_eval
from dqn_learn_old import dqn_train_old
from dqn_eval_old import dqn_eval_old
from ddqn_learn import ddqn_train
from ddqn_eval import ddqn_eval
from ddqn_rankPriority_learn import ddqn_rank_train
# from ddqn_rankPriorityWeighted_learn import ddqn_rank_train
from duel_rankPriority_learn import duel_rank_train
from duel_learn import duel_train
from scheduler import Scheduler
parser = argparse = argparse.ArgumentParser(description='Deep Q Network Pytorch Implementation.')
parser.add_argument('--mode', type=str, help='train or eval', default='train')
parser.add_argument('--era', type=str, help='old or new', default='new')
parser.add_argument('--model_type', type=str, help='Model architecture, eg. DQN', default='dqn', choices=['dqn', 'ddqn', 'duel'])
parser.add_argument('--environment', type=str, help='Game environment, eg. SpaceInvaders-v0', default='SpaceInvaders-v0')
parser.add_argument('--input_size', type=int, help='Input size for N x N. Resizing and/or padding is applied whenever necessary', default=84)
parser.add_argument('--batch_size', type=int, help='Batch size', default=32)
parser.add_argument('--rp_capacity', type=int, help='Replay Memory capacity', default=100000)
parser.add_argument('--rp_initial', type=int, help='Initial size to populate Replay Memory', default=50000)
parser.add_argument('--target_update_steps', type=int, help='The frequency with which the target network is updated', default=10000)
parser.add_argument('--frames_per_epoch', type=int, help='Num frames per epoch. Useful as a counter for eval', default=250000)
parser.add_argument('--max_frames', type=int, help='Num frames for the whole training.', default=200000000)
parser.add_argument('--frames_per_state', type=int, help='The number of most recent frames used as an input to the Q network. Actions are repeated over these frames.', default=4)
parser.add_argument('--inital_beta', type=float, help='Initial Beta. The exponent value for the importance sampling weights', default=0.5)
parser.add_argument('--final_beta', type=float, help='Final Beta value. See initial_beta for more info.', default=1.0)
parser.add_argument('--prob_alpha', type=float, help='Alpha value for the transition probability.A value of zero leades to uniform distribution.', default=0.7)
parser.add_argument('--discount_factor', type=float, help='Discount factor gamma used in the Q-learning udate', default=0.99)
parser.add_argument('--initial_explore', type=float, help='Initial value of epsilon in epsilon-greedy exploration', default=1.0)
parser.add_argument('--final_explore', type=float, help='Final value of epsilon in epsilon-greedy exploration', default=0.1)
parser.add_argument('--rmsprop_alpha', type=float, help='Smoothing constant for RMSprop. See pytorch doc for more info.', default=0.95)
parser.add_argument('--rmsprop_eps', type=float, help='Term added to the denominator to improve numerical stability for RMSprop. See pytorch doc for more info.', default=0.01)
parser.add_argument('--explore_frame', type=int, help='Num of frames over which the initial value of epsilon is linearly annealed to the final value', default=50000)
parser.add_argument('--learning_rate', type=float, help='Learning rate', default=0.00025)
parser.add_argument('--output_directory', type=str, help='Output directory to save weights, if empty, outputs to a local folder named \'saved_weights\'', default='./saved_weights/')
parser.add_argument('--last_checkpoint', type=str, help='Last saved weights that you wish to use to either resume training or for eval.', default='')
parser.add_argument('--replay_type', type=str, help='Replay Memory Type.', default='uniform', choices=['uniform', 'rank', 'proportional'])
args = parser.parse_args()
Optimizer = namedtuple("Optimizer", ["type", "kwargs"])
def main():
env = gym.make(args.environment).unwrapped
exploreScheduler = Scheduler(args.explore_frame, args.initial_explore, args.final_explore)
betaScheduler = Scheduler(args.max_frames, args.inital_beta, args.final_beta)
optimizer = Optimizer(type=optim.RMSprop, kwargs=dict(lr=args.learning_rate, alpha=args.rmsprop_alpha, eps=args.rmsprop_eps))
if args.model_type == 'dqn' and args.mode == 'train' and args.era == 'old':
if args.last_checkpoint:
if not os.path.isfile(args.last_checkpoint):
raise FileNotFoundError('Checkpoint file cannot be found!')
dqn_train_old(env, exploreScheduler, optimizer_constructor=optimizer,
model_type = args.model_type,
batch_size = args.batch_size,
rp_start = args.rp_initial,
rp_size = args.rp_capacity,
exp_frame = args.explore_frame,
exp_initial = args.initial_explore,
exp_final = args.final_explore,
gamma = args.discount_factor,
target_update_steps = args.target_update_steps,
frames_per_epoch = args.frames_per_epoch,
frames_per_state = args.frames_per_state,
output_directory = args.output_directory,
last_checkpoint = args.last_checkpoint)
elif args.model_type == 'dqn' and args.mode == 'eval' and args.era == 'old':
if args.last_checkpoint:
if not os.path.isfile(args.last_checkpoint):
raise FileNotFoundError('Checkpoint file cannot be found!')
dqn_eval_old(env, exploreScheduler, optimizer_constructor=optimizer,
model_type = args.model_type,
batch_size = args.batch_size,
rp_start = args.rp_initial,
rp_size = args.rp_capacity,
exp_frame = args.explore_frame,
exp_initial = args.initial_explore,
exp_final = args.final_explore,
gamma = args.discount_factor,
target_update_steps = args.target_update_steps,
frames_per_epoch = args.frames_per_epoch,
frames_per_state = args.frames_per_state,
output_directory = args.output_directory,
last_checkpoint = args.last_checkpoint)
elif args.model_type == 'dqn' and args.mode == 'train' and args.era == 'new':
if args.last_checkpoint:
if not os.path.isfile(args.last_checkpoint):
raise FileNotFoundError('Checkpoint file cannot be found!')
dqn_train(env, exploreScheduler, optimizer_constructor=optimizer,
model_type = args.model_type,
batch_size = args.batch_size,
rp_start = args.rp_initial,
rp_size = args.rp_capacity,
exp_frame = args.explore_frame,
exp_initial = args.initial_explore,
exp_final = args.final_explore,
gamma = args.discount_factor,
target_update_steps = args.target_update_steps,
frames_per_epoch = args.frames_per_epoch,
frames_per_state = args.frames_per_state,
output_directory = args.output_directory,
last_checkpoint = args.last_checkpoint,
envo=args.environment)
elif args.model_type == 'dqn' and args.mode == 'eval' and args.era == 'new':
if args.last_checkpoint:
if not os.path.isfile(args.last_checkpoint):
raise FileNotFoundError('Checkpoint file cannot be found!')
dqn_eval(env, exploreScheduler, optimizer_constructor=optimizer,
model_type = args.model_type,
batch_size = args.batch_size,
rp_start = args.rp_initial,
rp_size = args.rp_capacity,
exp_frame = args.explore_frame,
exp_initial = args.initial_explore,
exp_final = args.final_explore,
gamma = args.discount_factor,
target_update_steps = args.target_update_steps,
frames_per_epoch = args.frames_per_epoch,
frames_per_state = args.frames_per_state,
output_directory = args.output_directory,
last_checkpoint = args.last_checkpoint,
envo=args.environment)
elif args.model_type == 'ddqn' and args.mode == 'train':
if args.last_checkpoint:
if not os.path.isfile(args.last_checkpoint):
raise FileNotFoundError('Checkpoint file cannot be found!')
if args.replay_type == 'rank':
ddqn_rank_train(env, exploreScheduler, betaScheduler, optimizer_constructor=optimizer,
model_type = args.model_type,
batch_size = args.batch_size,
rp_start = args.rp_initial,
rp_size = args.rp_capacity,
exp_frame = args.explore_frame,
exp_initial = args.initial_explore,
exp_final = args.final_explore,
prob_alpha = args.prob_alpha,
gamma = args.discount_factor,
target_update_steps = args.target_update_steps,
frames_per_epoch = args.frames_per_epoch,
frames_per_state = args.frames_per_state,
output_directory = args.output_directory,
last_checkpoint = args.last_checkpoint,
max_frames=args.max_frames,
envo=args.environment)
elif args.replay_type == 'uniform':
ddqn_train(env, exploreScheduler, optimizer_constructor=optimizer,
model_type = args.model_type,
batch_size = args.batch_size,
rp_start = args.rp_initial,
rp_size = args.rp_capacity,
exp_frame = args.explore_frame,
exp_initial = args.initial_explore,
exp_final = args.final_explore,
gamma = args.discount_factor,
target_update_steps = args.target_update_steps,
frames_per_epoch = args.frames_per_epoch,
frames_per_state = args.frames_per_state,
output_directory = args.output_directory,
last_checkpoint = args.last_checkpoint,
envo=args.environment)
elif args.model_type == 'ddqn' and args.mode == 'eval':
if args.last_checkpoint:
if not os.path.isfile(args.last_checkpoint):
raise FileNotFoundError('Checkpoint file cannot be found!')
#TODO: create ddqn rank eval
ddqn_eval(env, exploreScheduler, optimizer_constructor=optimizer,
model_type = args.model_type,
batch_size = args.batch_size,
rp_start = args.rp_initial,
rp_size = args.rp_capacity,
exp_frame = args.explore_frame,
exp_initial = args.initial_explore,
exp_final = args.final_explore,
gamma = args.discount_factor,
target_update_steps = args.target_update_steps,
frames_per_epoch = args.frames_per_epoch,
frames_per_state = args.frames_per_state,
output_directory = args.output_directory,
last_checkpoint = args.last_checkpoint,
envo=args.environment)
elif args.model_type == 'duel' and args.mode == 'train':
if args.last_checkpoint:
if not os.path.isfile(args.last_checkpoint):
raise FileNotFoundError('Checkpoint file cannot be found!')
if args.replay_type == 'rank':
duel_rank_train(env, exploreScheduler, betaScheduler, optimizer_constructor=optimizer,
model_type = args.model_type,
batch_size = args.batch_size,
rp_start = args.rp_initial,
rp_size = args.rp_capacity,
exp_frame = args.explore_frame,
exp_initial = args.initial_explore,
exp_final = args.final_explore,
prob_alpha = args.prob_alpha,
gamma = args.discount_factor,
target_update_steps = args.target_update_steps,
frames_per_epoch = args.frames_per_epoch,
frames_per_state = args.frames_per_state,
output_directory = args.output_directory,
last_checkpoint = args.last_checkpoint,
max_frames=args.max_frames,
envo=args.environment)
else:
duel_train(env, exploreScheduler, optimizer_constructor=optimizer,
model_type = args.model_type,
batch_size = args.batch_size,
rp_start = args.rp_initial,
rp_size = args.rp_capacity,
exp_frame = args.explore_frame,
exp_initial = args.initial_explore,
exp_final = args.final_explore,
prob_alpha = args.prob_alpha,
gamma = args.discount_factor,
target_update_steps = args.target_update_steps,
frames_per_epoch = args.frames_per_epoch,
frames_per_state = args.frames_per_state,
output_directory = args.output_directory,
last_checkpoint = args.last_checkpoint,
max_frames=args.max_frames,
envo=args.environment)
#TODO: create duel rank eval
print("pass...")
if __name__ == '__main__':
main()