Skip to content

Commit 790be73

Browse files
committed
updae test.
1 parent ea03270 commit 790be73

1 file changed

Lines changed: 11 additions & 19 deletions

File tree

tests/test_mnist.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,25 @@
22

33
import sys
44

5+
import numpy as np
56
import pytest
67
import torch as th
78

89
sys.path.insert(0, "./src/")
910

1011
from src.mnist import cross_entropy, normalize_batch
1112

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:
2814
"""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+
3224

3325

3426
norm_testdata = [

0 commit comments

Comments
 (0)