flgo.algorithm.fedbase
BasicClient
Bases: BasicParty
Source code in flgo\algorithm\fedbase.py
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 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 |
|
get_batch_data()
Get the batch of training data
Returns:
Type | Description |
---|---|
a batch of data |
Source code in flgo\algorithm\fedbase.py
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 |
|
get_time_response()
Get the latency amount of the client
Returns:
Type | Description |
---|---|
self.latency_amount if client not dropping out |
Source code in flgo\algorithm\fedbase.py
837 838 839 840 841 842 843 844 |
|
is_dropped()
Check if the client drops out during communicating.
Returns:
Type | Description |
---|---|
True if the client was being dropped |
Source code in flgo\algorithm\fedbase.py
759 760 761 762 763 764 765 766 |
|
is_idle()
Check if the client is available to participate training.
Returns:
Type | Description |
---|---|
True if the client is available according to the active_rate else False |
Source code in flgo\algorithm\fedbase.py
750 751 752 753 754 755 756 757 |
|
is_working()
Check if the client is training the model.
Returns:
Type | Description |
---|---|
True if the client is working |
Source code in flgo\algorithm\fedbase.py
768 769 770 771 772 773 774 775 776 |
|
pack(model, *args, **kwargs)
Packing the package to be send to the server. The operations of compression of encryption of the package should be done here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
the locally trained model |
required |
Returns:
Name | Type | Description |
---|---|---|
package | a dict that contains the necessary information for the server |
Source code in flgo\algorithm\fedbase.py
735 736 737 738 739 740 741 742 743 744 745 746 747 748 |
|
register_server(server=None)
Register the server to self.server
Source code in flgo\algorithm\fedbase.py
802 803 804 805 806 807 808 |
|
reply(svr_pkg)
Reply a package to the server. The whole local_movielens_recommendation procedure should be defined here. The standard form consists of three procedure: unpacking the server_package to obtain the global model, training the global model, and finally packing the updated model into client_package.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
svr_pkg |
dict
|
the package received from the server |
required |
Returns:
Name | Type | Description |
---|---|---|
client_pkg |
dict
|
the package to be send to the server |
Source code in flgo\algorithm\fedbase.py
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 |
|
set_batch_size(batch_size=None)
Set local_movielens_recommendation training batch size
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_size |
int
|
the training batch size |
None
|
Source code in flgo\algorithm\fedbase.py
819 820 821 822 823 824 825 826 827 |
|
set_learning_rate(lr=None)
Set the learning rate of local_movielens_recommendation training
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lr |
float
|
a real number |
None
|
Source code in flgo\algorithm\fedbase.py
829 830 831 832 833 834 835 |
|
set_local_epochs(epochs=None)
Set local_movielens_recommendation training epochs
Source code in flgo\algorithm\fedbase.py
810 811 812 813 814 815 816 817 |
|
test(model, flag='val')
Evaluate the model on the dataset owned by the client
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
flgo.utils.fmodule.FModule
|
the model need to be evaluated |
required |
flag |
str
|
choose the data to evaluate the model |
'val'
|
Returns:
Name | Type | Description |
---|---|---|
metric |
dict
|
the evaluating results (e.g. metric = {'loss':1.02}) |
Source code in flgo\algorithm\fedbase.py
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 |
|
train(model)
Standard local_movielens_recommendation training procedure. Train the transmitted model with local_movielens_recommendation training dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
FModule
|
the global model |
required |
Source code in flgo\algorithm\fedbase.py
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 |
|
train_loss(model)
Get the loss value of the model on local_movielens_recommendation training data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
flgo.utils.fmodule.FModule | torch.nn.Module
|
model |
required |
Returns:
Type | Description |
---|---|
the training loss of model on self's training data |
Source code in flgo\algorithm\fedbase.py
778 779 780 781 782 783 784 785 786 787 788 |
|
unpack(received_pkg)
Unpack the package received from the server
Parameters:
Name | Type | Description | Default |
---|---|---|---|
received_pkg |
dict
|
a dict contains the global model as default |
required |
Returns:
Type | Description |
---|---|
the unpacked information |
Source code in flgo\algorithm\fedbase.py
704 705 706 707 708 709 710 711 712 713 714 715 |
|
update_device(dev)
Update running-time GPU device to dev
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dev |
torch.device
|
target dev |
required |
Source code in flgo\algorithm\fedbase.py
868 869 870 871 872 873 874 875 876 |
|
val_loss(model)
Get the loss value of the model on local_movielens_recommendation validating data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
flgo.utils.fmodule.FModule | torch.nn.Module
|
model |
required |
Returns:
Type | Description |
---|---|
the validation loss of model on self's validation data |
Source code in flgo\algorithm\fedbase.py
790 791 792 793 794 795 796 797 798 799 800 |
|
BasicParty
Source code in flgo\algorithm\fedbase.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 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 |
|
communicate_with(target_id, package={})
Send the package to target object according to its id, and receive the response from it
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_id |
int
|
the id of the object to communicate with |
required |
package |
dict
|
the package to be sended to the object |
{}
|
Returns:
Name | Type | Description |
---|---|---|
client_package |
dict
|
the reply from the target object and will be 'None' if losing connection |
Source code in flgo\algorithm\fedbase.py
180 181 182 183 184 185 186 187 188 189 190 |
|
get_classname()
Get the class name of self.
Returns:
Name | Type | Description |
---|---|---|
class_name |
str
|
the class name |
Source code in flgo\algorithm\fedbase.py
107 108 109 110 111 112 113 114 |
|
get_data(flag='val')
Get self's attibute '{flag}_data' if this attribute exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
flag |
str
|
the name of the data |
'val'
|
Returns:
Name | Type | Description |
---|---|---|
flag_data |
Any
|
self.{flag}_data |
Source code in flgo\algorithm\fedbase.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
get_data_names()
Get the names of data hold by self.
Returns:
Name | Type | Description |
---|---|---|
data_names |
list
|
the names of data hold by self |
Source code in flgo\algorithm\fedbase.py
98 99 100 101 102 103 104 105 |
|
initialize(*args, **kwargs)
API for customizing the initializing process of the object
Source code in flgo\algorithm\fedbase.py
192 193 194 |
|
message_handler(package)
Handling the received message by excuting the corresponding action.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
package |
dict
|
the package received from other parties (i.e. the content of the message) |
required |
Returns:
Type | Description |
---|---|
action_reult |
Source code in flgo\algorithm\fedbase.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
register_action_to_mtype(action_name, mtype)
Register an existing method as the action corresponding to the message type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action_name |
str
|
the name of the instance method |
required |
mtype |
the message type |
required |
Source code in flgo\algorithm\fedbase.py
22 23 24 25 26 27 28 29 30 31 32 |
|
register_objects(parties, parties_name='parties')
Set self's attribute party_names (e.g. parties as default) to be parties if self has no attribute named party_names. Otherwise, parties will be extend to the attribute party_names of self.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parties |
list
|
a list of objects |
required |
parties_name |
str
|
the name of attribute to store parties |
'parties'
|
Example:
>>> a = BasicParty()
>>> b = BasicParty()
>>> c = BasicParty()
>>> a.register_objects([b, c], 'parties')
>>> a.parties # will be [b,c]
>>> d = BasicParty()
>>> a.register_objects([d], 'parties')
>>> a.parties # will be [b,c,d]
Source code in flgo\algorithm\fedbase.py
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 |
|
set_data(data, flag='train')
Set self's attibute 'xxx_data' to be data where xxx is the flag. For example, after calling self.set_data([1,2,3], 'test'), self.test_data will be [1,2,3]. Particularly, If the flag is 'train', the batchsize and the num_steps will be reset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
anything |
required | |
flag |
str
|
the name of the data |
'train'
|
Source code in flgo\algorithm\fedbase.py
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 |
|
set_id(id=None)
Set self's attibute 'id' to be id where self.id = id
Source code in flgo\algorithm\fedbase.py
124 125 126 127 128 129 |
|
set_message(mtype, package={})
Set the message type of a package.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mtype |
Any
|
the message type |
required |
package |
dict
|
a dict |
{}
|
Returns:
Name | Type | Description |
---|---|---|
package_with_mtype |
dict
|
a dict with the message type |
Source code in flgo\algorithm\fedbase.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
set_model(model, model_name='model')
Set self's attibute 'model_name' to be model. For example, after calling self.set_model(my_model, 'model'), self.model will be my_model.
Source code in flgo\algorithm\fedbase.py
116 117 118 119 120 121 122 |
|
BasicServer
Bases: BasicParty
Source code in flgo\algorithm\fedbase.py
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 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 |
|
available_clients
property
Return all the available clients at the current round.
Returns:
Type | Description |
---|---|
a list of indices of currently available clients |
aggregate(models, *args, **kwargs)
Aggregate the locally trained models into the new one. The aggregation will be according to self.aggregate_option where
pk = nk/n where n=self.data_vol K = |S_t| N = |S|
weighted_scale |uniform (default) |weighted_com (original fedavg) |other
N/K * Σpk * model_k |1/K * Σmodel_k |(1-Σpk) * w_old + Σpk * model_k |Σ(pk/Σpk) * model_k
Parameters:
Name | Type | Description | Default |
---|---|---|---|
models |
list
|
a list of local_movielens_recommendation models |
required |
Returns:
Type | Description |
---|---|
the aggregated model |
Example:
>>> models = [m1, m2] # m1, m2 are models with the same architecture
>>> m_new = self.aggregate(models)
Source code in flgo\algorithm\fedbase.py
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 |
|
communicate(selected_clients, mtype=0, asynchronous=False)
The whole simulating communication procedure with the selected clients. This part supports for simulating the client dropping out.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
selected_clients |
list of int
|
the clients to communicate with |
required |
mtype |
anytype
|
type of message |
0
|
asynchronous |
bool
|
asynchronous communciation or synchronous communcation |
False
|
Returns:
Type | Description |
---|---|
the unpacked response from clients that is created ny self.unpack() |
Source code in flgo\algorithm\fedbase.py
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 |
|
communicate_with(target_id, package={})
Communicate with the object under system simulator that simulates the network latency. Send the package to target object according to its id, and receive the response from it
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_id |
int
|
the id of the object to communicate with |
required |
package |
dict
|
the package to be sended to the object |
{}
|
Returns:
Name | Type | Description |
---|---|---|
client_package |
dict
|
the reply from the target object and |
will be 'None' if losing connection |
Source code in flgo\algorithm\fedbase.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
|
get_tolerance_for_latency()
Get the tolerance for latency of waiting for clients' responses
Returns:
Type | Description |
---|---|
a int number (i.e. self.tolerance_for_latency) |
Source code in flgo\algorithm\fedbase.py
577 578 579 580 581 582 583 584 |
|
global_lr_scheduler(current_round)
Control the step size (i.e. learning rate) of local_movielens_recommendation training
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_round |
int
|
the current communication round |
required |
Source code in flgo\algorithm\fedbase.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|
global_test(model=None, flag='val')
Collect local_movielens_recommendation testing result of all the clients.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
flgo.utils.fmodule.FModule | torch.nn.Module
|
the model to be sevaluated |
None
|
flag |
str
|
choose the data to evaluate the model |
'val'
|
Returns:
Name | Type | Description |
---|---|---|
metrics |
dict
|
a dict contains key-value pairs like (metric_name, |
the lists of metric results of the clients) |
Source code in flgo\algorithm\fedbase.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
|
init_algo_para(algo_para)
Initialize the algorithm-dependent hyper-parameters for the server and all the clients.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
algo_paras |
dict
|
the dict that defines the hyper-parameters (i.e. name, value and type) for the algorithm. |
required |
Example:
>>> # s is an instance of Server and s.clients are instances of Client
>>> s.u # will raise error
>>> [c.u for c in s.clients] # will raise errors too
>>> s.init_algo_para({'u': 0.1})
>>> s.u # will be 0.1
>>> [c.u for c in s.clients] # will be [0.1, 0.1,..., 0.1]
Note
Once option['algo_para']
is not None
, the value of the pre-defined hyperparameters will be replaced by the list of values in option['algo_para']
,
which requires the length of option['algo_para']
is equal to the length of algo_paras
Source code in flgo\algorithm\fedbase.py
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 |
|
iterate()
The standard iteration of each federated communication round that contains three necessary procedure in FL: client selection, communication and model aggregation.
Returns:
Type | Description |
---|---|
False if the global model is not updated in this iteration |
Source code in flgo\algorithm\fedbase.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
pack(client_id, mtype=0, *args, **kwargs)
Pack the necessary information for the client's local_movielens_recommendation training. Any operations of compression or encryption should be done here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id |
int
|
the id of the client to communicate with |
required |
mtype |
the message type |
0
|
Returns:
Type | Description |
---|---|
a dict contains necessary information (e.g. a copy of the global model as default) |
Source code in flgo\algorithm\fedbase.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
register_clients(clients)
Regiser clients to self.clients, and update related attributes (e.g. self.num_clients)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clients |
list
|
a list of objects |
required |
Source code in flgo\algorithm\fedbase.py
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
|
run()
Running the FL symtem where the global model is trained and evaluated iteratively.
Source code in flgo\algorithm\fedbase.py
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 |
|
sample()
Sample the clients. There are three types of sampling manners: full sample, uniform sample without replacement, and MDSample with replacement. Particularly, if 'available' is in self.sample_option, the server will only sample from currently available clients.
Returns:
Type | Description |
---|---|
a list of the ids of the selected clients |
Example:
>>> selected_clients=self.sample()
>>> selected_clients
>>> # The selected_clients is a list of clients' ids
Source code in flgo\algorithm\fedbase.py
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 |
|
set_tolerance_for_latency(tolerance)
Set the tolerance for latency of waiting for clients' responses
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tolerance |
int
|
the amounts of virtual time units |
required |
Source code in flgo\algorithm\fedbase.py
586 587 588 589 590 591 592 593 |
|
test(model=None, flag='test')
Evaluate the model on the test dataset owned by the server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
flgo.utils.fmodule.FModule
|
the model need to be evaluated |
None
|
flag |
str
|
choose the data to evaluate the model |
'test'
|
Returns:
Name | Type | Description |
---|---|---|
metrics |
dict
|
the dict contains the evaluating results |
Source code in flgo\algorithm\fedbase.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
|
unpack(packages_received_from_clients)
Unpack the information from the received packages. Return models and losses as default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
packages_received_from_clients |
list
|
a list of packages |
required |
Returns:
Name | Type | Description |
---|---|---|
res |
dict
|
collections.defaultdict that contains several lists of the clients' reply |
Source code in flgo\algorithm\fedbase.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
|
wait_time(t=1)
Wait for the time of the virtual clock to pass t units
Source code in flgo\algorithm\fedbase.py
595 596 597 598 599 600 |
|