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

Method fit

ann_class2/batch_norm_tf.py:92–166  ·  view source on GitHub ↗
(self, X, Y, Xtest, Ytest, activation=tf.nn.relu, learning_rate=1e-2, epochs=15, batch_sz=100, print_period=100, show_fig=True)

Source from the content-addressed store, hash-verified

90 self.session = session
91
92 def fit(self, X, Y, Xtest, Ytest, activation=tf.nn.relu, learning_rate=1e-2, epochs=15, batch_sz=100, print_period=100, show_fig=True):
93 X = X.astype(np.float32)
94 Y = Y.astype(np.int32)
95
96 # initialize hidden layers
97 N, D = X.shape
98 self.layers = []
99 M1 = D
100 for M2 in self.hidden_layer_sizes:
101 h = HiddenLayerBatchNorm(M1, M2, activation)
102 self.layers.append(h)
103 M1 = M2
104
105 # final layer
106 K = len(set(Y))
107 h = HiddenLayer(M1, K, lambda x: x)
108 self.layers.append(h)
109
110 if batch_sz is None:
111 batch_sz = N
112
113
114 # note! we will need to build the output differently
115 # for train and test (prediction)
116
117 # set up theano functions and variables
118 tfX = tf.placeholder(tf.float32, shape=(None, D), name='X')
119 tfY = tf.placeholder(tf.int32, shape=(None,), name='Y')
120
121 # for later use
122 self.tfX = tfX
123
124 # for training
125 logits = self.forward(tfX, is_training=True)
126 cost = tf.reduce_mean(
127 tf.nn.sparse_softmax_cross_entropy_with_logits(
128 logits=logits,
129 labels=tfY
130 )
131 )
132 # train_op = tf.train.AdamOptimizer(learning_rate).minimize(cost)
133 # train_op = tf.train.RMSPropOptimizer(learning_rate, decay=0.99, momentum=0.9).minimize(cost)
134 train_op = tf.train.MomentumOptimizer(learning_rate, momentum=0.9, use_nesterov=True).minimize(cost)
135 # train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
136
137 # for testing
138 test_logits = self.forward(tfX, is_training=False)
139 self.predict_op = tf.argmax(test_logits, 1)
140
141 # accuracy = tf.reduce_mean(1.0*(tfY == tf.argmax(logits, 1)))
142
143 # init the variables
144 self.session.run(tf.global_variables_initializer())
145
146 n_batches = N // batch_sz
147 costs = []
148 for i in range(epochs):
149 if n_batches > 1:

Callers 1

mainFunction · 0.95

Calls 5

forwardMethod · 0.95
scoreMethod · 0.95
HiddenLayerClass · 0.70
runMethod · 0.45

Tested by

no test coverage detected