-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathlogistic.py
362 lines (275 loc) · 10.7 KB
/
logistic.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
# Copyright (c) Yuta Saito, Yusuke Narita, and ZOZO Technologies, Inc. All rights reserved.
# Licensed under the Apache 2.0 License.
"""Contextual Logistic Bandit Algorithms."""
from dataclasses import dataclass
from typing import Optional
import numpy as np
from scipy.optimize import minimize
from sklearn.utils import check_random_state
from sklearn.utils import check_scalar
from ..utils import sigmoid
from .base import BaseContextualPolicy
@dataclass
class BaseLogisticPolicy(BaseContextualPolicy):
"""Base class for contextual bandit policies using logistic regression.
Parameters
----------
dim: int
Number of dimensions of context vectors.
n_actions: int
Number of actions.
len_list: int, default=1
Length of a list of actions in a recommendation/ranking inferface, slate size.
When Open Bandit Dataset is used, 3 should be set.
batch_size: int, default=1
Number of samples used in a batch parameter update.
alpha_: float, default=1.
Prior parameter for the online logistic regression.
lambda_: float, default=1.
Regularization hyperparameter for the online logistic regression.
random_state: int, default=None
Controls the random seed in sampling actions.
"""
alpha_: float = 1.0
lambda_: float = 1.0
def __post_init__(self) -> None:
"""Initialize class."""
super().__post_init__()
check_scalar(self.alpha_, "alpha_", float)
if self.alpha_ <= 0.0:
raise ValueError(f"`alpha_`= {self.alpha_}, must be > 0.0.")
check_scalar(self.lambda_, "lambda_", float)
if self.lambda_ <= 0.0:
raise ValueError(f"`lambda_`= {self.lambda_}, must be > 0.0.")
self.alpha_list = self.alpha_ * np.ones(self.n_actions)
self.lambda_list = self.lambda_ * np.ones(self.n_actions)
self.model_list = [
MiniBatchLogisticRegression(
lambda_=self.lambda_list[i],
alpha=self.alpha_list[i],
dim=self.dim,
random_state=self.random_state,
)
for i in np.arange(self.n_actions)
]
def update_params(self, action: int, reward: float, context: np.ndarray) -> None:
"""Update policy parameters.
Parameters
----------
action: int
Selected action by the policy.
reward: float
Observed reward for the chosen action and position.
context: array-like, shape (1, dim_context)
Observed context vector.
"""
self.n_trial += 1
self.action_counts[action] += 1
self.reward_lists[action].append(reward)
self.context_lists[action].append(context)
if self.n_trial % self.batch_size == 0:
for action, model in enumerate(self.model_list):
if not len(self.reward_lists[action]) == 0:
model.fit(
X=np.concatenate(self.context_lists[action], axis=0),
y=np.array(self.reward_lists[action]),
)
self.reward_lists = [[] for _ in np.arange(self.n_actions)]
self.context_lists = [[] for _ in np.arange(self.n_actions)]
@dataclass
class LogisticEpsilonGreedy(BaseLogisticPolicy):
"""Logistic Epsilon Greedy.
Parameters
-----------
dim: int
Number of dimensions of context vectors.
n_actions: int
Number of actions.
len_list: int, default=1
Length of a list of actions in a recommendation/ranking inferface, slate size.
When Open Bandit Dataset is used, 3 should be set.
batch_size: int, default=1
Number of samples used in a batch parameter update.
random_state: int, default=None
Controls the random seed in sampling actions.
alpha_: float, default=1.
Prior parameter for the online logistic regression.
lambda_: float, default=1.
Regularization hyperparameter for the online logistic regression.
epsilon: float, default=0.
Exploration hyperparameter that must take value in the range of [0., 1.].
"""
epsilon: float = 0.0
def __post_init__(self) -> None:
"""Initialize class."""
check_scalar(self.epsilon, "epsilon", float, min_val=0.0, max_val=1.0)
self.policy_name = f"logistic_egreedy_{self.epsilon}"
super().__post_init__()
def select_action(self, context: np.ndarray) -> np.ndarray:
"""Select action for new data.
Parameters
----------
context: array-like, shape (1, dim_context)
Observed context vector.
Returns
----------
selected_actions: array-like, shape (len_list, )
List of selected actions.
"""
if self.random_.rand() > self.epsilon:
theta = np.array(
[model.predict_proba(context) for model in self.model_list]
).flatten()
return theta.argsort()[::-1][: self.len_list]
else:
return self.random_.choice(
self.n_actions, size=self.len_list, replace=False
)
@dataclass
class LogisticUCB(BaseLogisticPolicy):
"""Logistic Upper Confidence Bound.
Parameters
------------
dim: int
Number of dimensions of context vectors.
n_actions: int
Number of actions.
len_list: int, default=1
Length of a list of actions in a recommendation/ranking inferface, slate size.
When Open Bandit Dataset is used, 3 should be set.
batch_size: int, default=1
Number of samples used in a batch parameter update.
random_state: int, default=None
Controls the random seed in sampling actions.
alpha_: float, default=1.
Prior parameter for the online logistic regression.
lambda_: float, default=1.
Regularization hyperparameter for the online logistic regression.
epsilon: float, default=0.
Exploration hyperparameter that must be greater than or equal to 0.0.
References
----------
Lihong Li, Wei Chu, John Langford, and Robert E Schapire.
"A Contextual-bandit Approach to Personalized News Article Recommendation," 2010.
"""
epsilon: float = 0.0
def __post_init__(self) -> None:
"""Initialize class."""
check_scalar(self.epsilon, "epsilon", float, min_val=0.0)
self.policy_name = f"logistic_ucb_{self.epsilon}"
super().__post_init__()
def select_action(self, context: np.ndarray) -> np.ndarray:
"""Select action for new data.
Parameters
------------
context: array-like, shape (1, dim_context)
Observed context vector.
Returns
----------
selected_actions: array-like, shape (len_list, )
List of selected actions.
"""
theta = np.array(
[model.predict_proba(context) for model in self.model_list]
).flatten()
std = np.array(
[
np.sqrt(np.sum((model._q ** (-1)) * (context**2)))
for model in self.model_list
]
).flatten()
ucb_score = theta + self.epsilon * std
return ucb_score.argsort()[::-1][: self.len_list]
@dataclass
class LogisticTS(BaseLogisticPolicy):
"""Logistic Thompson Sampling.
Parameters
----------
dim: int
Number of dimensions of context vectors.
n_actions: int
Number of actions.
len_list: int, default=1
Length of a list of actions in a recommendation/ranking inferface, slate size.
When Open Bandit Dataset is used, 3 should be set.
batch_size: int, default=1
Number of samples used in a batch parameter update.
random_state: int, default=None
Controls the random seed in sampling actions.
alpha_: float, default=1.
Prior parameter for the online logistic regression.
lambda_: float, default=1.
Regularization hyperparameter for the online logistic regression.
References
----------
Olivier Chapelle and Lihong Li.
"An empirical evaluation of thompson sampling," 2011.
"""
policy_name: str = "logistic_ts"
def __post_init__(self) -> None:
"""Initialize class."""
super().__post_init__()
def select_action(self, context: np.ndarray) -> np.ndarray:
"""Select action for new data.
Parameters
----------
context: array-like, shape (1, dim_context)
Observed context vector.
Returns
----------
selected_actions: array-like, shape (len_list, )
List of selected actions.
"""
theta = np.array(
[model.predict_proba_with_sampling(context) for model in self.model_list]
).flatten()
return theta.argsort()[::-1][: self.len_list]
@dataclass
class MiniBatchLogisticRegression:
"""MiniBatch Online Logistic Regression Model."""
lambda_: float
alpha: float
dim: int
random_state: Optional[int] = None
def __post_init__(self) -> None:
"""Initialize Class."""
self._m = np.zeros(self.dim)
self._q = np.ones(self.dim) * self.lambda_
self.random_ = check_random_state(self.random_state)
def loss(self, w: np.ndarray, *args) -> float:
"""Calculate loss function."""
X, y = args
return (
0.5 * (self._q * (w - self._m)).dot(w - self._m)
+ np.log(1 + np.exp(-y * w.dot(X.T))).sum()
)
def grad(self, w: np.ndarray, *args) -> np.ndarray:
"""Calculate gradient."""
X, y = args
return self._q * (w - self._m) + (-1) * (
((y * X.T) / (1.0 + np.exp(y * w.dot(X.T)))).T
).sum(axis=0)
def sample(self) -> np.ndarray:
"""Sample coefficient vector from the prior distribution."""
return self.random_.normal(self._m, self.sd(), size=self.dim)
def fit(self, X: np.ndarray, y: np.ndarray):
"""Update coefficient vector by the mini-batch data."""
self._m = minimize(
self.loss,
self._m,
args=(X, y),
jac=self.grad,
method="L-BFGS-B",
options={"maxiter": 20, "disp": False},
).x
P = (1 + np.exp(1 + X.dot(self._m))) ** (-1)
self._q = self._q + (P * (1 - P)).dot(X**2)
def sd(self) -> np.ndarray:
"""Standard deviation for the coefficient vector."""
return self.alpha * (self._q) ** (-1.0)
def predict_proba(self, X: np.ndarray) -> np.ndarray:
"""Predict extected probability by the expected coefficient."""
return sigmoid(X.dot(self._m))
def predict_proba_with_sampling(self, X: np.ndarray) -> np.ndarray:
"""Predict extected probability by the sampled coefficient."""
return sigmoid(X.dot(self.sample()))