-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
152 lines (132 loc) · 6.1 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
"""The main entrypoint."""
import argparse
import ctypes
import multiprocessing as mp
import numpy as np
import torch
from model import MLPBase, CNNBase, Discrete, Normal
from train import train_step
from worker import GamePlayer
from gae import gae
from util import get_gym_env_info
from running_mean_std import RunningMeanStd, apply_normalizer
from tracker import WandBTracker, ConsoleTracker
parser = argparse.ArgumentParser()
parser.add_argument('--name')
parser.add_argument('--env_name', default="PongNoFrameskip-v4")
parser.add_argument('--model', default="mlp")
parser.add_argument('--gamma', default=.99, type=float)
parser.add_argument('--lam', default=.95, type=float)
parser.add_argument('--epsilon', default=.1, type=float)
parser.add_argument('--value_loss_coef', default=.5, type=float)
parser.add_argument('--entropy_coef', default=.01, type=float)
parser.add_argument('--num_workers', default=8, type=int)
parser.add_argument('--num_iterations', default=10**10, type=int)
parser.add_argument('--num_steps', default=128, type=int)
parser.add_argument('--ppo_epochs', default=4, type=int)
parser.add_argument('--num_batches', default=4, type=int)
parser.add_argument('--lr', default=2.5e-4, type=float)
parser.add_argument('--device', default="cpu")
parser.add_argument('--end_on_life_loss', default=False)
parser.add_argument('--clip_rewards', default=False)
parser.add_argument('--logger', default="console")
args = parser.parse_args()
args.batch_size = int(args.num_workers / args.num_batches)
args.num_actions, args.obs_shape, args.num_obs, action_type = \
get_gym_env_info(args.env_name)
device = torch.device(args.device)
# Define common shapes for convenience
scalar_shape = (args.num_workers, args.num_steps)
if args.model == "cnn":
# This is the shape after preprocessing (reshape + greyscale)
batch_obs_shape = (args.num_workers, args.num_steps, 1, 84, 84)
args.steps_to_skip = 4
args.end_on_life_loss = True
elif args.model == "mlp":
batch_obs_shape = (args.num_workers, args.num_steps, args.num_obs)
args.steps_to_skip = 1
# Make a shared array to get observations from each process
# and wrap it with Numpy
shared_obs_c = mp.Array(ctypes.c_float, int(np.prod(batch_obs_shape)))
shared_obs = np.frombuffer(shared_obs_c.get_obj(), dtype=np.float32)
shared_obs = np.reshape(shared_obs, batch_obs_shape)
# Make arrays to store all other rollout info
rewards = np.zeros(scalar_shape, dtype=np.float32)
discounted_rewards = np.zeros(scalar_shape, dtype=np.float32)
episode_ends = np.zeros(scalar_shape, dtype=np.float32)
values = np.zeros(scalar_shape, dtype=np.float32)
policy_probs = np.zeros(scalar_shape, dtype=np.float32)
if action_type == "continuous":
action_shape = (args.num_workers, args.num_steps, args.num_actions)
actions = np.zeros(action_shape, dtype=np.float32)
elif action_type == "discrete":
actions = np.zeros(scalar_shape, dtype=np.int32)
# Build the key classes
if args.logger == "wandb":
tracker = WandBTracker(args.name, args)
else:
tracker = ConsoleTracker(args.name, args)
game_player = GamePlayer(args, shared_obs)
if action_type == "discrete":
dist = Discrete(args.num_actions)
elif action_type == "continuous":
dist = Normal(args.num_actions)
if args.model == "cnn":
model = CNNBase(1, args.num_actions, dist).to(device)
elif args.model == "mlp":
model = MLPBase(args.num_obs, args.num_actions, dist).to(device)
optim = torch.optim.Adam(model.parameters(), lr=args.lr)
reward_normalizer = RunningMeanStd(shape=())
obs_normalizer = RunningMeanStd(shape=(args.num_obs, ))
# Main loop
i = 0
for i in range(args.num_iterations):
# Run num_steps of the game in each worker and accumulate results in
# the data arrays
game_player.run_rollout(args, shared_obs, rewards, discounted_rewards,
values, policy_probs, actions, model,
obs_normalizer, device, episode_ends)
observations = shared_obs.copy()
if args.model == "mlp":
# Normalize rewards
rewards = apply_normalizer(rewards, reward_normalizer,
update_data=discounted_rewards,
center=False)
# Compute advantages and future discounted rewards with GAE
advantages = gae(rewards, values, episode_ends, args.gamma, args.lam)
advantages = advantages.astype(np.float32)
rewards_to_go = advantages + values
# normalize advantages
raw_advantages = advantages.copy()
advantages = (advantages - advantages.mean()) / (advantages.std() + 1e-5)
train_data = [advantages, rewards_to_go, values, actions, observations,
policy_probs]
train_data = [torch.tensor(x).to(device) for x in train_data]
# Split the data into batches in the num_workers dimension
for epoch in range(args.ppo_epochs):
for batch in range(args.num_batches):
start = batch * args.batch_size
end = (batch + 1) * args.batch_size
# slice out batches from train_data
batch_data = [x[start:end] for x in train_data]
# flatten (batch_size,num_steps,...) into ((batch_size*num_steps,...)
batch_data = [x.reshape((-1, ) + x.shape[2:]) for x in batch_data]
# Step batch
train_step(model, optim, batch_data, args, i, tracker)
tracker.log_iteration_time(args.num_workers * args.num_steps, i)
if i % 5 == 0:
tracker.add_histogram("episode/episode_length",
game_player.episode_length, i)
tracker.add_histogram("episode/episode_rewards",
game_player.episode_rewards, i)
if i % 25 == 0:
tracker.add_histogram("training/raw_advantages",
raw_advantages, i)
tracker.add_histogram("training/rewards",
rewards, i)
tracker.add_histogram("training/observations",
observations, i)
tracker.add_histogram("training/reward_std",
np.sqrt(reward_normalizer.var), i)
if args.model == "cnn":
tracker.add_image("images/obs", observations[0, 0, 0], i)