|
a |
|
b/autoencoder/test_autoencoder.py |
|
|
1 |
import sys |
|
|
2 |
import os |
|
|
3 |
import collections |
|
|
4 |
|
|
|
5 |
import numpy as np |
|
|
6 |
|
|
|
7 |
lib_path = 'I:/code' |
|
|
8 |
if not os.path.exists(lib_path): |
|
|
9 |
lib_path = '/media/6T/.tianle/.lib' |
|
|
10 |
if os.path.exists(lib_path) and lib_path not in sys.path: |
|
|
11 |
sys.path.append(lib_path) |
|
|
12 |
|
|
|
13 |
import torch |
|
|
14 |
import torch.nn as nn |
|
|
15 |
|
|
|
16 |
from dl.models.basic_models import DenseLinear, get_list, get_attr |
|
|
17 |
from dl.utils.train import cosine_similarity |
|
|
18 |
from autoencoder.autoencoder import * |
|
|
19 |
|
|
|
20 |
|
|
|
21 |
# Had run in jupyter notebook but not here |
|
|
22 |
# test Loss_view_similarity |
|
|
23 |
x = torch.randn(11, 10) |
|
|
24 |
model = MultiviewAE(in_dims=[2,3,5], hidden_dims=[7], out_dim=11) |
|
|
25 |
loss_fn_g = Loss_view_similarity(sections=7, loss_type='hub', explicit_target=True, |
|
|
26 |
cal_target='mean-feature', target=None, fusion_type='multiply', graph_laplacian=False) |
|
|
27 |
loss_fn_d = Loss_view_similarity(sections=7, loss_type='hub', explicit_target=False, |
|
|
28 |
cal_target='mean-feature', target=None, fusion_type='sum', graph_laplacian=True) |
|
|
29 |
loss_fn_c = Loss_view_similarity(sections=7, loss_type='hub', explicit_target=True, |
|
|
30 |
cal_target='mean-similarity', target=None, fusion_type='sum', graph_laplacian=False) |
|
|
31 |
|
|
|
32 |
optimizer = torch.optim.Adam(model.parameters(), lr=1e-2) |
|
|
33 |
loss_history = [] |
|
|
34 |
for i in range(100): |
|
|
35 |
xs = model(x)[-1] |
|
|
36 |
loss_g = loss_fn_g(xs) |
|
|
37 |
optimizer.zero_grad() |
|
|
38 |
loss_g.backward() |
|
|
39 |
optimizer.step() |
|
|
40 |
with torch.no_grad(): |
|
|
41 |
loss_d = loss_fn_d(xs) |
|
|
42 |
loss_c = loss_fn_c(xs) |
|
|
43 |
loss_history.append([loss_g.item(), loss_d.item(), loss_c.item()]) |
|
|
44 |
plt.plot(loss_history) |
|
|
45 |
plt.show() |
|
|
46 |
|
|
|
47 |
xs = xs.split(7, dim=1) |
|
|
48 |
circle_similarity_mats = [cosine_similarity(xs[i-1], xs[i]) for i in range(1, len(xs))] |
|
|
49 |
self_similarity_mats = [cosine_similarity(xs[i]) for i in range(len(xs))] |
|
|
50 |
print([(x.mean().item(), x.std().item()) for x in xs]) |
|
|
51 |
print([(m.mean().item(), m.std().item()) for m in circle_similarity_mats]) |
|
|
52 |
print([(m.mean().item(), m.std().item()) for m in self_similarity_mats]) |