Here are some tips for training small transformer language models:
Use a small model size. For transformers, this means using fewer layers, fewer attention heads, and smaller feedforward network dimensions. For example, you can try a model with 2-3 layers, 2-4 attention heads, and a feedforward dimension of 128-512.
Use a smaller vocabulary size. Limit your vocabulary to the most frequent words, around 5,000-20,000 words. This will make your model faster to train and reduce the number of parameters.
Share word embeddings between encoder and decoder. This ties the encoder and decoder representations together and allows knowledge to transfer between them. It also reduces the number of parameters.
Use a smaller batch size. A batch size of 8-32 works well for small models. This will make training faster and require less memory.
Use mixed precision training. Training with half-precision floating point numbers (FP16) instead of single precision (FP32) can make transformer models train up to twice as fast, allowing you to train larger models.
Share parameters between layers. You can share parameters like word embeddings, position embeddings, and layer normalization between transformer layers. This reduces the number of total parameters and can improve performance of small models.
Use transfer learning. Initialize your small model with the weights from a larger, pre-trained model like BERT or GPT-2. Then continue training on your target task. This leverages the knowledge in the pre-trained model and leads to better performance with less training data and compute.
Use checkpoint averaging. Average the weights from multiple checkpoints during training instead of just using the final checkpoint. This can produce a model with better performance, even when training with limited data.
Use data augmentation. Techniques like synonym replacement, random deletion, and backtranslation can be used to increase your training data and improve small model performance.
Hope this helps! Let me know if you have any other questions.