-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.py
65 lines (56 loc) · 2.7 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
from __future__ import absolute_import, division, print_function
import os
import argparse
import torch
from envs import create_atari_env
from model import ES
from train import train_loop, render_env
parser = argparse.ArgumentParser(description='ES')
parser.add_argument('--env-name', default='PongDeterministic-v4',
metavar='ENV', help='environment')
parser.add_argument('--lr', type=float, default=0.1, metavar='LR',
help='learning rate')
parser.add_argument('--lr-decay', type=float, default=1, metavar='LRD',
help='learning rate decay')
parser.add_argument('--sigma', type=float, default=0.05, metavar='SD',
help='noise standard deviation')
parser.add_argument('--useAdam', action='store_true',
help='bool to determine if to use adam optimizer')
parser.add_argument('--n', type=int, default=40, metavar='N',
help='batch size, must be even')
parser.add_argument('--max-episode-length', type=int, default=100000,
metavar='MEL', help='maximum length of an episode')
parser.add_argument('--max-gradient-updates', type=int, default=100000,
metavar='MGU', help='maximum number of updates')
parser.add_argument('--restore', default='', metavar='RES',
help='checkpoint from which to restore')
parser.add_argument('--small-net', action='store_true',
help='Use simple MLP on CartPole')
parser.add_argument('--variable-ep-len', action='store_true',
help="Change max episode length during training")
parser.add_argument('--silent', action='store_true',
help='Silence print statements during training')
parser.add_argument('--test', action='store_true',
help='Just render the env, no training')
if __name__ == '__main__':
args = parser.parse_args()
assert args.n % 2 == 0
if args.small_net and args.env_name not in ['CartPole-v0', 'CartPole-v1',
'MountainCar-v0']:
args.env_name = 'CartPole-v1'
print('Switching env to CartPole')
env = create_atari_env(args.env_name)
chkpt_dir = 'checkpoints/%s/' % args.env_name
if not os.path.exists(chkpt_dir):
os.makedirs(chkpt_dir)
synced_model = ES(env.observation_space.shape[0],
env.action_space, args.small_net)
for param in synced_model.parameters():
param.requires_grad = False
if args.restore:
state_dict = torch.load(args.restore)
synced_model.load_state_dict(state_dict)
if args.test:
render_env(args, synced_model, env)
else:
train_loop(args, synced_model, env, chkpt_dir)