-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathrun.py
70 lines (54 loc) · 1.73 KB
/
run.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
import numpy as np
import time
import os
from VAE import VAE
import pickle
import gzip
np.random.seed(42)
hu_encoder = 400
hu_decoder = 400
n_latent = 20
continuous = False
n_epochs = 40
if continuous:
print("Loading Freyface data")
# Retrieved from: http://deeplearning.net/data/mnist/mnist.pkl.gz
f = open('freyfaces.pkl', 'rb')
x = pickle.load(f, encoding='latin1')
f.close()
x_train = x[:1500]
x_valid = x[1500:]
else:
print("Loading MNIST data")
# Retrieved from: http://deeplearning.net/data/mnist/mnist.pkl.gz
f = gzip.open('mnist.pkl.gz', 'rb')
(x_train, t_train), (x_valid, t_valid), (x_test, t_test) = pickle.load(f, encoding='latin1')
f.close()
path = "./"
print("instantiating model")
model = VAE(continuous, hu_encoder, hu_decoder, n_latent, x_train)
batch_order = np.arange(int(model.N / model.batch_size))
epoch = 0
LB_list = []
if os.path.isfile(path + "params.pkl"):
print("Restarting from earlier saved parameters!")
model.load_parameters(path)
LB_list = np.load(path + "LB_list.npy")
epoch = len(LB_list)
if __name__ == "__main__":
print("iterating")
while epoch < n_epochs:
epoch += 1
start = time.time()
np.random.shuffle(batch_order)
LB = 0.
for batch in batch_order:
batch_LB = model.update(batch, epoch)
LB += batch_LB
LB /= len(batch_order)
LB_list = np.append(LB_list, LB)
print("Epoch {0} finished. LB: {1}, time: {2}".format(epoch, LB, time.time() - start))
np.save(path + "LB_list.npy", LB_list)
model.save_parameters(path)
valid_LB = model.likelihood(x_valid.astype(theano.config.floatX))
print("LB on validation set: {0}".format(valid_LB))