medcat.components.ner.trf.transformers_ner
Classes:
-
TransformersNER– -
TransformersNERComponent–TODO: Add documentation
Functions:
Attributes:
-
TrCBCreator– -
logger–
TransformersNER
TransformersNER(cdb: CDB, base_tokenizer: BaseTokenizer, component: TransformersNERComponent, config: Optional[ConfigTransformersNER] = None, training_arguments=None)
Bases: AbstractEntityProvidingComponent
Methods:
-
create_new– -
create_new_component– -
deserialise_from– -
get_folder_name– -
get_init_attrs– -
get_strategy– -
get_type– -
ignore_attrs– -
include_properties– -
load_existing– -
predict_entities– -
save– -
serialise_to–
Attributes:
-
name– -
should_save(bool) –
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
52 53 54 55 56 57 58 | |
name
class-attribute
instance-attribute
name = 'transformers_ner'
create_new
classmethod
create_new(cdb: CDB, base_tokenizer: BaseTokenizer, config: Optional[ConfigTransformersNER] = None, training_arguments=None) -> TransformersNER
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
60 61 62 63 64 65 66 67 68 | |
create_new_component
classmethod
create_new_component(cnf: ComponentConfig, tokenizer: BaseTokenizer, cdb: CDB, vocab: Vocab, model_load_path: Optional[str]) -> TransformersNER
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
deserialise_from
classmethod
deserialise_from(folder_path: str, **init_kwargs) -> TransformersNER
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
127 128 129 130 131 132 133 134 135 | |
get_folder_name
get_folder_name() -> str
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
121 122 | |
get_init_attrs
classmethod
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
140 141 142 | |
get_strategy
get_strategy() -> SerialisingStrategy
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
137 138 | |
get_type
get_type()
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
99 100 | |
ignore_attrs
classmethod
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
144 145 146 | |
include_properties
classmethod
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
148 149 150 | |
load_existing
classmethod
load_existing(cdb: CDB, base_tokenizer: BaseTokenizer, load_path: str, training_arguments=None, config: Optional[ConfigTransformersNER] = None) -> TransformersNER
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
89 90 91 92 93 94 95 96 97 | |
predict_entities
predict_entities(doc: MutableDocument, ents: list[MutableEntity] | None = None) -> list[MutableEntity]
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
111 112 113 114 115 116 117 | |
save
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
106 107 108 109 | |
serialise_to
serialise_to(folder_path: str) -> None
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
124 125 | |
TransformersNERComponent
TransformersNERComponent(cdb: CDB, base_tokenizer: BaseTokenizer, config: Optional[ConfigTransformersNER] = None, training_arguments=None)
TODO: Add documentation
Methods:
-
batch_generator– -
create_eval_pipeline– -
eval– -
expand_model_with_concepts–Expand the model with new concepts and their preferred names, which
-
get_hash–A partial hash trying to catch differences between models.
-
pipe–Process many documents at once.
-
train–Train or continue training a model give a json_path containing a
Attributes:
-
base_tokenizer– -
cdb– -
config– -
model– -
tokenizer– -
training_arguments–
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
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 | |
base_tokenizer
instance-attribute
base_tokenizer = base_tokenizer
cdb
instance-attribute
cdb = cdb
config
instance-attribute
config = config
model
instance-attribute
model = from_pretrained(model_name)
training_arguments
instance-attribute
training_arguments = TrainingArguments(output_dir='./results', logging_dir='./logs', num_train_epochs=10, per_device_train_batch_size=1, per_device_eval_batch_size=1, weight_decay=0.14, warmup_ratio=0.01, learning_rate=4.47e-05, eval_accumulation_steps=1, gradient_accumulation_steps=4, do_eval=True, eval_strategy='epoch', logging_strategy='epoch', save_strategy='epoch', metric_for_best_model='eval_recall', load_best_model_at_end=True, remove_unused_columns=False)
batch_generator
staticmethod
batch_generator(stream: Iterable[MutableDocument], batch_size_chars: int) -> Iterable[list[MutableDocument]]
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 | |
create_eval_pipeline
create_eval_pipeline()
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
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 | |
eval
eval(json_path: Union[str, list, None] = None, dataset=None, ignore_extra_labels=False, meta_requirements=None)
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | |
expand_model_with_concepts
expand_model_with_concepts(cui2preferred_name: dict[str, str], use_avg_init: bool = True) -> None
Expand the model with new concepts and their preferred names, which requires subsequent retraining on the model.
Parameters:
-
(cui2preferred_nameDict[str, str]) –Dictionary where each key is the literal ID of the concept to be added and each value is its preferred name.
-
(use_avg_initbool, default:True) –Whether to use the average of existing weights or biases as the initial value for the new concept. Defaults to True.
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 | |
get_hash
get_hash() -> str
A partial hash trying to catch differences between models.
Returns:
-
str(str) –The hex hash.
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
336 337 338 339 340 341 342 343 344 345 346 347 348 | |
pipe
pipe(stream: Iterable[Union[MutableDocument, None]], *args, **kwargs) -> Iterator[tuple[MutableDocument, list[MutableEntity]]]
Process many documents at once.
Parameters:
-
(streamIterable[MutableDocument]) –List of documents.
-
–*argsExtra arguments (not used here).
-
–**kwargsExtra keyword arguments (not used here).
Yields:
-
Doc(tuple[MutableDocument, list[MutableEntity]]) –The same document.
Returns:
-
Iterator[tuple[MutableDocument, list[MutableEntity]]]–Iterator[tuple[MutableDocument, list[MutableEntity]]]: The stream of documents and entities
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 | |
train
train(json_path: Union[str, list, None] = None, ignore_extra_labels=False, dataset=None, meta_requirements=None, train_json_path: Union[str, list, None] = None, test_json_path: Union[str, list, None] = None, trainer_callbacks: Optional[list[TrCBCreator]] = None) -> tuple
Train or continue training a model give a json_path containing a MedCATtrainer export. It will continue training if an existing model is loaded or start new training if the model is blank/new.
Parameters:
-
(json_pathstr or list, default:None) –Path/Paths to a MedCATtrainer export containing the meta_annotations we want to train for.
-
–ignore_extra_labelsMakes only sense when an existing deid model was loaded and from the new data we want to ignore labels that did not exist in the old model.
-
–datasetDefaults to None.
-
–meta_requirementsDefaults to None
-
(train_json_pathUnion[str, list, None], default:None) –The json path for the training data. Defaults to None.
-
(test_json_pathUnion[str, list, None], default:None) –The json path for the test data. Defaults to None.
-
(trainer_callbackslist[TrCBCreator], default:None) –A list of trainer callbacks for collecting metrics during the training at the client side. The transformers Trainer object will be passed in when each callback is called.
Returns:
-
Tuple(tuple) –The dataframe, examples, and the dataset
Source code in medcat-v2/medcat/components/ner/trf/transformers_ner.py
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 | |