-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_fmnist.py
225 lines (181 loc) · 8 KB
/
model_fmnist.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import torch
import torch.nn as nn
import torch.nn.functional as F
from common_DNNTM import Config
import numpy as np
import torch
import copy
from resnet import *
import math
#torch.cuda.set_device(Config.device_id)
class left_neural_net(nn.Module):
def __init__(self,nclass=Config.num_classes):
super(left_neural_net, self).__init__()
self.backbone = ResNet18_F(nclass)
def forward(self, x):
h,_ = self.backbone(x)
return torch.nn.functional.softmax(h,dim=-1)
class left_neural_aux_net_one_source(nn.Module):
def __init__(self,num_class=Config.num_classes, fea_dim=512,linear_dim=64):
super(left_neural_aux_net_one_source, self).__init__()
self.num_class = num_class
self.backbone_for_NT = ResNet18_F(num_class)
self.linear_1 = nn.Linear(fea_dim,linear_dim)
self.fea_to_NT_layer = fea_to_NT(num_class=num_class, linear_dim=linear_dim)
#self.bias=nn.Parameter(torch.zeros((num_class,num_class),requires_grad=True))
def forward(self, x):
_,x=self.backbone_for_NT(x)
x = F.relu(self.linear_1(x))
noise_matrices_for_x = self.fea_to_NT_layer(x)
return noise_matrices_for_x
def copy_backbone(self, param_dict,param_dict2):
self.backbone_for_NT.load_state_dict(param_dict)
self.linear_1.load_state_dict(param_dict2)
return
def copy_NT_layer(self, param_dict):
self.fea_to_NT_layer.load_state_dict(param_dict)
return
# def copy_bias(self, param_dict):
# self.bias.data = param_dict
# self.bias.requires_grad = True
# return
class left_neural_aux_net_multi_source_with_fea_to_NT_layers(nn.Module):
def __init__(self,num_class=Config.num_classes, fea_dim=512, worker_num=Config.expert_num, linear_dim=64):
super(left_neural_aux_net_multi_source_with_fea_to_NT_layers, self).__init__()
self.num_class = num_class
self.worker_num = worker_num
self.backbone_for_NT = ResNet18_F(num_class)
self.linear_1 = nn.Linear(fea_dim,linear_dim)
for r in range(self.worker_num):
m_name = "worker"+str(r)
self.add_module(m_name,fea_to_NT(num_class=num_class, linear_dim=linear_dim))
#self.bias=nn.Parameter(torch.zeros((num_class,num_class),requires_grad=True))
def forward(self, x, worker_id=-1, no_grad=False):
if(no_grad):
with torch.no_grad():
_,x=self.backbone_for_NT(x)
x=F.relu(self.linear_1(x))
else:
_,x=self.backbone_for_NT(x)
x=F.relu(self.linear_1(x))
if(worker_id<0):
noise_matrices_for_x = torch.zeros((x.size(0),self.worker_num,self.num_class,self.num_class)).cuda()
for r in range(self.worker_num):
m_name = "worker"+str(r)
module = getattr(self,m_name)
noise_matrices_for_x[:,r,:,:]= module(x)#,self.bias)
return noise_matrices_for_x
else:
m_name = "worker"+str(worker_id)
module = getattr(self,m_name)
noise_matrices_for_x= module(x)#,self.bias)
return noise_matrices_for_x
def copy_backbone(self, param_dict,param_dict2):
self.backbone_for_NT.load_state_dict(param_dict)
self.linear_1.load_state_dict(param_dict2)
return
def copy_NT_layer(self, param_dict):
for r in range(self.worker_num):
m_name = "worker"+str(r)
module = getattr(self,m_name)
module.load_state_dict(param_dict)
return
def get_NT_layer(self, worker_id):
m_name = "worker"+str(worker_id)
module = getattr(self,m_name)
return module
# def copy_bias(self, param_dict):
# self.bias.data = param_dict
# self.bias.requires_grad = True
# return
class fea_to_NT(nn.Module):
def __init__(self,num_class=Config.num_classes, fea_dim=512, linear_dim=64):
super(fea_to_NT, self).__init__()
self.num_class=num_class
self.to_NT=nn.Linear(linear_dim, num_class*num_class, bias=True)
def forward(self, x, no_softmax=False):
noise_matrices_for_x= self.to_NT(x).view(-1,self.num_class,self.num_class)
if(no_softmax):
return noise_matrices_for_x
noise_matrices_for_x= F.softmax(noise_matrices_for_x.cuda(), dim=-1)
return noise_matrices_for_x
class GraphConvolution(nn.Module):
"""
Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
"""
def __init__(self, in_features, out_features, bias=False):
super(GraphConvolution, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = nn.Parameter(torch.Tensor(in_features, out_features))
if bias:
self.bias = nn.Parameter(torch.Tensor(1, 1, out_features))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input, adj):
support = torch.matmul(input, self.weight)
output = torch.matmul(adj, support)
if self.bias is not None:
return output + self.bias
else:
return output
def __repr__(self):
return self.__class__.__name__ + ' (' \
+ str(self.in_features) + ' -> ' \
+ str(self.out_features) + ')'
class left_neural_aux_net_multi_source_with_gcn(nn.Module):
def __init__(self, emb, adj, num_class=Config.num_classes,fea_dim=512, worker_num=Config.expert_num, linear_dim=64):
super(left_neural_aux_net_multi_source_with_gcn, self).__init__()
self.num_class = num_class
self.worker_num = worker_num
self.backbone_for_NT = ResNet18_F(num_class)
self.linear_1 = nn.Linear(fea_dim,linear_dim)
self.fea_to_NT_layer = fea_to_NT(num_class=num_class, linear_dim=linear_dim)
#self.bias=nn.Parameter(torch.zeros((num_class,num_class),requires_grad=True))
self.linear_dim = linear_dim
self.gc1 = GraphConvolution(worker_num, 64)
self.relu = nn.LeakyReLU(0.2)
self.gc2 = GraphConvolution(64, linear_dim*num_class*num_class+num_class*num_class)
#self.linear_2 = nn.Linear(128,linear_dim*num_class*num_class)
#self.gc3 = GraphConvolution(128, linear_dim*num_class*num_class)
self.emb=emb.float()
self.adj=self.gen_adj(adj.float())
def forward(self, x, no_grad=False):
if(no_grad):
with torch.no_grad():
_,x=self.backbone_for_NT(x)
x=F.relu(self.linear_1(x))
else:
_,x=self.backbone_for_NT(x)
x=F.relu(self.linear_1(x))
base_noise_matrices_for_x = self.fea_to_NT_layer(x,no_softmax=True).unsqueeze(1)
f = self.gc1(self.emb, self.adj)
f = self.relu(f)
f = self.gc2(f, self.adj)
f = f.reshape(self.worker_num, self.linear_dim+1, self.num_class,self.num_class)
f = torch.einsum("rijk,ni->nrjk",(f[:,:-1],x))+f[:,-1].unsqueeze(0)
f = f #+ base_noise_matrices_for_x
noise_matrices_for_x = F.softmax(f, dim=-1)
return noise_matrices_for_x
def copy_backbone(self, param_dict,param_dict2):
self.backbone_for_NT.load_state_dict(param_dict)
self.linear_1.load_state_dict(param_dict2)
return
# def copy_bias(self, param_dict):
# self.bias.data = param_dict
# self.bias.requires_grad = True
# return
def copy_NT_layer(self, param_dict):
self.fea_to_NT_layer.load_state_dict(param_dict)
return
def gen_adj(self, A):
D = torch.pow(A.sum(1).float(), -0.5)
D = torch.diag(D)
adj = torch.matmul(torch.matmul(A, D).t(), D)
return adj