def test_evaluate(self): modelBuilder = PytorchModelBuilder(model_creator=model_creator_pytorch, optimizer_creator=optimizer_creator, loss_creator=loss_creator) model = modelBuilder.build(config={ "lr": 1e-2, "batch_size": 32, }) model.fit_eval(data=(self.data["x"], self.data["y"]), validation_data=(self.data["val_x"], self.data["val_y"]), epochs=20) mse_eval = model.evaluate(x=self.data["val_x"], y=self.data["val_y"]) try: import onnx import onnxruntime mse_eval_onnx = model.evaluate_with_onnx(x=self.data["val_x"], y=self.data["val_y"]) np.testing.assert_almost_equal(mse_eval, mse_eval_onnx) except ImportError: pass # incremental training test model.fit_eval(data=(self.data["x"], self.data["y"]), validation_data=(self.data["val_x"], self.data["val_y"]), epochs=20) mse_eval = model.evaluate(x=self.data["val_x"], y=self.data["val_y"]) try: import onnx import onnxruntime mse_eval_onnx = model.evaluate_with_onnx(x=self.data["val_x"], y=self.data["val_y"]) np.testing.assert_almost_equal(mse_eval, mse_eval_onnx) except ImportError: pass
def test_create_not_torch_model(self): def model_creator(config): return torch.Tensor(3, 5) modelBuilder = PytorchModelBuilder(model_creator=model_creator, optimizer_creator=optimizer_creator, loss_creator=loss_creator) with pytest.raises(ValueError): model = modelBuilder.build(config={ "lr": 1e-2, "batch_size": 32, })
def test_fit_evaluate(self): modelBuilder = PytorchModelBuilder(model_creator=model_creator_pytorch, optimizer_creator=optimizer_creator, loss_creator=loss_creator) model = modelBuilder.build(config={ "lr": 1e-2, "batch_size": 32, }) val_result = model.fit_eval(data=(self.data["x"], self.data["y"]), validation_data=(self.data["val_x"], self.data["val_y"]), epochs=20) assert val_result is not None
def test_dataloader_fit_evaluate(self): modelBuilder = PytorchModelBuilder(model_creator=model_creator_pytorch, optimizer_creator=optimizer_creator, loss_creator=loss_creator) model = modelBuilder.build( config={ "lr": 1e-2, "batch_size": 32, "train_size": 500, "valid_size": 100, "shuffle": True }) val_result = model.fit_eval(data=train_dataloader_creator, validation_data=valid_dataloader_creator, epochs=20) assert model.config["train_size"] == 500 assert model.config["valid_size"] == 100 assert model.config["shuffle"] is True assert val_result is not None
def test_predict(self): modelBuilder = PytorchModelBuilder(model_creator=model_creator_pytorch, optimizer_creator=optimizer_creator, loss_creator=loss_creator) model = modelBuilder.build(config={ "lr": 1e-2, "batch_size": 32, }) model.fit_eval(data=(self.data["x"], self.data["y"]), validation_data=(self.data["val_x"], self.data["val_y"]), epochs=20) pred = model.predict(x=self.data["val_x"]) pred_full_batch = model.predict(x=self.data["val_x"], batch_size=len(self.data["val_x"])) np.testing.assert_almost_equal(pred, pred_full_batch) try: import onnx import onnxruntime pred_onnx = model.predict_with_onnx(x=self.data["val_x"]) np.testing.assert_almost_equal(pred, pred_onnx) except ImportError: pass