File size: 2,958 Bytes
aa0bed2
e2f8b29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa0bed2
e2f8b29
aa0bed2
e2f8b29
 
aa0bed2
 
 
e2f8b29
aa0bed2
e2f8b29
 
aa0bed2
 
e2f8b29
aa0bed2
e2f8b29
 
 
 
 
 
 
 
 
 
 
 
aa0bed2
e2f8b29
 
aa0bed2
 
 
e2f8b29
aa0bed2
e2f8b29
 
aa0bed2
 
e2f8b29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa0bed2
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Test training curve generators — now using real mini-training."""

from __future__ import annotations

from ml_training_debugger.scenarios import sample_scenario
from ml_training_debugger.simulation import (
    gen_data_batch_stats,
    gen_loss_history,
    gen_val_accuracy_history,
    gen_val_loss_history,
)


class TestGenLossHistory:
    def test_returns_20_floats(self):
        s = sample_scenario("task_001", seed=42)
        hist = gen_loss_history(s)
        assert len(hist) == 20
        assert all(isinstance(v, (float, int)) for v in hist)

    def test_task_001_has_instability(self):
        s = sample_scenario("task_001", seed=42)
        hist = gen_loss_history(s)
        # With high LR, loss should show instability (high max or spikes)
        max_loss = max(v for v in hist if v != float("inf"))
        assert max_loss > 5.0  # Real training with high LR produces spikes

    def test_task_003_reasonable(self):
        s = sample_scenario("task_003", seed=42)
        hist = gen_loss_history(s)
        # Data leakage — training looks normal
        assert all(v != float("inf") for v in hist)

    def test_task_005_no_crash(self):
        s = sample_scenario("task_005", seed=42)
        hist = gen_loss_history(s)
        assert len(hist) == 20


class TestGenValAccuracy:
    def test_returns_20_floats(self):
        s = sample_scenario("task_001", seed=42)
        hist = gen_val_accuracy_history(s)
        assert len(hist) == 20
        assert all(isinstance(v, float) for v in hist)

    def test_task_003_leakage_shows_higher_acc(self):
        s = sample_scenario("task_003", seed=42)
        hist = gen_val_accuracy_history(s)
        # With data leakage, val accuracy should be somewhat elevated
        avg_acc = sum(hist) / len(hist)
        assert avg_acc > 0.0  # At minimum non-zero

    def test_task_005_low_accuracy(self):
        s = sample_scenario("task_005", seed=42)
        hist = gen_val_accuracy_history(s)
        # BatchNorm eval mode — model can't learn properly
        assert len(hist) == 20


class TestGenValLoss:
    def test_returns_20_floats(self):
        s = sample_scenario("task_001", seed=42)
        hist = gen_val_loss_history(s)
        assert len(hist) == 20


class TestGenDataBatchStats:
    def test_leakage_high_overlap(self):
        s = sample_scenario("task_003", seed=42)
        stats = gen_data_batch_stats(s)
        assert stats["class_overlap_score"] > 0.5
        assert stats["duplicate_ratio"] > 0.0

    def test_normal_low_overlap(self):
        s = sample_scenario("task_001", seed=42)
        stats = gen_data_batch_stats(s)
        assert stats["class_overlap_score"] < 0.3

    def test_confusion_matrix_present(self):
        s = sample_scenario("task_003", seed=42)
        stats = gen_data_batch_stats(s)
        assert "confusion_matrix" in stats
        cm = stats["confusion_matrix"]
        assert len(cm) == 10
        assert len(cm[0]) == 10