forked from eth-sri/dp-sniper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel.py
54 lines (41 loc) · 1.47 KB
/
parallel.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from typing import List
from dpsniper.mechanisms.abstract import Mechanism
from dpsniper.mechanisms.laplace import LaplaceMechanism
from dpsniper.mechanisms.sparse_vector_technique import *
import numpy as np
class ParallelMechanism(Mechanism):
"""
A wrapper class running multiple mechanisms in parallel and returning all outputs.
"""
def __init__(self, mechanisms: List[Mechanism]):
self.mechanisms = mechanisms
def m(self, a, n_samples: int =1):
b_tup = ()
for mech in self.mechanisms:
b = mech.m(a, n_samples)
if len(b.shape) == 1:
b = np.atleast_2d(b).T
b_tup = b_tup + (b, )
all_b = np.column_stack(b_tup)
return all_b
def get_n_parallel(self):
return len(self.mechanisms)
class LaplaceParallel(ParallelMechanism):
"""
Running multiple instances of LaplaceMechanism in parallel.
"""
def __init__(self, n_parallel: int, eps: float = 0.1):
mechanisms = []
for i in range(0, n_parallel):
mechanisms.append(LaplaceMechanism(eps=eps))
super().__init__(mechanisms)
class SVT34Parallel(ParallelMechanism):
"""
Running SparseVectorTechnique3 and SparseVectorTechnique4 in parallel.
"""
def __init__(self, eps=0.1, c=2, t=1):
mechanisms = [
SparseVectorTechnique3(eps=eps, c=c, t=t),
SparseVectorTechnique4(eps=eps, c=c, t=t)
]
super().__init__(mechanisms)