-
Notifications
You must be signed in to change notification settings - Fork 72
/
sdcn.py
215 lines (169 loc) · 6.37 KB
/
sdcn.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
211
212
213
214
215
from __future__ import print_function, division
import argparse
import random
import numpy as np
from sklearn.cluster import KMeans
from sklearn.metrics.cluster import normalized_mutual_info_score as nmi_score
from sklearn.metrics import adjusted_rand_score as ari_score
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
from torch.optim import Adam
from torch.utils.data import DataLoader
from torch.nn import Linear
from utils import load_data, load_graph
from GNN import GNNLayer
from evaluation import eva
from collections import Counter
# torch.cuda.set_device(1)
class AE(nn.Module):
def __init__(self, n_enc_1, n_enc_2, n_enc_3, n_dec_1, n_dec_2, n_dec_3,
n_input, n_z):
super(AE, self).__init__()
self.enc_1 = Linear(n_input, n_enc_1)
self.enc_2 = Linear(n_enc_1, n_enc_2)
self.enc_3 = Linear(n_enc_2, n_enc_3)
self.z_layer = Linear(n_enc_3, n_z)
self.dec_1 = Linear(n_z, n_dec_1)
self.dec_2 = Linear(n_dec_1, n_dec_2)
self.dec_3 = Linear(n_dec_2, n_dec_3)
self.x_bar_layer = Linear(n_dec_3, n_input)
def forward(self, x):
enc_h1 = F.relu(self.enc_1(x))
enc_h2 = F.relu(self.enc_2(enc_h1))
enc_h3 = F.relu(self.enc_3(enc_h2))
z = self.z_layer(enc_h3)
dec_h1 = F.relu(self.dec_1(z))
dec_h2 = F.relu(self.dec_2(dec_h1))
dec_h3 = F.relu(self.dec_3(dec_h2))
x_bar = self.x_bar_layer(dec_h3)
return x_bar, enc_h1, enc_h2, enc_h3, z
class SDCN(nn.Module):
def __init__(self, n_enc_1, n_enc_2, n_enc_3, n_dec_1, n_dec_2, n_dec_3,
n_input, n_z, n_clusters, v=1):
super(SDCN, self).__init__()
# autoencoder for intra information
self.ae = AE(
n_enc_1=n_enc_1,
n_enc_2=n_enc_2,
n_enc_3=n_enc_3,
n_dec_1=n_dec_1,
n_dec_2=n_dec_2,
n_dec_3=n_dec_3,
n_input=n_input,
n_z=n_z)
self.ae.load_state_dict(torch.load(args.pretrain_path, map_location='cpu'))
# GCN for inter information
self.gnn_1 = GNNLayer(n_input, n_enc_1)
self.gnn_2 = GNNLayer(n_enc_1, n_enc_2)
self.gnn_3 = GNNLayer(n_enc_2, n_enc_3)
self.gnn_4 = GNNLayer(n_enc_3, n_z)
self.gnn_5 = GNNLayer(n_z, n_clusters)
# cluster layer
self.cluster_layer = Parameter(torch.Tensor(n_clusters, n_z))
torch.nn.init.xavier_normal_(self.cluster_layer.data)
# degree
self.v = v
def forward(self, x, adj):
# DNN Module
x_bar, tra1, tra2, tra3, z = self.ae(x)
sigma = 0.5
# GCN Module
h = self.gnn_1(x, adj)
h = self.gnn_2((1-sigma)*h + sigma*tra1, adj)
h = self.gnn_3((1-sigma)*h + sigma*tra2, adj)
h = self.gnn_4((1-sigma)*h + sigma*tra3, adj)
h = self.gnn_5((1-sigma)*h + sigma*z, adj, active=False)
predict = F.softmax(h, dim=1)
# Dual Self-supervised Module
q = 1.0 / (1.0 + torch.sum(torch.pow(z.unsqueeze(1) - self.cluster_layer, 2), 2) / self.v)
q = q.pow((self.v + 1.0) / 2.0)
q = (q.t() / torch.sum(q, 1)).t()
return x_bar, q, predict, z
def target_distribution(q):
weight = q**2 / q.sum(0)
return (weight.t() / weight.sum(1)).t()
def train_sdcn(dataset):
model = SDCN(500, 500, 2000, 2000, 500, 500,
n_input=args.n_input,
n_z=args.n_z,
n_clusters=args.n_clusters,
v=1.0).to(device)
print(model)
optimizer = Adam(model.parameters(), lr=args.lr)
# KNN Graph
adj = load_graph(args.name, args.k)
adj = adj.cuda()
# cluster parameter initiate
data = torch.Tensor(dataset.x).to(device)
y = dataset.y
with torch.no_grad():
_, _, _, _, z = model.ae(data)
kmeans = KMeans(n_clusters=args.n_clusters, n_init=20)
y_pred = kmeans.fit_predict(z.data.cpu().numpy())
y_pred_last = y_pred
model.cluster_layer.data = torch.tensor(kmeans.cluster_centers_).to(device)
eva(y, y_pred, 'pae')
for epoch in range(200):
if epoch % 1 == 0:
# update_interval
_, tmp_q, pred, _ = model(data, adj)
tmp_q = tmp_q.data
p = target_distribution(tmp_q)
res1 = tmp_q.cpu().numpy().argmax(1) #Q
res2 = pred.data.cpu().numpy().argmax(1) #Z
res3 = p.data.cpu().numpy().argmax(1) #P
eva(y, res1, str(epoch) + 'Q')
eva(y, res2, str(epoch) + 'Z')
eva(y, res3, str(epoch) + 'P')
x_bar, q, pred, _ = model(data, adj)
kl_loss = F.kl_div(q.log(), p, reduction='batchmean')
ce_loss = F.kl_div(pred.log(), p, reduction='batchmean')
re_loss = F.mse_loss(x_bar, data)
loss = 0.1 * kl_loss + 0.01 * ce_loss + re_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='train',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--name', type=str, default='reut')
parser.add_argument('--k', type=int, default=3)
parser.add_argument('--lr', type=float, default=1e-3)
parser.add_argument('--n_clusters', default=3, type=int)
parser.add_argument('--n_z', default=10, type=int)
parser.add_argument('--pretrain_path', type=str, default='pkl')
args = parser.parse_args()
args.cuda = torch.cuda.is_available()
print("use cuda: {}".format(args.cuda))
device = torch.device("cuda" if args.cuda else "cpu")
args.pretrain_path = 'data/{}.pkl'.format(args.name)
dataset = load_data(args.name)
if args.name == 'usps':
args.n_clusters = 10
args.n_input = 256
if args.name == 'hhar':
args.k = 5
args.n_clusters = 6
args.n_input = 561
if args.name == 'reut':
args.lr = 1e-4
args.n_clusters = 4
args.n_input = 2000
if args.name == 'acm':
args.k = None
args.n_clusters = 3
args.n_input = 1870
if args.name == 'dblp':
args.k = None
args.n_clusters = 4
args.n_input = 334
if args.name == 'cite':
args.lr = 1e-4
args.k = None
args.n_clusters = 6
args.n_input = 3703
print(args)
train_sdcn(dataset)