-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgroup_dpo_vectorised.py
1036 lines (875 loc) · 50.9 KB
/
group_dpo_vectorised.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from collections import defaultdict
import copy
import cvxpy as cp
import numpy as np
from typing import List,Union
from envs.group_linear_bandit import GroupLinearBandit
from utils.collect_data import GroupTransition, ret_uniform_policy, collect_preference_data
from utils.utils import softmax, softmax_2D, sigmoid
from utils.logger import Logger
import wandb
class GroupDirectPolicyOptimizationVectorised:
""" Group DPO/IPO. Includes justdpo (DPO), linear (IPO), and log (IPO).
The DPO implementation is *vectorised* (unlike class GroupDirectPolicyOptimization).
The DPO loss and grad are calculated using the known policy
π = exp(r)/sum(exp(r_all)), for r = <φ(s,a,g), θ> (vectorisation).
"""
def __init__(
self,
state_dim: int, ## state s drawn as vector of `state_dim` elements from Uniform(0,1)
action_num: int, ## number of actions in Action Space
group_num: int, ## number of groups
feature_dim: int, ## feature_dim = 2 * state_dim (num elements in vector φ(s,a,g) )
feature_func, ## φ(s,a,g)
ref_policy, ## π_ref(a|s)
reg_coef: float, ## β scaling in the DPO gradient & loss -- controls KL Divergence from π_ref
step_size: float, ## η_θ step size for Gradient Descent on the DPO/IPO loss (if not is_adaptive)
num_iters: int, ## number of update steps on Training dataset
is_adaptive: bool = False, ## if is_adaptive, step size in Update step is adaptive to the historical grad
ada_coef: float = None, ## coef scaling the inverted-sqrt historical grad in Update step if is_adaptive
logger: Logger = None, ## logger
wandb_use: bool = False, ## recording results in WandB
ipo_grad_type: str = 'justdpo', ## `justdpo` (vectorised version), `linear` (IPO), or `log` (IPO)
param_limit: int = 1, ## elements of vector θ range in [0, param_limit]
lamba: float=0, ## L2 regularisation for closed-form regression of IPO objective in Linear Bandits case
train_agent: bool=True, ## if True, use self.train(); else, use self.random_train() func
report_iter: int = 2000 ## log metrics after these iters
) -> None:
self.state_dim = state_dim
self.action_num = action_num
self.group_num = group_num
self.feature_dim = feature_dim
self.feature_func = feature_func
self.ref_policy = ref_policy
self.reg_coef = reg_coef
self.step_size = step_size
self.num_iters = num_iters
self.logger = logger
self.wandb_use = wandb_use
self.ipo_grad_type = ipo_grad_type
self.hist_group_loss=np.zeros(group_num)
self.group_loss=np.zeros(group_num)
self.is_adaptive = is_adaptive
self.ada_coef = ada_coef
self.hist_grad_squared_norm = 0.0
# initialize the learnt policy parameter θ -- same param for all groups g
self.param = np.random.uniform(0, param_limit, self.feature_dim)
self.lamba = lamba
self.train_agent=train_agent
self.report_iter = report_iter
print('Vectorised DPO; step size = ', self.step_size)
def ret_action_prob(self, state: np.ndarray, group_id: int) -> np.ndarray:
arr = np.zeros(self.action_num, np.float32)
for action_idx in range(self.action_num):
feature = self.feature_func(state, action_idx, group_id) # (num_states, state_dim*2=feature_dim)
arr[action_idx] = np.dot(feature, self.param)
prob = softmax(arr)
return prob
def ret_policy(self):
action_num = self.action_num
feature_func = copy.deepcopy(self.feature_func)
param = self.param
def policy(state: np.ndarray, group_id: int) -> np.ndarray:
arr = np.zeros(action_num, np.float32)
for action_idx in range(action_num):
feature = feature_func(state, action_idx, group_id)
arr[action_idx] = np.dot(feature, param)
prob = softmax(arr)
return prob
return policy
def sample_action(self, state: np.ndarray, group_id: int) -> int:
prob = self.action_prob(state, group_id)
sampled_act = np.random.choice(a=self.action_num, size=1, replace=True, p=prob)
return sampled_act
def update_once_nonvectorised(self, dataset: List[GroupTransition]) -> float:
grad = np.zeros_like(self.param)
group_loss=np.zeros(self.group_num)
cur_group_counts=np.zeros(self.group_num)
for transition in dataset:
state, action_one, action_two, group_id, pref = (
transition.state,
transition.action_0,
transition.action_1,
transition.group_id,
transition.pref,
)
pref_act = action_two if pref == 1 else action_one
non_pref_act = action_two if pref == 0 else action_one
feat_pref_act, feat_non_pref_act = (
self.feature_func(state, pref_act, group_id),
self.feature_func(state, non_pref_act,group_id),
)
#print(feat_pref_act,feat_non_pref_act,self.param)
cur_policy_act_prob = self.ret_action_prob(state,group_id)
ref_policy_act_prob = self.ref_policy(state,group_id)
if self.ipo_grad_type=='justdpo':
log_ratio_diff = self.reg_coef * (feat_pref_act - feat_non_pref_act) @ (self.param) # VECTORISED REDEFINITION instead of log-sum
coef = sigmoid(-log_ratio_diff)
group_loss[group_id] += -np.log(sigmoid(log_ratio_diff))#+self.adj[group_id]/np.sqrt(self.group_counts[group_id]) #calculate group losses
elif self.ipo_grad_type=='linear':
lin_diff = (feat_pref_act - feat_non_pref_act) @ (self.param) - 0.5*(1/self.reg_coef)
coef = -2*lin_diff/self.reg_coef
group_loss[group_id] += np.square(lin_diff)#+self.adj[group_id]/np.sqrt(self.group_counts[group_id])
elif self.ipo_grad_type=='log':
log_diff = (
np.log((cur_policy_act_prob[pref_act]*ref_policy_act_prob[non_pref_act])/(cur_policy_act_prob[non_pref_act]*ref_policy_act_prob[pref_act])+1e-6 )
)
coef = -2*(log_diff-0.5*(1/self.reg_coef))/self.reg_coef
group_loss[group_id] += np.square((log_diff-0.5*(1/self.reg_coef)))#+self.adj[group_id]/np.sqrt(self.group_counts[group_id])
else:
raise ValueError('value not implemented')
#print(group_id,self.adj[group_id]/np.sqrt(self.group_counts[group_id]) )
cur_group_counts[group_id] += 1
neg_cur_data_grad = (
self.reg_coef * coef * (feat_pref_act - feat_non_pref_act)
)
grad -= neg_cur_data_grad
grad /= len(dataset)
group_loss=group_loss/cur_group_counts
#print(group_loss)
self.hist_grad_squared_norm += np.sum(np.square(grad))
self.hist_group_loss += group_loss
self.group_loss = group_loss
if self.is_adaptive:
step_size = self.ada_coef / np.sqrt(self.hist_grad_squared_norm)
else:
step_size = self.step_size
self.param = self.param - step_size * grad
return np.sqrt(np.sum(np.square(grad))) # grad L2-norm
def update_once(self, dataset: List[GroupTransition]) -> float:
grad = np.zeros_like(self.param)
group_loss=np.zeros(self.group_num)
cur_group_counts=np.zeros(self.group_num)
group_id_idx_all = defaultdict(list)
feature_diff_all = np.zeros((len(dataset), self.feature_dim))
pref_act_all = []
non_pref_act_all = []
cur_policy_act_prob_all = np.zeros((len(dataset), self.action_num))
ref_policy_act_prob_all = np.zeros((len(dataset), self.action_num))
for idx, transition in enumerate(dataset):
state, action_one, action_two, group_id, pref = (
transition.state,
transition.action_0,
transition.action_1,
transition.group_id,
transition.pref,
)
pref_act = action_two if pref == 1 else action_one
non_pref_act = action_two if pref == 0 else action_one
pref_act_all.append(pref_act)
non_pref_act_all.append(non_pref_act)
feat_pref_act, feat_non_pref_act = (
self.feature_func(state, pref_act, group_id),
self.feature_func(state, non_pref_act,group_id),
)
feature_diff_all[idx,:] = feat_pref_act - feat_non_pref_act
cur_policy_act_prob_all[idx,:] = self.ret_action_prob(state,group_id)
ref_policy_act_prob_all[idx,:] = self.ref_policy(state,group_id)
group_id_idx_all[group_id].append(idx) # get dataset indices for each group
cur_group_counts[group_id] += 1
######################################################################################
################### VECTORISED REDEFINITION across all transitions ###################
######################################################################################
if self.ipo_grad_type=='justdpo':
log_ratio_diff_all = self.reg_coef * feature_diff_all @ self.param.reshape(self.feature_dim,1) # log_ratio_diff_all shape (len(dataset),1)
coef = sigmoid(-log_ratio_diff_all) # shape (len(dataset),1)
for group_id in range(self.group_num):
group_indices = group_id_idx_all[group_id]
group_loss[group_id] = np.sum(-np.log(sigmoid(log_ratio_diff_all[group_indices])))#+self.adj[group_id]/np.sqrt(self.group_counts[group_id]) #calculate group losses
self.total_loss = np.sum(-np.log(sigmoid(log_ratio_diff_all)))/len(dataset)
elif self.ipo_grad_type=='linear':
lin_diff = feature_diff_all @ self.param.reshape(self.feature_dim,1) - 0.5*(1/self.reg_coef)
coef = -2*lin_diff/self.reg_coef
for group_id in range(self.group_num):
group_indices = group_id_idx_all[group_id]
group_loss[group_id] = np.sum(np.square(lin_diff[group_indices]))#+self.adj[group_id]/np.sqrt(self.group_counts[group_id])
elif self.ipo_grad_type=='log':
row_indices = np.arange(cur_policy_act_prob_all.shape[0])
log_diff = (
np.log((cur_policy_act_prob_all[row_indices,pref_act_all]*ref_policy_act_prob_all[row_indices,non_pref_act])/
(cur_policy_act_prob_all[row_indices,non_pref_act]*ref_policy_act_prob_all[row_indices,pref_act_all])+1e-6 )
)
coef = -2*(log_diff-0.5*(1/self.reg_coef))/self.reg_coef
for group_id in range(self.group_num):
group_indices = group_id_idx_all[group_id]
group_loss[group_id] = np.sum(np.square((log_diff[group_indices]-0.5*(1/self.reg_coef))))#+self.adj[group_id]/np.sqrt(self.group_counts[group_id])
else:
raise ValueError('value not implemented')
neg_cur_data_grad = self.reg_coef * coef * feature_diff_all
grad = np.sum(-neg_cur_data_grad, axis=0) / len(dataset)
group_loss /= cur_group_counts
self.hist_grad_squared_norm += np.sum(np.square(grad))
self.hist_group_loss += group_loss
self.group_loss = group_loss
if self.is_adaptive:
step_size = self.ada_coef / np.sqrt(self.hist_grad_squared_norm)
else:
step_size = self.step_size
self.param = self.param - step_size * grad
#print(f'GRAD -- {grad} || PARAM -- {self.param}\n')
self.theta_update = step_size * grad
return np.sqrt(np.sum(np.square(grad))) # grad L2-norm
def evaluate_ipo_loss(self, dataset: List[GroupTransition], policy=None) -> float:
"""
Evaluate the loss on the dataset for any policy.
"""
if policy is None:
policy = self.ret_policy()
feature_diff_all = np.zeros((len(dataset), self.feature_dim))
pref_act_all = []
non_pref_act_all = []
eval_policy_act_prob_all = np.zeros((len(dataset), self.action_num))
ref_policy_act_prob_all = np.zeros((len(dataset), self.action_num))
for idx, transition in enumerate(dataset):
state, action_one, action_two, group_id, pref = (
transition.state,
transition.action_0,
transition.action_1,
transition.group_id,
transition.pref,
)
pref_act = action_two if pref == 1 else action_one
non_pref_act = action_two if pref == 0 else action_one
pref_act_all.append(pref_act)
non_pref_act_all.append(non_pref_act)
feat_pref_act, feat_non_pref_act = (
self.feature_func(state, pref_act, group_id),
self.feature_func(state, non_pref_act,group_id),
)
feature_diff_all[idx,:] = feat_pref_act - feat_non_pref_act
eval_policy_act_prob_all[idx,:] = policy(state,group_id)
ref_policy_act_prob_all[idx,:] = self.ref_policy(state,group_id)
if self.ipo_grad_type=='linear':
lin_diff = feature_diff_all @ self.param.reshape(self.feature_dim,1) - 0.5*(1/self.reg_coef)
coef = lin_diff
elif self.ipo_grad_type=='log':
row_indices = np.arange(eval_policy_act_prob_all.shape[0])
log_diff=(
np.log((eval_policy_act_prob_all[row_indices,pref_act_all]*ref_policy_act_prob_all[row_indices,non_pref_act_all])/
(eval_policy_act_prob_all[row_indices,non_pref_act_all]*ref_policy_act_prob_all[row_indices,pref_act_all]) + 1e-6)
)
coef=(log_diff-0.5*(1/self.reg_coef))
else: # self.ipo_grad_type=='linear'
lin_diff = feature_diff_all @ self.param.reshape(self.feature_dim,1) - 0.5*(1/self.reg_coef)
coef = lin_diff
loss = np.sum(np.square(coef)) / len(dataset)
return loss
def evaluate_ipo_grad(self, dataset: List[GroupTransition], policy=None) -> float:
"""
Evaluate the loss on the dataset for any policy.
"""
if policy is None:
policy = self.ret_policy()
grad = np.zeros_like(self.param)
feature_diff_all = np.zeros((len(dataset), self.feature_dim))
pref_act_all = []
non_pref_act_all = []
eval_policy_act_prob_all = np.zeros((len(dataset), self.action_num))
ref_policy_act_prob_all = np.zeros((len(dataset), self.action_num))
for idx, transition in enumerate(dataset):
state, action_one, action_two, group_id, pref = (
transition.state,
transition.action_0,
transition.action_1,
transition.group_id,
transition.pref,
)
pref_act = action_two if pref == 1 else action_one
non_pref_act = action_two if pref == 0 else action_one
pref_act_all.append(pref_act)
non_pref_act_all.append(non_pref_act)
feat_pref_act, feat_non_pref_act = (
self.feature_func(state, pref_act, group_id),
self.feature_func(state, non_pref_act,group_id),
)
feature_diff_all[idx,:] = feat_pref_act - feat_non_pref_act
eval_policy_act_prob_all[idx,:] = policy(state,group_id)
ref_policy_act_prob_all[idx,:] = self.ref_policy(state,group_id)
if self.ipo_grad_type=='linear':
lin_diff = feature_diff_all @ self.param.reshape(self.feature_dim,1) - 0.5*(1/self.reg_coef)
coef = lin_diff
elif self.ipo_grad_type=='log':
row_indices = np.arange(eval_policy_act_prob_all.shape[0])
log_diff=(
np.log((eval_policy_act_prob_all[row_indices,pref_act_all]*ref_policy_act_prob_all[row_indices,non_pref_act_all])/
(eval_policy_act_prob_all[row_indices,non_pref_act_all]*ref_policy_act_prob_all[row_indices,pref_act_all]) + 1e-6)
)
coef=(log_diff-0.5*(1/self.reg_coef))
else:
print(self.param,feat_pref_act-feat_non_pref_act)
lin_diff = feature_diff_all @ self.param.reshape(self.feature_dim,1) - 0.5*(1/self.reg_coef)
coef = lin_diff
cur_data_grad = 2 * coef * feature_diff_all
grad = np.sum(cur_data_grad, axis=0) / len(dataset)
return np.sqrt(np.sum(np.square(grad)))
def evaluate_ipo_grp_loss(self, dataset: List[GroupTransition], policy=None) -> float:
"""
Evaluate the loss on the dataset for any policy.
"""
if policy is None:
policy = self.ret_policy()
loss = np.zeros(self.group_num)
counts = np.zeros(self.group_num)
group_id_idx_all = defaultdict(list)
feature_diff_all = np.zeros((len(dataset), self.feature_dim))
pref_act_all = []
non_pref_act_all = []
eval_policy_act_prob_all = np.zeros((len(dataset), self.action_num))
ref_policy_act_prob_all = np.zeros((len(dataset), self.action_num))
for idx, transition in enumerate(dataset):
state, action_one, action_two, group_id, pref = (
transition.state,
transition.action_0,
transition.action_1,
transition.group_id,
transition.pref,
)
pref_act = action_two if pref == 1 else action_one
non_pref_act = action_two if pref == 0 else action_one
pref_act_all.append(pref_act)
non_pref_act_all.append(non_pref_act)
feat_pref_act, feat_non_pref_act = (
self.feature_func(state, pref_act, group_id),
self.feature_func(state, non_pref_act,group_id),
)
feature_diff_all[idx,:] = feat_pref_act - feat_non_pref_act
eval_policy_act_prob_all[idx,:] = policy(state,group_id)
ref_policy_act_prob_all[idx,:] = self.ref_policy(state,group_id)
group_id_idx_all[group_id].append(idx) # get dataset indices for each group
counts[group_id] += 1
if self.ipo_grad_type=='linear':
lin_diff = feature_diff_all @ self.param.reshape(self.feature_dim,1) - 0.5*(1/self.reg_coef)
coef = lin_diff
elif self.ipo_grad_type=='log':
row_indices = np.arange(eval_policy_act_prob_all.shape[0])
log_diff=(
np.log((eval_policy_act_prob_all[row_indices,pref_act_all]*ref_policy_act_prob_all[row_indices,non_pref_act_all])/
(eval_policy_act_prob_all[row_indices,non_pref_act_all]*ref_policy_act_prob_all[row_indices,pref_act_all]) + 1e-6)
)
coef=(log_diff-0.5*(1/self.reg_coef))
else: # self.ipo_grad_type=='linear'
lin_diff = feature_diff_all @ self.param.reshape(self.feature_dim,1) - 0.5*(1/self.reg_coef)
coef = lin_diff
for group_id in range(self.group_num):
group_indices = group_id_idx_all[group_id]
loss[group_id] = np.sum(np.square(coef[group_indices]))
loss = loss/counts
return loss
def evaluate_grp_loss(self, dataset: List[GroupTransition], policy=None) -> float:
"""
Evaluate the loss on the dataset for any policy.
"""
if policy is None:
policy = self.ret_policy()
loss = np.zeros(self.group_num)
counts = np.zeros(self.group_num)
group_id_idx_all = defaultdict(list)
feature_diff_all = np.zeros((len(dataset), self.feature_dim))
for idx, transition in enumerate(dataset):
state, action_one, action_two, group_id, pref = (
transition.state,
transition.action_0,
transition.action_1,
transition.group_id,
transition.pref,
)
pref_act = action_two if pref == 1 else action_one
non_pref_act = action_two if pref == 0 else action_one
feat_pref_act, feat_non_pref_act = (
self.feature_func(state, pref_act, group_id),
self.feature_func(state, non_pref_act,group_id),
)
feature_diff_all[idx,:] = feat_pref_act - feat_non_pref_act
group_id_idx_all[group_id].append(idx) # get dataset indices for each group
counts[group_id] += 1
# VECTORISATION for log_ratio_diff
log_ratio_diff = self.reg_coef * feature_diff_all @ self.param.reshape(self.feature_dim,1)
for group_id in range(self.group_num):
group_indices = group_id_idx_all[group_id]
loss[group_id] = np.sum(-np.log(sigmoid(log_ratio_diff[group_indices])))
loss = loss/counts
return loss
def evaluate_loss(self, dataset: List[GroupTransition], policy=None) -> float:
"""
Evaluate the loss on the dataset for any policy.
"""
if policy is None:
policy = self.ret_policy()
loss = 0.0
feature_diff_all = np.zeros((len(dataset), self.feature_dim))
for idx, transition in enumerate(dataset):
state, action_one, action_two, group_id, pref = (
transition.state,
transition.action_0,
transition.action_1,
transition.group_id,
transition.pref,
)
pref_act = action_two if pref == 1 else action_one
non_pref_act = action_two if pref == 0 else action_one
feat_pref_act, feat_non_pref_act = (
self.feature_func(state, pref_act, group_id),
self.feature_func(state, non_pref_act,group_id),
)
feature_diff_all[idx,:] = feat_pref_act - feat_non_pref_act
# VECTORISATION for log_ratio_diff
log_ratio_diff = self.reg_coef * feature_diff_all @ self.param.reshape(self.feature_dim,1)
loss = np.sum(-np.log(sigmoid(log_ratio_diff))) / len(dataset)
return loss
def Regression(self, dataset: List[GroupTransition],lamba: float)-> float:
Y=[]
group_id_mat=[]
for transition in dataset:
state, action_one, action_two, group_id, pref = (
transition.state,
transition.action_0,
transition.action_1,
transition.group_id,
transition.pref,
)
pref_act = action_two if pref == 1 else action_one
non_pref_act = action_two if pref == 0 else action_one
feat_pref_act, feat_non_pref_act = (
self.feature_func(state, pref_act, group_id),
self.feature_func(state, non_pref_act,group_id),
)
Y.append(feat_pref_act-feat_non_pref_act)
group_id_mat.append(group_id)
Y=np.array(Y)
group_id_mat=np.array(group_id_mat)
print(Y,Y.shape)
coef=np.linalg.inv(Y.transpose()@Y/self.group_num+lamba*np.eye(Y.shape[1]))
print(np.linalg.det(np.matmul(Y.transpose(),Y)))
variate=np.matmul(Y.transpose()/self.group_num,np.ones([len(dataset),1]))
self.param=np.matmul(coef,variate).ravel()/(2*self.reg_coef)
unique_groups = np.unique(group_id_mat)
for group_id in unique_groups:
group_indices = np.where(group_id_mat == group_id)[0]
Y_group = Y[group_indices, :]
# Perform the calculation for each group
result_group = np.square(np.dot(Y_group, self.param) - 1/(2*self.reg_coef))
result_group_avg=np.mean(result_group)
print(result_group_avg.shape)
# Append the result for this group to the overall result
self.group_loss[group_id]=result_group_avg
live_grad=(([email protected]).T-1/(2*self.reg_coef)).dot(Y)+lamba*self.param
return np.sqrt(np.sum(np.square(live_grad)))
def random_train(self, dataset: List[GroupTransition],
val_dataset: list[GroupTransition],
test_dataset: list[GroupTransition], env: GroupLinearBandit, optimal_reward: List[float]) -> float:
grad_norm=self.evaluate_ipo_grad(dataset)
live_grad=grad_norm
train_loss=self.evaluate_ipo_loss(dataset)
val_loss = self.evaluate_ipo_loss(val_dataset)
train_grp_loss = self.evaluate_ipo_grp_loss(dataset)
val_grp_loss = self.evaluate_ipo_grp_loss(val_dataset)
kl_dist=self.evaluate_KL(env=env,states=test_dataset)
formatted_kl=", ".join([f"{kld:.4f}" for kld in kl_dist])
#Evaluate the reward on the test dataset:
#print(optimal_reward,self.evaluate_reward(env=env,
# states=test_dataset))
rew_err = [float(a - b)/a for a, b in zip(optimal_reward,self.evaluate_reward(env=env,
states=test_dataset) )]
formatted_rew=", ".join([f"{reward:.4f}" for reward in rew_err])
max_reward_err=max(rew_err)
max_reward_err_index=rew_err.index(max_reward_err)
max_kl_dist=max(kl_dist)
max_kl_dist_index=kl_dist.index(max_kl_dist)
max_train_grp_loss=np.max(train_grp_loss)
max_val_grp_loss=np.max(val_grp_loss)
max_train_grp_loss_index=np.argmax(train_grp_loss)
max_val_grp_loss_index=np.argmax(val_grp_loss)
step=0
logging_str = (f"Iteration: {step: d}, train_loss: {train_loss: .4f}, "
f"val_loss: {val_loss: .4f}, grad_norm: {grad_norm:.4f}, live_grad: {live_grad:.4f}, "
f"reward_err: {formatted_rew}, KL_dist: {formatted_kl}, param: {self.param}, "
f"train_grp_loss: {train_grp_loss}, val_grp_loss: {val_grp_loss}, "
f"max_reward_err: {max_reward_err: .4f}, max_reward_err_index: {max_reward_err_index}, "
f"max_kl_dist: {max_kl_dist: .4f}, max_kl_dist_index: {max_kl_dist_index}, "
f"max_train_grp_loss: {max_train_grp_loss: .4f}, max_train_grp_loss_index: {max_train_grp_loss_index}, "
f"max_val_grp_loss: {max_val_grp_loss: .4f}, max_val_grp_loss_index: {max_val_grp_loss_index}, ")
if self.wandb_use:
d_wandb = {
"Iteration": step, "train_loss": train_loss,
"val_loss": val_loss, "grad_norm": grad_norm, "live_grad": live_grad,
"max_reward_err": max_reward_err , "max_reward_err_index": max_reward_err_index,
"max_kl_dist" : max_kl_dist, "max_kl_dist_index": max_kl_dist_index,
"max_train_grp_loss": max_train_grp_loss, "max_train_grp_loss_index": max_train_grp_loss_index,
"max_val_grp_loss": max_val_grp_loss, "max_val_grp_loss_index": max_val_grp_loss_index,
}
# Assuming rew_err is a list
for i, err in enumerate(rew_err):
key = f"reward_err_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = err
for i, param in enumerate(self.param):
key = f"reward_param_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = param
for i, grp_ls in enumerate(train_grp_loss):
key = f"train_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = grp_ls
for i, grp_ls in enumerate(val_grp_loss):
key = f"val_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = grp_ls
for i, kld in enumerate(kl_dist):
key = f"KL_distance_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = kld
wandb.log(d_wandb)
if self.logger:
self.logger.info(logging_str)
else:
print(logging_str)
rew = self.evaluate_reward(env, test_dataset)
return rew
def train(self, dataset: List[GroupTransition],
val_dataset: list[GroupTransition],
test_dataset: list[GroupTransition], env: GroupLinearBandit, optimal_reward: List[float]) -> float:
print('ipo grad type: ', self.ipo_grad_type)
if self.ipo_grad_type=='Regression':
print('Hellooooo Regression IPO')
"""
grad_norm=self.evaluate_ipo_grad(dataset)
live_grad=grad_norm
train_loss=self.evaluate_ipo_loss(dataset)
val_loss = self.evaluate_ipo_loss(val_dataset)
train_grp_loss = self.evaluate_ipo_grp_loss(dataset)
val_grp_loss = self.evaluate_ipo_grp_loss(val_dataset)
kl_dist=self.evaluate_KL(env=env,states=test_dataset)
formatted_kl=", ".join([f"{kld:.4f}" for kld in kl_dist])
#Evaluate the reward on the test dataset:
#print(optimal_reward,self.evaluate_reward(env=env,
# states=test_dataset))
rew_err = [float(a - b)/a for a, b in zip(optimal_reward,self.evaluate_reward(env=env,
states=test_dataset) )]
formatted_rew=", ".join([f"{reward:.4f}" for reward in rew_err])
max_reward_err=max(rew_err)
max_reward_err_index=rew_err.index(max_reward_err)
max_kl_dist=max(kl_dist)
max_kl_dist_index=kl_dist.index(max_kl_dist)
max_train_grp_loss=np.max(train_grp_loss)
max_val_grp_loss=np.max(val_grp_loss)
max_train_grp_loss_index=np.argmax(train_grp_loss)
max_val_grp_loss_index=np.argmax(val_grp_loss)
step=-1
logging_str = (f"Iteration: {step: d}, train_loss: {train_loss: .4f}, "
f"val_loss: {val_loss: .4f}, grad_norm: {grad_norm:.4f}, live_grad: {live_grad:.4f}, "
f"reward_err: {formatted_rew}, KL_dist: {formatted_kl}, param: {self.param}, "
f"train_grp_loss: {train_grp_loss}, val_grp_loss: {val_grp_loss}, "
f"max_reward_err: {max_reward_err: .4f}, max_reward_err_index: {max_reward_err_index}, "
f"max_kl_dist: {max_kl_dist: .4f}, max_kl_dist_index: {max_kl_dist_index}, "
f"max_train_grp_loss: {max_train_grp_loss: .4f}, max_train_grp_loss_index: {max_train_grp_loss_index}, "
f"max_val_grp_loss: {max_val_grp_loss: .4f}, max_val_grp_loss_index: {max_val_grp_loss_index}, ")
if self.wandb_use:
d_wandb = {
"Iteration": step, "train_loss": train_loss,
"val_loss": val_loss, "grad_norm": grad_norm, "live_grad": live_grad,
"max_reward_err": max_reward_err , "max_reward_err_index": max_reward_err_index,
"max_kl_dist" : max_kl_dist, "max_kl_dist_index": max_kl_dist_index,
"max_train_grp_loss": max_train_grp_loss, "max_train_grp_loss_index": max_train_grp_loss_index,
"max_val_grp_loss": max_val_grp_loss, "max_val_grp_loss_index": max_val_grp_loss_index,
}
# Assuming rew_err is a list
for i, err in enumerate(rew_err):
key = f"reward_err_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = err
for i, param in enumerate(self.param):
key = f"reward_param_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = param
for i, grp_ls in enumerate(train_grp_loss):
key = f"train_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = grp_ls
for i, grp_ls in enumerate(val_grp_loss):
key = f"val_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = grp_ls
for i, kld in enumerate(kl_dist):
key = f"KL_distance_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = kld
wandb.log(d_wandb)
if self.logger:
self.logger.info(logging_str)
else:
print(logging_str)
"""
live_grad=self.Regression(dataset,lamba=self.lamba)
grad_norm=self.evaluate_ipo_grad(dataset)
train_loss=self.evaluate_ipo_loss(dataset)
val_loss = self.evaluate_ipo_loss(val_dataset)
train_grp_loss = self.evaluate_ipo_grp_loss(dataset)
val_grp_loss = self.evaluate_ipo_grp_loss(val_dataset)
kl_dist=self.evaluate_KL(env=env,states=test_dataset)
formatted_kl=", ".join([f"{kld:.4f}" for kld in kl_dist])
#Evaluate the reward on the test dataset:
#print(optimal_reward,self.evaluate_reward(env=env,
# states=test_dataset))
rew_err = [float(a - b)/a for a, b in zip(optimal_reward,self.evaluate_reward(env=env,
states=test_dataset) )]
formatted_rew=", ".join([f"{reward:.4f}" for reward in rew_err])
max_reward_err=max(rew_err)
max_reward_err_index=rew_err.index(max_reward_err)
max_kl_dist=max(kl_dist)
max_kl_dist_index=kl_dist.index(max_kl_dist)
max_train_grp_loss=np.max(train_grp_loss)
max_val_grp_loss=np.max(val_grp_loss)
max_train_grp_loss_index=np.argmax(train_grp_loss)
max_val_grp_loss_index=np.argmax(val_grp_loss)
step=0
logging_str = (f"Iteration: {step: d}, train_loss: {train_loss: .4f}, "
f"val_loss: {val_loss: .4f}, grad_norm: {grad_norm:.4f}, live_grad: {live_grad:.4f}, "
f"reward_err: {formatted_rew}, KL_dist: {formatted_kl}, param: {self.param}, "
f"train_grp_loss: {train_grp_loss}, val_grp_loss: {val_grp_loss}, "
f"max_reward_err: {max_reward_err: .4f}, max_reward_err_index: {max_reward_err_index}, "
f"max_kl_dist: {max_kl_dist: .4f}, max_kl_dist_index: {max_kl_dist_index}, "
f"max_train_grp_loss: {max_train_grp_loss: .4f}, max_train_grp_loss_index: {max_train_grp_loss_index}, "
f"max_val_grp_loss: {max_val_grp_loss: .4f}, max_val_grp_loss_index: {max_val_grp_loss_index}, ")
if self.wandb_use:
d_wandb = {
"Iteration": step, "train_loss": train_loss,
"val_loss": val_loss, "grad_norm": grad_norm, "live_grad": live_grad,
"max_reward_err": max_reward_err , "max_reward_err_index": max_reward_err_index,
"max_kl_dist" : max_kl_dist, "max_kl_dist_index": max_kl_dist_index,
"max_train_grp_loss": max_train_grp_loss, "max_train_grp_loss_index": max_train_grp_loss_index,
"max_val_grp_loss": max_val_grp_loss, "max_val_grp_loss_index": max_val_grp_loss_index,
}
# Assuming rew_err is a list
for i, err in enumerate(rew_err):
key = f"reward_err_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = err
for i, param in enumerate(self.param):
key = f"reward_param_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = param
for i, grp_ls in enumerate(train_grp_loss):
key = f"train_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = grp_ls
for i, grp_ls in enumerate(val_grp_loss):
key = f"val_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = grp_ls
for i, kld in enumerate(kl_dist):
key = f"KL_distance_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = kld
wandb.log(d_wandb)
if self.logger:
self.logger.info(logging_str)
else:
print(logging_str)
rew = self.evaluate_reward(env, test_dataset)
return rew
for step in range(self.num_iters):
grad_norm = self.update_once(dataset)
if step % self.report_iter == 0:
print('UPDATE PARAM GroupDPO: ', self.theta_update)
print('TOTAL LOSS GroupDPO: ', self.total_loss)
if self.ipo_grad_type=='justdpo':
train_loss = self.evaluate_loss(dataset)
val_loss = self.evaluate_loss(val_dataset)
else:
train_loss = self.evaluate_ipo_loss(dataset)
val_loss = self.evaluate_ipo_loss(val_dataset)
if self.ipo_grad_type=='justdpo':
train_grp_loss = self.evaluate_grp_loss(dataset)
val_grp_loss = self.evaluate_grp_loss(val_dataset)
else:
train_grp_loss = self.evaluate_ipo_grp_loss(dataset)
val_grp_loss = self.evaluate_ipo_grp_loss(val_dataset)
kl_dist=self.evaluate_KL(env=env,states=test_dataset)
#Evaluate the reward on the test dataset:
#print(optimal_reward,self.evaluate_reward(env=env,
# states=test_dataset))
rew_err = [float(a - b)/a for a, b in zip(optimal_reward,self.evaluate_reward(env=env,
states=test_dataset) )]
formatted_rew=", ".join([f"{reward:.4f}" for reward in rew_err])
formatted_kl=", ".join([f"{kld:.4f}" for kld in kl_dist])
max_reward_err=max(rew_err)
max_reward_err_index=rew_err.index(max_reward_err)
max_kl_dist=max(kl_dist)
max_kl_dist_index=kl_dist.index(max_kl_dist)
max_train_grp_loss=np.max(train_grp_loss)
max_val_grp_loss=np.max(val_grp_loss)
max_cur_train_grp_loss=np.max(self.group_loss)
max_train_grp_loss_index=np.argmax(train_grp_loss)
max_val_grp_loss_index=np.argmax(val_grp_loss)
max_cur_train_grp_loss_index=np.argmax(self.group_loss)
logging_str = (f"Iteration: {step: d}, train_loss: {train_loss: .4f}, "
f"val_loss: {val_loss: .4f}, grad_norm: {grad_norm:.4f}, "
f"reward_err: {formatted_rew}, KL_dist: {formatted_kl}, param: {self.param}"
f"train_grp_loss: {train_grp_loss}, val_grp_loss: {val_grp_loss}, "
f"train_hist_grp_loss: {self.hist_group_loss}, cur_train_grp_loss: {self.group_loss},"
f"max_reward_err: {max_reward_err: .4f}, max_reward_err_index: {max_reward_err_index}, "
f"max_kl_dist: {max_kl_dist: .4f}, max_kl_dist_index: {max_kl_dist_index}, "
f"max_train_grp_loss: {max_train_grp_loss: .4f}, max_train_grp_loss_index: {max_train_grp_loss_index}, "
f"max_val_grp_loss: {max_val_grp_loss: .4f}, max_val_grp_loss_index: {max_val_grp_loss_index}, "
f"max_cur_train_grp_loss: {max_cur_train_grp_loss: .4f}, max_cur_train_grp_loss_index: {max_cur_train_grp_loss_index}, ")
if self.wandb_use:
d_wandb = {
"Iteration": step, "train_loss": train_loss,
"val_loss": val_loss, "grad_norm": grad_norm,
"max_reward_err": max_reward_err , "max_reward_err_index": max_reward_err_index,
"max_kl_dist" : max_kl_dist, "max_kl_dist_index": max_kl_dist_index,
"max_train_grp_loss": max_train_grp_loss, "max_train_grp_loss_index": max_train_grp_loss_index,
"max_val_grp_loss": max_val_grp_loss, "max_val_grp_loss_index": max_val_grp_loss_index,
"max_cur_train_grp_loss": max_cur_train_grp_loss, "max_cur_train_grp_loss_index": max_cur_train_grp_loss_index
}
# Assuming rew_err is a list
for i, err in enumerate(rew_err):
key = f"reward_err_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = err
for i, param in enumerate(self.param):
key = f"reward_param_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = param
for i, kld in enumerate(kl_dist):
key = f"KL_distance_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = kld
for i, hist_grp_ls in enumerate(self.hist_group_loss):
key = f"hist_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = hist_grp_ls
for i, grp_ls in enumerate(self.group_loss):
key = f"cur_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = grp_ls
for i, grp_ls in enumerate(train_grp_loss):
key = f"train_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = grp_ls
for i, grp_ls in enumerate(val_grp_loss):
key = f"val_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = grp_ls
wandb.log(d_wandb)
if self.logger:
self.logger.info(logging_str)
else:
print(logging_str)
if self.ipo_grad_type=='justdpo':
train_loss = self.evaluate_loss(dataset)
val_loss = self.evaluate_loss(val_dataset)
else:
train_loss = self.evaluate_ipo_loss(dataset)
val_loss = self.evaluate_ipo_loss(val_dataset)
if self.ipo_grad_type=='justdpo':
train_grp_loss = self.evaluate_grp_loss(dataset)
val_grp_loss = self.evaluate_grp_loss(val_dataset)
else:
train_grp_loss = self.evaluate_ipo_grp_loss(dataset)
val_grp_loss = self.evaluate_ipo_grp_loss(val_dataset)
kl_dist=self.evaluate_KL(env=env,states=test_dataset)
formatted_kl=", ".join([f"{kld:.4f}" for kld in kl_dist])
#Evaluate the reward on the test dataset:
#print(optimal_reward,self.evaluate_reward(env=env,
# states=test_dataset))
rew_err = [float(a - b)/a for a, b in zip(optimal_reward,self.evaluate_reward(env=env,
states=test_dataset) )]
formatted_rew=", ".join([f"{reward:.4f}" for reward in rew_err])
max_reward_err=max(rew_err)
max_reward_err_index=rew_err.index(max_reward_err)
max_kl_dist=max(kl_dist)
max_kl_dist_index=kl_dist.index(max_kl_dist)
max_train_grp_loss=np.max(train_grp_loss)
max_val_grp_loss=np.max(val_grp_loss)
max_cur_train_grp_loss=np.max(self.group_loss)
max_train_grp_loss_index=np.argmax(train_grp_loss)
max_val_grp_loss_index=np.argmax(val_grp_loss)
max_cur_train_grp_loss_index=np.argmax(self.group_loss)
logging_str = (f"Iteration: {step: d}, train_loss: {train_loss: .4f}, "
f"val_loss: {val_loss: .4f}, grad_norm: {grad_norm:.4f}, "
f"reward_err: {formatted_rew}, KL_dist: {formatted_kl}, param: {self.param}"
f"train_grp_loss: {train_grp_loss}, val_grp_loss: {val_grp_loss}, "
f"train_hist_grp_loss: {self.hist_group_loss}, cur_train_grp_loss: {self.group_loss},"
f"max_reward_err: {max_reward_err: .4f}, max_reward_err_index: {max_reward_err_index}, "
f"max_kl_dist: {max_kl_dist: .4f}, max_kl_dist_index: {max_kl_dist_index}, "
f"max_train_grp_loss: {max_train_grp_loss: .4f}, max_train_grp_loss_index: {max_train_grp_loss_index}, "
f"max_val_grp_loss: {max_val_grp_loss: .4f}, max_val_grp_loss_index: {max_val_grp_loss_index}, "
f"max_cur_train_grp_loss: {max_cur_train_grp_loss: .4f}, max_cur_train_grp_loss_index: {max_cur_train_grp_loss_index}, ")
if self.wandb_use:
d_wandb = {
"Iteration": step, "train_loss": train_loss,
"val_loss": val_loss, "grad_norm": grad_norm,
"max_kl_dist" : max_kl_dist, "max_kl_dist_index": max_kl_dist_index,
"max_train_grp_loss": max_train_grp_loss, "max_train_grp_loss_index": max_train_grp_loss_index,
"max_val_grp_loss": max_val_grp_loss, "max_val_grp_loss_index": max_val_grp_loss_index,
"max_cur_train_grp_loss": max_cur_train_grp_loss, "max_cur_train_grp_loss_index": max_cur_train_grp_loss_index
}
# Assuming rew_err is a list
for i, err in enumerate(rew_err):
key = f"reward_err_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = err
for i, param in enumerate(self.param):
key = f"reward_param_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = param
for i, kld in enumerate(kl_dist):
key = f"KL_distance_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = kld
for i, grp_ls in enumerate(train_grp_loss):
key = f"train_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = grp_ls
for i, hist_grp_ls in enumerate(self.hist_group_loss):
key = f"hist_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = hist_grp_ls
for i, grp_ls in enumerate(self.group_loss):
key = f"cur_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = grp_ls
for i, grp_ls in enumerate(val_grp_loss):
key = f"val_group_loss_{i + 1}" # Creating dynamic key, e.g., "reward_err_1", "reward_err_2", ...
d_wandb[key] = grp_ls
wandb.log(d_wandb)
if self.logger:
self.logger.info(logging_str)
else:
print(logging_str)
rew = self.evaluate_reward(env, test_dataset)
#rew = float(rew)
return rew
def train_by_cvxpy(self, dataset: List[GroupTransition], env: GroupLinearBandit) -> float:
pref_features, non_pref_features = [], []
pref_ref_policy, non_pref_ref_policy = [], []
for transition in dataset:
state, action_one, action_two, group_id, pref = (
transition.state,
transition.action_0,
transition.action_1,
transition.group_id,
transition.pref,
)
if pref == 1:
pref_act = action_two
non_pref_act = action_one
else:
pref_act = action_one
non_pref_act = action_two
feature_pref_act, feature_non_pref_act = (
self.feature_func(state, pref_act, group_id),
self.feature_func(state, non_pref_act, group_id),
)
pref_features.append(feature_pref_act)
non_pref_features.append(feature_non_pref_act)
act_prob = self.ref_policy(state)
pref_ref_policy.append(act_prob[pref_act])
non_pref_ref_policy.append(act_prob[non_pref_act])
pref_features = np.stack(pref_features, axis=0)
non_pref_features = np.stack(non_pref_features, axis=0)
pref_ref_policy = np.stack(pref_ref_policy, axis=0)
non_pref_ref_policy = np.stack(non_pref_ref_policy, axis=0)
theta = cp.Variable(self.feature_dim)
log_policy_diff = (non_pref_features - pref_features) @ theta
log_ref_policy_diff = cp.log(non_pref_ref_policy) - cp.log(pref_ref_policy)