-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscrete.py
175 lines (143 loc) · 5.59 KB
/
discrete.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
import numpy as np
import torch
from torch import optim
from data import generate_nonstationary_sources, generate_mixing_matrix, to_one_hot
from metrics import mean_corr_coef as mcc
from models import DiscreteIVAE, DiscreteVAE
def sigmoid(x):
"""
Sigmoid activation function
@param x: input array
@return:
out: output array
"""
return 1 / (1 + np.exp(-x))
def run_ivae(S, X, U, Sb, Xb, Ub, epochs=20, seed=None, n_layers=2, hidden_dim=20, lr=1e-2, device='cpu'):
print('starting ica')
if seed is not None:
torch.manual_seed(seed)
dl = Sb[0].shape[1]
dd = Xb[0].shape[1]
ns = Ub[0].shape[1]
model = DiscreteIVAE(dl, dd, ns, activation='none', n_layers=n_layers, hidden_dim=hidden_dim, device=device)
optimizer = optim.Adam(model.parameters(), lr=lr)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.1, patience=4, verbose=True)
epoch_loss_hist = []
epoch_perf_hist = []
# training loop
it = 0
n_batches = len(Xb)
n_iter = epochs * n_batches
model.train()
while it < n_iter:
elbo_mean = 0
perf_mean = 0
for c in range(n_batches):
it += 1
optimizer.zero_grad()
x = torch.Tensor(Xb[c]).to(device)
u = torch.Tensor(Ub[c]).to(device)
z = Sb[c]
elbo, z_est = model.elbo(x, u)
elbo.mul(-1).backward()
optimizer.step()
elbo_mean += elbo.item()
perf = mcc(z, z_est.cpu().detach().numpy())
perf_mean += perf
elbo_mean /= n_batches
epoch_loss_hist.append(elbo_mean)
perf_mean /= n_batches
epoch_perf_hist.append(perf_mean)
scheduler.step(elbo_mean)
print('epoch {}:\tloss: {};\tperf: {}'.format(int(it / n_batches), elbo_mean, perf_mean))
# _, _, Z, _ = model(torch.Tensor(X).to(device), torch.Tensor(U).to(device))
decoder_params, encoder_params, Z, prior_params = model(torch.Tensor(X).to(device), torch.Tensor(U).to(device))
perf = mcc(Z.detach().cpu().numpy(), S)
print(perf)
print('decoder_params')
print(decoder_params)
print('---')
with open('run/logs/discrete/ica_discrete.txt', 'a') as f:
f.write(str(perf) + '\n')
np.savez_compressed('run/logs/discrete/ica_{}.npz'.format(args.seed),
l=np.array(epoch_loss_hist), p=np.array(epoch_perf_hist))
def run_vae(S, X, Sb, Xb, epochs=20, seed=None, n_layers=2, hidden_dim=20, lr=1e-2, device='cpu'):
print('starting vae')
if seed is not None:
torch.manual_seed(seed)
dl = Sb[0].shape[1]
dd = Xb[0].shape[1]
model = DiscreteVAE(dl, dd, activation='none', n_layers=n_layers, hidden_dim=hidden_dim, device=device)
optimizer = optim.Adam(model.parameters(), lr=lr)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.1, patience=4, verbose=True)
epoch_loss_hist = []
epoch_perf_hist = []
# training loop
it = 0
n_batches = len(Xb)
n_iter = epochs * n_batches
model.train()
while it < n_iter:
elbo_mean = 0
perf_mean = 0
for c in range(n_batches):
it += 1
optimizer.zero_grad()
x = torch.Tensor(Xb[c]).to(device)
z = Sb[c]
elbo, z_est = model.elbo(x)
elbo.mul(-1).backward()
optimizer.step()
elbo_mean += elbo.item()
perf = mcc(z, z_est.cpu().detach().numpy())
perf_mean += perf
elbo_mean /= n_batches
epoch_loss_hist.append(elbo_mean)
perf_mean /= n_batches
epoch_perf_hist.append(perf_mean)
scheduler.step(elbo_mean)
print('epoch {}:\tloss: {};\tperf: {}'.format(int(it / n_batches), elbo_mean, perf_mean))
_, _, Z = model(torch.Tensor(X).to(device))
perf = mcc(Z.detach().cpu().numpy(), S)
print(perf)
with open('log/discrete/vae_discrete.txt', 'a') as f:
f.write(str(perf) + '\n')
np.savez_compressed('log/discrete/vae_{}.npz'.format(args.seed),
l=np.array(epoch_loss_hist), p=np.array(epoch_perf_hist))
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='tcl vs vaeica on simulated data')
parser.add_argument('-s', '--seed', type=int, default=1, dest='seed', help='random seed - default: 1')
parser.add_argument('-m', '--method', type=str, default='ica', dest='method', help='method')
args = parser.parse_args()
print('start {} {}'.format(args.seed, args.method))
np.random.seed(1)
# nps = 3000
nps = 2000
# ns = 20
ns = 40
batch_size = 200
n = nps * ns
# dl, dd = 10, 100
dl, dd = 2, 2
S, U, _, _ = generate_nonstationary_sources(nps, ns, dl)
print(S.shape)
A = generate_mixing_matrix(dl, dd)
PX = sigmoid(np.dot(S, A))
# X = (np.sign(PX-0.5) + 1)/2
X = np.random.binomial(1, PX)
idx = np.random.permutation(n)
Xb, Sb, Ub = [], [], []
n_batches = int(n / batch_size)
for c in range(n_batches):
Sb += [S[idx][c * batch_size:(c + 1) * batch_size]]
Xb += [X[idx][c * batch_size:(c + 1) * batch_size]]
Ub += [U[idx][c * batch_size:(c + 1) * batch_size]]
Ub = to_one_hot(Ub, ns)
U = to_one_hot(U)[0]
if args.method == 'ica':
run_ivae(S, X, U, Sb, Xb, Ub, epochs=50, seed=args.seed, hidden_dim=50, n_layers=2, lr=1e-2, device='cuda')
elif args.method == 'vae':
run_vae(S, X, Sb, Xb, epochs=50, seed=args.seed, hidden_dim=50, n_layers=2, lr=1e-2, device='cuda')
else:
raise ValueError('wrong method {}'.format(args.method))