File size: 6,489 Bytes
9b1c753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# πŸš€ QUICK START GUIDE - Legal-BERT

## Prerequisites

```bash
# 1. Install dependencies
pip install -r requirements.txt

# 2. Download CUAD dataset
# Place at: dataset/CUAD_v1/CUAD_v1.json
```

## Verify Setup

```bash
python test_setup.py
```

Expected output:
```
πŸ§ͺ LEGAL-BERT PROJECT - QUICK TEST
================================================================================
πŸ” Testing imports...
  βœ… PyTorch
  βœ… Transformers
  βœ… scikit-learn
  βœ… Pandas
  βœ… NumPy
...
βœ… ALL TESTS PASSED!
πŸš€ Ready to train! Run: python train.py
```

## Training

```bash
python train.py
```

**What it does**:
1. Loads CUAD dataset (19,598 clauses)
2. Discovers 7 risk patterns automatically
3. Trains Legal-BERT for 5 epochs (~2-4 hours on GPU)
4. Saves checkpoints every epoch
5. Generates training history plot

**Output**:
```
checkpoints/
  β”œβ”€β”€ legal_bert_epoch_1.pt
  β”œβ”€β”€ legal_bert_epoch_2.pt
  β”œβ”€β”€ ...
  β”œβ”€β”€ training_history.png
  └── training_summary.json
models/legal_bert/
  └── final_model.pt
```

**Expected Results**:
- Train Accuracy: >60%
- Val Accuracy: >55%

## Evaluation

```bash
python evaluate.py
```

**What it does**:
1. Loads trained model
2. Evaluates on test set
3. Calculates comprehensive metrics
4. Generates visualizations
5. Saves detailed report

**Output**:
```
checkpoints/
  β”œβ”€β”€ evaluation_results.json
  β”œβ”€β”€ confusion_matrix.png
  └── risk_distribution.png
evaluation_report.txt
```

**Expected Results**:
- Accuracy: >70%
- F1-Score: >0.65
- Precision: >0.60
- Recall: >0.60

## Calibration

```bash
python calibrate.py
```

**What it does**:
1. Loads trained model
2. Applies temperature scaling
3. Calculates ECE/MCE
4. Saves calibrated model
5. Exports results

**Output**:
```
checkpoints/
  └── calibration_results.json
models/legal_bert/
  └── calibrated_model.pt
```

**Expected Results**:
- ECE: 0.15 β†’ <0.08
- MCE: 0.20 β†’ <0.12

## Complete Pipeline

```bash
# Run everything in sequence
python train.py && python evaluate.py && python calibrate.py
```

## Configuration

Edit `config.py` to customize:

```python
# Model settings
bert_model_name = "bert-base-uncased"
num_risk_categories = 7
max_sequence_length = 512

# Training settings
batch_size = 16          # Reduce if GPU OOM
num_epochs = 5           # Increase for better results
learning_rate = 2e-5     # Adjust for convergence

# Paths
data_path = "dataset/CUAD_v1/CUAD_v1.json"
checkpoint_dir = "checkpoints"
```

## Troubleshooting

### GPU Out of Memory
```python
# In config.py, reduce:
batch_size = 8  # or even 4
```

### Missing Dataset
```bash
# Error: Dataset not found
# Solution: Download CUAD and place at:
dataset/CUAD_v1/CUAD_v1.json
```

### Import Errors
```bash
# Reinstall dependencies
pip install -r requirements.txt --upgrade
```

### Visualization Errors
```bash
# If matplotlib errors occur
pip install matplotlib seaborn
# Or plots will be skipped (functionality still works)
```

## Performance Tips

### Speed Up Training
1. Use GPU (CUDA): Automatic if available
2. Increase batch size: `batch_size = 32`
3. Use fewer epochs: `num_epochs = 3`

### Improve Accuracy
1. Train longer: `num_epochs = 10`
2. Adjust learning rate: `learning_rate = 3e-5`
3. Use larger BERT: `bert_model_name = "bert-large-uncased"`

### Better Calibration
1. More validation data: Adjust splits in `data_loader.py`
2. More iterations: In `calibrate.py` increase `max_iter`

## File Structure

```
code2/
β”œβ”€β”€ train.py           ← Run this first
β”œβ”€β”€ evaluate.py        ← Then this
β”œβ”€β”€ calibrate.py       ← Finally this
β”œβ”€β”€ test_setup.py      ← Verify before training
β”‚
β”œβ”€β”€ config.py          ← Edit settings here
β”œβ”€β”€ data_loader.py     ← Loads CUAD dataset
β”œβ”€β”€ risk_discovery.py  ← Discovers patterns
β”œβ”€β”€ model.py           ← Legal-BERT architecture
β”œβ”€β”€ trainer.py         ← Training logic
β”œβ”€β”€ evaluator.py       ← Evaluation logic
β”œβ”€β”€ utils.py           ← Helper functions
β”‚
β”œβ”€β”€ README.md                 ← Full documentation
β”œβ”€β”€ IMPLEMENTATION.md         ← Implementation details
β”œβ”€β”€ COMPLETION_SUMMARY.md     ← What was done
└── QUICK_START.md            ← This file
```

## Common Commands

```bash
# Check setup
python test_setup.py

# Train model
python train.py

# Evaluate model  
python evaluate.py

# Calibrate model
python calibrate.py

# Run all
python train.py && python evaluate.py && python calibrate.py

# Python interactive (after training)
python
>>> from evaluator import LegalBertEvaluator
>>> # Load and analyze results
```

## Expected Timeline

| Task | Time (GPU) | Time (CPU) |
|------|-----------|-----------|
| Setup verification | 30 seconds | 30 seconds |
| Training (5 epochs) | 2-4 hours | 8-12 hours |
| Evaluation | 10 minutes | 20 minutes |
| Calibration | 5 minutes | 10 minutes |
| **Total** | **~3 hours** | **~10 hours** |

## Success Indicators

### After Training
βœ… Checkpoints saved in `checkpoints/`  
βœ… Training loss decreasing  
βœ… Validation accuracy >55%  
βœ… No CUDA errors  

### After Evaluation
βœ… Accuracy >70%  
βœ… F1-Score >0.65  
βœ… Confusion matrix generated  
βœ… Report saved  

### After Calibration
βœ… ECE <0.10  
βœ… Temperature ~1.5-2.5  
βœ… Calibrated model saved  

## Getting Help

1. Check `README.md` for detailed documentation
2. Check `IMPLEMENTATION.md` for technical details
3. Check `COMPLETION_SUMMARY.md` for what was implemented
4. Review error messages carefully
5. Verify setup with `python test_setup.py`

## Next Steps

After completing training, evaluation, and calibration:

1. **Analyze Results**: Check evaluation report
2. **Tune Parameters**: Adjust `config.py` if needed
3. **Retrain**: Run `train.py` again with new settings
4. **Deploy** (optional): Create API or web interface

## Key Metrics to Track

### Training
- Train Loss (should decrease)
- Val Loss (should decrease)
- Train Accuracy (should increase)
- Val Accuracy (should increase)

### Evaluation
- Overall Accuracy (>70%)
- F1-Score (>0.65)
- Per-pattern F1 (check which patterns need work)
- Regression RΒ² (>0.60 for severity/importance)

### Calibration
- ECE (target: <0.08)
- MCE (target: <0.12)
- Temperature (typically 1.5-2.5)

---

**Ready? Start with**: `python test_setup.py`

**Questions?** Check `README.md` for comprehensive documentation.

**πŸŽ‰ Good luck with your Legal-BERT training! πŸŽ‰**