Skip to content

feat: 損失・学習ループ・公式 main(losses / trainer / main) - #57

Closed
koki3070 wants to merge 3 commits into
uda-lab:stack/pr5-distance-persistencefrom
koki3070:pr6-losses-trainer-main
Closed

feat: 損失・学習ループ・公式 main(losses / trainer / main)#57
koki3070 wants to merge 3 commits into
uda-lab:stack/pr5-distance-persistencefrom
koki3070:pr6-losses-trainer-main

Conversation

@koki3070

Copy link
Copy Markdown
Contributor

概要

PR5(距離バックエンド・持続ホモロジー)の上に、損失・学習ループ・公式エントリポイントを追加する PR です(分割 PR の 6 本目)。

  • tda_ml/losses.py: 分類 BCE、楕円サイズ/異方性正則化、位相 Wasserstein 損失(distance_backend 対応)。
  • tda_ml/trainer.py: 学習・検証ループ、AMP、可視化フック、clean PD 計算。
  • tda_ml/main.py: NoisyMNISTDataset + Trainer による公式学習入口(python -m tda_ml.main --config dev 等)。
  • tda_ml/visualization.py: エポックごとの GT/予測/楕円プロット。
  • テスト: tests/test_trainer.py, tests/test_losses_topo_distance.py

Depends on #56(PR5)。未マージ時は PR1〜5 の変更も差分に含まれる場合があります。

楕円パラメータ

  • points: (N, 2) 各点の座標。
  • params: (N, 3) 各点の [a, b, theta](中心は points)。

位相損失とバックエンド

  • Mahalanobis: TopologicalLosssigmoid(logits) を outlier 確率として距離に織り込む。
  • ellphi: 確率重みは未実装(distance_backend と同様)。公平比較の注意は PR7 の README 参照。

この PR に含めないもの

  • experiments/run_backend_multiseed.py, REPRODUCIBILITY.mdPR7
  • CI の pytest / 再現スモーク(引き続き ruff のみ

テスト計画

  • CI: Ruff 成功
  • (任意)uv run pytest tests/test_trainer.py tests/test_losses_topo_distance.py
  • (任意)uv run python -m tda_ml.main --config dev(3 epoch スモーク)

losses, trainer, main, visualization と対応テスト。
Ellipse params [a,b,theta] at each point in points.

Co-authored-by: Cursor <cursoragent@cursor.com>
@t-uda
t-uda changed the base branch from main to stack/pr5-distance-persistence May 25, 2026 05:21

t-uda commented May 25, 2026

Copy link
Copy Markdown
Contributor

Codex review item 3(NaN skip 後に全 batch skip だと metrics が空入力になり得る件)については、確かに制御フロー上の edge case ではありますが、私は merge blocker ほどではない と見ています。

現状の train_epoch() は NaN loss の batch を skip し、epoch 末で train metrics を計算します。したがって、理屈の上では「その epoch の全 batch が NaN で skip された場合」に all_train_labels / all_train_preds が空のまま sklearn metrics に渡る可能性があります。

ただし、これは通常の設定で頻繁に起きる failure mode ではなく、数値的に完全に壊れた config / backend の検出に近い状況です。その意味では、研究実験の安全性としては優先度 medium 以下でよいと思います。

軽く直すなら、epoch 末の metrics 計算前に

if steps_completed == 0 or not all_train_labels:
    raise RuntimeError(
        f"All training batches were skipped at epoch={epoch}; loss was NaN for every batch."
    )

程度の明示的な controlled failure を入れるのが十分です。NaN metrics を無理に返して training を継続するより、ここまで壊れた場合は明示的に落とす方が分かりやすいと思います。

koki3070 and others added 2 commits May 28, 2026 14:09
Raise RuntimeError instead of calling sklearn metrics on empty labels
when an entire epoch produced only NaN-skipped batches.

Co-authored-by: Cursor <cursoragent@cursor.com>
@koki3070

Copy link
Copy Markdown
Contributor Author

epoch 末の train metrics 計算前に、全 batch が NaN skip された場合は RuntimeError で明示的に落とすようにしました。通常運用では起きにくい edge case ですが、空入力で metrics を計算するより分かりやすい挙動にしています。

Comment thread tda_ml/losses.py

if self.mode == 'barrier':
barrier_term = F.relu(aspect_ratios - self.barrier_threshold).pow(2).mean()
loss = 10.0 * barrier_term

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#60

Comment thread tda_ml/trainer.py
clean_pc = clean_pc.to(self.device, non_blocking=True)

if self.config.get('training', {}).get('rotation_augmentation', False):
theta = torch.rand(1, device=self.device) * 2 * 3.141592653589793

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#60

@t-uda t-uda left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@t-uda

t-uda commented May 29, 2026

Copy link
Copy Markdown
Contributor

スカッシュコミット da38fc4 を main に直接 push して適用済み(PR#56 同様、squash merge 累積によるコンフリクト回避のためローカルでマージ)。

@t-uda t-uda closed this May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants