Added proper unit tests for gradient calculation
This commit is contained in:
parent
97acaaafd3
commit
2002b1faa8
@ -42,17 +42,26 @@ class GradientTestCase(unittest.TestCase):
|
|||||||
"""Test case for the calculation of the gradient of the code-constraint-polynomial."""
|
"""Test case for the calculation of the gradient of the code-constraint-polynomial."""
|
||||||
def test_grad_h(self):
|
def test_grad_h(self):
|
||||||
"""Test the gradient of the code-constraint polynomial."""
|
"""Test the gradient of the code-constraint polynomial."""
|
||||||
H = np.array([[1, 0, 0],
|
# Hamming(7,4) code
|
||||||
[0, 1, 0]])
|
G = np.array([[1, 1, 1, 0, 0, 0, 0],
|
||||||
x = np.array([1, 2, 2])
|
[1, 0, 0, 1, 1, 0, 0],
|
||||||
R = np.array([0])
|
[0, 1, 0, 1, 0, 1, 0],
|
||||||
|
[1, 1, 0, 1, 0, 0, 1]])
|
||||||
|
H = np.array([[1, 0, 1, 0, 1, 0, 1],
|
||||||
|
[0, 1, 1, 0, 0, 1, 1],
|
||||||
|
[0, 0, 0, 1, 1, 1, 1]])
|
||||||
|
R = np.array([[0, 0, 1, 0, 0, 0, 0],
|
||||||
|
[0, 0, 0, 0, 1, 0, 0],
|
||||||
|
[0, 0, 0, 0, 0, 1, 0],
|
||||||
|
[0, 0, 0, 0, 0, 0, 1]])
|
||||||
|
|
||||||
|
x = np.array([1, 2, -1, -2, 2, 1, -1]) # Some randomly chosen vector
|
||||||
|
expected_grad_h = np.array([4, 26, -8, -36, 38, 28, -32]) # Manually calculated result
|
||||||
|
|
||||||
decoder = proximal.ProximalDecoder(H, R)
|
decoder = proximal.ProximalDecoder(H, R)
|
||||||
grad = decoder._grad_h(x)
|
grad_h = decoder._grad_h(x)
|
||||||
|
|
||||||
expected = 4 * (x**2 - 1)*x + 2 / x * np.array([0, 2, 0])
|
self.assertEqual(np.array_equal(grad_h, expected_grad_h), True)
|
||||||
|
|
||||||
self.assertEqual(np.array_equal(grad, expected), True)
|
|
||||||
|
|
||||||
def test_gen_A_B(self):
|
def test_gen_A_B(self):
|
||||||
"""Test the generation of the A and B sets used for the gradient calculation."""
|
"""Test the generation of the A and B sets used for the gradient calculation."""
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user