slise.optimisation
This script contains the loss functions and optimisation functions for SLISE.
loss_smooth(alpha, X, Y, epsilon, beta=100, lambda1=0, lambda2=0, weight=None)
Smoothed version of the SLISE loss (slise.optimisation.loss_sharp).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
ndarray
|
Linear model coefficients. |
required |
X |
ndarray
|
Data matrix. |
required |
Y |
ndarray
|
Response vector. |
required |
epsilon |
float
|
Error tolerance. |
required |
beta |
float
|
Sigmoid steepness. Defaults to 100. |
100
|
lambda1 |
float
|
LASSO/L1 regularisation coefficient. Defaults to 0. |
0
|
lambda2 |
float
|
Ridge/L2 regularisation coefficient. Defaults to 0. |
0
|
weight |
Optional[ndarray]
|
Weight vector for the data items. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
Loss value. |
Source code in slise/optimisation.py
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 |
|
loss_residuals(alpha, residuals2, epsilon2, beta=100.0, lambda1=0.0, lambda2=0.0, weight=None)
Smoothed version of the SLISE loss (slise.optimisation.loss_smooth), that takes already calculated residuals.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
ndarray
|
Linear model coefficients. |
required |
residuals2 |
ndarray
|
Squared residuals. |
required |
epsilon2 |
float
|
Squared error tolerance. |
required |
beta |
float
|
Sigmoid steepness. Defaults to 100. |
100.0
|
lambda1 |
float
|
LASSO/L1 regularisation coefficient. Defaults to 0. |
0.0
|
lambda2 |
float
|
Ridge/L2 regularisation coefficient. Defaults to 0. |
0.0
|
weight |
Optional[ndarray]
|
Weight vector for the data items. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
Loss value. |
Source code in slise/optimisation.py
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 |
|
loss_sharp(alpha, X, Y, epsilon, lambda1=0, lambda2=0, weight=None)
Exact version (no sigmoid smoothing) of the SLISE loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
ndarray
|
Linear model coefficients. |
required |
X |
ndarray
|
Data matrix. |
required |
Y |
ndarray
|
Response vector. |
required |
epsilon |
float
|
Error tolerance. |
required |
lambda1 |
float
|
LASSO/L1 regularisation coefficient. Defaults to 0. |
0
|
lambda2 |
float
|
Ridge/L2 regularisation coefficient. Defaults to 0. |
0
|
weight |
Optional[ndarray]
|
Weight vector for the data items. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
Loss value. |
Source code in slise/optimisation.py
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 |
|
loss_grad(alpha, X, Y, epsilon, beta, lambda1=0.0, lambda2=0.0, weight=None)
Smoothed version of the SLISE loss (slise.optimisation.loss_smooth), that also calculates the gradient.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
ndarray
|
Linear model coefficients. |
required |
X |
ndarray
|
Data matrix. |
required |
Y |
ndarray
|
Response vector. |
required |
epsilon |
float
|
Error tolerance. |
required |
beta |
float
|
Sigmoid steepness. |
required |
lambda1 |
float
|
Lasso/L1 regularisation coefficient. Defaults to 0.0. |
0.0
|
lambda2 |
float
|
Ridge/L2 regularisation coefficient. Defaults to 0.0. |
0.0
|
weight |
Optional[ndarray]
|
Weight vector for the data items. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Tuple[float, ndarray]
|
Tuple[float, np.ndarray]: Loss value and gradient vector. |
Source code in slise/optimisation.py
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 |
|
owlqn(loss_grad_fn, x0, lambda1=0, max_iterations=200, **kwargs)
Wrapper around owlqn that converts max_iter errors to warnings (see lbfgs.fmin_lbfgs
from PyLBFGS).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loss_grad_fn |
Callable[[ndarray], Tuple[float, ndarray]]
|
Function that calculates the loss and gradient. |
required |
x0 |
ndarray
|
Initial vector to be optimised. |
required |
lambda1 |
float
|
LASSO/L1 regularisation coefficient. Defaults to 1e-6. |
0
|
max_iterations |
int
|
Maximum number of optimisation steps. Defaults to 200. |
200
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Optimised vector. |
Source code in slise/optimisation.py
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 |
|
regularised_regression(X, Y, lambda1=1e-06, lambda2=1e-06, weight=None, max_iterations=200)
Train a linear regression model with lasso (L1) and/or ridge (L2) regularisation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
ndarray
|
Data matrix. |
required |
Y |
ndarray
|
Response vector. |
required |
lambda1 |
float
|
LASSO/L1 regularisation coefficient. Defaults to 1e-6. |
1e-06
|
lambda2 |
float
|
Ridge/L2 regularisation coefficient. Defaults to 1e-6. |
1e-06
|
weight |
Optional[ndarray]
|
Weight vector for the data items. Defaults to None. |
None
|
max_iterations |
int
|
Maximum number of optimisation steps. Defaults to 200. |
200
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The coefficients of the linear model. |
Source code in slise/optimisation.py
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 |
|
optimise_loss(alpha, X, Y, epsilon=0.1, beta=100, lambda1=0, lambda2=0, weight=None, max_iterations=200)
Optimise a smoothed SLISE loss with owl-qn
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
ndarray
|
Linear model coefficients. |
required |
X |
ndarray
|
Data matrix. |
required |
Y |
ndarray
|
Target vector |
required |
epsilon |
float
|
Error tolerance. Defaults to 0.1. |
0.1
|
beta |
float
|
Sigmoid steepness. Defaults to 100. |
100
|
lambda1 |
float
|
LASSO/L1 regularisation coefficient. Defaults to 1e-6. |
0
|
lambda2 |
float
|
Ridge/L2 regularisation coefficient. Defaults to 1e-6. |
0
|
weight |
Optional[ndarray]
|
Weight vector for the data items. Defaults to None. |
None
|
max_iterations |
int
|
Maximum number of optimisation steps. Defaults to 200. |
200
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The coefficients of the linear model. |
Source code in slise/optimisation.py
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 |
|
log_approximation_ratio(residuals2, epsilon2, beta1, beta2, weight=None)
Calculate log(K), where K is the approximation ratio between two smoothed losses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
residuals2 |
ndarray
|
Squared residuals. |
required |
epsilon2 |
float
|
Squared error tolerance. |
required |
beta1 |
float
|
Old sigmoid steepness. |
required |
beta2 |
float
|
New sigmoid steepness. |
required |
weight |
Optional[ndarray]
|
Weight vector. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
log of the approximation ratio between |
Source code in slise/optimisation.py
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 |
|
next_beta(residuals2, epsilon2=0.01, beta=0.0, weight=None, beta_max=2500, log_max_approx=0.14, min_beta_step=0.0005)
Calculate the next beta for the graduated optimisation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
residuals2 |
ndarray
|
Squared residuals. |
required |
epsilon2 |
float
|
Squared error tolerance. Defaults to 0.01. |
0.01
|
beta |
float
|
Sigmoid steepness. Defaults to 0. |
0.0
|
weight |
Optional[ndarray]
|
Weight vector. Defaults to None. |
None
|
beta_max |
float
|
Maximum |
2500
|
log_max_approx |
float
|
Log-maximum approximation ratio. Defaults to 0.14. |
0.14
|
min_beta_step |
float
|
Minimum increase of |
0.0005
|
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
next |
Source code in slise/optimisation.py
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 |
|
matching_epsilon(residuals2, epsilon2, beta, weight=None)
Approximately calculate the epsilon that minimises the approximation ratio to the exact loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
residuals2 |
ndarray
|
Squared residuals. |
required |
epsilon2 |
float
|
Squared error tolerance. |
required |
beta |
float
|
Sigmoid steepness. |
required |
weight |
Optional[ndarray]
|
Weight vector. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
(Approximatively) optimal epsilon for the exact loss (for the current solution). |
Source code in slise/optimisation.py
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 |
|
graduated_optimisation(alpha, X, Y, epsilon, beta=0, lambda1=0, lambda2=0, weight=None, beta_max=20, max_approx=1.15, max_iterations=200, debug=False)
Optimise alpha
using graduated optimisation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
ndarray
|
Initial linear model coefficients. |
required |
X |
ndarray
|
Data matrix. |
required |
Y |
ndarray
|
Response vector. |
required |
epsilon |
float
|
Error tolerance. |
required |
beta |
float
|
Initial sigmoid steepness. Defaults to 0. |
0
|
lambda1 |
float
|
L1 regularisation strength. Defaults to 0. |
0
|
lambda2 |
float
|
L2 regularisation strength. Defaults to 0. |
0
|
weight |
Optional[ndarray]
|
Weight vector for the data items. Defaults to None. |
None
|
beta_max |
float
|
Maximum sigmoid steepness (the final beta). Defaults to 20. |
20
|
max_approx |
float
|
Target approximation ratio when increasing beta. Defaults to 1.15. |
1.15
|
max_iterations |
int
|
Maximum number of iterations for owl-qn. Defaults to 200. |
200
|
debug |
bool
|
Print debug logs after each optimisation step. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Optimised |
Source code in slise/optimisation.py
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 |
|
set_threads(num=-1)
Set the number of numba threads.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num |
int
|
The number of threads (or -1 to keep the old value). Defaults to -1. |
-1
|
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The old number of theads (or -1 if unchanged). |
Source code in slise/optimisation.py
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 |
|
check_threading_layer()
Check which numba threading_layer is active, and warn if it is "workqueue".
Source code in slise/optimisation.py
779 780 781 782 783 784 785 786 787 788 789 790 791 792 |
|