-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtenseal_model.py
More file actions
163 lines (128 loc) · 5.8 KB
/
Copy pathtenseal_model.py
File metadata and controls
163 lines (128 loc) · 5.8 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# hardened model of TenSEAL
import tenseal as ts
def load_ts_model(plain_model, data_name):
if data_name == "mnist":
return TS_FMNIST_Sigmoid(plain_model)
elif data_name == "fmnist":
return TS_FMNIST_Sigmoid(plain_model)
elif data_name == "mnistm":
return TS_MNISTM_Tanh(plain_model)
elif data_name == "cifar10":
return TS_CIFAR10_GeLU(plain_model)
elif data_name == "credit":
return TS_Credit_Sigmoid(plain_model)
elif data_name == "bank":
return TS_Bank_Tanh(plain_model)
else:
raise NotImplementedError(f"Not implement TS_{data_name.upper()}")
class TS_CIFAR10_GeLU:
def __init__(self, torch_nn):
self.fc2_weight = torch_nn.fc2.weight.T.data.tolist()
self.fc2_bias = torch_nn.fc2.bias.data.tolist()
self.fc3_weight = torch_nn.fc3.weight.T.data.tolist()
self.fc3_bias = torch_nn.fc3.bias.data.tolist()
self.partial = True
self.conv_tag = False
self.activation_degree = [5, 5]
self.activation_params = [[0, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 0]]
def forward(self, enc_x):
enc_x = enc_x.polyval(self.activation_params[0])
enc_x = enc_x.mm(self.fc2_weight) + self.fc2_bias
enc_x = enc_x.polyval(self.activation_params[1])
enc_x = enc_x.mm(self.fc3_weight) + self.fc3_bias
return enc_x
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
class TS_FMNIST_Sigmoid:
def __init__(self, torch_nn):
self.conv1_weight = torch_nn.conv1.weight.data.view(
torch_nn.conv1.out_channels, torch_nn.conv1.kernel_size[0],
torch_nn.conv1.kernel_size[1]
).tolist()
self.conv1_bias = torch_nn.conv1.bias.data.tolist()
self.fc1_weight = torch_nn.fc1.weight.T.data.tolist()
self.fc1_bias = torch_nn.fc1.bias.data.tolist()
self.fc2_weight = torch_nn.fc2.weight.T.data.tolist()
self.fc2_bias = torch_nn.fc2.bias.data.tolist()
self.partial = False
self.conv_tag = True
self.activation_degree = [3, 3]
self.activation_params = [[0.5, 0.197, 0, -0.004],
[0.5, 0.197, 0, -0.004]]
def forward(self, enc_x, windows_nb):
enc_channels = []
for kernel, bias in zip(self.conv1_weight, self.conv1_bias):
y = enc_x.conv2d_im2col(kernel, windows_nb) + bias
enc_channels.append(y)
enc_x = ts.CKKSVector.pack_vectors(enc_channels)
enc_x = enc_x.polyval(self.activation_params[0])
enc_x = enc_x.mm(self.fc1_weight) + self.fc1_bias
enc_x = enc_x.polyval(self.activation_params[1])
enc_x = enc_x.mm(self.fc2_weight) + self.fc2_bias
return enc_x
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
class TS_MNISTM_Tanh:
def __init__(self, torch_nn):
self.fc1_weight = torch_nn.fc1.weight.T.data.tolist()
self.fc1_bias = torch_nn.fc1.bias.data.tolist()
self.fc2_weight = torch_nn.fc2.weight.T.data.tolist()
self.fc2_bias = torch_nn.fc2.bias.data.tolist()
self.partial = True
self.conv_tag = False
self.activation_degree = [4, 4]
self.activation_params = [[0.5, 0.197, 0, -0.004, 0],
[0.5, 0.197, 0, -0.004, 0]]
def forward(self, enc_x):
enc_x = enc_x.polyval(self.activation_params[0])
enc_x = enc_x.mm(self.fc1_weight) + self.fc1_bias
enc_x = enc_x.polyval(self.activation_params[1])
enc_x = enc_x.mm(self.fc2_weight) + self.fc2_bias
return enc_x
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
class TS_Credit_Sigmoid:
def __init__(self, torch_nn):
self.fc1_weight = torch_nn.fc1.weight.T.data.tolist()
self.fc1_bias = torch_nn.fc1.bias.data.tolist()
self.fc2_weight = torch_nn.fc2.weight.T.data.tolist()
self.fc2_bias = torch_nn.fc2.bias.data.tolist()
self.fc3_weight = torch_nn.fc3.weight.T.data.tolist()
self.fc3_bias = torch_nn.fc3.bias.data.tolist()
self.partial = False
self.conv_tag = False
self.activation_degree = [4, 4]
self.activation_params = [[0.5, 0.197, 0, -0.004, 0],
[0.5, 0.197, 0, -0.004, 0]]
def forward(self, enc_x):
enc_x = enc_x.mm(self.fc1_weight) + self.fc1_bias
enc_x = enc_x.polyval(self.activation_params[0])
enc_x = enc_x.mm(self.fc2_weight) + self.fc2_bias
enc_x = enc_x.polyval(self.activation_params[1])
enc_x = enc_x.mm(self.fc3_weight) + self.fc3_bias
return enc_x
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
class TS_Bank_Tanh:
def __init__(self, torch_nn):
self.fc1_weight = torch_nn.fc1.weight.T.data.tolist()
self.fc1_bias = torch_nn.fc1.bias.data.tolist()
self.fc2_weight = torch_nn.fc2.weight.T.data.tolist()
self.fc2_bias = torch_nn.fc2.bias.data.tolist()
self.fc3_weight = torch_nn.fc3.weight.T.data.tolist()
self.fc3_bias = torch_nn.fc3.bias.data.tolist()
self.partial = False
self.conv_tag = False
self.activation_degree = [4, 4]
self.activation_params = [[0.5, 0.197, 0, -0.004, 0],
[0.5, 0.197, 0, -0.004, 0]]
def forward(self, enc_x):
enc_x = enc_x.mm(self.fc1_weight) + self.fc1_bias
enc_x = enc_x.polyval(self.activation_params[0])
enc_x = enc_x.mm(self.fc2_weight) + self.fc2_bias
enc_x = enc_x.polyval(self.activation_params[1])
enc_x = enc_x.mm(self.fc3_weight) + self.fc3_bias
return enc_x
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)