Skip to content

Documentation for Noiseinjection Module

NoiseInjectionAttack

Bases: ModelAttack

Implements a noise injection attack on the received model weights.

This attack introduces noise into the model weights by adding random values scaled by a specified strength, potentially disrupting the model’s behavior.

Parameters:

Name Type Description Default
engine object

The training engine object that manages the aggregator.

required
attack_params dict

Parameters for the attack, including: - strength (int): The strength of the noise to be injected into the weights.

required
Source code in nebula/addons/attacks/model/noiseinjection.py
 6
 7
 8
 9
10
11
12
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
class NoiseInjectionAttack(ModelAttack):
    """
    Implements a noise injection attack on the received model weights.

    This attack introduces noise into the model weights by adding random values 
    scaled by a specified strength, potentially disrupting the model’s behavior.

    Args:
        engine (object): The training engine object that manages the aggregator.
        attack_params (dict): Parameters for the attack, including:
            - strength (int): The strength of the noise to be injected into the weights.
    """
    def __init__(self, engine, attack_params):
        """
        Initializes the NoiseInjectionAttack with the specified engine and parameters.

        Args:
            engine (object): The training engine object.
            attack_params (dict): Dictionary of attack parameters, including strength.
        """
        super().__init__(engine)
        self.strength = int(attack_params["strength"])
        self.round_start_attack = int(attack_params["round_start_attack"])
        self.round_stop_attack = int(attack_params["round_stop_attack"])

    def model_attack(self, received_weights):
        """
        Performs the noise injection attack by adding random noise to the model weights.

        The noise is generated from a normal distribution and scaled by the 
        specified strength, modifying each layer's weights in the model.

        Args:
            received_weights (dict): The aggregated model weights to be modified.

        Returns:
            dict: The modified model weights after applying the noise injection attack.
        """
        logging.info(f"[NoiseInjectionAttack] Performing noise injection attack with a strength of {self.strength}")
        lkeys = list(received_weights.keys())
        for k in lkeys:
            logging.info(f"Layer noised: {k}")
            received_weights[k].data += torch.randn(received_weights[k].shape) * self.strength
        return received_weights

__init__(engine, attack_params)

Initializes the NoiseInjectionAttack with the specified engine and parameters.

Parameters:

Name Type Description Default
engine object

The training engine object.

required
attack_params dict

Dictionary of attack parameters, including strength.

required
Source code in nebula/addons/attacks/model/noiseinjection.py
18
19
20
21
22
23
24
25
26
27
28
29
def __init__(self, engine, attack_params):
    """
    Initializes the NoiseInjectionAttack with the specified engine and parameters.

    Args:
        engine (object): The training engine object.
        attack_params (dict): Dictionary of attack parameters, including strength.
    """
    super().__init__(engine)
    self.strength = int(attack_params["strength"])
    self.round_start_attack = int(attack_params["round_start_attack"])
    self.round_stop_attack = int(attack_params["round_stop_attack"])

model_attack(received_weights)

Performs the noise injection attack by adding random noise to the model weights.

The noise is generated from a normal distribution and scaled by the specified strength, modifying each layer's weights in the model.

Parameters:

Name Type Description Default
received_weights dict

The aggregated model weights to be modified.

required

Returns:

Name Type Description
dict

The modified model weights after applying the noise injection attack.

Source code in nebula/addons/attacks/model/noiseinjection.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def model_attack(self, received_weights):
    """
    Performs the noise injection attack by adding random noise to the model weights.

    The noise is generated from a normal distribution and scaled by the 
    specified strength, modifying each layer's weights in the model.

    Args:
        received_weights (dict): The aggregated model weights to be modified.

    Returns:
        dict: The modified model weights after applying the noise injection attack.
    """
    logging.info(f"[NoiseInjectionAttack] Performing noise injection attack with a strength of {self.strength}")
    lkeys = list(received_weights.keys())
    for k in lkeys:
        logging.info(f"Layer noised: {k}")
        received_weights[k].data += torch.randn(received_weights[k].shape) * self.strength
    return received_weights