Positional Encoding incorporates information about the position of each embedding within a sequence.
Types:
Static parameters.
Learnable parameters (GPT models).
Other: Segment embeddings (BERT) - providing additional positional information.
Fomula to calculte the Positional Encoding PE:
pos: position - representing the position of the sine wave over time.
i: dimension index - controlling the number of oscillations for each wave.
Attention: focusing on the most relevant parts of the input data and their relationships.
The attention mechanism uses a comparable structure, but instead of string, it employs one-hot encoded vectors. [0, 1, 0, 0 , 0]
Attention mechanism employ the query, key and value matrices.
The query vector should align with the same row across the key and value matrices.
Attention mechanism can be applied to word embeddings to capture contextual relationships between words.
Softmax function can be incorporated on the output of the dot product between the query vector and the key to refine the attention formula.
For employing attention for sequences, you can consolidate all the query vectors into a single matrix.
In embedding sequence, words transform into matrices.
Self-attention mechanism in simple language modeling generate Query, Key and Value.
In context, embedding Query, Key and Value helps to modify the input row vectors.
In embedding sequence, words transform into matrices.
Self-attention mechanism in simple language modeling generate Query, Key and Value.
In context, embedding Query, Key and Value helps to modify the input row vectors.
You can retain context while classifying text by integrating transformer attention layers.
To create the text pipeline:
Create iterators, allocate training set, generate tokens, and construct a dataloader.
Design a custom collate function, apply padding, and create a data loader.
To create the model:
Instantiate the embedding layer, add positional encoding, and applty the transformer encoder layers.
Use the classifier layer to predict the label to which the input text belongs.
To train the model: Use the same process as a standard classification problem.
Gradient Accumulation is a technique that enables training with smaller batch sizes by accumulating gradients over multiple steps before applying them.
Useful when memory limiatations prevent large batch sizes from being used.
Memory Efficiency: By accumulating gradients across several mini-batches, gradient accumulation reduces memory load, allowing models to simulate the effect of a larger batch size.
Performance Gains: Accumulating gradients without frequent updates can stabilize training, improving model performance, especially on complex datasets.
Mixed-Precision training uses lower precision for specific calculations during training, which helps reduce memory comsumption and accelerates computation without substantially affecting accuracy.
Half-Precision Calculation: Caculations in half-precision (FP16) significantly reduce memory usage and speed up training times, especially effective for large models with high computational demands.
Automatic Mixed Precision (AMP): Frameworks like Pytorch and TensorFlow support AMP, which automatically selects the optimal precision for each operation.
Distributed Training enables the use of multiple GPUs or TPUs in parallel, reducing the overall training time.:
Data Parallelism: Each device receives a portion of the data batch, processes it, and then synchronizes gradients across all devices.
Model Parallelism: Split large model across multiple devices, enabling larger models to be trained.
Selecting the right optimizer can make a difference in both speed and performance:
AdamW (Adam with Weight Decay): combination between Adam's adaptive learning and weight decay, which helps in generalizing better during training.
LAMB (Layer-wise Adaptive Moments): Used in large-batch training.