-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbaseLineModel.py
210 lines (162 loc) · 6.56 KB
/
baseLineModel.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
import os.path as osp
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import torch
import torch.nn.functional as F
from torch.nn import Sequential, Linear, ReLU, GRU
import torch_geometric.transforms as T
# from torch_geometric.datasets import QM9
from torch_geometric.nn import GCNConv,global_add_pool,ChebConv,global_max_pool
from torch_geometric.data import DataLoader
from torch_geometric.utils import remove_self_loops
from torch_geometric.data import Dataset, Data, DataLoader
from infomax import *
class JavaEncoder(torch.nn.Module):
def __init__(self, num_features, dim):
super(JavaEncoder, self).__init__()
self.lin0 = torch.nn.Linear(num_features, dim)
nn = Sequential(Linear(5, 128), ReLU(), Linear(128, dim * dim))
self.conv = ChebConv(dim, dim, K=2,normalization='sym')
# self.gru = GRU(dim, dim)
# self.set2set = Set2Set(dim, processing_steps=3)
def forward(self, data,batch):
out = F.relu(self.lin0(data.x2.float()))
feat_map = []
# for i in range(3):
# out = F.relu(self.conv(out, data.edge_index2.long()))
# out, h = self.gru(m.unsqueeze(0), h)
# out = out.squeeze(0)
# print(out.shape) : [num_node x dim]
# out = Node2Vec(data.edge_index2.long(), embedding_dim=128, walk_length=20,context_size=10, walks_per_node=10,num_negative_samples=1, p=1, q=1, sparse=True)
out = F.relu(self.conv(out, data.edge_index2.long()))
feat_map.append(out)
out = global_add_pool(out, batch)
# print(out.shape)
return out, feat_map[-1]
class PyEncoder(torch.nn.Module):
def __init__(self, num_features, dim):
super(PyEncoder, self).__init__()
self.lin0 = torch.nn.Linear(num_features, dim)
nn = Sequential(Linear(5, 128), ReLU(), Linear(128, dim * dim))
self.conv = ChebConv(dim, dim, K=2,normalization='sym')
# self.gru = GRU(dim, dim)
# self.set2set = Set2Set(dim, processing_steps=3)
def forward(self, data,batch):
out = F.relu(self.lin0(data.x1.float()))
feat_map = []
# for i in range(3):
# out = F.relu(self.conv(out, data.edge_index1.long()))
# out, h = self.gru(m.unsqueeze(0), h)
# out = out.squeeze(0)
# print(out.shape) : [num_node x dim]
# out = Node2Vec(data.edge_index1.long(), embedding_dim=128, walk_length=20,context_size=10, walks_per_node=10,num_negative_samples=1, p=1, q=1, sparse=True)
out = F.relu(self.conv(out, data.edge_index1.long()))
feat_map.append(out)
out = global_add_pool(out, batch)
# print(out.shape)
return out, feat_map[-1]
##### REMOVED FROM THE MAIN SCRIPT
# class PriorDiscriminator(nn.Module):
# def __init__(self, input_dim):
# super().__init__()
# self.l0 = nn.Linear(input_dim, input_dim)
# self.l1 = nn.Linear(input_dim, input_dim)
# self.l2 = nn.Linear(input_dim, 1)
# def forward(self, x):
# h = F.relu(self.l0(x))
# h = F.relu(self.l1(h))
# return torch.sigmoid(self.l2(h))
class FF(nn.Module):
def __init__(self, input_dim, dim):
super().__init__()
self.block = nn.Sequential(
nn.Linear(input_dim, dim),
nn.ReLU(),
nn.Linear(dim, dim),
nn.ReLU(),
nn.Linear(dim, dim),
nn.ReLU()
)
self.linear_shortcut = nn.Linear(input_dim, dim)
def forward(self, x):
return self.block(x) + self.linear_shortcut(x)
class Net(torch.nn.Module):
def __init__(self, num_features, dim, use_unsup_loss=False, separate_encoder=False):
super(Net, self).__init__()
self.embedding_dim = dim
self.separate_encoder = separate_encoder
# self.local = True
# self.prior = False
# java encoder
self.encoder2 = JavaEncoder(num_features, dim)
# python encoder
self.encoder1 = PyEncoder(num_features, dim)
# FCs after concat
# self.fc1 = torch.nn.Linear(2 * dim, dim)
# self.fc2 = torch.nn.Linear(dim, 1)
self.fc2 = torch.nn.Linear(2*dim, 1)
self.init_emb()
def init_emb(self):
initrange = -1.5 / self.embedding_dim
for m in self.modules():
if isinstance(m, nn.Linear):
torch.nn.init.xavier_uniform_(m.weight.data)
if m.bias is not None:
m.bias.data.fill_(0.0)
def forward(self, data):
# print(data)
out1, M1 = self.encoder1(data,data.x1_batch)
out2, M2 = self.encoder2(data, data.x2_batch)
# print(out1.shape)
# print(out2.shape)
concatenatedEmb = torch.cat((out1,out2),dim=1)
# print(concatenatedEmb.shape)
# concatenatedEmb = F.relu(self.fc1(concatenatedEmb))
out = self.fc2(concatenatedEmb)
pred = out.view(-1)
return pred
def unsup_loss1(self, data,batch):
if self.separate_encoder:
y, M = self.unsup_encoder1(data,batch)
else:
y, M = self.encoder1(data,batch)
g_enc = self.global_d1(y)
l_enc = self.local_d1(M)
measure = 'JSD'
if self.local:
loss = local_global_loss_(l_enc, g_enc, data.edge_index1.long(),batch, measure)
return loss
def unsup_loss2(self, data,batch):
if self.separate_encoder:
y, M = self.unsup_encoder2(data,batch)
else:
y, M = self.encoder2(data,batch)
g_enc = self.global_d2(y)
l_enc = self.local_d2(M)
measure = 'JSD'
if self.local:
loss = local_global_loss_(l_enc, g_enc, data.edge_index2.long(),batch, measure)
return loss
def unsup_sup_loss1(self, data,batch):
y, M = self.encoder1(data,batch)
y_, M_ = self.unsup_encoder1(data,batch)
g_enc = self.ff11(y)
g_enc1 = self.ff12(y_)
measure = 'JSD'
loss = global_global_loss_(g_enc, g_enc1, data.edge_index1.long(), batch, measure)
return loss
def unsup_sup_loss2(self, data,batch):
y, M = self.encoder2(data,batch)
y_, M_ = self.unsup_encoder2(data,batch)
g_enc = self.ff21(y)
g_enc1 = self.ff22(y_)
measure = 'JSD'
loss = global_global_loss_(g_enc, g_enc1, data.edge_index2.long(),batch, measure)
return loss
# def align_unsup_sup_loss(self, data):
# y, M = self.encoder(data)
# y_, M_ = self.unsup_encoder(data)
# return F.mse_loss(y, y_)