literal
Module implementing the algorithm presented in ‘Design and Analysis of Learning Classifier Systems – A Probabilistic Approach’ by Jan Drugowitsch.
This implementation intentionally breaks with several Python conventions (e.g. PEP8 regarding variable naming) in order to stay as close as possible to the formulation of the algorithm in aforementioned work.
Also, other than in the remainder of this implementation, we do not as strictly avoid the overloaded term “classifier” within this module since it is sometimes used by the original algorithms.
The only deviations from the book are:
model_probability
returns L(q) - ln K! instead of L(q) + ln K! as the latter is presumably a typographical error in the book (the corresponding formula in Section 7 uses-
as well, which seems to be correct).- We initialize the mixing model parameters
V
using the correct scale ofb_beta / a_beta
(there is a typographical error in the TrainMixing algorithm in Drugowitsch's book). - We always use Moore-Penrose pseudo-inverses instead of actual inverses due to
(very seldomly) matrices being invertible—probably due to numerical
inaccuracies. This is also done in the code that Jan Drugowitsch published to
accompany his book:
1 <https://github.com/jdrugo/LCSBookCode/blob/master/cl.py#L120>
,2 <https://github.com/jdrugo/LCSBookCode/blob/master/cl.py#L385>
,3 <https://github.com/jdrugo/LCSBookCode/blob/master/cl.py#L409>
_. -
Since the IRLS training of the mixing weights sometimes starts to oscillate in an infinite loop between several weight values, we add a maximum number of iterations to the three main training loops:
-
submodel training (
train_classifier
) - mixing model training (
train_mixing
) - mixing weight training (
train_mix_weights
)
This seems reasonable, especially since Jan Drugowitsch's code does the same
(a behaviour that is not documented in the book).
* Since the oscillations in train_mix_weights
are (at least sometimes)
caused by the Kullback-Leibler divergence between G
and R
being
optimal followed by another unnecessary execution of the loop thereafter we
also abort if that is the case (i.e. if the Kullback-Leibler divergence is
zero) and always compute the divergence before starting the loop first time.
* We deal with minor numerical issues in a few places (e.g. in
train_mix_priors
, responsibilities
).
Within the code, comments referring to “LCSBookCode” refer to Jan Drugowitsch's
code <https://github.com/jdrugo/LCSBookCode>
_.
_kl(R, G)
Computes the negative Kullback-Leibler divergence between the given arrays.
Drugowitsch does not introduce this subroutine. We do so to reduce code
duplication in train_mix_weights
(where we deviated from the original
text by one additional calculation of _kl(R, G)
).
Source code in berbl/literal/__init__.py
hessian(Phi, G, a_beta, b_beta)
[PDF p. 243]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Phi |
array of shape (N, DV)
|
Mixing feature matrix. |
required |
G |
array of shape (N, K)
|
Mixing (“gating”) matrix. |
required |
a_beta |
array of shape (K,)
|
Mixing weight prior parameter (row vector). |
required |
b_beta |
array of shape (K,)
|
Mixing weight prior parameter (row vector). |
required |
Returns:
Type | Description |
---|---|
array of shape (K
|
Hessian matrix. |
Source code in berbl/literal/__init__.py
mixing(M, Phi, V)
[PDF p. 239]
Is zero wherever a rule does not match.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
M |
array of shape (N, K)
|
Matching matrix. |
required |
Phi |
array of shape (N, DV)
|
Mixing feature matrix. |
required |
V |
array of shape (DV, K)
|
Mixing weight matrix. |
required |
Returns:
Name | Type | Description |
---|---|---|
G |
array of shape (N, K)
|
Mixing (“gating”) matrix. |
Source code in berbl/literal/__init__.py
model_probability(matchs, X, Y, Phi, random_state, exp_min=np.log(np.finfo(None).tiny), ln_max=np.log(np.finfo(None).max))
[PDF p. 235]
Note that this deviates from [PDF p. 235] in that we return p(M | D) =
L(q) - ln K!
instead of L(q) + ln K!
because the latter is not
consistent with (7.3).
We also compute the matching matrix within this function instead of providing it to it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
M |
array of shape (N, K)
|
Matching matrix. |
required |
X |
array of shape (N, DX)
|
Input matrix. |
required |
Y |
array of shape (N, DY)
|
Output matrix. |
required |
Phi |
array of shape (N, DV)
|
Mixing feature matrix. |
required |
Returns:
Type | Description |
---|---|
metrics, params
|
Model metrics and model parameters. |
Source code in berbl/literal/__init__.py
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 |
|
responsibilities(X, Y, G, W, Lambda_1, a_tau, b_tau)
[PDF p. 240]
:param X: input matrix (N × DX) :param Y: output matrix (N × DY) :param G: mixing (“gating”) matrix (N × K) :param W: submodel weight matrices (list of DY × DX) :param Lambda_1: submodel covariance matrices (list of DX × DX) :param a_tau: submodel noise precision parameters :param b_tau: submodel noise precision parameters
:returns: responsibility matrix (N × K)
Source code in berbl/literal/__init__.py
train_classifier(m_k, X, Y)
[PDF p. 238]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
m_k |
array of shape (N,)
|
Matching vector of rule k. |
required |
X |
array of shape (N, DX)
|
Input matrix. |
required |
Y |
array of shape (N, DY)
|
Output matrix. |
required |
Returns:
Type | Description |
---|---|
W_k, Lambda_k_1, a_tau_k, b_tau_k, a_alpha_k, b_alpha_k
|
Weight matrix (DY × DX), covariance matrix (DX × DX), noise precision parameters, weight vector parameters. |
Source code in berbl/literal/__init__.py
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 |
|
train_mix_priors(V, Lambda_V_1)
[PDF p. 244]
:param V: mixing weight matrix (DV × K) :param Lambda_V_1: mixing covariance matrix (K DV × K DV)
:returns: mixing weight vector prior parameters a_beta, b_beta
Source code in berbl/literal/__init__.py
train_mix_weights(M, X, Y, Phi, W, Lambda_1, a_tau, b_tau, V, a_beta, b_beta)
Training routine for mixing weights based on a Laplace approximation (see Drugowitsch's book [PDF p. 241]).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
M |
array of shape (N, K)
|
Matching matrix. |
required |
X |
array of shape (N, DX)
|
Input matrix. |
required |
y |
array of shape (N, Dy)
|
Output matrix. |
required |
Phi |
array of shape (N, DV)
|
Mixing feature matrix. |
required |
W |
list (length K) of arrays of shape (DY, DX)
|
Submodel weight matrices. |
required |
Lambda_1 |
list (length K) of arrays of shape (DX, DX)
|
Submodel covariance matrices. |
required |
a_tau |
array of shape (K,)
|
Submodel noise precision parameter. |
required |
b_tau |
array of shape (K,)
|
Submodel noise precision parameter. |
required |
V |
array of shape (DV, K)
|
Mixing weight matrix. |
required |
a_beta |
array of shape (K,)
|
Mixing weight prior parameter (row vector). |
required |
b_beta |
array of shape (K,)
|
Mixing weight prior parameter (row vector). |
required |
lxi |
array of shape (N, K)
|
Parameter of Bouchard's bound. |
required |
alpha |
array of shape (N, 1)
|
Parameter of Bouchard's bound. |
required |
Returns:
Type | Description |
---|---|
V, Lambda_V_1
|
Updated mixing weight matrix and mixing weight covariance matrix. |
Source code in berbl/literal/__init__.py
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 |
|
train_mixing(M, X, Y, Phi, W, Lambda_1, a_tau, b_tau, exp_min, ln_max, random_state)
[PDF p. 238]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
M |
array of shape (N, K)
|
Matching matrix. |
required |
X |
array of shape (N, DX)
|
Input matrix. |
required |
Y |
array of shape (N, DY)
|
Output matrix. |
required |
Phi |
array of shape (N, DV)
|
Mixing feature matrix. |
required |
W |
list (length K) of arrays of shape (DY, DX)
|
Submodel weight matrices. |
required |
Lambda_1 |
list (length K) of arrays of shape (DX, DX)
|
Submodel covariance matrices. |
required |
a_tau |
array of shape (K,)
|
Submodel noise precision parameter. |
required |
b_tau |
array of shape (K,)
|
Submodel noise precision parameter. |
required |
Returns:
Type | Description |
---|---|
V, Lambda_V_1, a_beta, b_beta
|
Mixing weight matrix, mixing weight covariance matrix, mixing weight prior parameter vectors. |
Source code in berbl/literal/__init__.py
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 |
|
var_bound(M, X, Y, Phi, W, Lambda_1, a_tau, b_tau, a_alpha, b_alpha, V, Lambda_V_1, a_beta, b_beta)
[PDF p. 244]
:param M: matching matrix (N × K) :param X: input matrix (N × DX) :param Y: output matrix (N × DY) :param Phi: mixing feature matrix (N × DV) :param W: submodel weight matrices (list of DY × DX) :param Lambda_1: submodel covariance matrices (list of DX × DX) :param a_tau: submodel noise precision parameters :param b_tau: submodel noise precision parameters :param a_alpha: weight vector prior parameters :param b_alpha: weight vector prior parameters :param V: mixing weight matrix (DV × K) :param Lambda_V_1: mixing covariance matrix (K DV × K DV) :param a_beta: mixing weight prior parameter (row vector of length K) :param b_beta: mixing weight prior parameter (row vector of length K)
:returns: variational bound L(q)
Source code in berbl/literal/__init__.py
var_cl_bound(X, Y, W_k, Lambda_k_1, a_tau_k, b_tau_k, a_alpha_k, b_alpha_k, r_k)
[PDF p. 245]
:param X: input matrix (N × DX) :param Y: output matrix (N × DY) :param W_k: submodel weight matrix (DY × DX) :param Lambda_k_1: submodel covariance matrix (DX × DX) :param a_tau_k: submodel noise precision parameter :param b_tau_k: submodel noise precision parameter :param a_alpha_k: weight vector prior parameter :param b_alpha_k: weight vector prior parameter :param r_k: responsibility vector (NumPy row or column vector, we reshape to (-1) anyways)
:returns: rule component L_k(q) of variational bound
Source code in berbl/literal/__init__.py
var_mix_bound(G, R, V, Lambda_V_1, a_beta, b_beta)
[PDF p. 245]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
array of shape (N, K)
|
Mixing (“gating”) matrix. |
required |
R |
array of shape (N, K)
|
Responsibility matrix. |
required |
V |
array of shape (DV, K)
|
Mixing weight matrix. |
required |
Lambda_V_1 |
array of shape (K
|
Mixing weight covariance matrix. |
required |
a_beta |
array of shape (K,)
|
Mixing weight prior parameter (row vector). |
required |
b_beta |
array of shape (K,)
|
required |
Returns:
Name | Type | Description |
---|---|---|
L_M_q |
float
|
Mixing component L_M(q) of variational bound. |