-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
145 lines (121 loc) · 5.1 KB
/
model.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
from utils import *
from layers import GraphConvolution, GraphSageConvolution
import autograd_wl
##########################################
##########################################
##########################################
class Net(nn.Module):
def __init__(self, nfeat, nhid, num_classes, layers, dropout, multi_class):
super(Net, self).__init__()
self.layers = layers
self.nhid = nhid
self.multi_class = multi_class
self.gcs = None
self.gc_out = None
self.relu = nn.ReLU()
self.dropout = nn.Dropout(dropout)
if multi_class:
self.loss_f = nn.BCEWithLogitsLoss()
self.loss_f_vec = nn.BCEWithLogitsLoss(reduction='none')
else:
self.loss_f = nn.CrossEntropyLoss()
self.loss_f_vec = nn.CrossEntropyLoss(reduction='none')
def forward(self, x, adjs):
for ell in range(len(self.gcs)):
x = self.gcs[ell](x, adjs[ell])
x = self.relu(x)
x = self.dropout(x)
x = self.gc_out(x)
return x
def partial_grad(self, x, adjs, targets, weight=None):
outputs = self.forward(x, adjs)
if weight is None:
loss = self.loss_f(outputs, targets)
else:
if self.multi_class:
loss = self.loss_f_vec(outputs, targets)
loss = loss.mean(1) * weight
else:
loss = self.loss_f_vec(outputs, targets) * weight
loss = loss.sum()
loss.backward()
return loss.detach()
def partial_grad_with_norm(self, x, adjs, targets, weight):
num_samples = targets.size(0)
outputs = self.forward(x, adjs)
if self.multi_class:
loss = self.loss_f_vec(outputs, targets)
loss = loss.mean(1) * weight
else:
loss = self.loss_f_vec(outputs, targets) * weight
loss = loss.sum()
loss.backward()
grad_per_sample = autograd_wl.calculate_sample_grad(self.gc_out)
grad_per_sample = grad_per_sample*(1/weight/num_samples)
return loss.detach(), grad_per_sample.cpu().numpy()
def calculate_sample_grad(self, x, adjs, targets, batch_nodes):
# use smart way
outputs = self.forward(x, adjs)
loss = self.loss_f(outputs, targets[batch_nodes])
loss.backward()
grad_per_sample = autograd_wl.calculate_sample_grad(self.gc_out)
return grad_per_sample.cpu().numpy()
def calculate_loss_grad(self, x, adjs, targets, batch_nodes):
outputs = self.forward(x, adjs)
loss = self.loss_f(outputs[batch_nodes], targets[batch_nodes])
loss.backward()
return loss.detach()
def calculate_f1(self, x, adjs, targets, batch_nodes):
outputs = self.forward(x, adjs)
if self.multi_class:
outputs[outputs > 0] = 1
outputs[outputs <= 0] = 0
else:
outputs = outputs.argmax(dim=1)
return f1_score(outputs[batch_nodes].cpu().detach(), targets[batch_nodes].cpu().detach(), average="micro")
"""
This is a plain implementation of GCN
Used for FastGCN, LADIES
"""
class GCN(Net):
def __init__(self, nfeat, nhid, num_classes, layers, dropout, multi_class):
super(Net, self).__init__()
self.layers = layers
self.nhid = nhid
self.multi_class = multi_class
self.gcs = nn.ModuleList()
self.gcs.append(GraphConvolution(nfeat, nhid))
for _ in range(layers-1):
self.gcs.append(GraphConvolution(nhid, nhid))
self.gc_out = nn.Linear(nhid, num_classes)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(dropout)
self.gc_out.register_forward_hook(autograd_wl.capture_activations)
self.gc_out.register_backward_hook(autograd_wl.capture_backprops)
if multi_class:
self.loss_f = nn.BCEWithLogitsLoss()
self.loss_f_vec = nn.BCEWithLogitsLoss(reduction='none')
else:
self.loss_f = nn.CrossEntropyLoss()
self.loss_f_vec = nn.CrossEntropyLoss(reduction='none')
class GraphSageGCN(Net):
def __init__(self, nfeat, nhid, num_classes, layers, dropout, multi_class):
super(Net, self).__init__()
self.layers = layers
self.nhid = nhid
self.multi_class = multi_class
self.gcs = nn.ModuleList()
self.gcs.append(GraphSageConvolution(nfeat, nhid, use_lynorm=False))
for _ in range(layers-1):
self.gcs.append(GraphSageConvolution(2*nhid, nhid, use_lynorm=False))
self.gc_out = nn.Linear(2*nhid, num_classes)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(dropout)
self.gc_out.register_forward_hook(autograd_wl.capture_activations)
self.gc_out.register_backward_hook(autograd_wl.capture_backprops)
if multi_class:
self.loss_f = nn.BCEWithLogitsLoss()
self.loss_f_vec = nn.BCEWithLogitsLoss(reduction='none')
else:
self.loss_f = nn.CrossEntropyLoss()
self.loss_f_vec = nn.CrossEntropyLoss(reduction='none')