forked from modAL-python/modAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acquisition.py
157 lines (118 loc) · 5.43 KB
/
acquisition.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
"""
Acquisition functions for Bayesian optimization.
"""
from typing import Tuple
import numpy as np
from scipy.stats import norm
from scipy.special import ndtr
from sklearn.exceptions import NotFittedError
from modAL.utils.selection import multi_argmax
from modAL.utils.data import modALinput
from modAL.models.base import BaseLearner
def PI(mean, std, max_val, tradeoff):
return ndtr((mean - max_val - tradeoff)/std)
def EI(mean, std, max_val, tradeoff):
z = (mean - max_val - tradeoff) / std
return (mean - max_val - tradeoff)*ndtr(z) + std*norm.pdf(z)
def UCB(mean, std, beta):
return mean + beta*std
"""
---------------------
Acquisition functions
---------------------
"""
def optimizer_PI(optimizer: BaseLearner, X: modALinput, tradeoff: float = 0) -> np.ndarray:
"""
Probability of improvement acquisition function for Bayesian optimization.
Args:
optimizer: The :class:`~modAL.models.BayesianOptimizer` object for which the utility is to be calculated.
X: The samples for which the probability of improvement is to be calculated.
tradeoff: Value controlling the tradeoff parameter.
Returns:
Probability of improvement utility score.
"""
try:
mean, std = optimizer.predict(X, return_std=True)
mean, std = mean.reshape(-1, ), std.reshape(-1, )
except NotFittedError:
mean, std = np.zeros(shape=(X.shape[0], 1)), np.ones(shape=(X.shape[0], 1))
return PI(mean, std, optimizer.y_max, tradeoff)
def optimizer_EI(optimizer: BaseLearner, X: modALinput, tradeoff: float = 0) -> np.ndarray:
"""
Expected improvement acquisition function for Bayesian optimization.
Args:
optimizer: The :class:`~modAL.models.BayesianOptimizer` object for which the utility is to be calculated.
X: The samples for which the expected improvement is to be calculated.
tradeoff: Value controlling the tradeoff parameter.
Returns:
Expected improvement utility score.
"""
try:
mean, std = optimizer.predict(X, return_std=True)
mean, std = mean.reshape(-1, ), std.reshape(-1, )
except NotFittedError:
mean, std = np.zeros(shape=(X.shape[0], 1)), np.ones(shape=(X.shape[0], 1))
return EI(mean, std, optimizer.y_max, tradeoff)
def optimizer_UCB(optimizer: BaseLearner, X: modALinput, beta: float = 1) -> np.ndarray:
"""
Upper confidence bound acquisition function for Bayesian optimization.
Args:
optimizer: The :class:`~modAL.models.BayesianOptimizer` object for which the utility is to be calculated.
X: The samples for which the upper confidence bound is to be calculated.
beta: Value controlling the beta parameter.
Returns:
Upper confidence bound utility score.
"""
try:
mean, std = optimizer.predict(X, return_std=True)
mean, std = mean.reshape(-1, ), std.reshape(-1, )
except NotFittedError:
mean, std = np.zeros(shape=(X.shape[0], 1)), np.ones(shape=(X.shape[0], 1))
return UCB(mean, std, beta)
"""
--------------------------------------------
Query strategies using acquisition functions
--------------------------------------------
"""
def max_PI(optimizer: BaseLearner, X: modALinput, tradeoff: float = 0,
n_instances: int = 1) -> np.ndarray:
"""
Maximum PI query strategy. Selects the instance with highest probability of improvement.
Args:
optimizer: The :class:`~modAL.models.BayesianOptimizer` object for which the utility is to be calculated.
X: The samples for which the probability of improvement is to be calculated.
tradeoff: Value controlling the tradeoff parameter.
n_instances: Number of samples to be queried.
Returns:
The indices of the instances from X chosen to be labelled; the instances from X chosen to be labelled.
"""
pi = optimizer_PI(optimizer, X, tradeoff=tradeoff)
return multi_argmax(pi, n_instances=n_instances)
def max_EI(optimizer: BaseLearner, X: modALinput, tradeoff: float = 0,
n_instances: int = 1) -> np.ndarray:
"""
Maximum EI query strategy. Selects the instance with highest expected improvement.
Args:
optimizer: The :class:`~modAL.models.BayesianOptimizer` object for which the utility is to be calculated.
X: The samples for which the expected improvement is to be calculated.
tradeoff: Value controlling the tradeoff parameter.
n_instances: Number of samples to be queried.
Returns:
The indices of the instances from X chosen to be labelled; the instances from X chosen to be labelled.
"""
ei = optimizer_EI(optimizer, X, tradeoff=tradeoff)
return multi_argmax(ei, n_instances=n_instances)
def max_UCB(optimizer: BaseLearner, X: modALinput, beta: float = 1,
n_instances: int = 1) -> np.ndarray:
"""
Maximum UCB query strategy. Selects the instance with highest upper confidence bound.
Args:
optimizer: The :class:`~modAL.models.BayesianOptimizer` object for which the utility is to be calculated.
X: The samples for which the maximum upper confidence bound is to be calculated.
beta: Value controlling the beta parameter.
n_instances: Number of samples to be queried.
Returns:
The indices of the instances from X chosen to be labelled; the instances from X chosen to be labelled.
"""
ucb = optimizer_UCB(optimizer, X, beta=beta)
return multi_argmax(ucb, n_instances=n_instances)