gaussian.py 1.09 KB
Newer Older
Carl De Sousa Trias's avatar
Carl De Sousa Trias committed
1
2
3
4
5
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
from utils import *

def adding_noise(net, power, module_name):
    '''add gausian noise to the parameter of the network'''
    for name, parameters in net.named_parameters():
        if module_name in name:
            print("noise added")
            calcul = nn.utils.parameters_to_vector(parameters)
            sigma = torch.std(calcul, unbiased=False).item()
            noise = torch.normal(mean=0, std=power*sigma, size=parameters.size())
            parameters.data += noise.to(device)
    return net


def adding_noise_global(net, power):
    '''add gausian noise to the parameter of the network'''
    for name, module in net.named_modules():
        if isinstance(module, torch.nn.Conv2d) or isinstance(module, torch.nn.Linear):
            parameters=module.weight.data
            calcul = nn.utils.parameters_to_vector(parameters)
            sigma = torch.std(calcul, unbiased=False).item()
            noise = torch.normal(mean=0, std=power*sigma, size=parameters.size())
            parameters.data += noise.to(device)
    return net


'''
    M_ID=0
    param={'name':["features.17.w"],"S":5}
'''