Skip to content

Add Problem #41: Tanh & Soft-Capping - #10

Closed
ThierryHJ wants to merge 1 commit into
duoan:masterfrom
ThierryHJ:feature/add-tanh
Closed

Add Problem #41: Tanh & Soft-Capping#10
ThierryHJ wants to merge 1 commit into
duoan:masterfrom
ThierryHJ:feature/add-tanh

Conversation

@ThierryHJ

Copy link
Copy Markdown
Contributor

Summary

Adds a new Easy-difficulty problem where users implement tanh activation using three parts:

  • Forward (my_tanh) β€” compute tanh from scratch using exp, numerically stable via 2/(1+exp(-2x))-1
  • Manual Backward (tanh_backward) β€” compute gradient using 1 - tanhΒ²(x), note it depends on the output not the input
  • Soft-Capping (soft_cap_logits) β€” cap Β· tanh(logits/cap), a technique used in Gemma 2 and Griffin to bound logits

Motivation

  • Tanh is a classic activation function commonly asked in ML interviews
  • Implementing it from scratch tests understanding of exponentials, numerical stability, and the chain rule
  • Logit soft-capping is a modern technique (Gemma 2, Griffin) that applies tanh in a practical setting β€” good bonus context for interviews
  • Complements existing activation exercises: ReLU (KV cache MHA inferenceΒ #1) and GELU (Refactor Makefile for OS detection and commandsΒ #19)

Files Changed

FileStatusDescription
torch_judge/tasks/tanh.pyNEWTask definition with 6 test cases
templates/41_tanh.ipynbNEWTemplate notebook (blank stubs for 3 functions)
solutions/41_tanh_solution.ipynbNEWReference solution (numerically stable)
README.mdModifiedAdded row #41 to Fundamentals table, updated badge 40β†’41

Test Cases

#Test NameWhat It Verifies
1Matches torch.tanhOutput matches reference with atol=1e-5
2tanh(0) = 0 and bounded outputBoundary behavior, output in (-1, 1) for Β±100
3Shape preservationMulti-dim tensor shape unchanged
4Gradient flowbackward() succeeds, grad shape correct
5Manual backward (tanh_backward)Manual grad matches autograd
6Soft-capping bounds logitsOutput bounded within (-cap, cap), matches reference

Conventions Followed

  • Auto-discovery via _registry.py β€” no manual registration needed
  • Standard notebook format (markdown β†’ import β†’ implementation β†’ debug β†’ submit)
  • Colab badge on both template and solution notebooks
  • {fn} placeholder in all test code
  • torch.allclose() for numerical comparison with appropriate tolerances
  • Difficulty emoji consistent with existing Easy problems (🟒)

How to Test

# From repo root
python3 -c "from torch_judge.tasks._registry import TASKSprint('tanh' in TASKS) # True"

Open solutions/41_tanh_solution.ipynb in JupyterLab and run all cells β€” the final check('tanh') should show 6/6 tests passed.

Add Problem #41 covering tanh forward, manual backward, and logit
soft-capping (used in Gemma 2, Griffin). Includes numerically stable
implementation using 2/(1+exp(-2x))-1 to handle large inputs.
- templates/41_tanh.ipynb: template with 3 function stubs
- solutions/41_tanh_solution.ipynb: working solution (stable formula)
- torch_judge/tasks/tanh.py: 6 judge tests (accuracy, bounds, shape,
gradient, manual backward, soft-capping)
- README.md: add row to Fundamentals table, bump badge 40 β†’ 41
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@duoan

Copy link
Copy Markdown
Owner

Hi @ThierryHJ , thanks for contribution.

This is a fundamental problem of math which would not align the repo goal.

@duoanduoan closed this May 25, 2026
Sign up for freeto 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

@ThierryHJ@duoan