def test_copy(self): # train model #1 metric = Metric({"name": "logloss"}) nn = NeuralNetworkAlgorithm(self.params) nn.fit(self.X, self.y) y_predicted = nn.predict(self.X) loss = metric(self.y, y_predicted) # create model #2 nn2 = NeuralNetworkAlgorithm(self.params) # model #2 is not initialized in constructor self.assertTrue(nn2.model is None) # do a copy and use it for predictions nn2 = nn.copy() self.assertEqual(type(nn), type(nn2)) y_predicted = nn2.predict(self.X) loss2 = metric(self.y, y_predicted) self.assertEqual(loss, loss2) # fit model #1, there should be improvement in loss nn.fit(self.X, self.y) y_predicted = nn.predict(self.X) loss3 = metric(self.y, y_predicted) self.assertTrue(loss3 < loss) # the loss of model #2 should not change y_predicted = nn2.predict(self.X) loss4 = metric(self.y, y_predicted) assert_almost_equal(loss2, loss4)
def test_fit_predict(self): metric = Metric({"name": "logloss"}) nn = NeuralNetworkAlgorithm(self.params) loss_prev = None for _ in range(3): nn.fit(self.X, self.y) y_predicted = nn.predict(self.X) loss = metric(self.y, y_predicted) if loss_prev is not None: self.assertTrue(loss + 0.000001 < loss_prev) loss_prev = loss
def test_save_and_load(self): metric = Metric({"name": "logloss"}) nn = NeuralNetworkAlgorithm(self.params) nn.fit(self.X, self.y) y_predicted = nn.predict(self.X) loss = metric(self.y, y_predicted) with tempfile.NamedTemporaryFile() as tmp: nn.save(tmp.name) json_desc = nn.get_params() nn2 = NeuralNetworkAlgorithm(json_desc["params"]) nn2.load(tmp.name) y_predicted = nn2.predict(self.X) loss2 = metric(self.y, y_predicted) assert_almost_equal(loss, loss2)
def test_save_and_load(self): metric = Metric({"name": "logloss"}) nn = NeuralNetworkAlgorithm(self.params) nn.fit(self.X, self.y) y_predicted = nn.predict(self.X) loss = metric(self.y, y_predicted) filename = os.path.join(tempfile.gettempdir(), os.urandom(12).hex()) nn.save(filename) json_desc = nn.get_params() nn2 = NeuralNetworkAlgorithm(json_desc["params"]) nn2.load(filename) #Finished with the file, delete it os.remove(filename) y_predicted = nn2.predict(self.X) loss2 = metric(self.y, y_predicted) assert_almost_equal(loss, loss2)