-
Notifications
You must be signed in to change notification settings - Fork 25
/
model.py
130 lines (107 loc) · 4.82 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
import copy
import math
import torch
from graph_learners import *
from layers import GCNConv_dense, GCNConv_dgl, SparseDropout
from torch.nn import Sequential, Linear, ReLU
# GCN for evaluation.
class GCN(nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels, num_layers, dropout, dropout_adj, Adj, sparse):
super(GCN, self).__init__()
self.layers = nn.ModuleList()
if sparse:
self.layers.append(GCNConv_dgl(in_channels, hidden_channels))
for _ in range(num_layers - 2):
self.layers.append(GCNConv_dgl(hidden_channels, hidden_channels))
self.layers.append(GCNConv_dgl(hidden_channels, out_channels))
else:
self.layers.append(GCNConv_dense(in_channels, hidden_channels))
for i in range(num_layers - 2):
self.layers.append(GCNConv_dense(hidden_channels, hidden_channels))
self.layers.append(GCNConv_dense(hidden_channels, out_channels))
self.dropout = dropout
self.dropout_adj_p = dropout_adj
self.Adj = Adj
self.Adj.requires_grad = False
self.sparse = sparse
if self.sparse:
self.dropout_adj = SparseDropout(dprob=dropout_adj)
else:
self.dropout_adj = nn.Dropout(p=dropout_adj)
def forward(self, x):
if self.sparse:
Adj = copy.deepcopy(self.Adj)
Adj.edata['w'] = F.dropout(Adj.edata['w'], p=self.dropout_adj_p, training=self.training)
else:
Adj = self.dropout_adj(self.Adj)
for i, conv in enumerate(self.layers[:-1]):
x = conv(x, Adj)
x = F.relu(x)
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.layers[-1](x, Adj)
return x
class GraphEncoder(nn.Module):
def __init__(self, nlayers, in_dim, hidden_dim, emb_dim, proj_dim, dropout, dropout_adj, sparse):
super(GraphEncoder, self).__init__()
self.dropout = dropout
self.dropout_adj_p = dropout_adj
self.sparse = sparse
self.gnn_encoder_layers = nn.ModuleList()
if sparse:
self.gnn_encoder_layers.append(GCNConv_dgl(in_dim, hidden_dim))
for _ in range(nlayers - 2):
self.gnn_encoder_layers.append(GCNConv_dgl(hidden_dim, hidden_dim))
self.gnn_encoder_layers.append(GCNConv_dgl(hidden_dim, emb_dim))
else:
self.gnn_encoder_layers.append(GCNConv_dense(in_dim, hidden_dim))
for _ in range(nlayers - 2):
self.gnn_encoder_layers.append(GCNConv_dense(hidden_dim, hidden_dim))
self.gnn_encoder_layers.append(GCNConv_dense(hidden_dim, emb_dim))
if self.sparse:
self.dropout_adj = SparseDropout(dprob=dropout_adj)
else:
self.dropout_adj = nn.Dropout(p=dropout_adj)
self.proj_head = Sequential(Linear(emb_dim, proj_dim), ReLU(inplace=True),
Linear(proj_dim, proj_dim))
def forward(self,x, Adj_, branch=None):
if self.sparse:
if branch == 'anchor':
Adj = copy.deepcopy(Adj_)
else:
Adj = Adj_
Adj.edata['w'] = F.dropout(Adj.edata['w'], p=self.dropout_adj_p, training=self.training)
else:
Adj = self.dropout_adj(Adj_)
for conv in self.gnn_encoder_layers[:-1]:
x = conv(x, Adj)
x = F.relu(x)
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.gnn_encoder_layers[-1](x, Adj)
z = self.proj_head(x)
return z, x
class GCL(nn.Module):
def __init__(self, nlayers, in_dim, hidden_dim, emb_dim, proj_dim, dropout, dropout_adj, sparse):
super(GCL, self).__init__()
self.encoder = GraphEncoder(nlayers, in_dim, hidden_dim, emb_dim, proj_dim, dropout, dropout_adj, sparse)
def forward(self, x, Adj_, branch=None):
z, embedding = self.encoder(x, Adj_, branch)
return z, embedding
@staticmethod
def calc_loss(x, x_aug, temperature=0.2, sym=True):
batch_size, _ = x.size()
x_abs = x.norm(dim=1)
x_aug_abs = x_aug.norm(dim=1)
sim_matrix = torch.einsum('ik,jk->ij', x, x_aug) / torch.einsum('i,j->ij', x_abs, x_aug_abs)
sim_matrix = torch.exp(sim_matrix / temperature)
pos_sim = sim_matrix[range(batch_size), range(batch_size)]
if sym:
loss_0 = pos_sim / (sim_matrix.sum(dim=0) - pos_sim)
loss_1 = pos_sim / (sim_matrix.sum(dim=1) - pos_sim)
loss_0 = - torch.log(loss_0).mean()
loss_1 = - torch.log(loss_1).mean()
loss = (loss_0 + loss_1) / 2.0
return loss
else:
loss_1 = pos_sim / (sim_matrix.sum(dim=1) - pos_sim)
loss_1 = - torch.log(loss_1).mean()
return loss_1