Skip to content

feat(transformer-ner) Added class weighting for loss calculation and exposing training params. - #647

Open
adam-sutton-1992 wants to merge 18 commits into
mainfrom
transformer_ner_class_weights_training
Open

adam-sutton-1992 wants to merge 18 commits into
mainfrom
transformer_ner_class_weights_training

Conversation

@adam-sutton-1992

Copy link
Copy Markdown
Contributor

Hihi,

Adding class weighting exposing some training params. Previously the loss was calculated via the CRF outputs which predicted output tokens. This improved performance over baseline loss by enforcing transitions between tokens to make sense (i.e. B->I or E prediction tokens only, O -> B or S prediction tokens, etc...). I tested class weights for each predicted class with the new stats module and making these changes results in performance increases. Here they are:

  1. config.class_weights - A dict of how to weigh each label class towards the calculated loss. Generally speaking in this task making O lower increases recall at the cost of precision. Increasing B and S also encourages detecting the start of concepts more liberally. Defaults to a uniform weighting.
  2. config.crf_loss_weight - Weigh the importance of the loss function that was previously the standard. This is crf outputs. Effectively you can increase/decrease the importance of the output being consistent.
  3. config.learning_rate - The learning rate was set to 1e-5, without it being exposed to be edited nicely. This fixes that.
  4. config.weight_decay - same as above.

This also comes with methods in transformer_ner which exposes these changes and nicely saves / loads the parameters.

The changes to class weights and loss weight can significantly improve performance on the NER Only task. Going from a best Recall of 0.9 to 0.96, where the vocab based nerrule based method with training can achieve about 9.75. This also has a higher precision compare to the vocab based ner method. Reaching about 0.4.

@mart-r

mart-r commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

Please resync with main branch. Hard to review if this includes changes that are (or may be) already merged. For instance, I can see that changes from #639 are presented in this PR as well.

tomolopolis and others added 8 commits October 28, 2026 12:11
…634)

* feat(medcat-trainer): Add text and name contains filters to documents API

Co-authored-by: tomolopolis <[email protected]>

* feat(medcat-trainer): Search clinical notes by text span in annotate view

Co-authored-by: tomolopolis <[email protected]>

* feat(medcat-trainer): Index dataset documents into Solr on upload and add Solr backed document search endpoint

Co-authored-by: tomolopolis <[email protected]>

* feat(medcat-trainer): Use Solr document search for clinical notes text search

Co-authored-by: tomolopolis <[email protected]>

* docs(medcat-trainer): Document Solr document search settings

Co-authored-by: tomolopolis <[email protected]>

* feat(medcat-trainer): Put clinical notes summary in a resizable split pane

Co-authored-by: tomolopolis <[email protected]>

* feat(medcat-trainer): improved styles and UX of doc search function

---------

Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: tomolopolis <[email protected]>
* fix(medcat-trainer): open FP/FN/TP example dialogs reliably

Clicking a concept count failed when the metrics table did not hand back
the original row, so the example dialog never opened. Current MedCAT stats
examples also use snake_case fields, which left the dialog unable to show
or link those occurrences, and unescaped clinical text could break rendering.

Resolve examples without object identity, accept both key styles, escape
note text, and mount the dialog on document.body.

Co-authored-by: tomolopolis <[email protected]>

* fix(medcat-trainer): restore TP, FN, and FP count colors

type=button matched Vuetify's color:inherit rule and overrode the outline
button colors. Keep true positives green, false negatives grey, and false
positives red.

Co-authored-by: tomolopolis <[email protected]>

* fix(medcat-trainer): paint non-zero TP, FN, and FP counts

Bootstrap outline colors were still lost once later button rules reset
--bs-btn-color. Set the NHS green, grey, and red on the count buttons
themselves so a non-zero number keeps its colour.

Co-authored-by: tomolopolis <[email protected]>

* fix(medcat-trainer): use theme variables for count colours

Non-zero TP, FN, and FP counts take --success, --warning, and --danger
from the NHS theme map instead of hardcoded hex values.

Co-authored-by: tomolopolis <[email protected]>

* fix(medcat-trainer): fix styling of metrics

* fix(medcat-trainer): fix test, fix clickable mention in span in modal appearing alongside the context to the right of the mention

---------

Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: tomolopolis <[email protected]>
* feat(medcat-trainer): add project group changes in mct-client

* chore(medcat-trainer): update to latest release
Bumps the actions-deps group with 1 update: [pypa/gh-action-pypi-publish](https://github.com/pypa/gh-action-pypi-publish).


Updates `pypa/gh-action-pypi-publish` from 1.14.2 to 1.14.3
- [Release notes](https://github.com/pypa/gh-action-pypi-publish/releases)
- [Commits](pypa/gh-action-pypi-publish@v1.14.2...v1.14.3)

---
updated-dependencies:
- dependency-name: pypa/gh-action-pypi-publish
  dependency-version: 1.14.3
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: actions-deps
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants