Mahmoud Amiri commited on
Commit
22cb24e
·
1 Parent(s): f9a81fe

update readme file

Browse files
Files changed (1) hide show
  1. README.md +234 -63
README.md CHANGED
@@ -1,88 +1,259 @@
 
 
 
 
 
 
 
1
  ---
2
- license: cc-by-4.0
3
- tags:
4
- - multilabel-classification
5
- - scientific-literature
6
- - tensorflow
7
- - mlp
8
- - chemistry
9
- datasets:
10
- - Bocklitz-Lab/lit2vec-subfield-classifier-dataset
11
- language:
12
- - en
13
- library_name: tensorflow
14
- model-index:
15
- - name: MLP Subfield Classifier (Lit2Vec)
16
- results: []
 
 
 
 
 
 
 
 
 
 
 
17
  ---
18
 
19
- # 🧪 MLP Multi-Label Classifier for Scientific Subfields
20
 
21
- A lightweight **multi-layer perceptron (MLP)** model trained to predict **multiple chemistry subfields** from precomputed Lit2Vec embeddings of scientific abstracts.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- Built using the [Lit2Vec Subfield Classifier Dataset](https://huggingface.co/datasets/Bocklitz-Lab/lit2vec-subfield-classifier-dataset), this model supports **multi-label classification** across 18 chemistry-related subfields.
24
 
25
  ---
26
 
27
- ## 📊 Subfields Predicted (18 labels)
28
-
29
- - Catalysis
30
- - Organic Chemistry
31
- - Polymer Chemistry
32
- - Inorganic Chemistry
33
- - Materials Science
34
- - Analytical Chemistry
35
- - Physical Chemistry
36
- - Biochemistry
37
- - Environmental Chemistry
38
- - Energy Chemistry
39
- - Medicinal Chemistry
40
- - Chemical Engineering
41
- - Supramolecular Chemistry
42
- - Radiochemistry & Nuclear Chemistry
43
- - Forensic & Legal Chemistry
44
- - Food Chemistry
45
- - Chemical Education
46
- - Others
47
-
48
- Each prediction returns **probabilities** per class (using sigmoid), and labels with probabilities > 0.5 are returned as positives.
49
 
50
  ---
51
 
52
- ## 🧠 Model Architecture
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- - Input size: `768` (Lit2Vec embedding)
55
- - Hidden layers: `[256, 256]`
56
- - Activation: ReLU
57
- - Dropout: `0.3`
58
- - Output: 18 sigmoid units (multi-label)
59
- - Loss: Weighted binary crossentropy
60
- - Optimizer: Adam
61
- - Framework: TensorFlow 2 / Keras
62
 
63
  ---
64
 
65
- ## 🔧 Usage Example
 
 
 
 
66
 
67
  ```python
68
- from huggingface_hub import hf_hub_download
69
- import tensorflow as tf
70
  import numpy as np
 
 
71
  import json
72
 
73
- # Load model
74
- model_path = hf_hub_download("your-username/my-mlp-model", "mlp_model.h5")
75
- model = tf.keras.models.load_model(model_path, compile=False)
 
 
76
 
77
  # Load label mapping
78
- label_map_path = hf_hub_download("your-username/my-mlp-model", "label_mapping.json")
79
  with open(label_map_path) as f:
80
- label_mapping = json.load(f)["index_to_label"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- # Predict on new embedding
83
- embedding = np.random.rand(768).tolist() # or use Lit2Vec
84
- pred = model.predict(np.array([embedding]))[0]
85
 
86
- # Get labels with probability > 0.5
87
- labels = [label_mapping[str(i)] for i, p in enumerate(pred) if p > 0.5]
88
- print("Predicted labels:", labels)
 
 
 
 
 
 
 
1
+ # Lit2Vec Subfield Classifier (MLP)
2
+
3
+ > Multi-label classifier for chemistry subfields using dense text embeddings.
4
+
5
+ **Repo:** [https://huggingface.co/Bocklitz-Lab/lit2vec-subfield-classifier-model](https://huggingface.co/Bocklitz-Lab/lit2vec-subfield-classifier-model)
6
+ **Dataset:** [https://huggingface.co/datasets/Bocklitz-Lab/lit2vec-subfield-classifier-dataset](https://huggingface.co/datasets/Bocklitz-Lab/lit2vec-subfield-classifier-dataset)
7
+
8
  ---
9
+
10
+ ## Model Summary
11
+
12
+ This model is a **Keras MLP** for **multi-label** classification of chemistry-related scientific texts.
13
+ It consumes a **dense embedding vector** and predicts one or more **subfields** (e.g., *Catalysis*, *Energy Chemistry*, *Materials Science*).
14
+
15
+ * **Input:** dense embedding vector (`embedding` from the dataset)
16
+ * **Output:** 18 sigmoid probabilities (one per subfield)
17
+ * **Task:** multilabel text classification (thresholded at 0.5 by default)
18
+
19
+ ---
20
+
21
+ ## Intended Use & Limitations
22
+
23
+ **Intended use**
24
+
25
+ * Subfield tagging for chemistry abstracts/summaries
26
+ * Metadata enrichment for literature databases
27
+ * Retrieval, filtering, and analytics
28
+
29
+ **Limitations**
30
+
31
+ * Trained only on chemistry texts → may not generalize to other domains
32
+ * Requires the **same embedding space** as the dataset encoder (raw text is **not** accepted directly)
33
+ * Long-tail subfields (few examples) may have lower F1
34
+
35
  ---
36
 
37
+ ## Labels
38
 
39
+ | ID | Subfield |
40
+ | -- | ---------------------------------- |
41
+ | 0 | Catalysis |
42
+ | 1 | Organic Chemistry |
43
+ | 2 | Polymer Chemistry |
44
+ | 3 | Inorganic Chemistry |
45
+ | 4 | Materials Science |
46
+ | 5 | Analytical Chemistry |
47
+ | 6 | Physical Chemistry |
48
+ | 7 | Biochemistry |
49
+ | 8 | Environmental Chemistry |
50
+ | 9 | Energy Chemistry |
51
+ | 10 | Medicinal Chemistry |
52
+ | 11 | Chemical Engineering |
53
+ | 12 | Supramolecular Chemistry |
54
+ | 13 | Radiochemistry & Nuclear Chemistry |
55
+ | 14 | Forensic & Legal Chemistry |
56
+ | 15 | Food Chemistry |
57
+ | 16 | Chemical Education |
58
+ | 17 | Others |
59
 
60
+ The repo includes `label_mapping.json`.
61
 
62
  ---
63
 
64
+ ## Training Details
65
+
66
+ * **Framework:** TensorFlow/Keras
67
+ * **Architecture:**
68
+
69
+ * Input → Dense(256, ReLU) → (BatchNorm) → Dropout(0.3)
70
+ * Dense(256, ReLU) → (BatchNorm) → Dropout(0.3)
71
+ * Output: Dense(18, **sigmoid**)
72
+ * **Loss:** **Weighted** Binary Cross-Entropy (per-class weights from train frequency)
73
+ * **Optimizer:** Adam (ReduceLROnPlateau)
74
+ * **Callbacks:** EarlyStopping (restore best), ReduceLROnPlateau, optional W\&B logging
75
+ * **Validation:** 5-fold CV on train+val; final training on official splits
76
+ * **Best epoch (val):** 11 (from W\&B)
 
 
 
 
 
 
 
 
 
77
 
78
  ---
79
 
80
+ ## Evaluation
81
+
82
+ **Validation (final run):**
83
+
84
+ * PR-AUC: **0.8688**
85
+ * ROC-AUC: **0.9725**
86
+ * Binary Accuracy: **0.9597**
87
+
88
+ **Test (held-out split, threshold = 0.5):**
89
+
90
+ * **Micro F1:** **0.81**
91
+ * **Macro F1:** **0.75**
92
+ * **Weighted F1:** **0.80**
93
+ * **Samples F1:** **0.80**
94
+
95
+ **Per-label (F1, support):**
96
+
97
+ | Subfield | F1 | Support |
98
+ | ---------------------------------- | -------- | ------- |
99
+ | Catalysis | 0.80 | 197 |
100
+ | Organic Chemistry | 0.70 | 245 |
101
+ | Polymer Chemistry | 0.72 | 120 |
102
+ | Inorganic Chemistry | 0.71 | 203 |
103
+ | Materials Science | 0.80 | 917 |
104
+ | Analytical Chemistry | 0.71 | 633 |
105
+ | Physical Chemistry | 0.63 | 240 |
106
+ | Biochemistry | **0.92** | 2106 |
107
+ | Environmental Chemistry | 0.79 | 508 |
108
+ | Energy Chemistry | 0.79 | 166 |
109
+ | Medicinal Chemistry | 0.82 | 1343 |
110
+ | Chemical Engineering | **0.53** | 413 |
111
+ | Supramolecular Chemistry | 0.68 | 34 |
112
+ | Radiochemistry & Nuclear Chemistry | 0.65 | 20 |
113
+ | Forensic & Legal Chemistry | 0.70 | 16 |
114
+ | Food Chemistry | 0.83 | 282 |
115
+ | Chemical Education | 0.85 | 20 |
116
+ | Others | 0.83 | 19 |
117
 
118
+ **Notes**
119
+
120
+ * Strong performance on frequent classes like **Biochemistry**, **Medicinal Chemistry**, **Food Chemistry**.
121
+ * Lower F1 on long-tail or heterogeneous labels like **Chemical Engineering** and **Physical Chemistry**.
122
+
123
+ If included in the repo, the plot `f1_vs_freq.png` shows F1 vs. training label frequency.
 
 
124
 
125
  ---
126
 
127
+ ## Usage
128
+
129
+ > The model expects the **same embedding space** as the dataset’s `embedding`. If you want to apply it to new texts, you must compute embeddings with the **same encoder** used to create the dataset.
130
+
131
+ ### Quick start (load from Hub, inference)
132
 
133
  ```python
 
 
134
  import numpy as np
135
+ from huggingface_hub import hf_hub_download
136
+ from tensorflow import keras
137
  import json
138
 
139
+ REPO_ID = "Bocklitz-Lab/lit2vec-subfield-classifier-model"
140
+
141
+ # Download files
142
+ model_path = hf_hub_download(REPO_ID, filename="mlp_model.h5")
143
+ label_map_path = hf_hub_download(REPO_ID, filename="label_mapping.json")
144
 
145
  # Load label mapping
 
146
  with open(label_map_path) as f:
147
+ m = json.load(f)
148
+ index_to_label = {int(k): v for k, v in m["index_to_label"].items()}
149
+
150
+ # Load model (no need for custom loss at inference)
151
+ model = keras.models.load_model(model_path, compile=False)
152
+
153
+ # x must be a single embedding vector with the correct dimension
154
+ x = np.load("example_embedding.npy").astype("float32") # shape (D,)
155
+ x = x[None, :] # shape (1, D)
156
+
157
+ probs = model.predict(x, verbose=0)[0]
158
+ threshold = 0.5
159
+ pred_ids = [i for i, p in enumerate(probs) if p > threshold]
160
+ pred_labels = [index_to_label[i] for i in pred_ids]
161
+ print(list(zip(pred_labels, probs[pred_ids])))
162
+ ```
163
+
164
+ ### Batch inference
165
+
166
+ ```python
167
+ X = np.load("embeddings_batch.npy").astype("float32") # shape (N, D)
168
+ probs = model.predict(X, verbose=0) # shape (N, 18)
169
+ labels_per_row = [[index_to_label[i] for i, p in enumerate(row) if p > 0.5] for row in probs]
170
+ ```
171
+
172
+ > **Tip:** If you need to **fine-tune** the model, recompile it and reuse the weighted BCE:
173
+ >
174
+ > * At load time, pass `compile=False` and then `model.compile(loss="binary_crossentropy", ...)` for inference-only.
175
+ > * For training with class weights, reintroduce the weighted loss you used in the training script.
176
+
177
+ ---
178
+
179
+ ## Files in this repository
180
+
181
+ * `mlp_model.h5` – Keras model weights/graph
182
+ * `label_mapping.json` – name ↔ id mapping
183
+ * `training_history.json` – training curves (optional)
184
+ * `f1_vs_freq.png` – F1 vs frequency plot (optional)
185
+ * `README.md` – this model card
186
+
187
+ ---
188
+
189
+ ## Dataset
190
+
191
+ **Lit2Vec Subfield Classifier Dataset**
192
+
193
+ * \~39.9k CC-BY scientific texts with **embeddings** and **subfield labels**
194
+ * Splits: \~80/10/10 (train/val/test)
195
+ * [https://huggingface.co/datasets/Bocklitz-Lab/lit2vec-subfield-classifier-dataset](https://huggingface.co/datasets/Bocklitz-Lab/lit2vec-subfield-classifier-dataset)
196
+
197
+ ---
198
+
199
+ ## Model Index
200
+
201
+ ```yaml
202
+ model-index:
203
+ - name: Lit2Vec Subfield Classifier (MLP)
204
+ results:
205
+ - task:
206
+ type: text-classification
207
+ name: Multi-label text classification
208
+ dataset:
209
+ name: Lit2Vec Subfield Classifier Dataset
210
+ type: Bocklitz-Lab/lit2vec-subfield-classifier-dataset
211
+ split: test
212
+ metrics:
213
+ - type: micro_f1
214
+ value: 0.81
215
+ - type: macro_f1
216
+ value: 0.75
217
+ - type: weighted_f1
218
+ value: 0.80
219
+ - type: pr_auc
220
+ value: 0.8688
221
+ - type: roc_auc
222
+ value: 0.9725
223
+ ```
224
+
225
+ ---
226
+
227
+ ## License
228
+
229
+ * **Model:** CC BY 4.0
230
+ * **Dataset:** CC BY 4.0
231
+
232
+ ---
233
+
234
+ ## Citations
235
+
236
+ **Dataset**
237
+
238
+ ```bibtex
239
+ @dataset{lit2vec_classifier_2025,
240
+ author = {Mahmoud Amiri, Thomas Bocklitz},
241
+ title = {Lit2Vec Subfield Classifier Dataset},
242
+ year = {2025},
243
+ publisher = {Hugging Face},
244
+ howpublished = {\url{https://huggingface.co/datasets/Bocklitz-Lab/lit2vec-subfield-classifier-dataset}},
245
+ note = {Submitted to Nature Scientific Data}
246
+ }
247
+ ```
248
 
249
+ **Model**
 
 
250
 
251
+ ```bibtex
252
+ @misc{lit2vec_mlp_classifier_2025,
253
+ title = {Lit2Vec Subfield Classifier Model},
254
+ author = {Mahmoud Amiri and Thomas Bocklitz},
255
+ year = {2025},
256
+ publisher = {Hugging Face},
257
+ howpublished = {\url{https://huggingface.co/Bocklitz-Lab/lit2vec-subfield-classifier-model}}
258
+ }
259
+ ```