Federated ProxQuasiNewton
Necessary mixin and utils for prox newton method.
fed_PQN
FedProxQuasiNewton
Bases: LocMakeFedPQNFisherGradientNLL
, AggChooseStepComputeAscentDirection
Mixin class to implement a Prox Newton method for box constraints.
It implements the method presented here: https://www.cs.utexas.edu/~inderjit/public_papers/pqnj_sisc10.pdf More context can be found here https://optml.mit.edu/papers/sksChap.pdf
Methods:
Name | Description |
---|---|
run_fed_PQN |
The method to run the Prox Quasi Newton algorithm. It relies on the methods inherited from the LocMakeFedPQNFisherGradientNLL and AggChooseStepComputeAscentDirection classes. |
Source code in fedpydeseq2/core/fed_algorithms/fed_PQN/fed_PQN.py
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 |
|
run_fed_PQN(train_data_nodes, aggregation_node, local_states, PQN_shared_state, first_iteration_mode, round_idx, clean_models, refit_mode=False)
Run the Prox Quasi Newton algorithm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train_data_nodes
|
List of TrainDataNode. |
required | |
aggregation_node
|
The aggregation node. |
required | |
local_states
|
Local states. Required to propagate intermediate results. |
required | |
PQN_shared_state
|
The input shared state. The requirements for this shared state are defined in the LocMakeFedPQNFisherGradientNLL class and depend on the first_iteration_mode. |
required | |
first_iteration_mode
|
Literal['irls_catch'] | None
|
The first iteration mode. This defines the input requirements for the algorithm, and is passed to the make_local_fisher_gradient_nll method at the first iteration. |
required |
round_idx
|
The current round. |
required | |
clean_models
|
If True, the models are cleaned. |
required | |
refit_mode
|
bool
|
Whether to run on |
False
|
Returns:
Name | Type | Description |
---|---|---|
local_states |
dict
|
Local states. Required to propagate intermediate results. |
irls_final_shared_states |
dict
|
Shared states containing the final IRLS results. It contains nothing for now. |
round_idx |
int
|
The updated round index. |
Source code in fedpydeseq2/core/fed_algorithms/fed_PQN/fed_PQN.py
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 |
|
substeps
AggChooseStepComputeAscentDirection
Mixin class to compute the right ascent direction.
An ascent direction is a direction that is positively correlated to the gradient. This direction will be used to compute the next iterate in the proximal quasi newton algorithm. As our aim will be to mimimize the negative log likelihood, we will move in the opposite direction, that is in the direction of minus the ascent direction.
Attributes:
Name | Type | Description |
---|---|---|
num_jobs |
int
|
The number of cpus to use. |
joblib_verbosity |
int
|
The joblib verbosity. |
joblib_backend |
str
|
The backend to use for the IRLS algorithm. |
irls_batch_size |
int
|
The batch size to use for the IRLS algorithm. |
max_beta |
float
|
The maximum value for the beta parameter. |
beta_tol |
float
|
The tolerance for the beta parameter. |
PQN_num_iters_ls |
int
|
The number of iterations to use for the line search. |
PQN_c1 |
float
|
The c1 parameter for the line search. |
PQN_ftol |
float
|
The ftol parameter for the line search. |
PQN_num_iters |
int
|
The number of iterations to use for the proximal quasi newton algorithm. |
Methods:
Name | Description |
---|---|
choose_step_and_compute_ascent_direction |
A remote method. Choose the best step size and compute the next ascent direction. |
Source code in fedpydeseq2/core/fed_algorithms/fed_PQN/substeps.py
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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 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 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 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 600 601 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 637 638 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 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 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 |
|
choose_step_and_compute_ascent_direction(shared_states)
Choose best step size and compute next ascent direction.
By "ascent direction", we mean the direction that is positively correlated with the gradient.
The role of this function is twofold.
1) It chooses the best step size for each gene, and updates the beta values as well as the nll values. This allows to define the next iterate. Note that at the first iterate, it simply computes the nll, gradient and fisher information at the current beta values, to define the next ascent direction.
2) For this new iterate (or the current one if we are at the first round), it computes the gradient scaling matrix, which is used to scale the gradient in the proximal newton algorithm. From this gradient scaling matrix, and the gradient, it computes the ascent direction (and the newton decrement).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shared_states
|
list[dict]
|
A list of dictionaries containing the following keys: - beta: ndarray The log fold changes, of shape (n_non_zero_genes, n_params). - local_nll: ndarray The local nll, of shape (n_genes,), where n_genes is the current number of genes that are active (True in the PQN_mask). - local_fisher: ndarray The local fisher matrix, of shape (n_genes, n_params, n_params). - local_gradient: ndarray The local gradient, of shape (n_genes, n_params). - PQN_diverged_mask: ndarray A boolean mask indicating if the gene has diverged in the prox newton algorithm, of shape (n_non_zero_genes,). - PQN_mask: ndarray A boolean mask indicating if the gene should be used for the proximal newton step, of shape (n_non_zero_genes,). - global_reg_nll: ndarray The global regularized nll, of shape (n_non_zero_genes,). - newton_decrement_on_mask: Optional[ndarray] The newton decrement, of shape (n_ngenes,). This is None at the first round of the prox newton algorithm. - round_number_PQN: int The current round number of the prox newton algorithm. - ascent_direction_on_mask: Optional[ndarray] The ascent direction, of shape (n_genes, n_params), where n_genes is the current number of genes that are active (True in the PQN_mask). - irls_diverged_mask: ndarray A boolean mask indicating if the gene has diverged in the IRLS algorithm. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
A dictionary containing all the necessary info to run the method. If we are not at the last iteration, it contains the following fields: - beta: ndarray The log fold changes, of shape (n_non_zero_genes, n_params). - PQN_mask: ndarray A boolean mask indicating if the gene should be used for the proximal newton step. It is of shape (n_non_zero_genes,) - PQN_diverged_mask: ndarray A boolean mask indicating if the gene has diverged in the prox newton algorithm. It is of shape (n_non_zero_genes,) - ascent_direction_on_mask: np.ndarray The ascent direction, of shape (n_genes, n_params), where n_genes is the current number of genes that are active (True in the PQN_mask). - newton_decrement_on_mask: np.ndarray The newton decrement, of shape (n_ngenes,). - round_number_PQN: int The current round number of the prox newton algorithm. - global_reg_nll: ndarray The global regularized nll, of shape (n_non_zero_genes,). - irls_diverged_mask: ndarray A boolean mask indicating if the gene has diverged in the IRLS algorithm. If we are at the last iteration, it contains the following fields: - beta: ndarray The log fold changes, of shape (n_non_zero_genes, n_params). - PQN_diverged_mask: ndarray A boolean mask indicating if the gene has diverged in the prox newton algorithm. It is of shape (n_non_zero_genes,) - irls_diverged_mask: ndarray A boolean mask indicating if the gene has diverged in the IRLS algorithm. |
Source code in fedpydeseq2/core/fed_algorithms/fed_PQN/substeps.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 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 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 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 600 601 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 637 638 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 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 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 |
|
LocMakeFedPQNFisherGradientNLL
Mixin to compute local values, gradient and Fisher information of the NLL.
Attributes:
Name | Type | Description |
---|---|---|
local_adata |
AnnData
|
The local AnnData. |
num_jobs |
int
|
The number of cpus to use. |
joblib_verbosity |
int
|
The joblib verbosity. |
joblib_backend |
str
|
The backend to use for the IRLS algorithm. |
irls_batch_size |
int
|
The batch size to use for the IRLS algorithm. |
max_beta |
float
|
The maximum value for the beta parameter. |
PQN_num_iters_ls |
int
|
The number of iterations to use for the line search. |
PQN_min_mu |
float
|
The min_mu parameter for the Proximal Quasi Newton algorithm. |
Methods:
Name | Description |
---|---|
make_local_fisher_gradient_nll |
A remote_data method. Make the local nll, gradient and fisher matrix. |
Source code in fedpydeseq2/core/fed_algorithms/fed_PQN/substeps.py
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 |
|
make_local_fisher_gradient_nll(data_from_opener, shared_state, first_iteration_mode=None, refit_mode=False)
Make the local nll, gradient and fisher information matrix.
Given an ascent direction :math:d
(an ascent direction being positively
correlated to the gradient of the starting point) and a starting point
:math:beta
, this function
computes the nll, gradient and Fisher information at the points
:math:beta + t * d
,
for :math:t
in step_sizes
(step sizes are :math:0.5^i
for :math:i
in :math:0,...,19
.
Moreover, if the iteration is the first one, the step sizes are not used, and instead, the nll, gradient and fisher information are computed at the current beta values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_from_opener
|
AnnData
|
Not used. |
required |
shared_state
|
dict
|
A dictionary containing the following keys: - PQN_mask: ndarray A boolean mask indicating if the gene should be used for the proximal newton step. It is of shape (n_non_zero_genes,) Used, but not modified. - round_number_PQN: int The current round number of the prox newton algorithm. Used but not modified. - ascent_direction_on_mask: Optional[ndarray] The ascent direction, of shape (n_genes, n_params), where n_genes is the current number of genes that are active (True in the PQN_mask). Used but not modified. - beta: ndarray The log fold changes, of shape (n_non_zero_genes, n_params). Used but not modified. - global_reg_nll: ndarray The global regularized nll, of shape (n_non_zero_genes,). Not used and not modified. - newton_decrement_on_mask: Optional[ndarray] The newton decrement, of shape (n_ngenes,). It is None at the first round of the prox newton algorithm. Not used and not modified. - irls_diverged_mask: ndarray A boolean mask indicating if the gene has diverged in the IRLS algorithm. Not used and not modified. - PQN_diverged_mask: ndarray A boolean mask indicating if the gene has diverged in the prox newton algorithm. Not used and not modified. |
required |
first_iteration_mode
|
Optional[Literal['irls_catch']]
|
For the first iteration, this function behaves differently. If first_iteration_mode is None, then we are not at the first iteration. If first_iteration_mode is not None, the function will expect a different shared state than the one described above, and will construct the initial shared state from it. If first_iteration_mode is "irls_catch", then we assume that we are using the PQN algorithm as a method to catch IRLS when it fails The function will expect a shared state that contains the following fields: - beta: ndarray The log fold changes, of shape (n_non_zero_genes, n_params). - irls_diverged_mask: ndarray A boolean mask indicating if the gene has diverged in the IRLS algorithm. - irls_mask : ndarray The mask of genes that were still active for the IRLS algorithm. |
None
|
refit_mode
|
bool
|
Whether to run on |
False
|
Returns:
Type | Description |
---|---|
dict
|
The state to share to the server.
It contains the following fields:
- beta: ndarray
The log fold changes, of shape (n_non_zero_genes, n_params).
- local_nll: ndarray
The local nll, of shape (n_step_sizes, n_genes,), where
n_genes is the current number of genes that are active (True
in the PQN_mask). n_step_sizes is the number of step sizes
considered, which is |
Raises:
Type | Description |
---|---|
ValueError
|
If first_iteration_mode is not None or "irls_catch". |
Source code in fedpydeseq2/core/fed_algorithms/fed_PQN/substeps.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 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 |
|
utils
Utility functions for the proximal Newton optimization.
This optimization is used in the catching of the IRLS algorithm.
compute_ascent_direction_decrement(gradient_scaling_matrix, gradient, beta, max_beta)
Compute the ascent direction and decrement.
We do this from the gradient scaling matrix, the gradient, the beta and the max beta, which embodies the box constraints.
Please look at this paper for the precise references to the equations: https://www.cs.utexas.edu/~inderjit/public_papers/pqnj_sisc10.pdf
By ascent direction, we mean that the direction we compute is positively correlated with the gradient. As our aim is to minimize the function, we want to move in the opposite direction of the ascent direction, but it is simpler to compute the ascent direction to avoid sign errors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gradient_scaling_matrix
|
ndarray
|
The gradient scaling matrix, of shape (n_genes, n_params, n_params). |
required |
gradient
|
ndarray
|
The gradient per gene, of shape (n_genes, n_params). |
required |
beta
|
ndarray
|
Beta on those genes, of shape (n_genes, n_params). |
required |
max_beta
|
float
|
The max absolute value for beta. |
required |
Returns:
Name | Type | Description |
---|---|---|
ascent_direction |
ndarray
|
The new ascent direction, of shape (n_genes, n_params). |
newton_decrement |
ndarray
|
The newton decrement associated to these ascent directions of shape (n_genes, ) |
Source code in fedpydeseq2/core/fed_algorithms/fed_PQN/utils.py
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 |
|
compute_gradient_scaling_matrix_fisher(fisher, backend, num_jobs, joblib_verbosity, batch_size)
Compute the gradient scaling matrix using the Fisher information.
In this case, we simply invert the provided Fisher matrix to get the gradient scaling matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fisher
|
ndarray
|
The Fisher matrix, of shape (n_genes, n_params, n_params) |
required |
backend
|
str
|
The backend to use for parallelization |
required |
num_jobs
|
int
|
The number of cpus to use |
required |
joblib_verbosity
|
int
|
The verbosity level of joblib |
required |
batch_size
|
int
|
The batch size to use for the computation |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The gradient scaling matrix, of shape (n_genes, n_params, n_params) |
Source code in fedpydeseq2/core/fed_algorithms/fed_PQN/utils.py
make_fisher_gradient_nll_step_sizes_batch(design_matrix, size_factors, beta, dispersions, counts, ascent_direction, step_sizes, beta_min, beta_max, min_mu=0.5)
Make local gradient, fisher matrix, and nll for multiple steps.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
design_matrix
|
ndarray
|
The design matrix, of shape (n_obs, n_params). |
required |
size_factors
|
ndarray
|
The size factors, of shape (n_obs). |
required |
beta
|
ndarray
|
The log fold change matrix, of shape (batch_size, n_params). |
required |
dispersions
|
ndarray
|
The dispersions, of shape (batch_size). |
required |
counts
|
ndarray
|
The counts, of shape (n_obs,batch_size). |
required |
ascent_direction
|
ndarray
|
The ascent direction, of shape (batch_size, n_params). |
required |
step_sizes
|
ndarray | None
|
A list of step sizes to evaluate, of size (n_steps, ). |
required |
beta_min
|
float | None
|
The minimum value tolerated for beta. |
required |
beta_max
|
float | None
|
The maximum value tolerated for beta. |
required |
min_mu
|
float
|
Lower bound on estimated means, to ensure numerical stability. |
0.5
|
Returns:
Name | Type | Description |
---|---|---|
H |
Optional[ndarray]
|
The Fisher information matrix, of shape (n_steps, batch_size, n_params, n_params). |
gradient |
ndarray
|
The gradient, of shape (n_steps, batch_size, n_params). |
nll |
ndarray
|
The nll evaluations on all steps, of size (n_steps, batch_size). |
Source code in fedpydeseq2/core/fed_algorithms/fed_PQN/utils.py
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 |
|