This project builds a decoder-only Transformer trained to generate text character by character, mimicking Victor Hugo's writing style. The architecture follows the original "Attention Is All You Need" paper, with improvements such as residual connections and pre-layer normalization from the "On Layer Normalization in the Transformer Architecture" paper.
The model is a stack of Transformer blocks, each combining a multi-head causal self-attention layer with a position-wise feed-forward network.
- Embedding: token embeddings + learned positional embeddings
- Attention: multi-head causal self-attention (4 heads, head size 32)
- Depth: 6 Transformer blocks
- Embedding dimension: 128
- Regularization: dropout (0.2) on attention weights and feed-forward activations
| Hyperparameter | Value |
|---|---|
| Batch size | 32 |
| Block size (context) | 64 |
| Training steps | 5000 |
| Learning rate | 3e-4 |
| Optimizer | AdamW |
| Train / val split | 90% / 10% |
Loss is tracked every 100 steps on both the training and validation sets.
The model was prompted with an empty context and generated 2000 characters after training (text contains grammatical and semantic errors since it's a small charachter-level model trained with limited capacity)
Vos chasses ! comme une au bristre et la pourtant,
Le bouche de trand la pluie !
Dansez de vers la tortes écer ses de luttens ; Tou pense un femme un brance.
Au caprond frontement dans le l'ombre de front, Devemble du rémeille, Viene sous l'espdez des choses distins-feur ;
Le vieux qui voiller il j'astendis ; mais juis mais parte de fronter.
Je préférait se vent sudans les flammes ?
Leurs pensées, les pleines près sont le maint passé, Ni gain regardent de la combre un pensée, Et roule availle à sur les monts fronts d'étent l'ébrange, Mais dans leur corps sans les fembles ;
Les pieds du petitessérant ses pois. La côte est comme un compre ; Ma larche femme sourd saint la noute. Et ! Me voir que desturs cherf, De l'herbe alles de d'une rétreau qui vene tourre obscur quans les arges.
Prète leur ! sont iciffazait ! Pour cache voir ! Le vole mortére, c'est l'ombre au choir,
Son erra démon n'enfant sante échante ;
Il s'arche sous ma veul, plaise de siècle à s'éclaire,
Les larrge deux fixants,
De le ténèbre et rêve, La forte !
...
Place your training data at data/victor_hugo-texts.txt, then run:
python model.pyThe script will print training progress, save the loss plot, and output a 2000-character sample generated by the trained model.
- Vaswani et al., Attention Is All You Need (2017)
- Xiong et al., On Layer Normalization in the Transformer Architecture (2020)