finetune¶
knowledge_graph.clinical.finetune
Adapt GLiNER-Relex or HunFlair2 on the training notes, select on the validation notes, and score the test notes.
python -m knowledge_graph.clinical.finetune --model gliner_relex # writes runs/clinical/gliner_relex/ python -m knowledge_graph.clinical.finetune --model hunflair2 python -m knowledge_graph.clinical.finetune --model gliner_relex --evaluate-only
segments(nlp, source, condition)
¶
Source code in src/knowledge_graph/clinical/finetune.py
26 27 | |
reference_keys(notes)
¶
Reference mention spans and relations of the notes.
Source code in src/knowledge_graph/clinical/finetune.py
30 31 32 33 34 | |
gliner_examples(model, nlp, notes)
¶
GLiNER-Relex training inputs: word-aligned reference entities and multi-label relations per fixed256 input.
Source code in src/knowledge_graph/clinical/finetune.py
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 | |
flair_examples(nlp, notes)
¶
HunFlair2 training sentences: Disease/Chemical reference spans, longest first, without token overlap.
Source code in src/knowledge_graph/clinical/finetune.py
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 | |
gliner_predictions(model, source, annotation, segments)
¶
Joint predictions for a note's inputs, using one reference map and one segment per model batch.
Source code in src/knowledge_graph/clinical/finetune.py
106 107 108 109 110 111 112 113 114 115 116 117 | |
flair_predictions(model, source, segment)
¶
Source code in src/knowledge_graph/clinical/finetune.py
120 121 122 123 124 125 | |
GlinerRelex
¶
Joint GLiNER-Relex extraction, selected on the mean of validation NER and joint RE micro-F1.
Source code in src/knowledge_graph/clinical/finetune.py
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 | |
joint = True
class-attribute
instance-attribute
¶
model = GLiNER.from_pretrained(path).to('cuda')
instance-attribute
¶
collator = self.model._create_data_collator()
instance-attribute
¶
examples(nlp, notes)
¶
Source code in src/knowledge_graph/clinical/finetune.py
139 140 | |
optimizer()
¶
Source code in src/knowledge_graph/clinical/finetune.py
142 143 144 145 146 | |
loss(example)
¶
Source code in src/knowledge_graph/clinical/finetune.py
148 149 150 151 152 | |
release(example)
¶
Source code in src/knowledge_graph/clinical/finetune.py
154 155 | |
predict(source, annotation, segments)
¶
Source code in src/knowledge_graph/clinical/finetune.py
157 158 | |
save(out)
¶
Source code in src/knowledge_graph/clinical/finetune.py
160 161 | |
best(out)
classmethod
¶
Source code in src/knowledge_graph/clinical/finetune.py
163 164 165 | |
HunFlair2
¶
HunFlair2: Disease and Chemical spans only; selected on validation exact-span NER micro-F1 over all six gold types.
Source code in src/knowledge_graph/clinical/finetune.py
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 | |
joint = False
class-attribute
instance-attribute
¶
model = Classifier.load(path)
instance-attribute
¶
examples(nlp, notes)
¶
Source code in src/knowledge_graph/clinical/finetune.py
181 182 | |
optimizer()
¶
Source code in src/knowledge_graph/clinical/finetune.py
184 185 | |
loss(example)
¶
Source code in src/knowledge_graph/clinical/finetune.py
187 188 189 | |
release(example)
¶
Source code in src/knowledge_graph/clinical/finetune.py
191 192 | |
predict(source, annotation, segments)
¶
Source code in src/knowledge_graph/clinical/finetune.py
194 195 196 | |
save(out)
¶
Source code in src/knowledge_graph/clinical/finetune.py
198 199 | |
best(out)
classmethod
¶
Source code in src/knowledge_graph/clinical/finetune.py
201 202 203 | |
predict(system, nlp, notes, condition, path=None)
¶
Exact-span NER (and joint RE for GLiNER-Relex) of the notes under one condition.
Source code in src/knowledge_graph/clinical/finetune.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
train(system, examples, valid, nlp, out)
¶
Train on fixed256 inputs with early stopping on validation.
Source code in src/knowledge_graph/clinical/finetune.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
main()
¶
Source code in src/knowledge_graph/clinical/finetune.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |