![transformers-2](https://private-user-images.githubusercontent.com/44279105/276710895-10b80f59-04df-4c34-93d7-889252d0aefb.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQzMDcyMDIsIm5iZiI6MTcyNDMwNjkwMiwicGF0aCI6Ii80NDI3OTEwNS8yNzY3MTA4OTUtMTBiODBmNTktMDRkZi00YzM0LTkzZDctODg5MjUyZDBhZWZiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIyVDA2MDgyMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTk5ODcyYmJkNGM0MmQzY2NhNTMzMTAxMzhlNDIzYjI5MDlmMTNmZjIzYmVjMDBlM2ZiNWQ2M2JmNTgwZjVlMTAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.wq9Tr1fXswRNiajrP02lc7VSp72r7ayMMhZjkINJGp8)
Nano-BERT is a straightforward, lightweight and comprehensible custom implementation of BERT, inspired by the foundational "Attention is All You Need" paper. The primary objective of this project is to distill the essence of transformers by simplifying the complexities and unnecessary details, making it an ideal starting point for those aiming to grasp the fundamental ideas behind transformers.
Key Features and Focus π:
-
Simplicity and Understandability: Nano-BERT prioritizes simplicity and clarity, making it accessible for anyone looking to understand the core concepts of transformers.
-
Multi-Headed Self Attention: The implementation of multi-headed self-attention is intentionally less efficient but more descriptive. Each attention head is treated as a separate object, emphasizing transparency over optimization techniques like matrix transposition and efficient multiplication.
-
Educational Purposes: This project is designed for educational purposes, offering a learning platform for individuals interested in transformer architectures.
-
Customizability: Nano-BERT allows extensive customization, enabling users to experiment with various parameters such as the number of layers, heads, and embedding sizes. It serves as a playground for exploring the impact of different configurations on model performance.
-
Inspiration: The project draws inspiration from ongoing research endeavors related to efficient LLM fine-tuning space-model. Additionally, it is influenced by the deep learning series conducted by Andrej Karpathy YouTube, particularly the nanoGPT project.
-
Motivation and Development: Nano-BERT originated from the author's curiosity about embedding custom datasets into a three-dimensional space using BERT. To achieve this, the goal was to construct a fully customizable version of BERT, providing complete control over the model's behavior. The motivation was to comprehend how BERT could handle datasets with words as tokens, diverging from the common sub-word approach.
Community Engagement π¬: While Nano-BERT is not intended for production use, contributions, suggestions, and feedback from the community are highly encouraged. Users are welcome to propose improvements, simplifications, or enhanced descriptions by creating pull requests or issues.
Exploration and Experimentation π: Nano-BERT's flexibility enables users to experiment freely. Parameters like the number of layers, heads, and embedding sizes can be tailored to specific needs. This customizable nature empowers users to explore diverse configurations and assess their impact on model outcomes.
Note: Nano-BERT was developed with a focus on educational exploration and understanding, and it should be utilized within the scope of educational and experimental contexts only!
- Python 3.10.x
- pip*
pip install torch
Note: to be able to run demos you might need some additional packages, but for base model all you needs is pytorch
pip install tqdm scikit-learn matplotlib plotly
pip
version is coming soon!
git clone https://github.com/StepanTita/nano-BERT.git
from nano_bert.model import NanoBERT
from nano_bert.tokenizer import WordTokenizer
vocab = [...] # a list of tokens (or words) to use in tokenizer
tokenizer = WordTokenizer(vocab=vocab, max_seq_len=128)
# Usage:
input_ids = tokenizer('This is a sentence') # or tokenizer(['This', 'is', 'a', 'sentence'])
# Instantiate the NanoBERT model
nano_bert = NanoBERT(input_ids)
# Example usage
embedded_text = nano_bert.embedding(input_ids)
print(embedded_text)
For all of the following experiments we use the following configuration:
n_layer = 1
n_head = 1
dropout = 0.1
n_embed = 3
max_seq_len = 128
epochs = 200
batch_size = 32
Dataset | Accuracy | F-1 Score |
---|---|---|
IMDB Sentiment (2-class) | 0.734 | 0.745 |
HateXplain Data (2-class) | 0.693 | 0.597 |
Result plots IMDB:
![Embeddings-3d-1](https://private-user-images.githubusercontent.com/44279105/276724662-ceb24c65-b210-4a59-b69b-7d1ee98015f7.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQzMDcyMDIsIm5iZiI6MTcyNDMwNjkwMiwicGF0aCI6Ii80NDI3OTEwNS8yNzY3MjQ2NjItY2ViMjRjNjUtYjIxMC00YTU5LWI2OWItN2QxZWU5ODAxNWY3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIyVDA2MDgyMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTg1OGQxYzQ2NjJiZDA2YWQyYjI0ZDMwMWM2NmZmYWNhYWE2OGNiODE3ODgzNTUzYjhiYTBkYmZiNWNhNGUxNmEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.zGb7MW7V61ZhLhQ6g0HSto4VnnK1wTm5PAIEvwbcipg)
![Embeddings-3d-2](https://private-user-images.githubusercontent.com/44279105/276724658-49319d39-052d-44d3-9ede-785a06986a1a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQzMDcyMDIsIm5iZiI6MTcyNDM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2MDgyMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWJhZGFjZjNhNDAzYmZiOTExNDBkODIwZTcwMWZjMDJhODcxOWVkYzE1N2MzMGVlZWNmMWJkNTY5ZGU4N2Y2ZjgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.gUH9IKSpo86hpZ6U-naaZbNhn-WmVVOyXBCJeCqaLmM)
![Embeddings-3d-4](https://private-user-images.githubusercontent.com/44279105/276724651-fa29c671-a95d-4414-9a9e-e2113c677bc4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQzMDcyMDIsIm5iZiI6MTcyNDMwNjkwMiwicGF0aCI6Ii80NDI3OTEwNS8yNzY3MjQ2NTEtZmEyOWM2NzEtYTk1ZC00NDE0LTlhOWUtZTIxMTNjNjc3YmM0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIyVDA2MDgyMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTNkY2IxZDcxMzI4OGMyMjI5NWEyNTlhODVlNzkxM2QyNTQzM2Y1MTkyYjJhOTEyNjNmNDE5OTg3YTM0Zjk5NWYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.SVJJ1vQGOob3eiUVbBu_bQq5QOB6QPBQVJABqpYfke8)
![Embeddings-3d-5](https://private-user-images.githubusercontent.com/44279105/276724649-158fa412-462e-4eac-a217-dc73c819029a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQzMDcyMDIsIm5iZiI6MTcyNDMwNjkwMiwicGF0aCI6Ii80NDI3OTEwNS8yNzY3MjQ2NDktMTU4ZmE0MTItNDYyZS00ZWFjLWEyMTctZGM3M2M4MTkwMjlhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIyVDA2MDgyMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTg0YTIyNjI3YzdmY2E1MTc1ZDk2YjE5MmYyY2RhNWZlNGRjYTNiNzA4ZTkzZjA5MDg0NzA2OTNlN2I1NWZkMTMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.gR2zApcLLprnH5ozQ7gTFOC1y989wk-ux6L_3U7oaYI)
Note: see demo.ipynb and imdb_demo.ipynb for better examples
This project is licensed under the MIT License. See the LICENSE.md file for details.