-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_mnist.py
196 lines (156 loc) · 7.03 KB
/
main_mnist.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
''' Version 1.000
Code provided by Daniel Jiwoong Im
Permission is granted for anyone to copy, use, modify, or distribute this
program and accompanying programs and documents for any purpose, provided
this copyright notice is retained and prominently displayed, along with
a note saying that the original programs are available from our
web page.
The programs and documents are distributed without any warranty, express or
implied. As the programs were written for research purposes only, they have
not been tested to the degree that would be advisable in any important
application. All use of these programs is entirely at the user's own risk.'''
'''Demo of Denoising Criterion for Variational Auto-encoding Framework.
For more information, see :http://arxiv.org/abs/1511.06406
'''
import os,sys
import numpy as np
import scipy as sp
import theano
import theano.tensor as T
import pickle,cPickle
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import gzip
from utils import *
import timeit, time
from optimize import *
from dvae import *
import theano.sandbox.rng_mrg as RNG_MRG
rng = np.random.RandomState()
MRG = RNG_MRG.MRG_RandomStreams(rng.randint(2 ** 30))
#datapath='/groups/branson/home/imd/Documents/machine_learning_uofg/data/MNIST/'
datapath='/home/daniel/Documents/data/mnist/'
if not os.path.exists(os.path.dirname(os.path.realpath(__file__)) + "/figs/"):
os.makedirs(os.path.dirname(os.path.realpath(__file__)) + "/figs/")
if not os.path.exists(os.path.dirname(os.path.realpath(__file__)) + "/params/"):
os.makedirs(os.path.dirname(os.path.realpath(__file__)) + "/params/")
def lets_train(train_set, valid_set, test_set, opt_params, model_params, model_type, opt_method='ADAM'):
[batch_sz, epsilon, momentum, num_epoch, N, Nv, Nt, binaryF, crossEntropyF, corrupt_in, ntype] = opt_params
[batch_sz, D, num_hids, rng, num_z, binaryF] = model_params
opt_params = [batch_sz, epsilon, momentum, num_epoch, N, Nv, Nt, corrupt_in, ntype]
# compute number of minibatches for training, validation and testing
num_batch_train = N / batch_sz
num_batch_valid = Nv / batch_sz
num_batch_test = Nt / batch_sz
vae = DVAE2(model_params)
opt = Optimize(opt_params)
if opt_method=='MGD':
train_model, update_momentum, get_valid_cost \
= opt.MGD(vae, train_set, valid_set, test_set, binaryF, crossEntropyF)
else:
train_model, get_valid_cost, get_test_cost = opt.ADAM(vae, train_set, valid_set, test_set, binaryF, crossEntropyF)
get_samples = opt.get_samples(vae, binaryF)
best_vl = np.infty
k=0
constant=3
bookkeeping_vl = []
for epoch in xrange(num_epoch+1):
costs=[]
if constant**k == epoch+1:
eps = epsilon * get_epsilon_decay(k+1, num_epoch, constant)
k+=1
exec_start = timeit.default_timer()
for batch_i in xrange(num_batch_train):
if opt_method=='MGD': update_momentum()
cost_i = train_model(batch_i, lr=eps)
if not np.isnan(cost_i):
costs.append(cost_i)
exec_finish = timeit.default_timer()
if epoch==0:
print 'Exec Time %f ' % ( exec_finish - exec_start)
if epoch % 50 == 0 or epoch < 2 or epoch == (num_epoch-1):
costs_vl = []
for batch_j in xrange(num_batch_valid):
cost_vl_j = get_valid_cost(batch_j)
costs_vl.append(cost_vl_j)
cost_vl = np.mean(np.asarray(costs_vl))
cost_tr = np.mean(np.asarray(costs))
bookkeeping_vl.append(cost_vl)
print 'Epoch %d, epsilon %g, train cost %g, valid cost %g' % (epoch, eps, cost_tr, cost_vl)
if best_vl > cost_vl and epoch > 0.4*num_epoch:
best_vl = cost_vl
save_the_weight(vae.params, './params/'+ model_type +'_param_mnist')
costs_te = []
for batch_j in xrange(num_batch_test):
cost_te_j = get_test_cost(batch_j)
costs_te.append(cost_te_j)
cost_te = np.mean(np.asarray(costs_te))
print '*** Epoch %d, test cost %g ***' % (epoch, cost_te)
costs_te = []
for batch_j in xrange(num_batch_test):
cost_te_j = get_test_cost(batch_j)
costs_te.append(cost_te_j)
cost_te = np.mean(np.asarray(costs_te))
print '*** Epoch %d, test cost %g ***' % (epoch, cost_te)
return vae, cost_te
##################
## Hyper-params ##
##################
batch_sz = 100
epsilon = 0.0001
momentum = 0.0
num_epoch = 5000
num_hid = 200
num_z = 50
num_class = 10
num_trial = 1
binaryF = True
CrossEntropyF = True
corrupt_in = 0.0
model_type = 'dvae'
rnn_type = 'grnn'
data_type = 'mnist'
print 'model type: ' + model_type
print 'rnn type: ' + rnn_type
print 'data Type: ' + data_type
ntype = 'salt_pepper'
if __name__ == '__main__':
#Note that there are two different binary MNIST in the literature.
if data_type == 'hugo_mnist':
dataset = datapath+'/binarized_mnist.npz'
data = np.load(dataset)
train_set = [data['train_data'], np.zeros(int(data['train_length']),)]
valid_set = [data['valid_data'], np.zeros(int(data['valid_length']),)]
test_set = [data['test_data'] , np.zeros(int(data['test_length' ]),)]
N ,D = train_set[0].shape; Nv,D = valid_set[0].shape; Nt,D = test_set[0].shape
train_set = shared_dataset(train_set)
valid_set = shared_dataset(valid_set)
test_set = shared_dataset(test_set )
else:
dataset = datapath+'/mnist.pkl.gz'
f = gzip.open(dataset, 'rb')
train_set_o, valid_set_o, test_set_o = cPickle.load(f)
N ,D = train_set_o[0].shape
Nv,D = valid_set_o[0].shape
Nt,D = test_set_o[0].shape
f.close()
print 'N, D %d %d' % (N,D)
book_keeping = []
for i in xrange(num_trial):
if data_type != 'hugo_mnist':
train_set = [rng.binomial(1, train_set_o[0]), train_set_o[1]]
valid_set = [rng.binomial(1, valid_set_o[0]), valid_set_o[1]]
test_set = [rng.binomial(1, test_set_o[0]), test_set_o[1]]
train_set = shared_dataset(train_set)
valid_set = shared_dataset(valid_set)
test_set = shared_dataset(test_set )
print 'batch sz %d, epsilon %g, num_hid %d, num_z %d, num_epoch %d corrupt_in %g' % \
(batch_sz, epsilon, num_hid, num_z, num_epoch, corrupt_in)
num_hids = [num_hid]
opt_params = [batch_sz, epsilon, momentum, num_epoch, N, Nv, Nt, binaryF, CrossEntropyF, corrupt_in, ntype]
model_params = [batch_sz, D, num_hids, rng, num_z, binaryF]
vae, cost_te = lets_train(train_set, valid_set, test_set, opt_params, model_params, model_type )
book_keeping.append(cost_te)
book_keeping = np.asarray(book_keeping)
print '+++ Mean test nll %g std test nll %g +++' % (np.mean(book_keeping), np.std(book_keeping))