-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoencoder.py
105 lines (78 loc) · 3.66 KB
/
autoencoder.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
import theano as th
import theano.tensor as T
import numpy as np
import NeuralNet as NN
import Optimizer as opt
class AutoEncoder(NN.NeuralNet):
def __init__(self,
n_in,
layers,
input = None,
rng = np.random.RandomState(1234)):
x = input if input is not None else T.matrix('x')
output_layer_args = NN.LayerData(n_out = n_in,
activation = T.nnet.sigmoid)
NN.NeuralNet.__init__(self,
n_in,
n_in,
layers,
output_layer_args = output_layer_args,
error_fn = opt.reconstruction_xentropy,
input = x,
output = x,
rng = rng)
self.reconstruct = th.function([self.x],
self.output_layer.output)
def validation_error(self, x_valid, y_valid):
return np.mean( abs(self.reconstruct(x_valid) - x_valid) )
class DenoisingAutoEncoder(AutoEncoder):
def __init__(self,
n_in,
layers,
corruption_level,
rng = np.random.RandomState(1234)):
x = self.corrupt_data(T.matrix('x'), corruption_level, rng)
AutoEncoder.__init__(self,
n_in,
layers,
input = x,
rng = rng)
def corrupt_data(self, data, corruption_level, rng):
theano_rng = T.shared_randomstreams.RandomStreams(rng.randint(2 ** 30))
return theano_rng.binomial(size=data.shape, n=1, p=1 - corruption_level, dtype = th.config.floatX) * data
if __name__ == '__main__':
import cPickle as Pickle
(xtrain,ytrain), (xvalid,yvalid), (xtest, ytest) = Pickle.load(open('mnist.pkl'))
rng = np.random.RandomState()
random_arr = rng.uniform(
low=-np.sqrt(6. / (784 + 500)),
high=np.sqrt(6. / (784 + 500)),
size=(784, 500))
W_init = th.shared(np.asarray(random_arr, dtype=th.config.floatX), 'W', borrow = True)
# Autoencoder - NO NOISE
AE = AutoEncoder(n_in = 784,
layers = [NN.LayerData(n_out = 500, W = W_init)],
rng = rng)
print '... Training Autoencoder'
opt.gradient_descent(AE,
x_train = xtrain,
y_train = xtrain,
learning_rate = 0.1,
batch_size = 20,
n_epochs = 20,
x_valid = xvalid,
y_valid = xvalid)
# Autoencoder - 30% NOISE
DAE = DenoisingAutoEncoder(n_in = 784,
layers = [NN.LayerData(n_out = 500, W = W_init)],
corruption_level = 0.3,
rng = rng)
print '... Training Autoencoder'
opt.gradient_descent(DAE,
x_train = xtrain,
y_train = xtrain,
learning_rate = 0.1,
batch_size = 20,
n_epochs = 20,
x_valid = xvalid,
y_valid = xvalid)