-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDenoisingAutoEncoder.py
More file actions
68 lines (56 loc) · 2.22 KB
/
Copy pathDenoisingAutoEncoder.py
File metadata and controls
68 lines (56 loc) · 2.22 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
import numpy
import random
class DenoisingAutoEncoder:
def __init__(self,V,H,batchSize=3):
self.V = V
self.H = H
self.batchSize = batchSize
self.weight = numpy.random.randn(H,V)/10
self.visBias = numpy.random.randn(H)/10
self.hidBias = numpy.random.randn(V)/10
self.clearDelta()
def clearDelta(self):
self.saved = 0
self.sumDeltaWeight = numpy.zeros(self.H*self.V).reshape(self.H,self.V)
self.sumDeltaVisBias = numpy.zeros(self.H)
self.sumDeltaHidBias = numpy.zeros(self.V)
def sigmoid(self,x):
return 1.0/(1.0+numpy.exp(-x))
def noising(self,vis):
return vis+numpy.random.randn(self.V)/10
def train(self,vis,alpha=0.1):
hid = self.encode(self.noising(vis))
out = self.decode(hid)
deltaWeight = numpy.dot((numpy.dot(self.weight,vis-out)*hid*(1-hid)).reshape(self.H,1),self.noising(vis).reshape(1,self.V))
deltaWeight = deltaWeight + numpy.dot((vis-out).T.reshape(self.V,1),hid.reshape(1,self.H)).T
deltaVisBias = numpy.dot(self.weight,vis-out)*hid*(1-hid)
deltaHidBias = vis-out
self.saved += 1
self.sumDeltaWeight += deltaWeight
self.sumDeltaVisBias += deltaVisBias
self.sumDeltaHidBias += deltaHidBias
if self.saved==self.batchSize:
self.weight += alpha*self.sumDeltaWeight/self.batchSize
self.visBias += alpha*self.sumDeltaVisBias/self.batchSize
self.hidBias += alpha*self.sumDeltaHidBias/self.batchSize
self.clearDelta()
def encode(self,vis):
return self.sigmoid(numpy.dot(self.weight,vis)+self.visBias)
def decode(self,hid):
return self.sigmoid(numpy.dot(self.weight.T,hid)+self.hidBias)
if __name__=='__main__':
N = 100
D = 20
samples = [numpy.random.random(D) for _ in xrange(D)]
print samples
dae = DenoisingAutoEncoder(D,50)
for epoch in xrange(10000):
error = 0
for s in samples:
dae.train(s)
error += numpy.sum(numpy.abs(s-dae.decode(dae.encode(s))))
if epoch % 100 == 0:
print dae.encode(s)
if epoch % 100 == 0:
print error
random.shuffle(samples)