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

Class ANN

airline/ann.py:37–129  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35
36
37class ANN(object):
38 def __init__(self, hidden_layer_sizes):
39 self.hidden_layer_sizes = hidden_layer_sizes
40
41 def fit(self, X, Y, activation=T.tanh, learning_rate=1e-3, mu=0.5, reg=0, epochs=5000, batch_sz=None, print_period=100, show_fig=True):
42 X = X.astype(np.float32)
43 Y = Y.astype(np.float32)
44
45 # initialize hidden layers
46 N, D = X.shape
47 self.hidden_layers = []
48 M1 = D
49 count = 0
50 for M2 in self.hidden_layer_sizes:
51 h = HiddenLayer(M1, M2, activation, count)
52 self.hidden_layers.append(h)
53 M1 = M2
54 count += 1
55 W = np.random.randn(M1) / np.sqrt(M1)
56 b = 0.0
57 self.W = theano.shared(W, 'W_last')
58 self.b = theano.shared(b, 'b_last')
59
60 if batch_sz is None:
61 batch_sz = N
62
63 # collect params for later use
64 self.params = [self.W, self.b]
65 for h in self.hidden_layers:
66 self.params += h.params
67
68 # for momentum
69 dparams = [theano.shared(np.zeros(p.get_value().shape)) for p in self.params]
70
71 # set up theano functions and variables
72 thX = T.matrix('X')
73 thY = T.vector('Y')
74 Yhat = self.forward(thX)
75
76 rcost = reg*T.mean([(p*p).sum() for p in self.params])
77 cost = T.mean((thY - Yhat).dot(thY - Yhat)) + rcost
78 prediction = self.forward(thX)
79 grads = T.grad(cost, self.params)
80
81 # momentum only
82 updates = [
83 (p, p + mu*dp - learning_rate*g) for p, dp, g in zip(self.params, dparams, grads)
84 ] + [
85 (dp, mu*dp - learning_rate*g) for dp, g in zip(dparams, grads)
86 ]
87
88 train_op = theano.function(
89 inputs=[thX, thY],
90 outputs=[cost, prediction],
91 updates=updates,
92 )
93
94 self.predict_op = theano.function(

Callers 1

ann.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected