MCPcopy Create free account
hub / github.com/NineAbyss/ZeroG / GraphInfoMax

Class GraphInfoMax

code/model.py:152–202  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

150
151
152class GraphInfoMax(nn.Module):
153
154 EPS = 1e-15
155
156 def __init__(self, in_dim, emb_dim, num_layer, kernel='gcn', drop_ratio=0,
157 act='relu', norm=None):
158 super().__init__()
159
160 self.emd_dim = emb_dim
161
162 self.encoder = Encoder(in_dim, emb_dim, num_layer, kernel, drop_ratio, act, norm, concat=False, last_act=False)
163 # self.pred_head = LinearPred(emb_dim, emb_dim, 2, 2)
164
165 self.weight = nn.Parameter(torch.empty(emb_dim, emb_dim))
166 # just try
167 uniform(self.emd_dim, self.weight)
168 # self.weight = nn.Parameter(torch.empty(768, 768))
169 # uniform(768, self.weight)
170
171
172 def forward(self, x, edge_index, edge_weigt=None, batch=None):
173
174 pos_h = self.encoder(x, edge_index, edge_weigt, batch)
175
176 x_cor = infomax_corruption(x, batch)
177 neg_h = self.encoder(x_cor, edge_index, edge_weigt, batch)
178
179 summary = torch.sigmoid(pos_h.mean(dim=0))
180
181 return pos_h, neg_h, summary
182
183 def discriminate(self, h, summary):
184
185 summary = summary.t() if summary.dim() > 1 else summary
186 value = torch.matmul(h, torch.matmul(self.weight, summary))
187 return torch.sigmoid(value)
188
189 def get_loss(self, pos_h, neg_h, summary):
190
191 pos_loss = -torch.log(self.discriminate(pos_h, summary) + self.EPS).mean()
192 neg_loss = -torch.log(1 - self.discriminate(neg_h, summary) + self.EPS).mean()
193
194 return pos_loss + neg_loss
195
196
197 # def predict(self, x, edge_index, edge_weigt=None, batch=None):
198
199 # h, _, _ = self.forward(x, edge_index, edge_weigt, batch)
200 # pred = F.softmax(self.pred_head(h), dim=-1)
201
202 # return pred
203
204class GraphMAE(nn.Module):
205

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected