-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathda.py
More file actions
115 lines (98 loc) · 3.87 KB
/
da.py
File metadata and controls
115 lines (98 loc) · 3.87 KB
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
import theano
import theano.tensor as T
from theano.tensor.shared_randomstreams import RandomStreams
import numpy
from ae import Autoencoder, CostType, Nonlinearity
class DenoisingAutoencoder(Autoencoder):
def __init__(self,
input,
nvis,
nhid,
rnd=None,
theano_rng=None,
bhid=None,
cost_type=CostType.MeanSquared,
momentum=1,
L1_reg=-1,
L2_reg=-1,
sparse_initialize=False,
nonlinearity=Nonlinearity.TANH,
bvis=None,
tied_weights=True):
# create a Theano random generator that gives symbolic random values
super(DenoisingAutoencoder, self).__init__(input,
nvis,
nhid,
rnd,
bhid,
cost_type,
momentum,
L1_reg=L1_reg,
L2_reg=L2_reg,
sparse_initialize=sparse_initialize,
nonlinearity=nonlinearity,
bvis=bvis,
tied_weights=tied_weights)
if not theano_rng :
theano_rng = RandomStreams(rnd.randint(2 ** 30))
self.theano_rng = theano_rng
def corrupt_input(self, in_data, corruption_level):
return self.theano_rng.binomial(self.x.shape, n=1, p=1-corruption_level,
dtype=theano.config.floatX) * self.x
def get_reconstructed_images(self, data):
h = self.encode(x_in=data)
x_rec = self.decode(h)
return x_rec
def debug_grads(self, data):
gfn = theano.function([self.x], self.gparams[0])
print "gradients:"
print gfn(data)
print "params:"
print self.hidden.W.get_value()
def fit(self,
data=None,
learning_rate=0.1,
learning_rate_decay=None,
batch_size=100,
n_epochs=60,
corruption_level=0.5,
weights_file=None,
sparsity_level=-1,
sparse_reg=-1,
shuffle_data=True,
lr_scaler=1.0,
recons_img_file="out/dae_reconstructed_pento.npy"):
if data is None:
raise Exception("Data can't be empty.")
index = T.iscalar('index')
data_shared = theano.shared(numpy.asarray(data.tolist(), dtype=theano.config.floatX))
n_batches = data.shape[0] / batch_size
corrupted_input = self.corrupt_input(data_shared, corruption_level)
(cost, updates) = self.get_sgd_updates(learning_rate, lr_scaler=lr_scaler, batch_size=batch_size,
sparsity_level=sparsity_level,
sparse_reg=sparse_reg, x_in=corrupted_input)
train_ae = theano.function([index],
cost,
updates=updates,
givens={self.x: data_shared[index * batch_size: (index + 1) * batch_size]})
print "Started the training."
ae_costs = []
batch_index = 0
for epoch in xrange(n_epochs):
idxs = numpy.arange(n_batches)
numpy.random.shuffle(idxs)
print "Training at epoch %d" % epoch
for batch_index in idxs:
ae_costs.append(train_ae(batch_index))
if False:
print "Cost: ", ae_costs[-1]
self.debug_grads(data_shared.get_value()[batch_index * batch_size: (batch_index + 1) * batch_size])
print "Training at epoch %d, %f" % (epoch, numpy.mean(ae_costs))
if weights_file is not None:
print "Saving weights..."
numpy.save(weights_file, self.params[0].get_value())
if recons_img_file is not None:
print "Saving reconstructed images..."
x_rec = self.get_reconstructed_images(data_shared)
numpy.save(recons_img_file, x_rec)
return ae_costs