Sandeep Chowdhary commited on
Update model card
Browse files
README.md
CHANGED
|
@@ -16,239 +16,114 @@ This model classifies content related to electric vehicles, sustainable transpor
|
|
| 16 |
|
| 17 |
## Model Details
|
| 18 |
|
| 19 |
-
-
|
| 20 |
-
-
|
| 21 |
-
-
|
| 22 |
-
-
|
| 23 |
-
-
|
| 24 |
-
-
|
| 25 |
-
|
| 26 |
-
## Training
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
5. **Model Training**: Fine-tuning Distilbert on the labeled dataset with optimal threshold tuning
|
| 35 |
-
6. **Validation**: Performance evaluation on held-out test sets using Jaccard similarity metrics
|
| 36 |
-
|
| 37 |
-
### Training Data Sources
|
| 38 |
-
- **Source Subreddits**: r/climate, r/climateactionplan, r/climatechange, r/climatechaos, r/climateco, r/climatecrisis, r/climatecrisiscanada, r/climatedisalarm, r/climatejobslist, r/climatejustice, r/climatememes, r/climatenews, r/climateoffensive, r/climatepolicy, r/climate_science
|
| 39 |
-
- **Data Period**: 2010-2023
|
| 40 |
-
- **Focus Areas**: Electric vehicle adoption, charging infrastructure, battery technology, sustainable mobility, transportation policy
|
| 41 |
-
- **Data Type**: Posts and comments filtered by regex patterns
|
| 42 |
-
- **Labeling Method**: GPT-assisted multilabel classification
|
| 43 |
-
|
| 44 |
-
### Regex Patterns Used
|
| 45 |
-
The model was trained on content filtered using sector-specific regex patterns:
|
| 46 |
-
|
| 47 |
-
**Transport Sector (EV-related terms):**
|
| 48 |
-
- Strong patterns: ev, electric vehicle, evs, bev, tesla model, supercharger, gigafactory
|
| 49 |
-
- Weak patterns: electric car, charging station, tax credit, e-bike, tesla
|
| 50 |
-
|
| 51 |
-
**Housing Sector (Solar energy terms):**
|
| 52 |
-
- Strong patterns: rooftop solar, solar pv, pv panel, photovoltaics, solar array
|
| 53 |
-
- Weak patterns: solar panel, solar power, battery storage, powerwall, solar tax credit
|
| 54 |
-
|
| 55 |
-
**Food Sector (Plant-based diet terms):**
|
| 56 |
-
- Strong patterns: vegan, plant-based diet, veganism, vegetarian, beyond meat
|
| 57 |
-
- Weak patterns: red meat, dairy free, plant protein, almond milk, flexitarian
|
| 58 |
|
| 59 |
## Labels
|
| 60 |
|
| 61 |
-
The model predicts
|
| 62 |
|
| 63 |
-
1.
|
| 64 |
-
2.
|
| 65 |
-
3.
|
| 66 |
-
4.
|
| 67 |
-
5.
|
| 68 |
-
6.
|
| 69 |
-
7.
|
| 70 |
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
**💡 Tip**: For best performance, use the optimal thresholds provided in the "Optimal Thresholds" section below instead of the default 0.5 threshold.
|
| 75 |
-
|
| 76 |
-
**🔧 Threshold Optimization**: The optimal thresholds were computed using Jaccard similarity optimization on the validation set. For your own dataset, consider re-optimizing thresholds using the same method from `paper4_BERT_finetuning.py`.
|
| 77 |
|
| 78 |
## Usage
|
| 79 |
|
| 80 |
```python
|
| 81 |
import torch
|
| 82 |
-
import numpy as np
|
| 83 |
from transformers import DistilBertTokenizer
|
| 84 |
-
import sys
|
| 85 |
-
import os
|
| 86 |
import tempfile
|
| 87 |
from huggingface_hub import snapshot_download
|
| 88 |
|
| 89 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 90 |
|
| 91 |
-
def print_sorted_label_scores(label_scores):
|
| 92 |
-
# Sort label_scores dict by score descending
|
| 93 |
-
sorted_items = sorted(label_scores.items(), key=lambda x: x[1], reverse=True)
|
| 94 |
-
for label, score in sorted_items:
|
| 95 |
-
print(f" {label}: {score:.6f}")
|
| 96 |
-
|
| 97 |
# Download and load model
|
| 98 |
model_link = "sanchow/electric_vehicles-distilbert-classifier"
|
| 99 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 100 |
-
snapshot_download(
|
| 101 |
-
repo_id=model_link,
|
| 102 |
-
local_dir=temp_dir,
|
| 103 |
-
local_dir_use_symlinks=False
|
| 104 |
-
)
|
| 105 |
|
| 106 |
-
# Import the model class
|
| 107 |
sys.path.insert(0, temp_dir)
|
| 108 |
from model_class import MultilabelClassifier
|
| 109 |
|
| 110 |
-
# Load tokenizer and model
|
| 111 |
tokenizer = DistilBertTokenizer.from_pretrained(temp_dir)
|
| 112 |
-
checkpoint = torch.load(os.path.join(temp_dir, 'model.pt'), map_location='cpu'
|
| 113 |
model = MultilabelClassifier(checkpoint['model_name'], len(checkpoint['label_names']))
|
| 114 |
model.load_state_dict(checkpoint['model_state_dict'])
|
| 115 |
model.to(device)
|
| 116 |
model.eval()
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
]
|
| 126 |
-
|
| 127 |
-
print(f"\n{sector_info['name']} classifier results:\n")
|
| 128 |
-
for i, test_text in enumerate(examples):
|
| 129 |
-
inputs = tokenizer(
|
| 130 |
-
test_text,
|
| 131 |
-
return_tensors="pt",
|
| 132 |
-
truncation=True,
|
| 133 |
-
max_length=512,
|
| 134 |
-
padding=True
|
| 135 |
-
).to(device)
|
| 136 |
-
with torch.no_grad():
|
| 137 |
-
outputs = model(**inputs)
|
| 138 |
-
predictions = outputs.cpu().numpy() if isinstance(outputs, (tuple, list)) else outputs.cpu().numpy()
|
| 139 |
-
label_scores = {label: float(score) for label, score in zip(checkpoint['label_names'], predictions[0])}
|
| 140 |
-
print(f"Example {i+1}: '{test_text}'")
|
| 141 |
-
print("Predictions (all label scores, highest first):")
|
| 142 |
-
print_sorted_label_scores(label_scores)
|
| 143 |
-
print("-" * 40)
|
| 144 |
```
|
| 145 |
|
| 146 |
## Applications
|
| 147 |
|
| 148 |
-
|
| 149 |
-
-
|
| 150 |
-
-
|
| 151 |
-
-
|
| 152 |
-
- **Market Research**: Tracking trends and interests in electric vehicles
|
| 153 |
|
| 154 |
|
| 155 |
-
## Performance
|
| 156 |
|
| 157 |
-
|
| 158 |
-
- Micro Jaccard
|
| 159 |
-
- Macro Jaccard
|
| 160 |
- F1 Score: 0.8772
|
| 161 |
- Accuracy: 0.8772
|
| 162 |
-
- Precision: 0.8772
|
| 163 |
-
- Recall: 0.8772
|
| 164 |
|
| 165 |
-
|
| 166 |
-
- Training Samples: ~600 per sector
|
| 167 |
-
- Validation Samples: ~150 per sector
|
| 168 |
-
- Test Samples: ~150 per sector
|
| 169 |
-
- Total GPT-labeled samples: ~900 per sector
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
## Optimal Thresholds
|
| 174 |
|
| 175 |
-
|
| 176 |
|
| 177 |
-
-
|
| 178 |
-
-
|
| 179 |
-
-
|
| 180 |
-
-
|
| 181 |
-
-
|
| 182 |
-
-
|
| 183 |
-
-
|
| 184 |
|
| 185 |
-
|
| 186 |
```python
|
| 187 |
-
# Define optimal thresholds for this model
|
| 188 |
optimal_thresholds = {'Alternative Modes': 0.28427787391225384, 'Charging Infrastructure': 0.3619448731592626, 'Environmental Benefit': 0.4029443119613918, 'Grid Impact And Energy Mix': 0.29907076386497516, 'Mineral Supply Chain': 0.2987419331439881, 'Policy And Mandates': 0.36899998622725905, 'Purchase Price': 0.3463644004166977}
|
| 189 |
-
|
| 190 |
-
# Apply thresholds to get binary predictions
|
| 191 |
-
for i, (label, score) in enumerate(zip(label_names, predictions[0])):
|
| 192 |
threshold = optimal_thresholds.get(label, 0.5)
|
| 193 |
if score > threshold:
|
| 194 |
-
print(f"{label}: {score:.3f}
|
| 195 |
```
|
| 196 |
|
| 197 |
|
| 198 |
-
## Threshold Optimization
|
| 199 |
-
|
| 200 |
-
The optimal thresholds provided above were computed using **Jaccard similarity optimization** on the validation dataset. This method finds the best threshold for each label that maximizes the Jaccard similarity between predicted and true labels.
|
| 201 |
-
|
| 202 |
-
### Optimization Method Used
|
| 203 |
-
|
| 204 |
-
The thresholds were optimized using the `find_optimal_thresholds_jaccard_global` function from `paper4_multilabel_threshold_optimizer.py`, which:
|
| 205 |
-
|
| 206 |
-
1. **Grid Search**: Tests threshold values from 0.1 to 0.9 in 0.05 increments
|
| 207 |
-
2. **Jaccard Optimization**: Maximizes micro-averaged Jaccard similarity
|
| 208 |
-
3. **Per-Label Optimization**: Finds optimal threshold for each label independently
|
| 209 |
-
4. **Global Optimization**: Considers the overall multilabel performance
|
| 210 |
-
|
| 211 |
-
### Re-optimizing for Your Dataset
|
| 212 |
-
|
| 213 |
-
For best results on your specific dataset, consider re-optimizing thresholds:
|
| 214 |
-
|
| 215 |
-
```python
|
| 216 |
-
from paper4_multilabel_threshold_optimizer import find_optimal_thresholds_jaccard_global
|
| 217 |
-
|
| 218 |
-
# Load your validation data
|
| 219 |
-
validation_data = {
|
| 220 |
-
'texts': ['your text 1', 'your text 2', ...],
|
| 221 |
-
'true_labels': [['label1', 'label2'], ['label3'], ...]
|
| 222 |
-
}
|
| 223 |
-
|
| 224 |
-
# Create sector models dict (as expected by the optimizer)
|
| 225 |
-
sector_models = {
|
| 226 |
-
'your_sector': {
|
| 227 |
-
'model': model,
|
| 228 |
-
'tokenizer': tokenizer,
|
| 229 |
-
'label_names': label_names
|
| 230 |
-
}
|
| 231 |
-
}
|
| 232 |
-
|
| 233 |
-
# Find optimal thresholds for your data
|
| 234 |
-
optimal_thresholds = find_optimal_thresholds_jaccard_global(
|
| 235 |
-
sector_models,
|
| 236 |
-
validation_data,
|
| 237 |
-
device=device
|
| 238 |
-
)
|
| 239 |
-
|
| 240 |
-
# Use the optimized thresholds
|
| 241 |
-
thresholds = optimal_thresholds['your_sector']
|
| 242 |
-
```
|
| 243 |
-
|
| 244 |
-
### Alternative Optimization Methods
|
| 245 |
-
|
| 246 |
-
You can also implement other threshold optimization strategies:
|
| 247 |
-
|
| 248 |
-
- **F1-score optimization**: Maximize F1-score instead of Jaccard
|
| 249 |
-
- **Precision/Recall trade-off**: Optimize for specific precision/recall requirements
|
| 250 |
-
- **Cost-sensitive optimization**: Weight different types of errors differently
|
| 251 |
-
|
| 252 |
## Citation
|
| 253 |
|
| 254 |
If you use this model in your research, please cite:
|
|
@@ -264,9 +139,9 @@ If you use this model in your research, please cite:
|
|
| 264 |
}
|
| 265 |
```
|
| 266 |
|
| 267 |
-
##
|
| 268 |
|
| 269 |
- Trained on Reddit data from specific subreddits
|
| 270 |
-
- May not generalize to other platforms
|
| 271 |
-
- Performance depends on
|
| 272 |
-
- Limited to English
|
|
|
|
| 16 |
|
| 17 |
## Model Details
|
| 18 |
|
| 19 |
+
- Model Type: Distilbert
|
| 20 |
+
- Task: Multilabel text classification
|
| 21 |
+
- Sector: Electric Vehicles
|
| 22 |
+
- Base Model: Distilbert base uncased
|
| 23 |
+
- Labels: 7
|
| 24 |
+
- Training Data: Reddit posts from climate subreddits (2010-2023)
|
| 25 |
+
|
| 26 |
+
## Training
|
| 27 |
+
|
| 28 |
+
Trained on GPT-labeled Reddit data:
|
| 29 |
+
1. Data collection from climate subreddits
|
| 30 |
+
2. Regex filtering for sector-specific content
|
| 31 |
+
3. GPT labeling for multilabel classification
|
| 32 |
+
4. 80/10/10 train/validation/test split
|
| 33 |
+
5. Fine-tuning with threshold optimization
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
## Labels
|
| 36 |
|
| 37 |
+
The model predicts 7 labels simultaneously:
|
| 38 |
|
| 39 |
+
1. Alternative Modes
|
| 40 |
+
2. Charging Infrastructure
|
| 41 |
+
3. Environmental Benefit
|
| 42 |
+
4. Grid Impact And Energy Mix
|
| 43 |
+
5. Mineral Supply Chain
|
| 44 |
+
6. Policy And Mandates
|
| 45 |
+
7. Purchase Price
|
| 46 |
|
| 47 |
|
| 48 |
+
Note: Label order in predictions matches the order above.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
## Usage
|
| 51 |
|
| 52 |
```python
|
| 53 |
import torch
|
|
|
|
| 54 |
from transformers import DistilBertTokenizer
|
|
|
|
|
|
|
| 55 |
import tempfile
|
| 56 |
from huggingface_hub import snapshot_download
|
| 57 |
|
| 58 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# Download and load model
|
| 61 |
model_link = "sanchow/electric_vehicles-distilbert-classifier"
|
| 62 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 63 |
+
snapshot_download(repo_id=model_link, local_dir=temp_dir, local_dir_use_symlinks=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
|
|
|
| 65 |
sys.path.insert(0, temp_dir)
|
| 66 |
from model_class import MultilabelClassifier
|
| 67 |
|
|
|
|
| 68 |
tokenizer = DistilBertTokenizer.from_pretrained(temp_dir)
|
| 69 |
+
checkpoint = torch.load(os.path.join(temp_dir, 'model.pt'), map_location='cpu')
|
| 70 |
model = MultilabelClassifier(checkpoint['model_name'], len(checkpoint['label_names']))
|
| 71 |
model.load_state_dict(checkpoint['model_state_dict'])
|
| 72 |
model.to(device)
|
| 73 |
model.eval()
|
| 74 |
+
|
| 75 |
+
# Predict
|
| 76 |
+
text = "Your text here"
|
| 77 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
| 78 |
+
with torch.no_grad():
|
| 79 |
+
predictions = model(**inputs).cpu().numpy()
|
| 80 |
+
|
| 81 |
+
# Get scores
|
| 82 |
+
label_scores = {label: float(score) for label, score in zip(checkpoint['label_names'], predictions[0])}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
```
|
| 84 |
|
| 85 |
## Applications
|
| 86 |
|
| 87 |
+
- Content analysis of social media discussions
|
| 88 |
+
- Research on public sentiment and discourse
|
| 89 |
+
- Policy analysis of key topics and concerns
|
| 90 |
+
- Market research on trends and interests
|
|
|
|
| 91 |
|
| 92 |
|
| 93 |
+
## Performance
|
| 94 |
|
| 95 |
+
Best model performance:
|
| 96 |
+
- Micro Jaccard: 0.4596
|
| 97 |
+
- Macro Jaccard: 0.5701
|
| 98 |
- F1 Score: 0.8772
|
| 99 |
- Accuracy: 0.8772
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
Dataset: ~900 GPT-labeled samples per sector (600 train, 150 validation, 150 test)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
## Optimal Thresholds
|
| 106 |
|
| 107 |
+
Use these thresholds for best performance:
|
| 108 |
|
| 109 |
+
- Alternative Modes: 0.284
|
| 110 |
+
- Charging Infrastructure: 0.362
|
| 111 |
+
- Environmental Benefit: 0.403
|
| 112 |
+
- Grid Impact And Energy Mix: 0.299
|
| 113 |
+
- Mineral Supply Chain: 0.299
|
| 114 |
+
- Policy And Mandates: 0.369
|
| 115 |
+
- Purchase Price: 0.346
|
| 116 |
|
| 117 |
+
Usage:
|
| 118 |
```python
|
|
|
|
| 119 |
optimal_thresholds = {'Alternative Modes': 0.28427787391225384, 'Charging Infrastructure': 0.3619448731592626, 'Environmental Benefit': 0.4029443119613918, 'Grid Impact And Energy Mix': 0.29907076386497516, 'Mineral Supply Chain': 0.2987419331439881, 'Policy And Mandates': 0.36899998622725905, 'Purchase Price': 0.3463644004166977}
|
| 120 |
+
for label, score in zip(label_names, predictions[0]):
|
|
|
|
|
|
|
| 121 |
threshold = optimal_thresholds.get(label, 0.5)
|
| 122 |
if score > threshold:
|
| 123 |
+
print(f"{label}: {score:.3f}")
|
| 124 |
```
|
| 125 |
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
## Citation
|
| 128 |
|
| 129 |
If you use this model in your research, please cite:
|
|
|
|
| 139 |
}
|
| 140 |
```
|
| 141 |
|
| 142 |
+
## Limitations
|
| 143 |
|
| 144 |
- Trained on Reddit data from specific subreddits
|
| 145 |
+
- May not generalize to other platforms
|
| 146 |
+
- Performance depends on GPT-generated labels
|
| 147 |
+
- Limited to English content
|