medcat.components.addons.relation_extraction.ml_utils
Functions:
-
create_dense_layers– -
create_tokenizer_pretrain–This method simply adds the default special tokens that we ecounter.
-
get_annotation_schema_tag–Gets to token sequences from the sequence_ouput for the specific token
-
load_bin_file– -
load_results– -
load_state–Used by RelCAT.load() and RelCAT.train()
-
save_bin_file– -
save_results– -
save_state–Used by RelCAT.save() and RelCAT.train()
-
split_list_train_test_by_class–Args:
Attributes:
-
logger–
create_dense_layers
create_dense_layers(relcat_config: ConfigRelCAT)
Source code in medcat-v2/medcat/components/addons/relation_extraction/ml_utils.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
create_tokenizer_pretrain
create_tokenizer_pretrain(tokenizer: BaseTokenizerWrapper, relcat_config: ConfigRelCAT) -> BaseTokenizerWrapper
This method simply adds the default special tokens that we ecounter.
Parameters:
-
(tokenizerBaseTokenizerWrapper) –BERT/Llama tokenizer.
-
(relcat_configConfigRelCAT) –The RelCAT config.
Returns:
-
BaseTokenizerWrapper(BaseTokenizerWrapper) –The same tokenizer.
Source code in medcat-v2/medcat/components/addons/relation_extraction/ml_utils.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
get_annotation_schema_tag
get_annotation_schema_tag(sequence_output: Tensor, input_ids: Tensor, special_tag: list) -> Tensor
Gets to token sequences from the sequence_ouput for the specific token tag ids in self.relcat_config.general.annotation_schema_tag_ids.
Parameters:
-
(sequence_outputTensor) –hidden states/embeddings for each token in the input text
-
(input_idsTensor) –input token ids
-
(special_taglist) –special annotation token id pairs
Returns:
-
Tensor–torch.Tensor: new seq_tags
Source code in medcat-v2/medcat/components/addons/relation_extraction/ml_utils.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |
load_bin_file
load_bin_file(file_name, path='./') -> Any
Source code in medcat-v2/medcat/components/addons/relation_extraction/ml_utils.py
100 101 102 103 | |
load_results
load_results(path: str, model_name: str = 'BERT', file_prefix: str = 'train') -> tuple[list, list, list]
Source code in medcat-v2/medcat/components/addons/relation_extraction/ml_utils.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
load_state
load_state(model, optimizer, scheduler, path: str = './', model_name: str = 'BERT', file_prefix: str = 'train', load_best: bool = False, relcat_config: ConfigRelCAT = ConfigRelCAT()) -> tuple[int, int]
Used by RelCAT.load() and RelCAT.train()
Parameters:
-
(modelRelExtrBaseModel) –RelExtrBaseModel, it has to be initialized before calling this method via RelExtr(Bert/Llama)Model(...)
-
(optimizer_type_) –optimizer
-
(scheduler_type_) –scheduler
-
(pathstr, default:'./') –Defaults to "./".
-
(model_namestr, default:'BERT') –Defaults to "BERT".
-
(file_prefixstr, default:'train') –Defaults to "train".
-
(load_bestbool, default:False) –Defaults to False.
-
(relcat_configConfigRelCAT, default:ConfigRelCAT()) –Defaults to ConfigRelCAT().
Returns:
Source code in medcat-v2/medcat/components/addons/relation_extraction/ml_utils.py
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 | |
save_bin_file
save_bin_file(file_name, data, path='./')
Source code in medcat-v2/medcat/components/addons/relation_extraction/ml_utils.py
106 107 108 | |
save_results
Source code in medcat-v2/medcat/components/addons/relation_extraction/ml_utils.py
223 224 225 226 | |
save_state
save_state(model, optimizer: AdamW, scheduler: MultiStepLR, epoch: int = 1, best_f1: float = 0.0, path: str = './', model_name: str = 'BERT', task: str = 'train', is_checkpoint=False, final_export=False) -> None
Used by RelCAT.save() and RelCAT.train() Saves the RelCAT model state. For checkpointing multiple files are created, best_f1, loss etc. score. If you want to export the model after training set final_export=True and leave is_checkpoint=False.
Parameters:
-
(modelBaseModel) –BertMode | LlamaModel etc.
-
(optimizerAdamW) –Defaults to None.
-
(schedulerMultiStepLR) –Defaults to None.
-
(epochint, default:1) –Defaults to None.
-
(best_f1float, default:0.0) –Defaults to None.
-
(pathstr, default:'./') –Defaults to "./".
-
(model_namestr, default:'BERT') –. Defaults to "BERT". This is used to checkpointing only.
-
(taskstr, default:'train') –Defaults to "train". This is used to checkpointing only.
-
(is_checkpointbool, default:False) –Defaults to False.
-
(final_exportbool, default:False) –Defaults to False, if True then is_checkpoint must be False also. Exports model.state_dict(), out into "model.dat".
Source code in medcat-v2/medcat/components/addons/relation_extraction/ml_utils.py
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 | |
split_list_train_test_by_class
split_list_train_test_by_class(data: list, sample_limit: int = -1, test_size: float = 0.2, shuffle: bool = True) -> tuple[list, list]
Parameters:
-
(datalist) –"output_relations": relation_instances, see create_base_relations_from_doc/csv for data columns
-
(sample_limitint, default:-1) –Limit the number of samples per class, useful for dataset balancing . Defaults to -1.
-
(test_sizefloat, default:0.2) –Defaults to 0.2.
-
(shufflebool, default:True) –Shuffle data randomly. Defaults to True.
Returns:
Source code in medcat-v2/medcat/components/addons/relation_extraction/ml_utils.py
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 | |