-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoise_attack.py
More file actions
38 lines (30 loc) · 1.47 KB
/
noise_attack.py
File metadata and controls
38 lines (30 loc) · 1.47 KB
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
31
32
33
34
35
36
37
38
import numpy as np
"""
Add noise to data based on multiplier or epsilon
Args:
data (np.ndarray): data to noise, in two dimensional np.array of signals e.g. (6,100) for the six signals and 100 data points each.
noise_multiplier (float): wanted noise_multipier for the random distribution.
noise_type (str of "laplace" or "gaussian"): wanted noise distribution.
clip_max (bool): clipping to max of each signal for dp, if false clip to 1 as sensitity of similarity func.
Returns:
np.ndarray: noised data.
"""
def create_noisy_data(data: np.ndarray, noise_multiplier: float=None, noise_type: str="laplace", clip_max: bool=False):
noisy_data = np.empty(data.shape)
for idx, signal_data in enumerate(data):
clip = 1 if not clip_max else np.max(signal_data) # 1 as sensitity of similarity func or max of each signal as clip for DP
if noise_type == "laplace":
noisy_data[idx] = signal_data + np.random.laplace(scale=clip*noise_multiplier, size=signal_data.shape)
elif noise_type == "gaussian":
noisy_data[idx] = signal_data + np.random.normal(scale=clip*noise_multiplier, size=signal_data.shape)
return noisy_data
def main():
noise_multiplier = 100.0
data = np.random.rand(6,100)
noisy_data = create_noisy_data(data, noise_multiplier)
diff = noisy_data[0][:100] - data[0][:100]
print("diff: ", diff)
print("min: ", diff.min())
print("max: ", diff.max())
if __name__ == "__main__":
main()