-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgraph_autoencoder.py
151 lines (114 loc) · 5.04 KB
/
graph_autoencoder.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
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
from torch.nn import init
import torch.nn.functional as F
import numpy as np
from torch_geometric.nn import GINConv, global_add_pool
class GraphConv(nn.Module):
def __init__(self, input_dim, output_dim,
dropout=0.0, bias=True):
super(GraphConv, self).__init__()
self.add_self = True
self.dropout = dropout
if dropout > 0.001:
self.dropout_layer = nn.Dropout(p=dropout)
self.normalize_embedding = True
self.input_dim = input_dim
self.output_dim = output_dim
self.weight = nn.Parameter(torch.FloatTensor(input_dim, output_dim).cuda())
if bias:
self.bias = nn.Parameter(torch.FloatTensor(output_dim).cuda())
else:
self.bias = None
def forward(self, x, adj):
y = torch.matmul(adj, x)
y = torch.matmul(y,self.weight)
if self.bias is not None:
y = y + self.bias
return y
class Encoder(nn.Module):
def __init__(self, feat_size,hiddendim,outputdim,dropout,batch):
super(Encoder, self).__init__()
self.gc1 = nn.Linear(feat_size, hiddendim, bias=False)
#self.gc2 = nn.Linear(hiddendim*2, hiddendim*2, bias=False)
#self.gc3 = nn.Linear(hiddendim*2, hiddendim, bias=False)
self.gc4 = nn.Linear(hiddendim, outputdim, bias=False)
self.proj_head = nn.Sequential(nn.Linear(outputdim, outputdim), nn.ReLU(inplace=True), nn.Linear(outputdim, outputdim))
self.leaky_relu = nn.LeakyReLU(0.5)
self.dropout = nn.Dropout(dropout)
self.batch=batch
def forward(self, x, adj):
x = self.leaky_relu(self.gc1(torch.matmul(adj, x)))
x=self.dropout(x)
x = self.gc4(torch.matmul(adj, x))
out, _ = torch.max(x, dim=1)
#out = global_add_pool(x,self.batch)
out=self.proj_head(out)
return x,out
class attr_Decoder(nn.Module):
def __init__(self, feat_size,hiddendim,outputdim,dropout):
super(attr_Decoder, self).__init__()
self.gc1 = nn.Linear(outputdim, hiddendim, bias=False)
#self.gc2 = nn.Linear(hiddendim, hiddendim*2, bias=False)
#self.gc3 = nn.Linear(hiddendim*2, hiddendim*2, bias=False)
self.gc4 = nn.Linear(hiddendim, feat_size, bias=False)
self.leaky_relu = nn.LeakyReLU(0.5)
self.dropout = nn.Dropout(dropout)
def forward(self, x, adj):
x = self.leaky_relu(self.gc1(torch.matmul(adj, x)))
x=self.dropout(x)
x = self.gc4(torch.matmul(adj, x))
return x
class stru_Decoder(nn.Module):
def __init__(self, feat_size,outputdim,dropout):
super(stru_Decoder, self).__init__()
#self.gc1 = nn.Linear(outputdim, outputdim, bias=False)
self.sigmoid = nn.Sigmoid()
#self.dropout = nn.Dropout(dropout)
def forward(self, x, adj):
x1=x.permute(0, 2, 1)
x = torch.matmul(x,x1)
x=self.sigmoid(x)
return x
class NetGe(nn.Module):
def __init__(self, feat_size, hiddendim, outputdim, dropout,batch):
super(NetGe, self).__init__()
self.shared_encoder = Encoder(feat_size, hiddendim, outputdim, dropout,batch)
self.attr_decoder = attr_Decoder(feat_size, hiddendim, outputdim, dropout)
self.struct_decoder = stru_Decoder(feat_size, outputdim, dropout)
def forward(self, x, adj):
x_fake= self.attr_decoder(x, adj)
s_fake = self.struct_decoder(x, adj)
x2,Feat_1=self.shared_encoder(x_fake, s_fake)
return x_fake,s_fake,x2,Feat_1
class NetDe(nn.Module):
def __init__(self, feat_size, hiddendim, outputdim, dropout,batch):
super(NetDe, self).__init__()
#self.gc1 = nn.Linear(feat_size, hiddendim, bias=False)
#self.gc2 = nn.Linear(hiddendim, outputdim, bias=False)
self.shared_encoder = Encoder(feat_size, hiddendim, outputdim, dropout,batch)
self.leaky_relu = nn.LeakyReLU(0.5)
self.dropout = nn.Dropout(dropout)
self.weight = nn.Parameter(torch.FloatTensor(outputdim, 1).cuda())
init.xavier_uniform_(self.weight)
self.m=nn.Sigmoid()
def apply_bn(self, x):
''' Batch normalization of 3D tensor x
'''
bn_module = nn.BatchNorm1d(x.size()[1]).cuda()
return bn_module(x)
def forward(self, x, adj):
# encode
#x = self.leaky_relu(self.gc1(torch.matmul(adj, x)))
#x=self.dropout(x)
#x = self.apply_bn(x)
#x = self.gc2(torch.matmul(adj, x))
#x = self.apply_bn(x)
#Feat = torch.mean(x,dim=1).squeeze(1)
x,Feat = self.shared_encoder(x, adj)
out_emb=torch.mm(Feat,self.weight)
out_emb=self.dropout(out_emb)
pred=self.m(out_emb)
pred=pred.view(-1, 1).squeeze(1)
return pred,Feat