예제 #1
0
    def test_inference_runner_multiple_tensor_features(self):
        examples = torch.from_numpy(
            np.array([1, 5, 3, 10, -14, 0, 0.5, 0.5],
                     dtype="float32")).reshape(-1, 2)
        examples = [
            torch.from_numpy(np.array([1, 5], dtype="float32")),
            torch.from_numpy(np.array([3, 10], dtype="float32")),
            torch.from_numpy(np.array([-14, 0], dtype="float32")),
            torch.from_numpy(np.array([0.5, 0.5], dtype="float32")),
        ]
        expected_predictions = [
            PredictionResult(ex, pred) for ex, pred in zip(
                examples,
                torch.Tensor([f1 * 2.0 + f2 * 3 + 0.5
                              for f1, f2 in examples]).reshape(-1, 1))
        ]

        model = PytorchLinearRegression(input_dim=2, output_dim=1)
        model.load_state_dict(
            OrderedDict([('linear.weight', torch.Tensor([[2.0, 3]])),
                         ('linear.bias', torch.Tensor([0.5]))]))
        model.eval()

        inference_runner = PytorchInferenceRunner(torch.device('cpu'))
        predictions = inference_runner.run_inference(examples, model)
        for actual, expected in zip(predictions, expected_predictions):
            self.assertTrue(_compare_prediction_result(actual, expected))
예제 #2
0
 def test_num_bytes(self):
     inference_runner = PytorchInferenceRunner(torch.device('cpu'))
     examples = torch.from_numpy(
         np.array([1, 5, 3, 10, -14, 0, 0.5, 0.5],
                  dtype="float32")).reshape(-1, 2)
     self.assertEqual((examples[0].element_size()) * 8,
                      inference_runner.get_num_bytes(examples))
예제 #3
0
 def test_namespace(self):
     inference_runner = PytorchInferenceRunner(torch.device('cpu'))
     self.assertEqual('RunInferencePytorch',
                      inference_runner.get_metrics_namespace())