-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
618 lines (518 loc) · 26.9 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
import torch
import torch.nn as nn
from networks import DDQN, Critic
import torch.optim as optim
import torch.nn.functional as F
from torch.nn.utils import clip_grad_norm_
import numpy as np
import random
from teacher import *
from torch.distributions import Categorical
import copy
class CQLAgent():
def __init__(self, state_size, action_size, hidden_size=64, device="cpu", config=None):
self.state_size = state_size
self.action_size = action_size
self.eps_rule = 1.0
self.device = device
self.tau = 1e-3
self.seed = 42
self.gamma = 0.99
# parameters for configuration file
self.env_name = config.env
self.lam = config.lam
self.lr = config.lr
self.use_teacher = config.use_teach
self.warm_start = config.warm_start
self.teacher_update = config.teacher_update
self.behaviour_policy = None # PPO.load("/home/ubuntu/OODOfflineRL/ppo-LunarLander-discrete-v2")
self.student_network = DDQN(state_size=self.state_size,
action_size=self.action_size,
layer_size=hidden_size
).to(self.device)
self.target_net = DDQN(state_size=self.state_size,
action_size=self.action_size,
layer_size=hidden_size
).to(self.device)
self.optimizer = optim.Adam(params=self.student_network.parameters(), lr=self.lr) #default 1e-4
# Teacher initialization for cartpole env
if 'Cart' in self.env_name:
self.teacher_actor = init_cart_nets('one_hot')
# Teacher initialization for lunar lander env
if 'Lunar' in self.env_name:
self.teacher_actor = init_lander_nets('one_hot')
if 'Mountain' in self.env_name:
self.teacher_actor = init_mountain_nets('one_hot')
# Initialize optimizer for teacher critic will use this for teacher training later
if self.use_teacher:
self.teacher_optimizer = optim.Adam(self.teacher_actor.parameters(), lr=1e-5)
#Function to check the entropy of the actions predicted
def calculate_entropy(self, predictions):
# Convert predictions to probabilities using softmax
probs = torch.nn.functional.softmax(predictions, dim=-1)
# Calculate entropy
entropy = -torch.sum(probs * torch.log(probs + 1e-8), dim=-1)
return entropy.mean()
#Function to calculate the uncertainity of the states over 100 forward passes
def calculate_uncertainity(self,action_rule,action_pred,states_mismatch):
num_forward_passes = 10
# self.student_network.eval()
# Lists to store predictions
predictions_pred = []
predictions_rule = []
for i in range(num_forward_passes):
Q_val = self.student_network(states_mismatch, training=False).detach()
# Gather the Q values for rule action and student action
Q_val_pred = Q_val.gather(1, action_pred)
Q_val_rule = Q_val.gather(1, action_rule)
# Append predictions to lists
predictions_pred.append(Q_val_pred.cpu().numpy())
predictions_rule.append(Q_val_rule.cpu().numpy())
# Convert predictions to tensors
predictions_pred = torch.tensor(predictions_pred)
predictions_rule = torch.tensor(predictions_rule)
# Calculate the variance for predictions over 10 forward passes
variance_pred = torch.var(predictions_pred, dim=0)
variance_rule = torch.var(predictions_rule, dim=0)
# self.student_network.train()
return torch.mean(variance_pred), torch.mean(variance_rule)
# Get action from the teacher network
# The network return a probability distribution over the action
def get_action_teacher(self, observation):
obs = torch.Tensor(observation)
self.teacher_actor.eval()
if 'Cart' in self.env_name:
obs = obs.view(1, -1)
probs = self.teacher_actor(obs)
if 'Cart' in self.env_name:
probs = probs.view(-1)
action = np.argmax(probs.cpu().data.numpy())
self.teacher_actor.train()
return action, probs.cpu()
def get_action_teacher_net(self, observation):
obs = torch.Tensor(observation)
self.teacher_actor.eval()
with torch.no_grad():
action_values = self.teacher_actor(obs)
self.teacher_actor.train()
return np.argmax(action_values.cpu().data.numpy()), action_values.cpu()
# Get the determinstic action from the network
def get_action(self, state, epsilon):
if random.random() > epsilon:
state = torch.from_numpy(state).float().unsqueeze(0).to(self.device)
self.student_network.eval()
with torch.no_grad():
action_values = self.student_network(state)
self.student_network.train()
action = np.argmax(action_values.cpu().data.numpy(), axis=1)
else:
action = random.choices(np.arange(self.action_size), k=1)
return action
def get_deter_action(self, state):
# state = torch.from_numpy(state).float().unsqueeze(0).to(self.device)
self.student_network.eval()
with torch.no_grad():
action_values = self.student_network(state.unsqueeze(0))
# print(f'state ----- {state} ----action_values--- {action_values}')
self.student_network.train()
action = np.argmax(action_values.cpu().data.numpy(), axis=1)
return action
# For now let us see if having a behaviour policy helps
# Replace actions for each step of behaviour policy
def calculate_rule_loss_behaviour(self, states):
# Let us only consider the simplest rule right now
# Obs[1] < 0.06 ∧ Obs[6] == 1 ∧ Obs[7] == 1 -> action == 0
accumulated_penalty = 0
for s in states:
predicted_action = self.get_deter_action(s)
state = np.array([s.cpu().data.numpy()])
#selct actions using PPO algorithm
actions, states = self.behaviour_policy.predict(
state, # type: ignore[arg-type]
state=None,
episode_start=1,
deterministic=True,
)
rule_action = actions[0]
# Calculate euclidean distance between both actions
dist = np.linalg.norm(predicted_action - rule_action)
accumulated_penalty += dist
return accumulated_penalty
# This is main function for integration of teacher loss with the student network
# Reduce the Q value for the stundent action
# Increase the Q value for the teacher predicted action
def calculate_teacher_loss(self, states, Q_expected, ep):
action_rule = []
action_pred = []
states_mismatch = []
rule_error = 0
# print(f'Q_expected ======= {Q_expected}')
state_copy = copy.deepcopy(states)
for s in state_copy:
condition = check_condition(s,self.env_name)
# Triggering in conditions for which the rules are written for, specific to environment
if (condition):
# Get the predicted action from the oriinal student_network
predicted_action = self.get_deter_action(s)
# selct actions using the rule based DDT
rule_action, prob = self.get_action_teacher(s)
# Increase Q value for rule action and decrease Q value for predicted action if there is an action mismatch
# If actions dont match for a particular state add it to the list of states
if(predicted_action!=rule_action):
# print(f'state mismatch ======= {s.cpu().data.numpy()} ========== {rule_action} ========= {predicted_action}')
states_mismatch.append(s.cpu().data.numpy())
action_rule.append(rule_action)
action_pred.append(predicted_action[0])
if len(states_mismatch)!=0:
action_rule = torch.from_numpy(np.vstack([a for a in action_rule])).long().to(self.device)
action_pred = torch.from_numpy(np.vstack([a for a in action_pred])).long().to(self.device)
states_mismatch = torch.from_numpy(np.vstack([s for s in states_mismatch])).float().to(self.device)
# print(f'action_pred ======= {action_pred}')
# Calculate Q values for only the states with mismatched action
Q_val = self.student_network(states_mismatch)
# Gathe the Q values for rule action and student action
Q_val_pred = Q_val.gather(1,action_pred)
Q_val_rule = Q_val.gather(1,action_rule)
# Shift the Q value of student action to teacher action
if ep<self.warm_start:
rule_error = F.mse_loss(Q_val_pred, Q_val_rule) + F.mse_loss(Q_val_rule, Q_val_pred)
else:
# Do not update the student network in this case and train the teacher network
rule_error = 0
'''if ep%self.teacher_update ==0 and torch.mean(Q_val_rule) < torch.mean(Q_val_pred):
var_student, var_teacher = self.calculate_uncertainity(action_rule,action_pred,states_mismatch)
if var_student < var_teacher:
teacher_loss = self.teacher_learn(states)
print(f'--------Training teacher-------------{teacher_loss}----------{var_student}------- {var_teacher}')
rule_error = F.mse_loss(Q_expected, Q_expected)
else:
rule_error = F.mse_loss(Q_val_pred, Q_val_rule) + F.mse_loss(Q_val_rule, Q_val_pred)
else:
rule_error = F.mse_loss(Q_val_pred, Q_val_rule) + F.mse_loss(Q_val_rule, Q_val_pred)'''
else:
# else set the rule error to be 0
rule_error = F.mse_loss(Q_expected, Q_expected)
# print(rule_error)
return rule_error
# Code for updating the teacher network based on uncertainity
# Uses cross entropy loss between the student logits and the teacher logits
def teacher_learn(self,experiences):
states = experiences
logits_teacher = self.teacher_actor(states)
# Calculate action probabilities of the student by applying softmax
Q_a_student = self.student_network(states)
logits_student = F.softmax(Q_a_student.detach())
criterion = nn.CrossEntropyLoss()
teacher_loss = criterion(logits_teacher, logits_student)
self.teacher_optimizer.zero_grad()
teacher_loss.backward()
clip_grad_norm_(self.teacher_actor.parameters(), 1.)
self.teacher_optimizer.step()
return teacher_loss
# Code for evaluating OOD data samples
def evaluate(self, experiences):
state, action = experiences
with torch.no_grad():
current_Qs = self.student_network(state).detach()
action_ex, prob = self.get_action_teacher(state).detach()
selected_actions = torch.argmax(prob, dim=1).to(self.device)
# print(f'action ------- {selected_actions} ------- {action}')
selected_actions = selected_actions.view(action.shape)
# calculate regularizing loss
max_Q_values, _ = torch.max(current_Qs, dim=1)
R_loss = torch.mean((max_Q_values- current_Qs.gather(1, selected_actions).squeeze(1)))
return R_loss.detach().cpu().item()
def cql_loss(self, q_values, current_action):
"""Computes the CQL loss for a batch of Q-values and actions."""
logsumexp = torch.logsumexp(q_values, dim=1, keepdim=True)
q_a = q_values.gather(1, current_action)
return (logsumexp - q_a).mean()
def learn(self, experiences, ep):
states, actions, rewards, next_states, dones = experiences
with torch.no_grad():
Q_targets_next = self.target_net(next_states).detach().max(1)[0].unsqueeze(1)
Q_targets = rewards + (self.gamma * Q_targets_next * (1 - dones))
Q_a_s = self.student_network(states)
# print(f'Q_a_s ======= {Q_a_s}')
Q_expected = Q_a_s.gather(1, actions)
cql1_loss = self.cql_loss(Q_a_s, actions)
bellman_error = F.mse_loss(Q_expected, Q_targets)
# If use teacher is true then integrate the rule loss otherwise set it to 0
if self.use_teacher:
rule_loss = self.calculate_teacher_loss(states, Q_expected, ep)
else:
rule_loss = F.mse_loss(Q_expected, Q_expected)
# Add a rule regulizer term
self.eps_rule = self.eps_rule-0.1
if self.eps_rule<0:
self.eps_rule = 0
q1_loss = cql1_loss + 0.5 * bellman_error + self.lam * rule_loss # 0.5 works best for lunar lander 0.9 for cartpole
self.optimizer.zero_grad()
q1_loss.backward()
clip_grad_norm_(self.student_network.parameters(), 1.)
self.optimizer.step()
# ------------------- update target student_network ------------------- #
self.soft_update(self.student_network, self.target_net)
return q1_loss.detach().item(), cql1_loss.detach().item(), bellman_error.detach().item(), rule_loss.detach().item()
def soft_update(self, local_model, target_model):
for target_param, local_param in zip(target_model.parameters(), local_model.parameters()):
target_param.data.copy_(self.tau*local_param.data + (1.0-self.tau)*target_param.data)
"===========================CQL Agent using Huber Loss================================="
class CQL():
def __init__(self,
state_size,
action_size,
device=None,
config = None):
# super(CQL, self).__init__(obs_space, action_space, discount, lr, seed)
# epsilon decay
self.initial_eps = 1.0
self.end_eps = 1e-2
self.eps_decay_period = 1000
self.lr = 1e-4
self.device = device
self.discount = 0.99
self.slope = (self.end_eps - self.initial_eps) / self.eps_decay_period
self.eval_eps = 0.
# parameters for configuration file
self.env_name = config.env
self.lam = config.lam
self.lr = config.lr
self.use_teacher = config.use_teach
self.warm_start = config.warm_start
self.teacher_update = config.teacher_update
self.tlr = config.tlr
# loss function
self.huber = nn.SmoothL1Loss()
self.ce = nn.CrossEntropyLoss()
# Number of training iterations
self.iterations = 0
# After how many training steps 'snap' target to main network?
self.target_update_freq = 100
# Q-Networks
self.student_network = Critic(state_size[0], action_size, 42).to(self.device)
self.student_network_target = copy.deepcopy(self.student_network)
# Optimization
self.optimizer = torch.optim.Adam(params=self.student_network.parameters(), lr=self.lr)
# Teacher initialization for cartpole env
if 'Cart' in self.env_name:
self.teacher_actor = init_cart_nets('one_hot')
# Teacher initialization for lunar lander env
if 'Lunar' in self.env_name:
self.teacher_actor = init_lander_nets('one_hot')
if 'Mountain' in self.env_name:
self.teacher_actor = init_mountain_nets('one_hot')
if 'Lava' in self.env_name:
self.teacher_actor = init_minigrid_nets('one_hot')
if 'Dynamic' in self.env_name:
self.teacher_actor = init_dynamic_nets('one_hot')
# Initialize optimizer for teacher critic will use this for teacher training later
if self.use_teacher:
self.teacher_optimizer = optim.Adam(self.teacher_actor.parameters(), lr=self.tlr)
# temperature parameter
self.alpha = 0.1
def get_action(self, state, epsilon=0):
self.student_network.eval()
with torch.no_grad():
state = torch.FloatTensor(state).to(self.device)
actions = self.student_network(state).cpu()
action = np.argmax(actions.data.numpy())
self.student_network.train()
return [action]
def get_deter_action(self, state):
# state = torch.from_numpy(state).float().unsqueeze(0).to(self.device)
self.student_network.eval()
with torch.no_grad():
action_values = self.student_network(state.unsqueeze(0))
# print(f'state ----- {state} ----action_values--- {action_values}')
self.student_network.train()
action = np.argmax(action_values.cpu().data.numpy(), axis=1)
return action
# Get action from the teacher network
# The network return a probability distribution over the action
def get_action_teacher(self, observation):
obs = torch.Tensor(observation)
self.teacher_actor.eval()
probs = self.teacher_actor(obs)
action = np.argmax(probs.cpu().data.numpy())
self.teacher_actor.train()
return action, probs.cpu()
#Function to check the entropy of the actions predicted
def calculate_entropy(self, predictions):
# Convert predictions to probabilities using softmax
probs = torch.nn.functional.softmax(predictions, dim=-1)
# Calculate entropy
entropy = -torch.sum(probs * torch.log(probs + 1e-8), dim=-1)
return entropy.mean()
#Function to calculate the uncertainity of the states over 100 forward passes
def calculate_uncertainity(self,action_rule,action_pred,states_mismatch):
num_forward_passes = 10
# self.student_network.eval()
# Lists to store predictions
predictions_pred = []
predictions_rule = []
for i in range(num_forward_passes):
Q_val = self.student_network(states_mismatch, training=False).detach()
# Gather the Q values for rule action and student action
Q_val_pred = Q_val.gather(1, action_pred)
Q_val_rule = Q_val.gather(1, action_rule)
# Append predictions to lists
predictions_pred.append(Q_val_pred.cpu().numpy())
predictions_rule.append(Q_val_rule.cpu().numpy())
# Convert predictions to tensors
predictions_pred = torch.tensor(predictions_pred)
predictions_rule = torch.tensor(predictions_rule)
# Calculate the variance for predictions over 10 forward passes
variance_pred = torch.var(predictions_pred, dim=0)
variance_rule = torch.var(predictions_rule, dim=0)
# self.student_network.train()
return torch.mean(variance_pred), torch.mean(variance_rule)
# This is main function for integration of teacher loss with the student network
# Reduce the Q value for the stundent action
# Increase the Q value for the teacher predicted action
def calculate_teacher_loss(self, states, Q_expected, ep, steps):
action_rule = []
action_pred = []
states_mismatch = []
rule_error = 0
# print(f'Q_expected ======= {Q_expected}')
state_copy = copy.deepcopy(states)
for s in state_copy:
condition = check_condition(s,self.env_name)
# Triggering in conditions for which the rules are written for, specific to environment
if (condition):
# Get the predicted action from the oriinal student_network
predicted_action = self.get_deter_action(s)
# selct actions using the rule based DDT
rule_action, prob = self.get_action_teacher(s)
# Increase Q value for rule action and decrease Q value for predicted action if there is an action mismatch
# If actions dont match for a particular state add it to the list of states
if(predicted_action!=rule_action):
# print(f'state mismatch ======= {s.cpu().data.numpy()} ========== {rule_action} ========= {predicted_action}')
states_mismatch.append(s.cpu().data.numpy())
action_rule.append(rule_action)
action_pred.append(predicted_action[0])
if len(states_mismatch)!=0:
action_rule = torch.from_numpy(np.vstack([a for a in action_rule])).long().to(self.device)
action_pred = torch.from_numpy(np.vstack([a for a in action_pred])).long().to(self.device)
states_mismatch = torch.from_numpy(np.vstack([s for s in states_mismatch])).float().to(self.device)
# print(f'action_pred ======= {action_pred}')
# Calculate Q values for only the states with mismatched action
Q_val = self.student_network(states_mismatch)
# Gathe the Q values for rule action and student action
Q_val_pred = Q_val.gather(1,action_pred)
Q_val_rule = Q_val.gather(1,action_rule)
# Shift the Q value of student action to teacher action
if ep<self.warm_start:
rule_error = F.mse_loss(Q_val_pred, Q_val_rule) + F.mse_loss(Q_val_rule, Q_val_pred)
else:
# Do not update the student network in this case and train the teacher network
# Check for high Q value and low uncertaininty
if torch.mean(Q_val_rule) < torch.mean(Q_val_pred):
if 'Mountain' in self.env_name:
rule_error = F.mse_loss(Q_val_pred, Q_val_rule) + F.mse_loss(Q_val_rule, Q_val_pred)
else:
# set rule error to be 0
rule_error = F.mse_loss(Q_expected, Q_expected)
# To not do uncertainity check every epoch and to reduce the update on the teacher network
if ep%self.teacher_update ==0 and steps<2:
var_student, var_teacher = self.calculate_uncertainity(action_rule,action_pred,states_mismatch)
if var_student < var_teacher:
teacher_loss = self.teacher_learn(states_mismatch)
print(f'--------Training teacher-------------{teacher_loss}----------{var_student}------- {var_teacher}')
rule_error = F.mse_loss(Q_expected, Q_expected)
else:
rule_error = F.mse_loss(Q_val_pred, Q_val_rule) + F.mse_loss(Q_val_rule, Q_val_pred)
else:
# If Q value not higher apply rule loss
rule_error = F.mse_loss(Q_val_pred, Q_val_rule) + F.mse_loss(Q_val_rule, Q_val_pred)
else:
# else if no state match domain condition set the rule error to be 0
rule_error = F.mse_loss(Q_expected, Q_expected)
# print(rule_error)
return rule_error
# Code for updating the teacher network based on uncertainity
# Uses cross entropy loss between the student logits and the teacher logits
def teacher_learn(self,states):
states = states
logits_teacher = self.teacher_actor(states)
# Calculate action probabilities of the student by applying softmax
Q_a_student = self.student_network(states)
with torch.no_grad():
logits_student = F.softmax(Q_a_student.detach())
criterion = nn.CrossEntropyLoss()
teacher_loss = criterion(logits_teacher, logits_student)
self.teacher_optimizer.zero_grad()
teacher_loss.backward()
clip_grad_norm_(self.teacher_actor.parameters(), 1.)
self.teacher_optimizer.step()
return teacher_loss
def policy(self, state, eval=False):
# set networks to eval mode
self.student_network.eval()
if eval:
eps = self.eval_eps
else:
eps = max(self.slope * self.iterations + self.initial_eps, self.end_eps)
# epsilon greedy policy
if self.rng.uniform(0, 1) > eps:
with torch.no_grad():
state = torch.FloatTensor(state).to(self.device)
q_val = self.student_network(state).cpu()
return q_val.argmax().item(), q_val, np.nan
else:
return self.rng.integers(self.action_space), np.nan, np.nan
# Code for evaluating OOD data samples
def evaluate(self, experiences):
state, action = experiences
with torch.no_grad():
current_Qs = self.student_network(state).detach()
prob = self.teacher_actor(state).detach()
selected_actions = torch.argmax(prob, dim=1).to(self.device)
# print(f'action ------- {selected_actions} ------- {action}')
selected_actions = selected_actions.view(action.shape)
# calculate regularizing loss
max_Q_values, _ = torch.max(current_Qs, dim=1)
O_loss = torch.mean((max_Q_values- current_Qs.gather(1, selected_actions).squeeze(1))).detach().cpu().item()
return O_loss
def learn(self, experiences, ep, steps):
# Sample replay buffer
state, action, reward, next_state, not_done = experiences
# set networks to train mode
self.student_network.train()
self.student_network_target.train()
### Train main network
# Compute the target Q value
with torch.no_grad():
q_val = self.student_network(next_state)
next_action = q_val.argmax(dim=1, keepdim=True)
target_Q = reward + not_done * self.discount * self.student_network_target(next_state).gather(1, next_action)
# Get current Q estimate
current_Qs = self.student_network(state)
current_Q = current_Qs.gather(1, action)
# Compute Q loss (Huber loss)
Q_loss = self.huber(current_Q, target_Q)
# calculate regularizing loss
R_loss = torch.mean(self.alpha * (torch.logsumexp(current_Qs, dim=1) - current_Qs.gather(1, action).squeeze(1)))
if self.use_teacher:
rule_loss = self.calculate_teacher_loss(state, current_Qs, ep, steps)
else:
# Set rule loss 0 when not using teacher
rule_loss = F.mse_loss(current_Qs, current_Qs)
loss_val = rule_loss.detach().item()
# Optimize the Q
self.optimizer.zero_grad()
'''if loss_val!=0:
(rule_loss).backward()
else:
(Q_loss + R_loss).backward()'''
loss = Q_loss + R_loss + self.lam * rule_loss
loss.backward()
self.optimizer.step()
self.iterations += 1
# Update target network by full copy every X iterations.
if self.iterations % self.target_update_freq == 0:
self.student_network_target.load_state_dict(self.student_network.state_dict())
return loss.detach().cpu().item(), R_loss.detach().cpu().item(), Q_loss.detach().cpu().item(), rule_loss.detach().item()
def get_name(self) -> str:
return "ConservativeQLearning"