.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/customize_attack.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__examples_customize_attack.py: Customize Attack ================== .. GENERATED FROM PYTHON SOURCE LINES 6-71 .. code-block:: Python import ray from ray import tune from ray.tune.stopper import MaximumIterationStopper from blades.algorithms.fedavg import FedavgConfig, Fedavg from fedlib.trainers import TrainerConfig from fedlib.trainers import Trainer from fedlib.clients import ClientCallback from blades.adversaries import Adversary class LabelFlipAdversary(Adversary): def on_trainer_init(self, trainer: Trainer): class LabelFlipCallback(ClientCallback): def on_train_batch_begin(self, data, target): return data, 10 - 1 - target for client in self.clients: client.to_malicious(callbacks_cls=LabelFlipCallback, local_training=True) class ExampleFedavgConfig(FedavgConfig): def __init__(self, algo_class=None): """Initializes a FedavgConfig instance.""" super().__init__(algo_class=algo_class or ExampleFedavg) self.dataset_config = { "type": "FashionMNIST", "num_clients": 10, "train_batch_size": 32, } self.global_model = "cnn" self.num_malicious_clients = 1 self.adversary_config = {"type": LabelFlipAdversary} class ExampleFedavg(Fedavg): @classmethod def get_default_config(cls) -> TrainerConfig: return ExampleFedavgConfig() if __name__ == "__main__": ray.init() config_dict = ( ExampleFedavgConfig() .resources( num_gpus_for_driver=0.0, num_cpus_for_driver=1, num_remote_workers=0, num_gpus_per_worker=0.0, ) .to_dict() ) print(config_dict) tune.run( ExampleFedavg, config=config_dict, stop=MaximumIterationStopper(100), ) .. _sphx_glr_download__examples_customize_attack.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: customize_attack.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: customize_attack.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: customize_attack.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_