File size: 825 Bytes
9d61c9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Required Libraries
import unittest

import torch

from models.helpers import positional_encoding


class TestPositionalEncoding(unittest.TestCase):
    def test_positional_encoding(self):
        # Test with d_model=128, length=10
        d_model = 128
        length = 10
        result = positional_encoding(d_model, length)

        # Assert that output is a torch.Tensor
        self.assertIsInstance(result, torch.Tensor)

        # Assert the output tensor shape is correct
        # The extra dimension from unsqueeze is considered
        expected_shape = (1, length, d_model)
        self.assertEqual(result.shape, expected_shape)

        # Assert that values lie in the range [-1, 1]
        self.assertTrue(torch.all((result >= -1) & (result <= 1)))


# Run tests
if __name__ == "__main__":
    unittest.main()