-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
57 lines (50 loc) · 1.6 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from train import device
from dgl.nn.pytorch import GraphConv
class VGAEModel(nn.Module):
def __init__(self, in_dim, hidden1_dim, hidden2_dim):
super(VGAEModel, self).__init__()
self.in_dim = in_dim
self.hidden1_dim = hidden1_dim
self.hidden2_dim = hidden2_dim
layers = [
GraphConv(
self.in_dim,
self.hidden1_dim,
activation=F.relu,
allow_zero_in_degree=True,
),
GraphConv(
self.hidden1_dim,
self.hidden2_dim,
activation=lambda x: x,
allow_zero_in_degree=True,
),
GraphConv(
self.hidden1_dim,
self.hidden2_dim,
activation=lambda x: x,
allow_zero_in_degree=True,
),
]
self.layers = nn.ModuleList(layers)
def encoder(self, g, features):
h = self.layers[0](g, features)
self.mean = self.layers[1](g, h)
self.log_std = self.layers[2](g, h)
gaussian_noise = torch.randn(features.size(0), self.hidden2_dim).to(
device
)
sampled_z = self.mean + gaussian_noise * torch.exp(self.log_std).to(
device
)
return sampled_z
def decoder(self, z):
adj_rec = torch.sigmoid(torch.matmul(z, z.t()))
return adj_rec
def forward(self, g, features):
z = self.encoder(g, features)
adj_rec = self.decoder(z)
return adj_rec