-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathtorchscript_example_test.py
More file actions
61 lines (51 loc) · 2.5 KB
/
Copy pathtorchscript_example_test.py
File metadata and controls
61 lines (51 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import torch
import unittest
import numpy as np
class TestTorchScriptModel(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Load the TorchScript model
cls.model = torch.jit.load('notebooks/simple_model_scripted.pt')
cls.model.eval() # Set the model to evaluation mode
def test_model_output_shape(self):
"""Test if the model outputs the correct shape."""
input_tensor = torch.randn(1, 5) # Adjust shape based on model input requirements
output_tensor = self.model(input_tensor)
self.assertEqual(output_tensor.shape, (1, 5), "Output shape mismatch")
def test_model_output_values(self):
"""Test if the model output values are within an expected range."""
input_tensor = torch.randn(1, 5)
output_tensor = self.model(input_tensor)
# Example: Check if all output values are within the range -1 to 1
self.assertTrue(torch.all(output_tensor >= -1) and torch.all(output_tensor <= 1),
"Output values out of expected range")
def test_model_with_different_inputs(self):
"""Test the model with various types of inputs to ensure robustness."""
inputs = [
torch.zeros(1, 5),
torch.ones(1, 5),
torch.randn(1, 5),
torch.full((1, 5), 0.5)
]
for input_tensor in inputs:
output_tensor = self.model(input_tensor)
self.assertEqual(output_tensor.shape, (1, 5), "Output shape mismatch with different inputs")
def test_model_gradients(self):
"""Test if the model's gradients are computed correctly."""
input_tensor = torch.randn(1, 5, requires_grad=True)
output_tensor = self.model(input_tensor)
output_tensor.sum().backward()
self.assertIsNotNone(input_tensor.grad, "Gradients were not computed")
def test_scripted_model_serialization(self):
"""Test if the scripted model can be reloaded and produce consistent outputs."""
input_tensor = torch.randn(1, 5)
output_original = self.model(input_tensor)
# Save and reload the scripted model
torch.jit.save(self.model, 'test_scripted_model.pt')
reloaded_model = torch.jit.load('test_scripted_model.pt')
reloaded_model.eval()
output_reloaded = reloaded_model(input_tensor)
self.assertTrue(torch.allclose(output_original, output_reloaded),
"Outputs differ after reloading the scripted model")
if __name__ == '__main__':
unittest.main()