MCPcopy Index your code
hub / github.com/lazyprogrammer/machine_learning_examples / ANN

Class ANN

unsupervised_class2/vanishing.py:32–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

30
31
32class ANN(object):
33 def __init__(self, hidden_layer_sizes):
34 self.hidden_layer_sizes = hidden_layer_sizes
35
36 def fit(self, X, Y, learning_rate=0.01, mu=0.99, epochs=30, batch_sz=100):
37 # cast to float32
38 learning_rate = np.float32(learning_rate)
39 mu = np.float32(mu)
40
41 N, D = X.shape
42 K = len(set(Y))
43
44 self.hidden_layers = []
45 mi = D
46 for mo in self.hidden_layer_sizes:
47 h = HiddenLayer(mi, mo)
48 self.hidden_layers.append(h)
49 mi = mo
50
51 # initialize logistic regression layer
52 W = init_weights((mo, K))
53 b = np.zeros(K, dtype=np.float32)
54 self.W = theano.shared(W)
55 self.b = theano.shared(b)
56
57 self.params = [self.W, self.b]
58 self.allWs = []
59 for h in self.hidden_layers:
60 self.params += h.params
61 self.allWs.append(h.W)
62 self.allWs.append(self.W)
63
64 X_in = T.matrix('X_in')
65 targets = T.ivector('Targets')
66 pY = self.forward(X_in)
67
68 cost = -T.mean( T.log(pY[T.arange(pY.shape[0]), targets]) )
69 prediction = self.predict(X_in)
70
71 updates = momentum_updates(cost, self.params, mu, learning_rate)
72 train_op = theano.function(
73 inputs=[X_in, targets],
74 outputs=[cost, prediction],
75 updates=updates,
76 )
77
78 n_batches = N // batch_sz
79 costs = []
80 lastWs = [W.get_value() for W in self.allWs]
81 W_changes = []
82 print("supervised training...")
83 for i in range(epochs):
84 print("epoch:", i)
85 X, Y = shuffle(X, Y)
86 for j in range(n_batches):
87 Xbatch = X[j*batch_sz:(j*batch_sz + batch_sz)]
88 Ybatch = Y[j*batch_sz:(j*batch_sz + batch_sz)]
89 c, p = train_op(Xbatch, Ybatch)

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected