-
Notifications
You must be signed in to change notification settings - Fork 27
/
idec.py
256 lines (204 loc) · 7.4 KB
/
idec.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*- coding: utf-8 -*-
#
# Copyright © dawnranger.
#
# 2018-05-08 10:15 <dawnranger123@gmail.com>
#
# Distributed under terms of the MIT license.
from __future__ import print_function, division
import argparse
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 MnistDataset, cluster_acc
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__()
# encoder
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)
# decoder
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):
# encoder
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)
# decoder
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, z
class IDEC(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,
alpha=1,
pretrain_path='data/ae_mnist.pkl'):
super(IDEC, self).__init__()
self.alpha = 1.0
self.pretrain_path = pretrain_path
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)
# cluster layer
self.cluster_layer = Parameter(torch.Tensor(n_clusters, n_z))
torch.nn.init.xavier_normal_(self.cluster_layer.data)
def pretrain(self, path=''):
if path == '':
pretrain_ae(self.ae)
# load pretrain weights
self.ae.load_state_dict(torch.load(self.pretrain_path))
print('load pretrained ae from', path)
def forward(self, x):
x_bar, z = self.ae(x)
# cluster
q = 1.0 / (1.0 + torch.sum(
torch.pow(z.unsqueeze(1) - self.cluster_layer, 2), 2) / self.alpha)
q = q.pow((self.alpha + 1.0) / 2.0)
q = (q.t() / torch.sum(q, 1)).t()
return x_bar, q
def target_distribution(q):
weight = q**2 / q.sum(0)
return (weight.t() / weight.sum(1)).t()
def pretrain_ae(model):
'''
pretrain autoencoder
'''
train_loader = DataLoader(dataset, batch_size=args.batch_size, shuffle=True)
print(model)
optimizer = Adam(model.parameters(), lr=args.lr)
for epoch in range(200):
total_loss = 0.
for batch_idx, (x, _, _) in enumerate(train_loader):
x = x.to(device)
optimizer.zero_grad()
x_bar, z = model(x)
loss = F.mse_loss(x_bar, x)
total_loss += loss.item()
loss.backward()
optimizer.step()
print("epoch {} loss={:.4f}".format(epoch,
total_loss / (batch_idx + 1)))
torch.save(model.state_dict(), args.pretrain_path)
print("model saved to {}.".format(args.pretrain_path))
def train_idec():
model = IDEC(
n_enc_1=500,
n_enc_2=500,
n_enc_3=1000,
n_dec_1=1000,
n_dec_2=500,
n_dec_3=500,
n_input=args.n_input,
n_z=args.n_z,
n_clusters=args.n_clusters,
alpha=1.0,
pretrain_path=args.pretrain_path).to(device)
# model.pretrain('data/ae_mnist.pkl')
model.pretrain()
train_loader = DataLoader(
dataset, batch_size=args.batch_size, shuffle=False)
optimizer = Adam(model.parameters(), lr=args.lr)
# cluster parameter initiate
data = dataset.x
y = dataset.y
data = torch.Tensor(data).to(device)
x_bar, hidden = model.ae(data)
kmeans = KMeans(n_clusters=args.n_clusters, n_init=20)
y_pred = kmeans.fit_predict(hidden.data.cpu().numpy())
nmi_k = nmi_score(y_pred, y)
print("nmi score={:.4f}".format(nmi_k))
hidden = None
x_bar = None
y_pred_last = y_pred
model.cluster_layer.data = torch.tensor(kmeans.cluster_centers_).to(device)
model.train()
for epoch in range(100):
if epoch % args.update_interval == 0:
_, tmp_q = model(data)
# update target distribution p
tmp_q = tmp_q.data
p = target_distribution(tmp_q)
# evaluate clustering performance
y_pred = tmp_q.cpu().numpy().argmax(1)
delta_label = np.sum(y_pred != y_pred_last).astype(
np.float32) / y_pred.shape[0]
y_pred_last = y_pred
acc = cluster_acc(y, y_pred)
nmi = nmi_score(y, y_pred)
ari = ari_score(y, y_pred)
print('Iter {}'.format(epoch), ':Acc {:.4f}'.format(acc),
', nmi {:.4f}'.format(nmi), ', ari {:.4f}'.format(ari))
if epoch > 0 and delta_label < args.tol:
print('delta_label {:.4f}'.format(delta_label), '< tol',
args.tol)
print('Reached tolerance threshold. Stopping training.')
break
for batch_idx, (x, _, idx) in enumerate(train_loader):
x = x.to(device)
idx = idx.to(device)
x_bar, q = model(x)
reconstr_loss = F.mse_loss(x_bar, x)
kl_loss = F.kl_div(q.log(), p[idx])
loss = args.gamma * kl_loss + reconstr_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='train',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--lr', type=float, default=0.001)
parser.add_argument('--n_clusters', default=7, type=int)
parser.add_argument('--batch_size', default=256, type=int)
parser.add_argument('--n_z', default=10, type=int)
parser.add_argument('--dataset', type=str, default='mnist')
parser.add_argument('--pretrain_path', type=str, default='data/ae_mnist')
parser.add_argument(
'--gamma',
default=0.1,
type=float,
help='coefficient of clustering loss')
parser.add_argument('--update_interval', default=1, type=int)
parser.add_argument('--tol', default=0.001, type=float)
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")
if args.dataset == 'mnist':
args.pretrain_path = 'data/ae_mnist.pkl'
args.n_clusters = 10
args.n_input = 784
dataset = MnistDataset()
print(args)
train_idec()