-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcesa_bianchi.py
178 lines (122 loc) · 4.83 KB
/
cesa_bianchi.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
import numpy as np
# import geometry_v3
import scipy as sp
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import TensorDataset, DataLoader
import copy
from scipy.optimize import minimize
import copy
import pickle
from torch.utils.data import Dataset
from torch.optim.lr_scheduler import StepLR
from scipy.special import logit, expit
class DeployedNetwork(nn.Module):
def __init__(self, d, m, output):
super(DeployedNetwork, self).__init__()
self.fc1 = nn.Linear(d, m)
self.activate1 = nn.ReLU()
self.fc2 = nn.Linear(m, output)
nn.init.normal_(self.fc1.weight, mean=0, std=0.1)
nn.init.normal_(self.fc2.weight, mean=0, std=0.1)
nn.init.zeros_(self.fc1.bias)
nn.init.zeros_(self.fc2.bias)
def forward(self, x):
x = self.fc2( self.activate1( self.fc1( x ) ) )
return x
class CustomDataset(Dataset):
def __init__(self, ):
self.obs = None
self.labels = None
def __len__(self):
return len(self.obs)
def __getitem__(self, index):
return self.obs[index], self.labels[index]
def append(self, X , y,):
self.obs = X if self.obs is None else np.concatenate( (self.obs, X), axis=0)
self.labels = y if self.labels is None else np.concatenate( (self.labels, y), axis=0)
class CesaBianchi():
def __init__(self, game, m, device):
self.name = 'cesabianchi'
self.device = device
self.game = game
self.N = game.n_actions
self.M = game.n_outcomes
self.A = None #geometry_v3.alphabet_size(game.FeedbackMatrix, self.N, self.M)
self.m = m
self.H = 50
self.K = 0
# self.batch == 0
def predictor(self,X,y):
y_pred = self.func(X).cpu().detach()
y_proba = expit(y_pred)
transformed_probas = torch.cat((1-y_proba, y_proba), dim=1)
return transformed_probas
def reset(self, d):
self.d = d
# self.batch == 0
self.memory_pareto = {}
self.memory_neighbors = {}
self.func = DeployedNetwork( self.d , self.m, 1).to(self.device)
self.func0 = copy.deepcopy(self.func)
self.hist = CustomDataset()
self.feedbacks = []
self.K = 0
self.beta = 1
self.norm_hist = 0
def get_action(self, t, X):
print(' ')
X_norm = X.float().to(self.device) / np.linalg.norm( X.detach().cpu() )
prediction = self.func( X.float().to(self.device) ).cpu().detach()
norm = np.linalg.norm( X_norm.detach().cpu() )
print('norm hist', self.norm_hist, 'current norm', norm)
self.X_prime = max( self.norm_hist, norm )
probability = expit( prediction.item() )
self.pred_action = 1 if probability < 0.5 else 2
print('prediction', prediction, 'proba', probability, 'prediction', self.pred_action)
b = self.beta * np.sqrt(self.K+1) * self.X_prime**2
p = b / ( b + abs( probability ) )
print('b', b, 'probability', p)
self.Z = np.random.binomial(1, p)
self.Z = 1-self.Z
if self.Z == 1:
action = 0
else:
action = self.pred_action
explored = 1 if self.Z == 1 else 0
if t<self.N:
action = t
history = {'monitor_action':action, 'explore':explored,}
return action, history
def update(self, action, feedback, outcome, t, X, loss):
if action == 0:
self.hist.append( X , [outcome] )
if (self.pred_action == 1 and outcome == 0) or (self.pred_action == 2 and outcome ==1):
self.K += 1
self.norm_hist = self.X_prime
if (t>self.N):
if (t<=50) or (t % 50 == 0 and t<1000 and t>50) or (t % 500 == 0 and t>=1000): #
losses = self.step(self.func, self.hist)
return None, None
def step(self, model, data, num_epochs=40, lr=0.001, batch_size=64):
#""Standard training/evaluation epoch over the dataset"""
dataloader = DataLoader(data, batch_size=batch_size, shuffle=True)
optimizer = optim.Adam(model.parameters(), lr=lr)
loss = nn.BCEWithLogitsLoss()
num = len(self.hist)
for _ in range(num_epochs):
batch_loss = 0.0
for X, y in dataloader:
X, y = X.to(self.device).float(), y.to(self.device).float()
pred = self.func(X).squeeze(1)
# print(pred.shape, y.shape)
l = loss(pred, y)
batch_loss += l.item()
optimizer.zero_grad()
l.backward()
optimizer.step()
# print(losses)
if batch_loss / num <= 1e-3:
return batch_loss / num
return None