md • Lines: 92This document summarizes our collaborative engineering journey in building a character-level name generator. It tracks the evolution from a basic Bigram model to a complex Trigram neural network, documenting the technical hurdles, architectural decisions, and the lessons learned along the way.
---
# Project Journal: Neural Name Synthesis
**Developer:** Palash
**Thought Partner:** Gemini
**Tech Stack:** Rust, Custom `iron_learn` library, GPU (RTX 3050 Laptop), CUDA/cuBLAS
---
## 1. The Starting Point: The Bigram Model
We began with a **Bigram** approach—predicting the next character based on exactly one previous character.
* **Architecture:** A deep 8-layer network using `Tanh` hidden layers and a `Sigmoid` output.
* **The Problem:** High "phonetic noise." Because the model only knew the current letter, it lacked the "memory" to form coherent syllables.
* **Discovery (The Restoration Tax):** We noticed that stopping and restarting training caused the Loss to spike.
* *Cause:* Precision loss during JSON serialization of weights and the loss of the Optimizer’s momentum (velocity).
* *Solution:* We moved to longer, uninterrupted training runs and minimized "naps" (`-s 0`) to maintain gradient velocity.
---
## 2. The Architectural Leap: Moving to Trigrams
To improve name quality, we shifted to a **Trigram** model, providing the network with two characters of context.
* **Data Refactoring:** We used `windows(3)` to slice the training data.
* **Input Engineering:** We doubled the input vector size. We used one-hot encoding for two characters, concatenated into a 50-wide vector ().
* **The Logic Fix:** We identified a critical bug in the generation loop where the "context window" wasn't sliding. We implemented a tuple-based shift: `context = (context.1, next_char)`.
---
## 3. The Hyperparameter War
With the Trigram model, the search space became more complex. We entered a cycle of aggressive tuning to find the "Golden Path."
### The Conflict of Learning Rates (LR)
* **The Vanishing Gradient:** At , the 8-layer `Tanh` network was too stable. The signal died before reaching the early layers.
* **The Exploding Gradient:** At , the weights grew uncontrollably, resulting in the dreaded `NaN` (Not a Number) output.
* **The Breakthrough:** We found that a wider hidden layer (40 neurons) with a moderate () allowed the model to actually "learn" phonetic structures.
---
## 4. The Loss Paradox: Low Numbers vs. Junk Output
We reached a milestone where the **Loss (BCE)** dropped from **60,000** to **1,026**. Mathematically, the model was "perfecting" its task, but the names became *more* like gibberish (`pxxxdj`, `vhmpioa`).
### The Diagnosis: The Sigmoid Trap
We realized the model was suffering from **Overfitting to Noise**.
* **Independent Outputs:** Because we used `Sigmoid` with `Binary Cross Entropy`, the model treated each of the 25 letters as a separate Yes/No question.
* **The Result:** The model learned it could say "Yes" to 'x', 'p', and 'j' simultaneously. It wasn't being forced to choose the *single best* next character.
---
## 5. Current Status and Next Steps
We have successfully built a high-performance GPU training pipeline. The model "knows" the data, but its "decision-making" (activation logic) needs an upgrade.
### The Engineering Roadmap:
1. **Switch to ReLU:** Implement `max(0, x)` to replace `Tanh`. This will solve the vanishing gradient issue and allow for faster, deeper learning.
2. **Implement Softmax:** Replace the final `Sigmoid` layer. Softmax will force the 25 outputs to sum to , creating a true probability distribution and forcing the model to pick a "winner."
3. **Cross-Entropy Loss:** Transition from BCE to Categorical Cross-Entropy to better align with the Softmax "winner-takes-all" logic.
---
## 6. Key Takeaways for the Blog
* **Loss is a Liar:** A decreasing loss curve doesn't always mean a better user experience; it depends on how the loss is measured (Sigmoid vs. Softmax).
* **Momentum Matters:** In deep learning, the "state" of the optimizer is as important as the weights themselves.
* **Context is King:** Moving from 1 to 2 characters of history (Bigram to Trigram) fundamentally changes the phonetic potential of the generator.
---
**Would you like me to draft a "Technical Appendix" for this document that includes the specific Rust math for the ReLU and Softmax kernels we discussed?**
\
https://gemini.google.com/share/302541a8e864
After all these happened, I fixed a lot of clippy comments.
I enabled feature gated and feature non-gated modules README.md doc tests