YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

Patent Green Technology Classification

A comprehensive deep learning system for classifying patent claims as green/environmentally beneficial technology using baseline models, LLM fine-tuning, and multi-agent debate.

Python PyTorch

🎯 Project Overview

This project implements an end-to-end pipeline for classifying patent claims as GREEN (environmentally beneficial) or NOT GREEN technology. The system progresses from baseline models to advanced LLM fine-tuning and culminates in a sophisticated multi-agent debate system for robust predictions.

Dataset: 1.37M patent claims from Hugging Face with 650+ CPC code columns

Key Achievements

Method F1 Score Key Advantage
Baseline (LogReg + Frozen) 0.777 Fast, lightweight
Fine-tuned PatentSBERTa 0.808 Best accuracy-speed tradeoff
QLoRA Mistral 0.811 JSON output, memory efficient
Multi-Agent Debate 0.867 Most interpretable, robust

πŸ“‹ Table of Contents


πŸš€ Installation & Requirements

Prerequisites

  • Python 3.8+
  • CUDA 11.8+ (for GPU acceleration)

Setup

1. Clone Repository

git clone https://github.com/KarolinaBohdan/Deep_Learning_Final.git
cd Deep_Learning_Final

2. Create Virtual Environment

python -m venv venv
source venv/bin/activate  # Linux/Mac
# OR
venv\Scripts\activate  # Windows

3. Install Dependencies

pip install --upgrade pip
pip install -r requirements.txt

4. Download Pre-trained Models (First Run)

# PatentSBERTa auto-downloads
python -c "from transformers import AutoModel; AutoModel.from_pretrained('AI-Growth-Lab/PatentSBERTa')"

# Mistral-7B-Instruct
python -c "from transformers import AutoModel; AutoModel.from_pretrained('mistralai/Mistral-7B-Instruct-v0.2')"

requirements.txt

# Core ML/DL
torch>=2.0.0
transformers>=4.41.0
peft>=0.11.0
trl>=0.8.0
datasets>=2.14.0

# Scientific Computing
numpy>=1.24.0
pandas>=2.0.0
scikit-learn>=1.3.0

# Utilities
tqdm>=4.66.0
joblib>=1.3.0
bitsandbytes>=0.42.0
accelerate>=0.25.0
pyarrow>=13.0.0

⚑ Quick Start

Classify New Patents

import torch
import json
from transformers import AutoTokenizer, AutoModel
from peft import PeftModel

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Load fine-tuned PatentSBERTa (fastest, best accuracy)
tokenizer = AutoTokenizer.from_pretrained("AI-Growth-Lab/PatentSBERTa")
model = AutoModel.from_pretrained(
    "partD_patentsberta_finetuned_evalsilver_only/",
    device_map=device
)

# Classify a claim
new_claim = "A method for converting wind energy into electrical power with 95% efficiency..."

inputs = tokenizer(new_claim, return_tensors="pt", truncation=True, max_length=256)
outputs = model(**inputs)
# Process embeddings for classification...

Batch Evaluation

python A3_qlora_inference_.py \
  --adapter_dir qlora_mistral_adapter_json \
  --eval_silver eval_silver.parquet \
  --output_csv predictions.csv

πŸ“ File Organization

Deep_Learning_Final/
β”‚
β”œβ”€β”€ README.md (this file)
β”œβ”€β”€ requirements.txt
β”‚
β”œβ”€β”€ ASSIGNMENT 2 - BASELINE & LLM INTEGRATION
β”‚   β”œβ”€β”€ Assignment 2_Part A.ipynb              [Data prep & splitting]
β”‚   β”œβ”€β”€ Assigment2_baseline_partA.py           [Frozen embeddings baseline]
β”‚   β”œβ”€β”€ A2_partB_uncertainty.py                [Uncertainty estimation]
β”‚   β”œβ”€β”€ A2_partC_llm_local.py                  [Mistral labeling + HITL]
β”‚   β”œβ”€β”€ A2_partD_finetune.py                   [PatentSBERTa fine-tuning]
β”‚   └── results/
β”‚       β”œβ”€β”€ A2_LR_results_partA.out
β”‚       β”œβ”€β”€ A2PartD_finetuned_results/
β”‚       └── finetune_part3_final_metrics/
β”‚
β”œβ”€β”€ ASSIGNMENT 3 - QLORA FINE-TUNING
β”‚   β”œβ”€β”€ A3_qlora_finetuning.py                 [QLoRA training]
β”‚   β”œβ”€β”€ A3_qlora_inference_.py                 [QLoRA inference]
β”‚   β”œβ”€β”€ qlora_mistral_adapter_json/            [LoRA adapter checkpoint]
β”‚   └── Qlora_patentsberta_finetuning_final_metrics/
β”‚
β”œβ”€β”€ ASSIGNMENT 4 - MULTI-AGENT DEBATE
β”‚   β”œβ”€β”€ mas_final.py                           [MAS implementation]
β”‚   β”œβ”€β”€ MAS_train_patentsberta_human3/         [Training procedures]
β”‚   └── results/
β”‚       β”œβ”€β”€ mas_debate_results.csv
β”‚       └── MAS evaluation reports
β”‚
β”œβ”€β”€ DATA FILES
β”‚   β”œβ”€β”€ patents_50k_green.parquet              [50k balanced dataset]
β”‚   β”œβ”€β”€ train_silver.parquet                   [30k training split]
β”‚   β”œβ”€β”€ eval_silver.parquet                    [10k eval split]
β”‚   β”œβ”€β”€ pool_unlabeled.parquet                 [10k pool split]
β”‚   β”œβ”€β”€ hitl_green_100.csv                     [100 HITL samples]
β”‚   β”œβ”€β”€ hitl_green_100_llm_corrected.csv       [100 HITL samples (verified)]
β”‚   └── embeddings/
β”‚       β”œβ”€β”€ X_train.npy
β”‚       β”œβ”€β”€ X_eval.npy
β”‚       └── X_pool.npy
β”‚
β”œβ”€β”€ MODEL CHECKPOINTS
β”‚   β”œβ”€β”€ baseline_logreg.joblib
β”‚   β”œβ”€β”€ partD_patentsberta_finetuned_evalsilver_only/
β”‚   └── qlora_mistral_adapter_json/
β”‚
└── M4_ASSIGNEMENT1_BDS_MFC.ipynb              [Portfolio assignment]

πŸ”„ Step-by-Step Workflow

Phase 1: Data Preparation

Load 1.37M raw patent claims
    ↓
Create is_green_silver labels from Y02* CPC codes
    ↓
Balance to 50k (25k green + 25k non-green)
    ↓
Split into train/eval/pool (60/20/20)
    ↓
Output: 4 parquet files

Run: jupyter notebook "Assignment 2_Part A.ipynb"

Phase 2: Baseline Establishment

Generate PatentSBERTa embeddings (frozen, 768-dim)
    ↓
Train Logistic Regression on train_silver
    ↓
Evaluate on eval_silver
    ↓
Output: Baseline metrics + model checkpoint

Run: python Assigment2_baseline_partA.py
Results: 0.777 F1 score

Phase 3: Uncertainty & LLM Labeling

Calculate uncertainty scores on pool_unlabeled
    ↓
Select top-100 uncertain samples
    ↓
Use Mistral-7B to generate initial labels + rationales
    ↓
Human review and correction (HITL)
    ↓
Output: 100 gold labels with human validation

Run: python A2_partC_llm_local.py

Phase 4: Fine-tuning PatentSBERTa

Load 30k train_silver + 100 gold labels
    ↓
Tokenize with PatentSBERTa tokenizer
    ↓
Fine-tune PatentSBERTa (unfrozen)
    ↓
Evaluate on eval_silver

Run: python A2_partD_finetune.py
Results: 0.808 F1 score

Phase 5: QLoRA Fine-tuning

Quantize Mistral-7B to 4-bit
    ↓
Add LoRA adapters (trainable)
    ↓
Train on 30k train_silver + 100 gold
    ↓
Output JSON predictions with rationales

Run Training:

python A3_qlora_finetuning.py \
  --output_dir qlora_mistral_adapter_json \
  --num_train_epochs 3 \
  --per_device_train_batch_size 4

Run Inference: python A3_qlora_inference_.py --adapter_dir qlora_mistral_adapter_json Results: 0.811 F1 score

Phase 6: Multi-Agent Debate System

Load fine-tuned Advocate (Mistral + LoRA)
    ↓
Load base Skeptic (Mistral base)
    ↓
Load base Judge (Mistral base)
    ↓
For each claim: Advocate argues GREEN, 
Skeptic argues NOT GREEN, Judge decides
    ↓
Output: Final predictions with explanations

Run:

python mas_final.py \
  --eval_silver eval_silver.parquet \
  --hitl_file hitl_green_100_llm_corrected.csv \
  --adapter_path qlora_mistral_adapter_json \
  --output mas_debate_results.csv

Results: 0.867 F1 score


πŸ€– Models & Methods

PatentSBERTa

  • Model: BERT fine-tuned on patent documents
  • Embedding Dimension: 768
  • Max Sequence Length: 256 tokens
  • Training Data: 16M patent documents
  • Use Case: Feature extraction, domain-specific understanding

Mistral-7B-Instruct

  • Parameters: 7 billion
  • Context Window: 8,192 tokens
  • Instruction Tuning: Yes (optimized for instruction following)
  • Fine-tuning Method: QLoRA (efficient, memory-friendly)
  • Quantization: 4-bit (14GB β†’ 3.5GB)

Multi-Agent Debate System

Three specialized agents provide robust predictions:

Patent Claim
    β”œβ”€β†’ ADVOCATE (Mistral + LoRA)  β†’ "Argue for GREEN (1)"
    β”‚
    β”œβ”€β†’ SKEPTIC (Mistral base)     β†’ "Argue against NOT GREEN (0)"
    β”‚
    └─→ JUDGE (Mistral base)       β†’ Reviews both, decides
        Output: {label, confidence, reasoning}

Why MAS Works:

  • βœ… Reduces single-model bias
  • βœ… Better calibration through perspective diversity
  • βœ… Explainable reasoning (debate arguments)
  • βœ… Robust: unlikely all 3 agents make same mistake

πŸ“Š Results & Metrics

Performance Comparison

Method F1 Score Key Advantage
Baseline (LogReg + Frozen) 0.777 Fast, lightweight
Fine-tuned PatentSBERTa 0.808 Best accuracy-speed tradeoff
QLoRA Mistral 0.811 JSON output, memory efficient
Multi-Agent Debate 0.867 Most interpretable, robust

πŸ“– Usage Guide

Classify New Patents

import torch
from transformers import AutoTokenizer, AutoModel

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Load fine-tuned PatentSBERTa
tokenizer = AutoTokenizer.from_pretrained("AI-Growth-Lab/PatentSBERTa")
model = AutoModel.from_pretrained(
    "partD_patentsberta_finetuned_evalsilver_only/",
    device_map=device
)

# Classify
new_claim = "A method for converting wind energy..."
inputs = tokenizer(new_claim, return_tensors="pt", truncation=True, max_length=256)
outputs = model(**inputs)
# Process embeddings...

Batch Evaluation

python A3_qlora_inference_.py \
  --adapter_dir qlora_mistral_adapter_json \
  --eval_silver eval_silver.parquet \
  --output_csv predictions.csv

Multi-Agent Debate on Custom Data

python mas_final.py \
  --eval_silver your_data.parquet \
  --adapter_path qlora_mistral_adapter_json \
  --output results.csv

πŸ“ˆ Green Technology Definition

The system classifies patents as GREEN if they describe:

  • βœ… Emissions reduction, exhaust treatment, carbon capture
  • βœ… Pollution control, filtration, removal
  • βœ… Recycling, waste processing, circular economy
  • βœ… Water treatment, purification, desalination
  • βœ… Renewable energy, storage, grid integration
  • βœ… Explicit energy-saving (not just "efficient")
  • βœ… Environmental monitoring reducing waste

🀝 Contributing

Contributors

  • Karolina Bohdan
  • Faraiba Farnan
  • Maleha Afzal

πŸ™ Acknowledgments

  • Hugging Face for the patent dataset and transformers library
  • AI-Growth-Lab for PatentSBERTa
  • Mistral AI for Mistral-7B-Instruct
  • Meta for PEFT and QLoRA implementations

πŸ“§ Questions?

For questions or issues, please open a GitHub issue or contact the project maintainers.

Happy classifying! πŸš€

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support