|
2 | 2 |
|
3 | 3 | import sys |
4 | 4 |
|
| 5 | +import numpy as np |
5 | 6 | import pytest |
6 | 7 | import torch as th |
7 | 8 |
|
8 | 9 | sys.path.insert(0, "./src/") |
9 | 10 |
|
10 | 11 | from src.mnist import cross_entropy, normalize_batch |
11 | 12 |
|
12 | | -testdata = [ |
13 | | - ( |
14 | | - th.tensor([[1.0, 0.0], [0.0, 1.0], [0.0, 0.0]]), |
15 | | - th.tensor([[0.2, 0.12], [0.42, 0.21], [0.22, 0.34]]), |
16 | | - th.tensor(1.5022, dtype=th.float32), |
17 | | - ), |
18 | | - ( |
19 | | - th.tensor([[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 1.0]]), |
20 | | - th.tensor([[0.8, 0.11], [0.22, 0.22], [0.1, 0.3], [0.08, 0.19]]), |
21 | | - th.tensor(0.7607, dtype=th.float32), |
22 | | - ), |
23 | | -] |
24 | | - |
25 | | - |
26 | | -@pytest.mark.parametrize("label, out, res", testdata) |
27 | | -def test_cross_entropy(label, out, res) -> None: |
| 13 | +def test_cross_entropy() -> None: |
28 | 14 | """Test if the cross entropy is implemented correctly.""" |
29 | | - result = cross_entropy(label=label, out=out) |
30 | | - ce = th.round(result, decimals=4) |
31 | | - assert th.allclose(ce, res) |
| 15 | + label = th.from_numpy(np.random.randint(0, 1, (64, 10)).astype(np.float64)) |
| 16 | + out = th.from_numpy(np.random.randn(64, 10)) |
| 17 | + out = out.softmax(dim=-1) |
| 18 | + my_result = cross_entropy(label=label, out=out) |
| 19 | + th_result = th.nn.functional.binary_cross_entropy( |
| 20 | + input=out, target=label, reduction="mean" |
| 21 | + ) |
| 22 | + assert th.allclose(my_result, th_result) |
| 23 | + |
32 | 24 |
|
33 | 25 |
|
34 | 26 | norm_testdata = [ |
|
0 commit comments