-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpong_utils.py
269 lines (198 loc) · 8.6 KB
/
pong_utils.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
from parallelEnv import parallelEnv
import matplotlib
import matplotlib.pyplot as plt
import torch
import numpy as np
from JSAnimation.IPython_display import display_animation
from matplotlib import animation
from IPython.display import display
import random as rand
RIGHT=4
LEFT=5
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# preprocess a single frame
# crop image and downsample to 80x80
# stack two frames together as input
def preprocess_single(image, bkg_color = np.array([144, 72, 17])):
img = np.mean(image[34:-16:2,::2]-bkg_color, axis=-1)/255.
return img
# convert outputs of parallelEnv to inputs to pytorch neural net
# this is useful for batch processing especially on the GPU
def preprocess_batch(images, bkg_color = np.array([144, 72, 17])):
list_of_images = np.asarray(images)
if len(list_of_images.shape) < 5:
list_of_images = np.expand_dims(list_of_images, 1)
# subtract bkg and crop
list_of_images_prepro = np.mean(list_of_images[:,:,34:-16:2,::2]-bkg_color,
axis=-1)/255.
batch_input = np.swapaxes(list_of_images_prepro,0,1)
return torch.from_numpy(batch_input).float().to(device)
# function to animate a list of frames
def animate_frames(frames):
plt.axis('off')
# color option for plotting
# use Greys for greyscale
cmap = None if len(frames[0].shape)==3 else 'Greys'
patch = plt.imshow(frames[0], cmap=cmap)
fanim = animation.FuncAnimation(plt.gcf(), \
lambda x: patch.set_data(frames[x]), frames = len(frames), interval=30)
display(display_animation(fanim, default_mode='once'))
# play a game and display the animation
# nrand = number of random steps before using the policy
def play(env, policy, time=2000, preprocess=None, nrand=5):
env.reset()
# star game
env.step(1)
# perform nrand random steps in the beginning
for _ in range(nrand):
frame1, reward1, is_done, _ = env.step(np.random.choice([RIGHT,LEFT]))
frame2, reward2, is_done, _ = env.step(0)
anim_frames = []
for _ in range(time):
frame_input = preprocess_batch([frame1, frame2])
prob = policy(frame_input)
# RIGHT = 4, LEFT = 5
action = RIGHT if rand.random() < prob else LEFT
frame1, _, is_done, _ = env.step(action)
frame2, _, is_done, _ = env.step(0)
if preprocess is None:
anim_frames.append(frame1)
else:
anim_frames.append(preprocess(frame1))
if is_done:
break
env.close()
animate_frames(anim_frames)
return
# collect trajectories for a parallelized parallelEnv object
def collect_trajectories(envs, policy, tmax=200, nrand=5):
# number of parallel instances
n=len(envs.ps)
#initialize returning lists and start the game!
state_list=[]
reward_list=[]
prob_list=[]
action_list=[]
envs.reset()
# start all parallel agents
envs.step([1]*n)
# perform nrand random steps
for _ in range(nrand):
fr1, re1, _, _ = envs.step(np.random.choice([RIGHT, LEFT],n))
fr2, re2, _, _ = envs.step([0]*n)
for t in range(tmax):
# prepare the input
# preprocess_batch properly converts two frames into
# shape (n, 2, 80, 80), the proper input for the policy
# this is required when building CNN with pytorch
batch_input = preprocess_batch([fr1,fr2])
# probs will only be used as the pi_old
# no gradient propagation is needed
# so we move it to the cpu
probs = policy(batch_input).squeeze().cpu().detach().numpy()
action = np.where(np.random.rand(n) < probs, RIGHT, LEFT)
probs = np.where(action==RIGHT, probs, 1.0-probs)
# advance the game (0=no action)
# we take one action and skip game forward
fr1, re1, is_done, _ = envs.step(action)
fr2, re2, is_done, _ = envs.step([0]*n)
reward = re1 + re2
# store the result
state_list.append(batch_input)
reward_list.append(reward)
prob_list.append(probs)
action_list.append(action)
# stop if any of the trajectories is done
# we want all the lists to be retangular
if is_done.any():
break
# return pi_theta, states, actions, rewards, probability
return prob_list, state_list, \
action_list, reward_list
# convert states to probability, passing through the policy
def states_to_prob(policy, states):
states = torch.stack(states)
policy_input = states.view(-1,*states.shape[-3:])
return policy(policy_input).view(states.shape[:-3])
# return sum of log-prob divided by T
# same thing as -policy_loss
def surrogate(policy, old_probs, states, actions, rewards,
discount = 0.995, beta=0.01):
discount = discount**np.arange(len(rewards))
rewards = np.asarray(rewards)*discount[:,np.newaxis]
# convert rewards to future rewards
rewards_future = rewards[::-1].cumsum(axis=0)[::-1]
mean = np.mean(rewards_future, axis=1)
std = np.std(rewards_future, axis=1) + 1.0e-10
rewards_normalized = (rewards_future - mean[:,np.newaxis])/std[:,np.newaxis]
# convert everything into pytorch tensors and move to gpu if available
actions = torch.tensor(actions, dtype=torch.int8, device=device)
old_probs = torch.tensor(old_probs, dtype=torch.float, device=device)
rewards = torch.tensor(rewards_normalized, dtype=torch.float, device=device)
# convert states to policy (or probability)
new_probs = states_to_prob(policy, states)
new_probs = torch.where(actions == RIGHT, new_probs, 1.0-new_probs)
ratio = new_probs/old_probs
# include a regularization term
# this steers new_policy towards 0.5
# add in 1.e-10 to avoid log(0) which gives nan
entropy = -(new_probs*torch.log(old_probs+1.e-10)+ \
(1.0-new_probs)*torch.log(1.0-old_probs+1.e-10))
return torch.mean(ratio*rewards + beta*entropy)
# clipped surrogate function
# similar as -policy_loss for REINFORCE, but for PPO
def clipped_surrogate(policy, old_probs, states, actions, rewards,
discount=0.995,
epsilon=0.1, beta=0.01):
discount = discount**np.arange(len(rewards))
rewards = np.asarray(rewards)*discount[:,np.newaxis]
# convert rewards to future rewards
rewards_future = rewards[::-1].cumsum(axis=0)[::-1]
mean = np.mean(rewards_future, axis=1)
std = np.std(rewards_future, axis=1) + 1.0e-10
rewards_normalized = (rewards_future - mean[:,np.newaxis])/std[:,np.newaxis]
# convert everything into pytorch tensors and move to gpu if available
actions = torch.tensor(actions, dtype=torch.int8, device=device)
old_probs = torch.tensor(old_probs, dtype=torch.float, device=device)
rewards = torch.tensor(rewards_normalized, dtype=torch.float, device=device)
# convert states to policy (or probability)
new_probs = states_to_prob(policy, states)
new_probs = torch.where(actions == RIGHT, new_probs, 1.0-new_probs)
# ratio for clipping
ratio = new_probs/old_probs
# clipped function
clip = torch.clamp(ratio, 1-epsilon, 1+epsilon)
clipped_surrogate = torch.min(ratio*rewards, clip*rewards)
# include a regularization term
# this steers new_policy towards 0.5
# add in 1.e-10 to avoid log(0) which gives nan
entropy = -(new_probs*torch.log(old_probs+1.e-10)+ \
(1.0-new_probs)*torch.log(1.0-old_probs+1.e-10))
# this returns an average of all the entries of the tensor
# effective computing L_sur^clip / T
# averaged over time-step and number of trajectories
# this is desirable because we have normalized our rewards
return torch.mean(clipped_surrogate + beta*entropy)
import torch
import torch.nn as nn
import torch.nn.functional as F
class Policy(nn.Module):
def __init__(self):
super(Policy, self).__init__()
# 80x80x2 to 38x38x4
# 2 channel from the stacked frame
self.conv1 = nn.Conv2d(2, 4, kernel_size=6, stride=2, bias=False)
# 38x38x4 to 9x9x32
self.conv2 = nn.Conv2d(4, 16, kernel_size=6, stride=4)
self.size=9*9*16
# two fully connected layer
self.fc1 = nn.Linear(self.size, 256)
self.fc2 = nn.Linear(256, 1)
# Sigmoid to
self.sig = nn.Sigmoid()
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = x.view(-1,self.size)
x = F.relu(self.fc1(x))
return self.sig(self.fc2(x))