Example #1
0
 def test_nnCostFunction(self):
     cost = nnCostFunction(unrolltheta(self.theta1, self.theta2), 
                           self.s_1, self.s_2, self.K, 
                           self.X, self.y, lamda=0)
     self.assertAlmostEqual(cost, 0.287629, places=6)
     
     cost_reg = nnCostFunction(unrolltheta(self.theta1, self.theta2),
                               self.s_1, self.s_2, self.K,
                               self.X, self.y, lamda=1.0)
     self.assertAlmostEqual(cost_reg, 0.383770, places=6)
Example #2
0
 def test_neuralnetwork(self):
     lamda = 1.0
     epsilon = 0.12
     initial_theta1 = generate_theta(self.theta1.shape, epsilon)
     initial_theta2 = generate_theta(self.theta2.shape, epsilon)
     initial_theta = unrolltheta(initial_theta1, initial_theta2)
     optimized_theta1, optimized_theta2 = nnTrain(initial_theta, self.s_1, self.s_2, self.K, self.X, self.y, lamda)
     predictions = nnPredict(optimized_theta1, optimized_theta2, self.X)
     expected = (self.orig_y - 1).reshape(-1)
     acc = accuracy(predictions,expected)
     print "Accuracy: {} (Should be around 95%, plus or minus 1% due to random initialization)".format(acc)
     self.assertGreater(acc, 93)