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

Method fit

ann_class2/dropout_theano.py:55–122  ·  view source on GitHub ↗
(self, X, Y, Xvalid, Yvalid, learning_rate=1e-2, mu=0.9, decay=0.9, epochs=10, batch_sz=100, show_fig=False)

Source from the content-addressed store, hash-verified

53 self.dropout_rates = p_keep
54
55 def fit(self, X, Y, Xvalid, Yvalid, learning_rate=1e-2, mu=0.9, decay=0.9, epochs=10, batch_sz=100, show_fig=False):
56 X = X.astype(np.float32)
57 Y = Y.astype(np.int32)
58 Xvalid = Xvalid.astype(np.float32)
59 Yvalid = Yvalid.astype(np.int32)
60
61 self.rng = RandomStreams()
62
63 # initialize hidden layers
64 N, D = X.shape
65 K = len(set(Y))
66 self.hidden_layers = []
67 M1 = D
68 count = 0
69 for M2 in self.hidden_layer_sizes:
70 h = HiddenLayer(M1, M2, count)
71 self.hidden_layers.append(h)
72 M1 = M2
73 count += 1
74 W = np.random.randn(M1, K) * np.sqrt(2.0 / M1)
75 b = np.zeros(K)
76 self.W = theano.shared(W, 'W_logreg')
77 self.b = theano.shared(b, 'b_logreg')
78
79 # collect params for later use
80 self.params = [self.W, self.b]
81 for h in self.hidden_layers:
82 self.params += h.params
83
84 # set up theano functions and variables
85 thX = T.matrix('X')
86 thY = T.ivector('Y')
87 pY_train = self.forward_train(thX)
88
89 # this cost is for training
90 cost = -T.mean(T.log(pY_train[T.arange(thY.shape[0]), thY]))
91 updates = momentum_updates(cost, self.params, learning_rate, mu)
92
93 train_op = theano.function(
94 inputs=[thX, thY],
95 updates=updates
96 )
97
98 # for evaluation and prediction
99 pY_predict = self.forward_predict(thX)
100 cost_predict = -T.mean(T.log(pY_predict[T.arange(thY.shape[0]), thY]))
101 prediction = self.predict(thX)
102 cost_predict_op = theano.function(inputs=[thX, thY], outputs=[cost_predict, prediction])
103
104 n_batches = N // batch_sz
105 costs = []
106 for i in range(epochs):
107 X, Y = shuffle(X, Y)
108 for j in range(n_batches):
109 Xbatch = X[j*batch_sz:(j*batch_sz+batch_sz)]
110 Ybatch = Y[j*batch_sz:(j*batch_sz+batch_sz)]
111
112 train_op(Xbatch, Ybatch)

Callers 1

mainFunction · 0.95

Calls 6

forward_trainMethod · 0.95
forward_predictMethod · 0.95
predictMethod · 0.95
HiddenLayerClass · 0.70
momentum_updatesFunction · 0.70
error_rateFunction · 0.70

Tested by

no test coverage detected