-
Notifications
You must be signed in to change notification settings - Fork 23
/
define_models.py
executable file
·169 lines (139 loc) · 4.13 KB
/
define_models.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
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
164
165
166
167
168
169
import torch
import torch.nn as nn
import torch.nn.functional as F
class attrinf_attack_model(nn.Module):
def __init__(self, inputs, outputs):
super(attrinf_attack_model, self).__init__()
self.classifier = nn.Linear(inputs, outputs)
def forward(self, x):
x = torch.flatten(x, 1)
x = self.classifier(x)
return x
class ShadowAttackModel(nn.Module):
def __init__(self, class_num):
super(ShadowAttackModel, self).__init__()
self.Output_Component = nn.Sequential(
# nn.Dropout(p=0.2),
nn.Linear(class_num, 128),
nn.ReLU(),
nn.Linear(128, 64),
)
self.Prediction_Component = nn.Sequential(
# nn.Dropout(p=0.2),
nn.Linear(1, 128),
nn.ReLU(),
nn.Linear(128, 64),
)
self.Encoder_Component = nn.Sequential(
# nn.Dropout(p=0.2),
nn.Linear(128, 256),
nn.ReLU(),
# nn.Dropout(p=0.2),
nn.Linear(256, 128),
nn.ReLU(),
# nn.Dropout(p=0.2),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, 2),
)
def forward(self, output, prediction):
Output_Component_result = self.Output_Component(output)
Prediction_Component_result = self.Prediction_Component(prediction)
final_inputs = torch.cat((Output_Component_result, Prediction_Component_result), 1)
final_result = self.Encoder_Component(final_inputs)
return final_result
class PartialAttackModel(nn.Module):
def __init__(self, class_num):
super(PartialAttackModel, self).__init__()
self.Output_Component = nn.Sequential(
# nn.Dropout(p=0.2),
nn.Linear(class_num, 128),
nn.ReLU(),
nn.Linear(128, 64),
)
self.Prediction_Component = nn.Sequential(
# nn.Dropout(p=0.2),
nn.Linear(1, 128),
nn.ReLU(),
nn.Linear(128, 64),
)
self.Encoder_Component = nn.Sequential(
# nn.Dropout(p=0.2),
nn.Linear(128, 256),
nn.ReLU(),
# nn.Dropout(p=0.2),
nn.Linear(256, 128),
nn.ReLU(),
# nn.Dropout(p=0.2),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, 2),
)
def forward(self, output, prediction):
Output_Component_result = self.Output_Component(output)
Prediction_Component_result = self.Prediction_Component(prediction)
final_inputs = torch.cat((Output_Component_result, Prediction_Component_result), 1)
final_result = self.Encoder_Component(final_inputs)
return final_result
class WhiteBoxAttackModel(nn.Module):
def __init__(self, class_num, total):
super(WhiteBoxAttackModel, self).__init__()
self.Output_Component = nn.Sequential(
nn.Dropout(p=0.2),
nn.Linear(class_num, 128),
nn.ReLU(),
nn.Linear(128, 64),
)
self.Loss_Component = nn.Sequential(
nn.Dropout(p=0.2),
nn.Linear(1, 128),
nn.ReLU(),
nn.Linear(128, 64),
)
self.Gradient_Component = nn.Sequential(
nn.Dropout(p=0.2),
nn.Conv2d(1, 1, kernel_size=5, padding=2),
nn.BatchNorm2d(1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
nn.Flatten(),
nn.Dropout(p=0.2),
nn.Linear(total, 256),
nn.ReLU(),
nn.Dropout(p=0.2),
nn.Linear(256, 128),
nn.ReLU(),
nn.Linear(128, 64),
)
self.Label_Component = nn.Sequential(
nn.Dropout(p=0.2),
nn.Linear(class_num, 128),
nn.ReLU(),
nn.Linear(128, 64),
)
self.Encoder_Component = nn.Sequential(
nn.Dropout(p=0.2),
nn.Linear(256, 256),
nn.ReLU(),
nn.Dropout(p=0.2),
nn.Linear(256, 128),
nn.ReLU(),
nn.Dropout(p=0.2),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, 2),
)
def forward(self, output, loss, gradient, label):
Output_Component_result = self.Output_Component(output)
Loss_Component_result = self.Loss_Component(loss)
Gradient_Component_result = self.Gradient_Component(gradient)
Label_Component_result = self.Label_Component(label)
# Loss_Component_result = F.softmax(Loss_Component_result, dim=1)
# Gradient_Component_result = F.softmax(Gradient_Component_result, dim=1)
# final_inputs = Output_Component_result
# final_inputs = Loss_Component_result
# final_inputs = Gradient_Component_result
# final_inputs = Label_Component_result
final_inputs = torch.cat((Output_Component_result, Loss_Component_result, Gradient_Component_result, Label_Component_result), 1)
final_result = self.Encoder_Component(final_inputs)
return final_result