# vedavyas0105/financial-sentiment-distillation architecture

> Architecture diagram of the GitHub repository vedavyas0105/financial-sentiment-distillation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vedavyas0105/financial-sentiment-distillation
- Repository: https://github.com/vedavyas0105/financial-sentiment-distillation

## Overview

##### Project Type and Purpose
- Type: A research-style NLP/ML pipeline implementing knowledge distillation for financial sentiment analysis.
- Purpose: Pre-train a compact student model (DistilBERT) to mimic a larger teacher model (FinBERT) on a financial-news dataset, with data augmentation, training, evaluation, and visualization.

##### (Implicit) File Structure
Although the repository shows only README.md and a notebook, the README references the following top-level scripts:
– train.py (training & distillation)
– evaluate.py (metrics calculation)
– Sentiment_Analysis.ipynb (interactive exploration)
A “data/” folder is implied for raw and augmented datasets.

##### Core Components
a. Data Ingestion & Preprocessing
- Raw financial‐news dataset (CSV/JSON)
- Data augmentation module (synonym replacement)
- Result: augmented training set
b. Teacher Model
- FinBERT (Hugging Face Transformers)
- Outputs “soft targets” (probability distributions)
c. Student Model
- DistilBERT encoder + layer norm + pooling + FC + softmax
- Learns from both hard labels and soft targets
d. Knowledge Distillation Engine
- Loss function = λ·KL-Divergence(teacher, student) + (1–λ)·CrossEntropy(student, labels)
- Implemented in train.py using PyTorch (or TensorFlow)
e. Training Pipeline
- Optimizer: Adam @ 5e-5
- Batch size 32, epochs 20, early stopping, LR scheduler
- Checkpointing & logging (e.g., TensorBoard)
f. Evaluation Module
- Confusion matrix, precision/recall/F1 (sklearn or custom)
- Accuracy & loss metrics
g. Visualization Module
- Matplotlib/Seaborn plots for training/validation curves and heatmaps
h. Deployment / Usage
- Scripts: train.py → student model artifact (.pt/.bin)
- evaluate.py → metrics report
- Notebook for ad-hoc analysis

##### Relationships & Data Flow
- Data Ingestion → Data Augmentation → Tokenization → Batching
- Batches → Teacher Model & Student Model in parallel
- Teacher logits + Student logits + Labels → Distillation Loss → Backprop into Student
- At epoch end → checkpoint student model → log metrics
- After training → Evaluate student model on validation/test splits
- Evaluation outputs → Visualization module → static plots

##### Architectural Patterns & Principles
- Modular pipeline: clear separation of data, models, training, and evaluation
- Plug-and-play models (any Hugging Face model can be swapped)
- Functional decomposition: each script/notebook handles one concern
- Use of callbacks for early stopping/LR scheduling (separation of training logic and control flow)
- CI-style reproducibility: configuration through CLI arguments or config files (implied)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Documentation
    Documentation["Project Overview\n(README.md)"]:::docComp

    %% Data Layer
    subgraph "Data Layer"
        RawDataset["Raw Dataset\n(CSV/JSON)"]:::dataComp
        Augmentation["Data Augmentation\n(synonym replacement)"]:::dataComp
        Tokenization["Tokenization\n(Token IDs)"]:::dataComp
    end

    %% Model Layer
    subgraph "Model Layer"
        Teacher["Teacher Model\nFinBERT"]:::modelComp
        Student["Student Model\nDistilBERT\nEncoder→LayerNorm→Pooling→FC→Softmax"]:::modelComp
        DistillLoss["Distillation Loss\nλ·KL-Divergence + (1–λ)·CrossEntropy"]:::trainComp
    end

    %% Training Pipeline
    subgraph "Training Pipeline"
        Batching["Batching\n(batch size=32)"]:::trainComp
        Optimizer["Optimizer & Scheduler\nAdam @5e-5"]:::trainComp
        Checkpoint["Model Checkpoints\n(.pt/.bin)"]:::dataComp
    end

    %% Evaluation & Visualization
    subgraph "Evaluation & Visualization"
        Evaluate["Evaluation Module\nConfusion Matrix, Precision/Recall/F1"]:::evalComp
        Visualization["Visualization Module\nMatplotlib/Seaborn plots"]:::evalComp
        MetricsReport["Metrics Report\n(JSON/CSV)"]:::dataComp
        Plots["Saved Plots\n(PNG/PDF)"]:::dataComp
    end

    %% Ad-hoc Analysis Notebook
    subgraph "Ad-hoc Analysis"
        Notebook["Interactive Exploration\n(Sentiment_Analysis.ipynb)"]:::notebookComp
    end

    %% Data & Control Flow
    Documentation --> RawDataset
    RawDataset --> Augmentation --> Tokenization --> Batching
    Batching --> Teacher
    Batching --> Student
    Teacher -->|"Teacher Logits"| DistillLoss
    Student -->|"Student Logits"| DistillLoss
    Batching -->|"Hard Labels"| DistillLoss
    DistillLoss -->|"Gradients"| Student
    Student --> Optimizer --> Student
    DistillLoss -.Loop over epochs and batches.-> Batching
    DistillLoss --> Checkpoint
    Checkpoint --> Evaluate --> MetricsReport
    MetricsReport --> Visualization --> Plots

    %% Notebook connections
    RawDataset --> Notebook
    Augmentation --> Notebook
    Visualization --> Notebook

    %% Click Events
    click Documentation "https://github.com/vedavyas0105/financial-sentiment-distillation/blob/main/README.md"
    click Notebook "https://github.com/vedavyas0105/financial-sentiment-distillation/blob/main/Sentiment_Analysis.ipynb"

    %% Styles
    classDef dataComp fill:#cff,stroke:#0aa,stroke-width:1px
    classDef modelComp fill:#cfc,stroke:#0a0,stroke-width:1px
    classDef trainComp fill:#fcf,stroke:#a0a,stroke-width:1px
    classDef evalComp fill:#fdd,stroke:#a00,stroke-width:1px
    classDef docComp fill:#eee,stroke:#666,stroke-width:1px
    classDef notebookComp fill:#efe,stroke:#393,stroke-width:1px
```
