Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Green Patent Detection: Advanced Agentic Workflow with QLoRA
|
| 2 |
+
|
| 3 |
+
## Project Summary
|
| 4 |
+
|
| 5 |
+
This is the final assignment repository for the **M4 Applied Deep Learning and Artificial Intelligence** course at Aalborg University. It synthesizes Assignments 2 and 3 into a state-of-the-art data labelling pipeline. A **Generative LLM is fine-tuned via QLoRA** to understand patent language, then integrated as the "brain" of a **Multi-Agent System (MAS)** to debate and label complex patent claims. Finally, a targeted **Human-in-the-Loop (HITL)** review step produces a gold dataset for a final PatentSBERTa fine-tuning.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## Pipeline Architecture
|
| 10 |
+
|
| 11 |
+
```
|
| 12 |
+
patents_50k_green.parquet
|
| 13 |
+
β
|
| 14 |
+
βΌ
|
| 15 |
+
[Part A & B] Baseline PatentSBERTa + Uncertainty Sampling
|
| 16 |
+
β β Top 100 high-risk claims (u β 1.0)
|
| 17 |
+
βΌ
|
| 18 |
+
[Part C β Step 1] QLoRA Fine-tuning on Colab (Qwen3-8B, 4-bit, 3 epochs)
|
| 19 |
+
β β qlora_green_patent_adapter (LoRA weights)
|
| 20 |
+
β β Qwen3-8B.Q4_K_M.gguf (served via LM Studio)
|
| 21 |
+
βΌ
|
| 22 |
+
[Part C β Step 2] Multi-Agent System (CrewAI)
|
| 23 |
+
β Agent 1 β Advocate (Qwen3-4B, argues for green: Advocator)
|
| 24 |
+
β Agent 2 β Skeptic (Qwen3-4B, argues against green: Skeptic)
|
| 25 |
+
β Agent 3 β Judge (QLoRA Qwen3-8B, final verdict: Judge)
|
| 26 |
+
βΌ
|
| 27 |
+
[Part D] Exception-Based HITL (only deadlocks / low-confidence)
|
| 28 |
+
β β 26 claims reviewed with deadlock, 3 human overrides
|
| 29 |
+
β β hitl_green_100_final.csv (gold labels)
|
| 30 |
+
βΌ
|
| 31 |
+
[Part D] Final PatentSBERTa Fine-tuning on gold dataset
|
| 32 |
+
β patentsberta_finetuned_final/
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
---
|
| 37 |
+
|
| 38 |
+
## Part C β Step 1: QLoRA Domain Adaptation
|
| 39 |
+
|
| 40 |
+
The generative LLM fine-tuning was performed on Google Colab (T4, 15 GB VRAM) using Unsloth's QLoRA implementation.
|
| 41 |
+
|
| 42 |
+
| Parameter | Value |
|
| 43 |
+
|---|---|
|
| 44 |
+
| Base model | `unsloth/Qwen3-8B-bnb-4bit` |
|
| 45 |
+
| LoRA rank (r) | 16 |
|
| 46 |
+
| LoRA alpha | 16 |
|
| 47 |
+
| Target modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
|
| 48 |
+
| Training examples | 2,000 (train_silver, Alpaca format) |
|
| 49 |
+
| Epochs | 3 (375 total steps) |
|
| 50 |
+
| Batch size | 4 Γ 4 gradient accumulation = effective 16 |
|
| 51 |
+
| Learning rate | 2e-4 (AdamW 8-bit, linear schedule) |
|
| 52 |
+
| Max sequence length | 2,048 tokens |
|
| 53 |
+
| Training loss | 0.8899 |
|
| 54 |
+
| Training time | ~105 minutes on T4 |
|
| 55 |
+
| VRAM usage | ~5 GB (4-bit quantization) |
|
| 56 |
+
|
| 57 |
+
The fine-tuned adapter was exported to GGUF Q4_K_M format (4.682 GB) and served locally via LM Studio for use in the MAS.
|
| 58 |
+
|
| 59 |
+
---
|
| 60 |
+
|
| 61 |
+
## Part C β Step 2: Multi-Agent System
|
| 62 |
+
|
| 63 |
+
Three agents collaborate to label each of the 100 high-risk patent claims using CrewAI as the orchestration framework. The QLoRA fine-tuned model serves as the Judge's brain.
|
| 64 |
+
|
| 65 |
+
| Agent | Model | Temperature | Role |
|
| 66 |
+
|---|---|---|---|
|
| 67 |
+
| Advocate | Qwen3-4B (LM Studio) | 0.1 | Argues FOR Y02 green classification |
|
| 68 |
+
| Skeptic | Qwen3-4B (LM Studio) | 0.1 | Argues AGAINST (identifies greenwashing) |
|
| 69 |
+
| Judge | QLoRA Qwen3-8B (LM Studio) | 0.1 | Weighs debate and produces final JSON label |
|
| 70 |
+
|
| 71 |
+
Each claim produces a structured JSON output: `classification` (0/1), `confidence` (Low/Medium/High), and `rationale`.
|
| 72 |
+
|
| 73 |
+
---
|
| 74 |
+
|
| 75 |
+
## Part D: Targeted HITL & Final Fine-tuning
|
| 76 |
+
|
| 77 |
+
**Exception-Based HITL** was applied β only intervening when agents reached a deadlock or produced low-confidence outputs.
|
| 78 |
+
|
| 79 |
+
| Metric | Value |
|
| 80 |
+
|---|---|
|
| 81 |
+
| Total claims reviewed by MAS | 100 |
|
| 82 |
+
| Auto-accepted (high confidence) | 74 |
|
| 83 |
+
| Escalated to human review | 26 |
|
| 84 |
+
| Human overrides | 3 |
|
| 85 |
+
| Human agreement rate with Judge | 88.5% |
|
| 86 |
+
|
| 87 |
+
The gold-labelled dataset (`hitl_green_100_final.csv`) was used to fine-tune PatentSBERTa for 3 epochs using CosineSimilarityLoss on an AMD Radeon RX 9070 XT via DirectML (fell back to CPU, completed in ~31 minutes).
|
| 88 |
+
|
| 89 |
+
---
|
| 90 |
+
|
| 91 |
+
## Performance Results
|
| 92 |
+
|
| 93 |
+
| Model Version | Training Data Source | F1 Score (Test Set) |
|
| 94 |
+
|---|---|---|
|
| 95 |
+
| 1. Baseline | Frozen Embeddings (No Fine-tuning) | 0.7494 |
|
| 96 |
+
| 2. Assignment 2 Model | Silver + Gold (Simple Generic LLM) | 0.7465 |
|
| 97 |
+
| 3. Assignment 3 Model | Silver + Gold (Advanced Techniques / MAS) | 0.7467 |
|
| 98 |
+
| 4. Final Model | Silver + Gold (QLoRA-Powered MAS + Targeted HITL) | 0.7530 |
|
| 99 |
+
|
| 100 |
+
The Final Model achieves the highest F1 score across all iterations, demonstrating that QLoRA domain adaptation combined with structured agent debate and targeted human review produces measurable improvements.
|
| 101 |
+
|
| 102 |
+
---
|
| 103 |
+
|
| 104 |
+
## Key Findings
|
| 105 |
+
|
| 106 |
+
**QLoRA advantages:**
|
| 107 |
+
- Adapts a generative LLM to patent language with only 0.53% of parameters trained
|
| 108 |
+
- Enables a domain-aware Judge that understands Y02 classification logic
|
| 109 |
+
- 4-bit quantization fits 8B model on a free 16 GB T4 GPU
|
| 110 |
+
|
| 111 |
+
**MAS + HITL advantages:**
|
| 112 |
+
- Debate structure surfaces disagreements that single-model approaches miss
|
| 113 |
+
- Exception-based HITL reduces human effort by 74% (26 vs 100 reviews)
|
| 114 |
+
- Gold labels are higher-quality than silver LLM labels alone
|
| 115 |
+
|
| 116 |
+
**Limitations:**
|
| 117 |
+
- DirectML (AMD GPU) not fully supported by sentence-transformers training β fell back to CPU
|
| 118 |
+
- torchao 0.16.0 conflicts with transformers lazy loader in certain environments
|
| 119 |
+
- LM Studio local inference adds latency (~3β5s per agent call)
|
| 120 |
+
|
| 121 |
+
---
|
| 122 |
+
|
| 123 |
+
## Repository Contents
|
| 124 |
+
|
| 125 |
+
| File | Description |
|
| 126 |
+
|---|---|
|
| 127 |
+
| `Final_Assignment.ipynb` | Main notebook (Parts AβD) |
|
| 128 |
+
| `patentsberta_finetuned_final/` | Final fine-tuned PatentSBERTa model |
|
| 129 |
+
| `hitl_green_100_final.csv` | Gold dataset β 100 claims with HITL labels and debate rationales |
|
| 130 |
+
| `final_classifier.joblib` | Serialised final Logistic Regression classifier |
|
| 131 |
+
| `qlora_outputs.zip` | QLoRA adapter weights (`qlora_green_patent_adapter/`) |
|
| 132 |
+
| `Part C Step 1.ipynb` | Colab notebook for QLoRA fine-tuning |
|
| 133 |
+
|
| 134 |
+
---
|
| 135 |
+
|
| 136 |
+
## Related Repositories
|
| 137 |
+
|
| 138 |
+
- [Assignment 1 β ](https://github.com/Ory999/Portfolio-Assignment-1-M4-SGD-Mechanics-Attention-Context)
|
| 139 |
+
- [Assignment 2 β ](https://huggingface.co/Ory999/Assignment_2)
|
| 140 |
+
- [Assignment 3 β ](https://huggingface.co/Ory999/Assignment_3)
|