-
Notifications
You must be signed in to change notification settings - Fork 2
/
Agent.py
315 lines (262 loc) · 11.1 KB
/
Agent.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from warnings import warn
from pdb import set_trace
from multiprocessing import Process, Queue
from functools import partial
from torch._C import device
from play import PyGymCallback, Player
import gym
import pygame
import os
import numpy as np
import time
import datetime as dt
from itertools import count
from typing import Tuple
from collections import deque
import scipy
import matplotlib.pyplot as plt
from scipy.stats import uniform, gamma, norm, exponnorm
import matplotlib.pyplot as plt
import torch
import torch.nn.functional as F
import torchvision.transforms as T
import torch.optim as optim
from torch import nn
torch.manual_seed(10)
class Encoder(nn.Module):
def __init__(self):
super(Encoder, self).__init__()
self.conv1 = nn.Conv2d(3, 64, 3)
self.conv2 = nn.Conv2d(64, 64, 3)
self.conv3 = nn.Conv2d(64, 1, 3)
self.conv_bn1 = nn.BatchNorm2d(64)
self.conv_bn2 = nn.BatchNorm2d(1)
self.linear_1 = nn.Linear(64, 100)
def forward(self, x):
x = x
x = F.max_pool2d(self.conv_bn1(self.conv1(x)), 2)
x = F.max_pool2d(self.conv_bn1(self.conv2(x)), 2)
x = F.max_pool2d(self.conv_bn1(self.conv2(x)), 2)
x = F.max_pool2d(self.conv_bn2(self.conv3(x)), 2)
x = x.view(x.size(0), -1)
x = self.linear_1(x) #encoded states might come in "-ve" so no Relu or softmax
return x
class Head(nn.Module):
def __init__(self):
super(Head,self).__init__()
self.linear_1 = nn.Linear(100,16)
self.linear_2 = nn.Linear(16,4)
def forward(self, x):
x = x
x = F.relu(self.linear_1(x))
x = F.relu(self.linear_2(x))
return x
class CreditAssignment():
def __init__(self, dist: scipy.stats.rv_continuous):
self.dist = dist
def __call__(self, s_start: float, s_end: float, h_start: float) -> float:
s_norm_start, s_norm_end = self._normalize(s_start, s_end, h_start)
start_cdf = self.dist.cdf(s_norm_start)
end_cdf = self.dist.cdf(s_norm_end)
return start_cdf - end_cdf
def _normalize(self, s_start: float, s_end: float, h_start: float) -> Tuple[float, float]:
s_norm_start = h_start - s_start
s_norm_end = h_start - s_end
return s_norm_start, s_norm_end
def show_dist(self, s_start: float, s_end: float, h_start: float):
s_norm_start, s_norm_end = self._normalize(s_start, s_end, h_start)
x = np.linspace(self.dist.ppf(.01), self.dist.ppf(.99))
plt.plot(x, self.dist.pdf(x), 'r-')
plt.vlines(s_norm_start,ymin=0, ymax=self.dist.pdf(s_norm_start), color='green')
plt.vlines(s_norm_end, ymin=0, ymax=self.dist.pdf(s_norm_end), color='green')
class BufferDeque():
def __init__(self, size):
self.memory = deque(maxlen=size)
def __len__(self):
return len(self.memory)
def __getitem__(self, index):
if isinstance(index, (tuple, list)):
pointer = list(self.memory)
return [pointer[i] for i in index]
return list(self.memory[index])
def push(self, tensor):
self.memory.append(tensor)
def random_sample(self, batch_size):
rand_idx = np.random.randint(len(self.memory), size=batch_size)
rand_batch = [self.memory[i] for i in rand_idx]
state, action, feedback, credit = [], [], [], []
for s, a, f, c in rand_batch:
state.append(s)
action.append(a)
feedback.append(f)
credit.append(c)
return torch.cat(state), torch.tensor(action), torch.tensor(feedback), torch.tensor(credit)
class NetworkController(PyGymCallback):
'''
state_start_time :- state start time is collected before step in before_step()
state_end_time :- state end time is collected after the step in after_step()
h_time :- time at which the feedback is recorded, which comes along with the feedback in a list in after_step function
questions:-
1. why random sample the credit?
2. Are my assumptons about start and end of state time correct?
3. Why perform the backward function in both after_set_action() and after_set() [as per psuedo code] isn't
performing it in after_set_action() enough ?
4. Not able to get the torch.stack(credit) to work in buffer.random_sample() ?
Yet-To-Do:-
adding backward function
'''
def __init__(self, encoder, head, queue, img_dims = (3, 160, 160), ts_len = 0.3, **kwargs):
super().__init__(**kwargs)
self.encoder = encoder
self.head = head
self.queue = queue
self.img_dims = img_dims
self.ts_len = ts_len
self.dims = 10000
self.buffer = BufferDeque(self.dims)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.sliding_window = deque()
self.opt = optim.Adam(list(self.head.parameters()), lr=1e-1, weight_decay = 1e-1 )
def backward(self, state, action, feedback, credit):
state = state.to(self.device)
credit = credit.to(self.device)
feedback = feedback.to(self.device)
action = action.to(self.device)
self.loss_list = []
h_hat = self.head(self.encoder(state))
h_hat_s_a = h_hat[:, action]
# h_hat_s_a.requires_grad = True
L = torch.mean(credit*(h_hat_s_a - feedback)**2)
self.opt.zero_grad()
L.backward()
self.opt.step()
self.loss_list.append(L)
# print(f"feedback : {feedback}")
# print(f"loss: {L}, q_values: {h_hat}")
def before_step(self):
self.state_start_time = time.time()
def before_set_action(self):
state = self.env.render(mode='rgb_array').transpose((2,0,1))
state = np.ascontiguousarray(state, dtype = np.float32)/255
state = torch.from_numpy(state)
resize = T.Compose([T.ToPILImage(),
T.Resize((self.img_dims[1:])),
T.ToTensor()])
state = resize(state).to(self.device).unsqueeze(0)
return state
def set_action(self):
self.play.state = self.before_set_action()
self.network_output = self.head(self.encoder(self.play.state.to(self.device)))
# self.play.action = np.argmax(self.network_output.cpu().detach().numpy())
self.play.action = torch.argmax(self.network_output)#.cpu().detach().numpy())
def after_set_action(self):
batch=64
loss_fn = nn.MSELoss(reduction = 'mean')
#only when buffer has 50 feedbacks
if len(self.buffer) > 50:
# Only train every certain number of steps
if self.t % 16 == 0:
#rand_batch = np.random.randint(len(self.buffer), size=batch_size)
state, action, feedback, credit = self.buffer.random_sample(batch)
self.backward( state, action, feedback, credit)
def after_step(self):
self.state_end_time = time.time()
fb_dict = self.queue.get()
# fb = torch.tensor(fb_dict["feedback"]).to(self.device)
fb = fb_dict['feedback']
h_time = fb_dict["h_time"]
self.sliding_window.append(
dict(
state = self.state,
action = self.action,
feedback = fb,
s_start = self.state_start_time,
s_end = self.state_end_time
)
)
if fb != 0:
ca = CreditAssignment(uniform(0.2, 0.8))
state, action, credit = [], [], []
for win in self.sliding_window:
credit_for_state = ca(s_start=win["s_start"], s_end=win["s_end"], h_start=h_time)
if credit_for_state !=0:
state.append(win['state'])
action.append(win['action'])
# credit.append(torch.tensor(credit_for_state, dtype=torch.float32).to(self.device))
credit.append(credit_for_state)
self.buffer.push([
win['state'],
win['action'],
fb,
credit_for_state
# torch.Tensor(credit)
])
state, action, credit = torch.cat(state), torch.tensor(action), torch.tensor(credit)
feedback = torch.full(credit.size(), fb)
self.backward( state, action, feedback, credit)
def after_play(self):
plt.title('Head_Network_Error')
plt.plot(self.loss_list)
plt.savefig('Test_Error')
class FeedbackListener(Process):
def __init__(self,fb_queue,video_size=(200, 100)):
super().__init__()
self.video_size = video_size
self.fb_queue = fb_queue
def run(self, fps=60):
self._init_pygames()
self.listening = True
while self.listening:
fb, fill = self._do_pygame_events()
self._update_screen(fill)
#add feedback to queue is feeback =! 0
self.clock.tick(fps)
self.fb_queue.put(
dict(
feedback = fb,
h_time = time.time()
))
def _init_pygames(self):
pygame.init()
self.screen = pygame.display.set_mode(self.video_size, pygame.RESIZABLE)
self.clock = pygame.time.Clock()
self._update_screen()
def _do_pygame_events(self):
fb, fill = 0, None
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
fill = self.screen.fill((0, 255, 0))
fb = 1
elif event.key == pygame.K_2:
fill = self.screen.fill((255, 0, 0))
fb = -1
elif event.type == pygame.VIDEORESIZE:
self.video_size = event.size
self._update_screen(fill)
elif event.type == pygame.QUIT:
self.listening = False
return fb, fill
def _update_screen(self, fill=None):
if fill is None:
fill = self.screen.fill((0, 0, 0))
pygame.display.update(fill)
def main():
env = gym.make("Bowling-v0").unwrapped
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
encoder = Encoder().to(device)
head_net = Head().to(device)
encoder.load_state_dict(torch.load("auto_encoder/Type_1/encoder.pt", map_location=device))
# Freeze encoder weights
for name, params in encoder.named_parameters():
params.requires_grad = False
# opt = torch.optim.Adam(head_net.parameters(), lr=1e-4, weight_decay=1e-1)
Feedback_queue = Queue()
listener = FeedbackListener(Feedback_queue) #pass it to listener ()
listener.start()
player = Player(callbacks=[NetworkController(encoder= encoder, head=head_net, queue=Feedback_queue,
env=env, zoom=4, fps=60, human=True)]) #pass the queue
player.play(n_episodes = 1)
listener.join()
if __name__ == "__main__":
main()