Skip to content

Commit d4e0fab

Browse files
author
Emanuele Guidotti
committed
Refactoring
1 parent afcaa2b commit d4e0fab

7 files changed

Lines changed: 167 additions & 44 deletions

File tree

README.md

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Classification with Born's Rule
22

3-
<img src="https://upload.wikimedia.org/wikipedia/en/thumb/0/08/Logo_for_Conference_on_Neural_Information_Processing_Systems.svg/1200px-Logo_for_Conference_on_Neural_Information_Processing_Systems.svg.png" align="right" height="128"/>This repository contains the code for the paper [Text Classification with Born's Rule](https://proceedings.neurips.cc/paper_files/paper/2022/hash/c88d0c9bea6230b518ce71268c8e49e0-Abstract-Conference.html). The classifier is implemented in python and available on [PyPI](https://pypi.org/project/bornrule/). The documentation is available [here](https://bornrule.eguidotti.com).
3+
<img src="https://upload.wikimedia.org/wikipedia/en/thumb/0/08/Logo_for_Conference_on_Neural_Information_Processing_Systems.svg/1200px-Logo_for_Conference_on_Neural_Information_Processing_Systems.svg.png" align="right" height="128"/>This repository contains the code for the papers [Text Classification with Born's Rule](https://proceedings.neurips.cc/paper_files/paper/2022/hash/c88d0c9bea6230b518ce71268c8e49e0-Abstract-Conference.html) and In-Database Text Classification with BornSQL (EDBT 2026, forthcoming). The code to reproduce the papers is available in the folders `nips` and `edbt`, respectively. The classifier is implemented in python and available on [PyPI](https://pypi.org/project/bornrule/). The complete documentation is available [here](https://bornrule.eguidotti.com).
44

55
## Installation
66

@@ -41,55 +41,34 @@ from bornrule.sql import BornClassifierSQL
4141
- Equivalent to the class `BornClassifier` but for in-database classification
4242
- Supports inputs represented as json `{feature: value, ...}`
4343

44-
## Paper replication
45-
46-
All the results in the paper are obtained using Python 3.9 on a Google Cloud Virtual Machine equipped with
47-
CentOS 7, 12 vCPU Intel Cascade Lake 85 GB RAM, 1 GPU NVIDIA Tesla A100, and CUDA 11.5.
48-
49-
Install this package:
50-
51-
```commandline
52-
pip install bornrule==0.1.0
53-
```
54-
55-
Install additional dependencies to replicate the paper:
56-
57-
```commandline
58-
pip install bs4==0.0.1 nltk==3.7 matplotlib==3.5.1
59-
```
60-
61-
Install [`pytorch`](https://pytorch.org) version `1.11.0` with GPU support. For CUDA 11.5 the command is:
62-
```commandline
63-
pip install torch==1.11.0+cu115 -f https://download.pytorch.org/whl/torch_stable.html
64-
```
44+
## Cite as
6545

66-
Install [`cupy`](https://docs.cupy.dev/en/stable/install.html) version `10.4.0`. For CUDA 11.5 the command is:
67-
```commandline
68-
pip install cupy-cuda115==10.4.0
69-
```
46+
>Emanuele Guidotti and Alfio Ferrara. Text Classification with Born's Rule. In *Advances in Neural Information Processing Systems*, volume 35, pages 30990–31001, 2022.
7047
71-
Run the script `nips.py`:
48+
A BibTeX entry for LaTeX users is:
7249

73-
```commandline
74-
python -u nips.py > nips.log &
50+
```bibtex
51+
@inproceedings{guidotti2022born,
52+
title = {Text Classification with Born's Rule},
53+
author = {Guidotti, Emanuele and Ferrara, Alfio},
54+
booktitle = {Advances in Neural Information Processing Systems},
55+
pages = {30990--31001},
56+
volume = {35},
57+
year = {2022}
58+
}
7559
```
7660

77-
The script generates a folder named `results` with all the results in the paper. Additional information are saved to the log file `nips.log`
78-
79-
## Cite as
80-
81-
>Emanuele Guidotti and Alfio Ferrara. Text Classification with Born's Rule. In *Advances in Neural Information Processing Systems*, volume 35, pages 30990–31001, 2022.
61+
>Emanuele Guidotti, Darya Shlyk, Stefano Montanelli, and Alfio Ferrara. In-Database Text Classification with BornSQL. *Proceedings of the 29th international conference on extending database technology*, forthcoming, 2026.
8262
8363
A BibTeX entry for LaTeX users is:
8464

8565
```bibtex
86-
@inproceedings{guidotti2022text,
87-
title = {Text Classification with Born's Rule},
88-
author = {Guidotti, Emanuele and Ferrara, Alfio},
89-
booktitle = {Advances in Neural Information Processing Systems},
90-
pages = {30990--31001},
91-
volume = {35},
92-
year = {2022}
66+
@inproceedings{guidotti2026bornsql,
67+
author = {Emanuele Guidotti and Darya Shlyk and Stefano Montanelli and Alfio Ferrara},
68+
title = {In-Database Text Classification with BornSQL},
69+
booktitle = {Proceedings of the 29th International Conference on Extending Database Technology (EDBT)},
70+
year = {2026},
71+
note = {Forthcoming}
9372
}
9473
```
9574

edbt/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# README
1+
# Paper replication
22

33
Install all dependencies
44

@@ -49,3 +49,20 @@ poetry run python bornsql.py --database <dbname> --dbms sqlite
4949
```
5050

5151
All results are stored into an `output/` folder that is generated in the working directory.
52+
53+
54+
## Cite as
55+
56+
>Emanuele Guidotti, Darya Shlyk, Stefano Montanelli, and Alfio Ferrara. In-Database Text Classification with BornSQL. *Proceedings of the 29th international conference on extending database technology*, forthcoming, 2026.
57+
58+
A BibTeX entry for LaTeX users is:
59+
60+
```bibtex
61+
@inproceedings{guidotti2026bornsql,
62+
author = {Emanuele Guidotti and Darya Shlyk and Stefano Montanelli and Alfio Ferrara},
63+
title = {In-Database Text Classification with BornSQL},
64+
booktitle = {Proceedings of the 29th International Conference on Extending Database Technology (EDBT)},
65+
year = {2026},
66+
note = {Forthcoming}
67+
}
68+
```

nips/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
# Paper replication
3+
4+
All the results in the paper are obtained using Python 3.9 on a Google Cloud Virtual Machine equipped with
5+
CentOS 7, 12 vCPU Intel Cascade Lake 85 GB RAM, 1 GPU NVIDIA Tesla A100, and CUDA 11.5.
6+
7+
Install this package:
8+
9+
```commandline
10+
pip install bornrule==0.1.0
11+
```
12+
13+
Install additional dependencies to replicate the paper:
14+
15+
```commandline
16+
pip install bs4==0.0.1 nltk==3.7 matplotlib==3.5.1
17+
```
18+
19+
Install [`pytorch`](https://pytorch.org) version `1.11.0` with GPU support. For CUDA 11.5 the command is:
20+
```commandline
21+
pip install torch==1.11.0+cu115 -f https://download.pytorch.org/whl/torch_stable.html
22+
```
23+
24+
Install [`cupy`](https://docs.cupy.dev/en/stable/install.html) version `10.4.0`. For CUDA 11.5 the command is:
25+
```commandline
26+
pip install cupy-cuda115==10.4.0
27+
```
28+
29+
Run the script `main.py`:
30+
31+
```commandline
32+
python main.py
33+
```
34+
35+
The script generates a folder named `results` with all the results in the paper.
36+
37+
## Cite as
38+
39+
>Emanuele Guidotti and Alfio Ferrara. Text Classification with Born's Rule. In *Advances in Neural Information Processing Systems*, volume 35, pages 30990–31001, 2022.
40+
41+
A BibTeX entry for LaTeX users is:
42+
43+
```bibtex
44+
@inproceedings{guidotti2022text,
45+
title = {Text Classification with Born's Rule},
46+
author = {Guidotti, Emanuele and Ferrara, Alfio},
47+
booktitle = {Advances in Neural Information Processing Systems},
48+
pages = {30990--31001},
49+
volume = {35},
50+
year = {2022}
51+
}
52+
```

nips/__init__.py

Whitespace-only changes.

nips/experiment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from sklearn.svm import SVC
1616
from sklearn.tree import DecisionTreeClassifier
1717
from bornrule import BornClassifier
18-
from .dataset import Dataset
19-
from .networks import BornSqrt
18+
from dataset import Dataset
19+
from networks import BornSqrt
2020

2121

2222
class Experiment:

nips/main.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import torch
2+
from metrics import l1_loss
3+
from experiment import Experiment
4+
from sklearn.metrics import accuracy_score
5+
6+
# Setup experiments:
7+
# - loss: the loss used to train neural networks
8+
# - score: the metric to evaluate the classification results
9+
# - needs_proba: True if score requires probabilities rather than predicted classes
10+
# - device: the torch device (CPU/GPU)
11+
# - data_dir: the directory where data are to be downloaded
12+
# - output_dir: the directory where results are to be saved
13+
kwargs = {
14+
'loss': l1_loss,
15+
'score': accuracy_score,
16+
'needs_proba': False,
17+
'device': torch.device('cuda:0' if torch.cuda.is_available() else 'cpu'),
18+
'data_dir': 'data',
19+
'output_dir': 'results',
20+
}
21+
22+
23+
# R8 dataset
24+
r8 = Experiment(dataset='r8', **kwargs)
25+
r8.data.summary()
26+
27+
# Timing
28+
r8.timing_gpu(runs=10)
29+
r8.timing_cpu(runs=10)
30+
r8.plot_timing(score_label='Accuracy Score')
31+
32+
# Train the networks and plot the learning curves
33+
r8.learning_curve(epochs=100, runs=10, batch_size=128)
34+
r8.plot_learning_curve(score_label="Accuracy Score", loss_label="L1-Loss")
35+
36+
37+
# R52 dataset
38+
r52 = Experiment(dataset='r52', **kwargs)
39+
r52.data.summary()
40+
41+
# Timing
42+
r52.timing_gpu(runs=10)
43+
r52.timing_cpu(runs=10)
44+
r52.plot_timing(score_label='Accuracy Score')
45+
46+
# Train the networks and plot the learning curves
47+
r52.learning_curve(epochs=100, runs=10, batch_size=128)
48+
r52.plot_learning_curve(score_label="Accuracy Score", loss_label="L1-Loss")
49+
50+
51+
# 20NG dataset
52+
ng = Experiment(dataset='20ng', **kwargs)
53+
ng.data.summary()
54+
55+
# Timing GPU
56+
ng.timing_gpu(runs=10)
57+
ng.timing_cpu(runs=10)
58+
ng.plot_timing(score_label='Accuracy Score')
59+
60+
# Train the networks and plot the learning curves
61+
ng.learning_curve(epochs=100, runs=10, batch_size=128)
62+
ng.plot_learning_curve(score_label="Accuracy Score", loss_label="L1-Loss")
63+
64+
# Plot explanation of Born layer for class 9 (baseball)
65+
ng.plot_explanation(c=9, batch_size=128, random_state=51)
66+
67+
# Table top 10 features for Born classifier
68+
ng.table_explanation(top=10)
69+
70+
# Ablation study
71+
ng.ablation_study()
72+
ng.plot_ablation()
73+
74+
# Table cross-validation times and scores (takes 24-48 hours)
75+
ng.cross_validation()
File renamed without changes.

0 commit comments

Comments
 (0)