CNNGPT - CNNGPT
Project description
CNN-Based Language Model
Detailed Explanation of Each Step
Initialization Parameters
vocab_size
: The size of the vocabulary (number of unique tokens).embedding_dim
: The dimension of the embeddings.num_layers
: The number of convolutional layers.kernel_size
: The size of the convolutional kernels.hidden_dim
: The dimension of the hidden representations (should matchembedding_dim
for residual connections).max_seq_len
: The maximum sequence length the model can handle.
Embedding and Positional Encoding
- Embeddings: Convert token IDs to dense vectors.
- Positional Encoding: Adds a learnable positional embedding to each token embedding.
Convolutional Blocks
- Causal Convolution: Uses padding on the left to ensure that the convolution at time
t
does not depend on future time steps. - Dilation: Expands the receptive field exponentially, allowing the model to capture long-term dependencies.
- GLU Activation: Introduces a gating mechanism that can control the flow of information.
- The output of the convolution is split into two halves along the channel dimension.
- One half is passed through a sigmoid function to act as a gate for the other half.
- Layer Normalization: Normalizes the outputs to improve training stability.
- Residual Connections: Adds the input to the output to facilitate training deeper networks.
Output Layer
- Projection: Maps the final hidden states to the vocabulary space to produce logits for each token.
Handling Tensor Sizes
Throughout the network, we carefully manage tensor shapes to maintain consistency:
- After embedding and positional encoding:
[batch_size, seq_len, embedding_dim]
- Before convolution: Transposed to
[batch_size, embedding_dim, seq_len]
- After convolution and GLU:
[batch_size, hidden_dim, seq_len]
- After layer normalization and residual connection: Same shape as input to convolution for residual addition.
- Before output layer: Transposed back to
[batch_size, seq_len, embedding_dim]
- Output logits:
[batch_size, seq_len, vocab_size]
Important Notes
- Causality: By appropriately padding and slicing the convolution outputs, we ensure that the model does not use future information when predicting the current time step.
- Residual Connections: The
embedding_dim
andhidden_dim
must be equal to correctly add the residual connection. - Layer Normalization: Applied over the feature dimension; we transpose the tensor to
[batch_size, seq_len, hidden_dim]
before applyingLayerNorm
. - GLU Activation Function: The gating mechanism enhances the model's capacity to model complex patterns.
- Flexibility: The model can handle sequences shorter than
max_seq_len
; positional encodings are sliced accordingly.
Conclusion
We have successfully translated the detailed algorithm into a PyTorch implementation, carefully following each step and ensuring that the code aligns with the design principles outlined earlier. This CNN-based language model leverages causal and dilated convolutions, gated activations, residual connections, and layer normalization to effectively model textual data for generation tasks.
By understanding each component and its role in the model, we can appreciate how this architecture captures both local and global dependencies in language, offering a powerful alternative to traditional models in natural language processing.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cnngpt-0.0.1.tar.gz
.
File metadata
- Download URL: cnngpt-0.0.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.3 Darwin/23.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e47046095da9740ce6a660749c9d746dbc3acc97dda1008e3fca833a89cebae7 |
|
MD5 | eb55af3ff91c12852982901279096670 |
|
BLAKE2b-256 | 2bf875812ad0ae13201e28f9c72a4ca2654f3fde82a4ac51d9f6113688acd7e6 |
File details
Details for the file cnngpt-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: cnngpt-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.3 Darwin/23.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccdbe248ca45e12a9caa5ca4282ab58303393d29abd04f77463be88974559d05 |
|
MD5 | 18b91ec32dca668ae7a1ea64d1042fdc |
|
BLAKE2b-256 | bbd7b8347fa4e97bb0945ddb2834b351e2d83a17d6b5bbeaade6305b25e3522d |