MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / DBN

Class DBN

unsupervised_class2/unsupervised.py:22–121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20
21
22class DBN(object):
23 def __init__(self, hidden_layer_sizes, UnsupervisedModel=AutoEncoder):
24 self.hidden_layers = []
25 count = 0
26 for M in hidden_layer_sizes:
27 ae = UnsupervisedModel(M, count)
28 self.hidden_layers.append(ae)
29 count += 1
30
31 def fit(self, X, pretrain_epochs=1):
32 self.D = X.shape[1] # save for later
33
34 current_input = X
35 for ae in self.hidden_layers:
36 ae.fit(current_input, epochs=pretrain_epochs)
37
38 # create current_input for the next layer
39 current_input = ae.hidden_op(current_input)
40
41 # return it here so we can use directly after fitting without calling forward
42 return current_input
43
44 def forward(self, X):
45 Z = X
46 for ae in self.hidden_layers:
47 Z = ae.forward_hidden(Z)
48 return Z
49
50 def fit_to_input(self, k, learning_rate=1.0, mu=0.99, epochs=100000):
51 # This is not very flexible, as you would ideally
52 # like to be able to activate any node in any hidden
53 # layer, not just the last layer.
54 # Exercise for students: modify this function to be able
55 # to activate neurons in the middle layers.
56
57 # cast hyperperams
58 learning_rate = np.float32(learning_rate)
59 mu = np.float32(mu)
60
61 # randomly initialize an image
62 X0 = init_weights((1, self.D))
63
64 # make the image a shared so theano can update it
65 X = theano.shared(X0, 'X_shared')
66
67 # get the output of the neural network
68 Y = self.forward(X)
69
70 # t = np.zeros(self.hidden_layers[-1].M)
71 # t[k] = 1
72
73 # # choose Y[0] b/c it's shape 1xD, we want just a D-size vector, not 1xD matrix
74 # cost = -(t*T.log(Y[0]) + (1 - t)*(T.log(1 - Y[0]))).sum()
75
76 # k = which output node to look at
77 # there is only 1 image, so we select the 0th row of X
78 cost = -T.log(Y[0,k])
79

Callers 3

mainFunction · 0.90
loadMethod · 0.85
mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected