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

Method fit

ann_class2/dropout_tensorflow.py:36–108  ·  view source on GitHub ↗
(self, X, Y, Xvalid, Yvalid, lr=1e-4, mu=0.9, decay=0.9, epochs=15, batch_sz=100, print_every=50)

Source from the content-addressed store, hash-verified

34 self.dropout_rates = p_keep
35
36 def fit(self, X, Y, Xvalid, Yvalid, lr=1e-4, mu=0.9, decay=0.9, epochs=15, batch_sz=100, print_every=50):
37 X = X.astype(np.float32)
38 Y = Y.astype(np.int64)
39 Xvalid = Xvalid.astype(np.float32)
40 Yvalid = Yvalid.astype(np.int64)
41
42 # initialize hidden layers
43 N, D = X.shape
44 K = len(set(Y))
45 self.hidden_layers = []
46 M1 = D
47 for M2 in self.hidden_layer_sizes:
48 h = HiddenLayer(M1, M2)
49 self.hidden_layers.append(h)
50 M1 = M2
51 W = np.random.randn(M1, K) * np.sqrt(2.0 / M1)
52 b = np.zeros(K)
53 self.W = tf.Variable(W.astype(np.float32))
54 self.b = tf.Variable(b.astype(np.float32))
55
56 # collect params for later use
57 self.params = [self.W, self.b]
58 for h in self.hidden_layers:
59 self.params += h.params
60
61 # set up theano functions and variables
62 inputs = tf.placeholder(tf.float32, shape=(None, D), name='inputs')
63 labels = tf.placeholder(tf.int64, shape=(None,), name='labels')
64 logits = self.forward(inputs)
65
66 cost = tf.reduce_mean(
67 tf.nn.sparse_softmax_cross_entropy_with_logits(
68 logits=logits,
69 labels=labels
70 )
71 )
72 train_op = tf.train.RMSPropOptimizer(lr, decay=decay, momentum=mu).minimize(cost)
73 # train_op = tf.train.MomentumOptimizer(lr, momentum=mu).minimize(cost)
74 # train_op = tf.train.AdamOptimizer(lr).minimize(cost)
75 prediction = self.predict(inputs)
76
77 # validation cost will be calculated separately since nothing will be dropped
78 test_logits = self.forward_test(inputs)
79 test_cost = tf.reduce_mean(
80 tf.nn.sparse_softmax_cross_entropy_with_logits(
81 logits=test_logits,
82 labels=labels
83 )
84 )
85
86 n_batches = N // batch_sz
87 costs = []
88 init = tf.global_variables_initializer()
89 with tf.Session() as session:
90 session.run(init)
91 for i in range(epochs):
92 print("epoch:", i, "n_batches:", n_batches)
93 X, Y = shuffle(X, Y)

Callers 1

mainFunction · 0.95

Calls 6

forwardMethod · 0.95
predictMethod · 0.95
forward_testMethod · 0.95
HiddenLayerClass · 0.70
error_rateFunction · 0.70
runMethod · 0.45

Tested by

no test coverage detected