https://github.com/aryashah2k/nlp-nlu
Repository holding code assignments for AT82.05 Artificial Intelligence: Natural Language Understanding (NLU) taught by Dr. Chaklam Silpasuwanchai @ AIT
Science Score: 36.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
✓Committers with academic emails
1 of 1 committers (100.0%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (10.1%) to scientific vocabulary
Repository
Repository holding code assignments for AT82.05 Artificial Intelligence: Natural Language Understanding (NLU) taught by Dr. Chaklam Silpasuwanchai @ AIT
Basic Info
Statistics
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
NLP Assignments
This repository contains my assignments for the AT82.05 Artificial Intelligence: Natural Language Understanding (NLU) course at AIT.
|Table of Contents| |-----------------| |A1: That's What I LIKE!| |A2: Language modelling| |A3: Make Your Own Machine Translation Language| |A4: Do You AGREE!| |A5: Optimization Human Preference| |A6: Lets Talk Yourselves!| |A7: Distillation Get Smaller, Get Faster| |Setup & Installation|
Project Structure
nlp-assignments/
app/ # Main Flask application
static/ # Static files (CSS, JS, images)
templates/ # HTML templates
assignments/ # Assignment-specific modules
a1_word_embeddings/
models/ # Trained models
utils.py # Utility functions
a2_language_model/ # Future assignment
core/ # Core application code
__init__.py
routes.py # Flask routes
data/ # Dataset storage
notebooks/ # Jupyter notebooks
reports/ # Assignment Report PDFs
tests/ # Test files
requirements.txt # Python dependencies
run.py # Application entry point
Assignments
A1: That's what I LIKE!
Word embeddings implementation including: - Skip-gram - Skip-gram with Negative Sampling - GloVe - Glove (Gensim) Evaluation
| App Demo |
|----------|
|
|
|App Tests|
|---------|
|
|
Screenshots
|Home|
|----|
||
|A1 Page|
|----|
||
Summary of Results
|Model|Skipgram|NEG|GloVe|GloVe (gensim) 100 Dim|GloVe (gensim) 300 Dim|Y_True| |-----|--------|---|-----|----------------------|----------------------|------| |MSE|0.2474|0.1879|0.2666|0.0750|0.0502|1.0000|
A2: Language Modelling
Train a model that can generate coherent and contextually relevant text based on a given input using LSTM
| App Demo |
|----------|
|
|
Screenshots
|A2 Page|
|-------|
||
Summary of Results
|Training History|
|----------------|
|
|
```text Generating text with different temperatures...
Prompt: To be or not
Temperature 0.20: To be or not . i ll be sworn to be a man . i have rather be a fool , and i am sure to be a man . i am a fool , and i am a fool , and i will be sworn . i am a good man , and i am not to be a man . i am a good man , and i will be a good man . i am a man , and i ll tell you . i am not a good man , and i ll be a man , and i will
Temperature 0.50: To be or not , or a great gallant man , and no more than a most king s body . the very fiend , the next of the king , and the king , a very fold fellow . i have a king , and the king of wales , or all the duke of york , with the king , the duke of wales , and the queen of gloucester , with the king , with the duke of westmoreland , and two a prince of venice ; a weaver in his forces and his train , attended of the king ,
Temperature 0.70: To be or not , or a great gallant man , and no more than a most king s body . ah , my gentle lord , i pray you , master , i am too guilty , and to the hand of these entreaty , after the instant that can be ; i have rather be glad his will , as you are in love , a rush . charles . how goes it ? if thou dost ; i say , how you may find him . exit . scene ii . inverness . the church of leonato s house enter helena
Temperature 0.80: To be or not ere it were so . i have rather add let me have a king s body with the great right . i pray them , sir , thou art sick ; but so , as to the hand of these entreaty , after the instant shall she be long ; let me not be nam d with my hat . i break you in a mocker . perchance that i can must be content to give ; their powers are wont d in a basket . exit . scene ii . inverness . the church of leonato s house enter
Temperature 1.00: To be or not ere it were so near i have , and let me have employ d s body with the great right . i pray them ; you shall go with it ; but so , constant to true mrs ; i prithee , after addition to beg of speech i ; let me not be nam d with my hat . i break you in a mocker . perchance that i can must argument if london give ; their lands are wont d in a twofold air ; and never put in myself obeying old of your wrong . dost thou
```
A3: Make Your Own Machine Translation Language
Translating between my Native Language Gujarati and English. I have experimented with different types of attention mechanisms, including general attention, multiplicative attention, and additive attention, to evaluate their effectiveness in the translation process.
| App Demo |
|----------|
|
|
Screenshots
|A3 Page|
|-------|
||
Summary of Results
| Attention Variant | Training Loss | Training PPL | Validation Loss | Validation PPL | BLEU Score | Training Time | |------------------|---------------|--------------|-----------------|----------------|------------|---------------| | multiplicative | 1.763 | 5.832 | 3.473 | 32.249 | 22.61 | 83.4m | | general | 1.765 | 5.841 | 3.447 | 31.403 | 27.01 | 76.9m | | additive | 1.681 | 5.371 | 3.355 | 28.652 | 17.90 | 85.8m |
|Training Curve(Additive)|Training Curve(Multiplicative)|Training Curve(General)|
|------------------------|------------------------|------------------------|
|
|
|
|
A4: Do You Agree
Training a pre-trained model like BERT from scratch, focusing on leveraging text embeddings to capture semantic similarity. Additionally, we will explore how to adapt the loss function for tasks like Natural Language Inference (NLI) to enhance the models ability to understand semantic relationships between texts
| App Demo |
|----------|
|
|
Screenshots
|A4 Page|
|-------|
||
Summary of Results
Overall Metrics (Final Epoch)
| Metric | Value | |--------|--------| | Accuracy | 40.25% | | Average Loss | 1.0340 | | Weighted F1-Score | 0.38 | | Macro F1-Score | 0.29 |
Class-wise Performance
| Class | Precision | Recall | F1-Score | Support | |-------|-----------|--------|-----------|---------| | Entailment | 0.43 | 0.38 | 0.41 | 273 | | Contradiction | 0.41 | 0.61 | 0.49 | 273 | | Neutral | 0.33 | 0.21 | 0.25 | 246 |
Cosine Similarity Analysis
| Class | Mean | Standard Deviation | |-------|------|-------------------| | Entailment | 0.7780 | 0.1795 | | Contradiction | 0.8076 | 0.1534 | | Neutral | 0.7976 | 0.1491 |
Final Conclusion
The model's performance on the SNLI/MNLI dataset shows several key characteristics:
Overall Performance:
- Moderate accuracy (40.25%) on the validation set
- Consistent but relatively high loss (1.0340)
- Balanced performance across classes with weighted F1-score of 0.38
Class-specific Performance:
- Best performance on Contradiction class (F1: 0.49)
- Moderate performance on Entailment class (F1: 0.41)
- Poorest performance on Neutral class (F1: 0.25)
Model Behavior:
- Shows good discrimination between classes (cosine similarities differ)
- Demonstrates learning progression (decreasing loss over epochs)
- Maintains consistent performance across validation checks
Areas for Improvement:
- Neutral class recognition needs significant improvement
- Overall accuracy could be enhanced with more training data
- Model confidence (cosine similarities) could be more distinctive between classes
The model shows promise but would benefit from further optimization and larger training datasets for production use.
A5: Optimization Human Preference
Using Hugging Face models to optimize human preference, specifically leveraging the Direct Preference Optimization (DPO) trainer. Worked with preference datasets, train a model, and push it to the Hugging Face model hub
| App Demo |
|----------|
|
|
Screenshots
|A5 Page|
|-------|
|
|
Summary of Results
|Inference Results|Training Time|Loss Plot|
|-----------------|-------------|---------|
|
|
|
|
Key Features
- Dataset Processing: Uses Anthropic's HH-RLHF (Human Harmlessness - Reinforcement Learning from Human Feedback) dataset containing pairs of preferred and dispreferred responses.
- Efficient Training with QLoRA: Implements Parameter-Efficient Fine-Tuning using QLoRA (Quantized Low-Rank Adaptation) to reduce memory requirements while maintaining performance.
- 4-bit Quantization: Utilizes the BitsAndBytes library to enable 4-bit quantization, significantly reducing the memory footprint of the model.
- Training Visualization: Includes a comprehensive visualization system that tracks and plots training metrics including losses and reward accuracy.
- Hugging Face Integration: Seamlessly uploads the trained model to Hugging Face Hub for easy sharing and deployment.
Technical Implementation
- Base Model: TinyLlama-1.1B-Chat-v1.0
- Training Method: Direct Preference Optimization (DPO)
- Quantization: 4-bit quantization using BitsAndBytes
- Parameter-Efficient Fine-Tuning: QLoRA with rank 16, alpha 32
- Target Modules: Attention layers (qproj, kproj, vproj, oproj) and MLP layers (gateproj, upproj, down_proj)
- Training Dataset: 5,000 examples from Anthropic's HH-RLHF
- Evaluation Dataset: 500 examples for validation
Training Process
- Loads and preprocesses the Anthropic HH-RLHF dataset
- Initializes the TinyLlama model with 4-bit quantization
- Applies QLoRA for parameter-efficient fine-tuning
- Trains using the DPO algorithm to optimize for human preferences
- Tracks and visualizes training metrics
- Saves and uploads the trained model to Hugging Face Hub
A6: Lets Talk Yourselves
Applied RAG (Retrieval-Augmented Generation) techniques in Langchain framework and from scratch to augment my chatbot that specializes in answering questions related to myself, my documents, resume, and any other relevant information
| App Demo |Generated JSON Response|
|----------|-----------------------|
|
|Click Here|
Screenshots
|Main_Chat|Sources|Recommendation|
|---------|-------|--------------|
||
|
|
|Retriever Analysis|Generator Analysis|Analyze Question|
|---------|-------|--------------|
||
|
|
Analysis of Both Scripts:
| Feature | personalrag.py | simplerag.py | |---------|----------------|---------------| | Framework | Uses LangChain for RAG implementation | Custom implementation without LangChain dependency | | Architecture | Structured around LangChain components (document loaders, text splitters, embeddings, vector stores) | Implements similar components but with custom classes | | Embedding Model | Uses HuggingFaceEmbeddings with 'sentence-transformers/all-mpnet-base-v2' | Custom SentenceEmbedder class using the same model | | Vector Store | Uses FAISS through LangChain's wrapper | Direct implementation with FAISS library | | LLM | Uses HuggingFacePipeline with 'google/flan-t5-base' | Custom TextGenerator class with the same model | | Document Processing | Uses RecursiveCharacterTextSplitter | Custom splitting function with similar parameters | | Memory | Includes ConversationBufferMemory for chat history | Maintains chat history in a simple list structure | | Prompt Template | Uses LangChain's PromptTemplate | Hardcoded prompt string in the query method | | Chain Type | Uses RetrievalQA with "stuff" chain type | Custom implementation combining retrieval and generation | | Persona | References "Arya Shah" in prompt | References "Arya Shah" in prompt | | Persistence | Saves vector store only | Saves both vector store and chat history |
Both scripts implement Retrieval-Augmented Generation (RAG) systems for answering questions about personal information, but with different approaches to the implementation architecture.
JSON Response
```json [ { "question": "How old are you?", "answer": "24 years old." }, { "question": "What is your highest level of education?", "answer": "Master's degrees." }, { "question": "What major or field of study did you pursue during your bachelor education?", "answer": "Computer Science." }, { "question": "How many years of work experience do you have?", "answer": "2 years." }, { "question": "What type of work or industry have you been involved in?", "answer": "technology industry." }, { "question": "Can you describe your current role or job responsibilities?", "answer": "Associate Software Engineer (Full-time)" }, { "question": "What are your core beliefs regarding the role of technology in shaping society?", "answer": "I believe that technological development should be guided by cultural values like respect, empathy, and community welfare." }, { "question": "How do you think cultural values should influence technological advancements?", "answer": "We create technology that truly serves humanity rather than the other way around." }, { "question": "As a master's student, what is the most challenging aspect of your studies so far?", "answer": "balancing the rigorous academic requirements across two programs simultaneously." }, { "question": "What specific research interests or academic goals do you hope to achieve during your time as a master's student?", "answer": "natural language processing, deep learning applications, computer vision, and metaheuristic optimization." } ]
```
A7: Distillation Get Smaller, Get Faster
In this assignment, I will will explore the comparison between Odd Layer and Even Layer Student Training Models and LoRA (Low-Rank Adaptation) on a distillation task using BERT from Huggingface
|App Demo Single Inference|App Demo Batch Inference|
|-------------------------|------------------------|
|
|
|
|Screenshot Part 1|Screenshot Part 2|
|-----------------|-----------------|
||
|
Results and Analysis
Performance Comparison
| Model Type | Test Loss | Accuracy | Precision | Recall | F1 Score |Results Overview| |------------|-----------|----------|-----------|--------|----------|----------------| | Odd Layer Distillation | 0.386519015 | 0.9182 | 0.909302326 | 0.930583102 | 0.919819643 |Click Here| | Even Layer Distillation | 0.376649498 | 0.9208 | 0.920792079 | 0.922253074 | 0.921521998 |Click Here| | LoRA | 0.208793605 | 0.9206 | 0.916470588 | 0.92701309 | 0.921711694 |Click Here|
|OddLayer Training Curves|EvenLayer Training Curves|Lora Training Curves|
|-------------------------|--------------------------|--------------------|
|
|
|
|
|Confusion Matrices|Metrics Comparison|
|------------------|------------------|
|
|
|
Best Performing Models
- Best Accuracy: Even Layer Distillation (0.9208)
- Best Precision: Even Layer Distillation (0.9208)
- Best Recall: Odd Layer Distillation (0.9306)
- Best F1 Score: LoRA (0.9217)
- Lowest Loss: LoRA (0.2088)
Analysis of Differences
Odd vs Even Layer Distillation
Even layer distillation outperforms odd layer distillation in terms of accuracy by 0.0026. However, even layer distillation has a higher F1 score by 0.0017.
This suggests that the even-numbered layers in BERT contain more task-relevant information for toxic comment classification. These layers might capture more semantic understanding needed for this task.
Distillation vs LoRA
The best distillation approach (Even Layer Distillation) outperforms LoRA in terms of accuracy by 0.0002. Similarly, LoRA has a higher F1 score by 0.0002.
Setup and Installation
- Clone the repository
- Create a virtual environment:
bash python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate - Install dependencies:
bash pip install -r requirements.txt - Run the application:
bash python run.py - Run tests:
bash pytest -vYou can also run the tests individually by specifying the target file to test
License
2024 Arya Shah. All rights reserved. | Under MIT License
Owner
- Name: Arya Shah
- Login: aryashah2k
- Kind: user
- Location: Mumbai, India
- Company: IIT Gandhinagar
- Website: https://www.linkedin.com/in/arya--shah/
- Twitter: aryashah2k
- Repositories: 96
- Profile: https://github.com/aryashah2k
Artificial Intelligence Engineer & Researcher
GitHub Events
Total
- Watch event: 6
- Push event: 31
- Create event: 2
Last Year
- Watch event: 6
- Push event: 31
- Create event: 2
Committers
Last synced: about 1 year ago
Top Committers
| Name | Commits | |
|---|---|---|
| aryashah2k | a****2@n****n | 34 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: about 1 year ago
All Time
- Total issues: 0
- Total pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Total issue authors: 0
- Total pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0