-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.py
256 lines (211 loc) · 9.12 KB
/
main.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
## similar to github.com/Michaelvll/DeepCCA main
import torch
import torch.nn as nn
import numpy as np
from linear_gcca import linear_gcca
from synth_data import create_synthData
from torch.utils.data import BatchSampler, SequentialSampler, RandomSampler
from models import DeepGCCA
# from utils import load_data, svm_classify
import time
import logging
try:
import cPickle as thepickle
except ImportError:
import _pickle as thepickle
import gzip
import numpy as np
import torch.nn as nn
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import pandas as pd
torch.set_default_tensor_type(torch.DoubleTensor)
class Solver():
def __init__(self, model, linear_gcca, outdim_size, epoch_num, batch_size, learning_rate, reg_par, device=torch.device('cpu')):
self.model = model # nn.DataParallel(model)
self.model.to(device)
self.epoch_num = epoch_num
self.batch_size = batch_size
self.loss = model.loss
self.optimizer = torch.optim.Adam(
self.model.model_list.parameters(), lr=learning_rate, weight_decay=reg_par)
self.scheduler = torch.optim.lr_scheduler.StepLR(
self.optimizer, step_size=200, gamma=0.7)
self.device = device
self.linear_gcca = linear_gcca()
self.outdim_size = outdim_size
formatter = logging.Formatter(
"[ %(levelname)s : %(asctime)s ] - %(message)s")
logging.basicConfig(
level=logging.DEBUG, format="[ %(levelname)s : %(asctime)s ] - %(message)s")
self.logger = logging.getLogger("Pytorch")
fh = logging.FileHandler("DCCA.log")
fh.setFormatter(formatter)
self.logger.addHandler(fh)
self.logger.info(self.model)
self.logger.info(self.optimizer)
def fit(self, x_list, vx_list=None, tx_list=None, checkpoint='checkpoint.model'):
"""
x1, x2 are the vectors needs to be make correlated
dim=[batch_size, feats]
"""
x_list = [x.to(device) for x in x_list]
data_size = x_list[0].size(0)
if vx_list is not None :
best_val_loss = 0
vx_list = [vx.to(self.device) for vx in vx_list]
if tx_list is not None :
tx_list = [tx.t0(self.device) for tx in tx_list]
train_losses = []
for epoch in range(self.epoch_num):
epoch_start_time = time.time()
self.model.train()
batch_idxs = list(BatchSampler(RandomSampler(
range(data_size)), batch_size=self.batch_size, drop_last=False))
for batch_idx in batch_idxs:
self.optimizer.zero_grad()
batch_x = [x[batch_idx, :] for x in x_list]
output = self.model(batch_x)
loss = self.loss(output)
train_losses.append(loss.item())
loss.backward()
self.optimizer.step()
self.scheduler.step()
train_loss = np.mean(train_losses)
info_string = "Epoch {:d}/{:d} - time: {:.2f} - training_loss: {:.4f}"
if vx_list is not None:
with torch.no_grad():
self.model.eval()
val_loss = self.test(vx_list)
info_string += " - val_loss: {:.4f}".format(val_loss)
if val_loss < best_val_loss:
self.logger.info(
"Epoch {:d}: val_loss improved from {:.4f} to {:.4f}, saving model to {}".format(epoch + 1, best_val_loss, val_loss, checkpoint))
best_val_loss = val_loss
torch.save(self.model.state_dict(), checkpoint)
else:
self.logger.info("Epoch {:d}: val_loss did not improve from {:.4f}".format(
epoch + 1, best_val_loss))
else:
torch.save(self.model.state_dict(), checkpoint)
epoch_time = time.time() - epoch_start_time
self.logger.info(info_string.format(
epoch + 1, self.epoch_num, epoch_time, train_loss))
# train_linear_gcca
if self.linear_gcca is not None:
_, outputs_list = self._get_outputs(x_list)
self.train_linear_gcca(outputs_list)
checkpoint_ = torch.load(checkpoint)
self.model.load_state_dict(checkpoint_)
if vx_list is not None:
loss = self.test(vx_list)
self.logger.info("loss on validation data: {:.4f}".format(loss))
if tx_list is not None:
loss = self.test(tx_list)
self.logger.info('loss on test data: {:.4f}'.format(loss))
def test(self, x_list, use_linear_gcca=False):
with torch.no_grad():
losses, outputs_list = self._get_outputs(x_list)
if use_linear_gcca:
print("Linear CCA started!")
outputs_list = self.linear_gcca.test(outputs_list)
return np.mean(losses), outputs_list
else:
return np.mean(losses)
def train_linear_gcca(self, x_list):
self.linear_gcca.fit(x_list, self.outdim_size)
def _get_outputs(self, x_list):
with torch.no_grad():
self.model.eval()
data_size = x_list[0].size(0)
batch_idxs = list(BatchSampler(SequentialSampler(
range(data_size)), batch_size=self.batch_size, drop_last=False))
losses = []
outputs_list = []
for batch_idx in batch_idxs:
batch_x = [x[batch_idx, :].to(self.device) for x in x_list]
outputs = self.model(batch_x)
outputs_list.append([o_j.clone().detach() for o_j in outputs])
loss = self.loss(outputs)
losses.append(loss.item())
outputs_final = []
for i in range(len(x_list)):
view = []
for j in range(len(outputs_list)):
view.append(outputs_list[j][i].clone().detach())
view = torch.cat(view, dim=0)
outputs_final.append(view)
return losses, outputs_final
def save(self, name):
torch.save(self.model, name)
if __name__ == '__main__':
############
# Parameters Section
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print("Using", torch.cuda.device_count(), "GPUs")
# the path to save the final learned features
save_name = './DGCCA.model'
# the size of the new space learned by the model (number of the new features)
outdim_size = 2
# number of layers with nodes in each one
layer_sizes1 = [256, 512, 128, outdim_size]
layer_sizes2 = [256, 512, 128, outdim_size]
layer_sizes3 = [256, 512, 128, outdim_size]
layer_sizes_list = [layer_sizes1, layer_sizes2, layer_sizes3]
# the parameters for training the network
learning_rate = 1e-2
epoch_num = 1000
batch_size = 400
# the regularization parameter of the network
# seems necessary to avoid the gradient exploding especially when non-saturating activations are used
reg_par = 1e-5
# specifies if all the singular values should get used to calculate the correlation or just the top outdim_size ones
# if one option does not work for a network or dataset, try the other one
use_all_singular_values = False
apply_linear_gcca = True
# end of parameters section
############
N = 400
views = create_synthData(N)
print(f'input views shape :')
for i, view in enumerate(views):
print(f'view_{i} : {view.shape}')
view = view.to(device)
# size of the input for view 1 and view 2
input_shape_list = [view.shape[-1] for view in views]
# Building, training, and producing the new features by DCCA
model = DeepGCCA(layer_sizes_list, input_shape_list, outdim_size,
use_all_singular_values, device=device).double()
l_gcca = None
if apply_linear_gcca:
l_gcca = linear_gcca
solver = Solver(model, l_gcca, outdim_size, epoch_num, batch_size,
learning_rate, reg_par, device=device)
# train1, train2 = data1[0][0], data2[0][0]
# val1, val2 = data1[1][0], data2[1][0]
# test1, test2 = data1[2][0], data2[2][0]
solver.fit(views, checkpoint=save_name)
# TODO: Save l_gcca model if needed
# set_size = [0, train1.size(0), train1.size(
# 0) + val1.size(0), train1.size(0) + val1.size(0) + test1.size(0)]
loss, outputs = solver.test(views, apply_linear_gcca)
import os
outDir = './'
N = 400
classes = ['Class1' for i in range(int(N/2))] + ['Class2' for i in range(int(N/2))]
dfs = []
for o in outputs:
df = pd.DataFrame(o, columns=['x', 'y'])
df['Classes'] = classes
dfs.append(df)
# Plot to PDF
with PdfPages(os.path.join(outDir, 'DGCCA.pdf')) as pdf:
for viewIdx, df in enumerate(dfs):
fig = sns.lmplot(x="x", y="y", fit_reg=False, markers=['+', 'o'], legend=False, hue="Classes", data=df).fig
plt.legend(loc='best')
plt.title('View %d' % (viewIdx))
pdf.savefig()
plt.close(fig)
# solver.model.load_state_dict(d)
# solver.model.parameters()