The primary purpose of this study is to develop a fast and reliable LLM-based tool for the automatic diagnosis coding of Colorado State University (CSU) veterinary medical summaries to 7,739 SNOMED-CT codes. To achieve this, a variety of human and veterinary clinical, and non-clinical foundational LLMs were fine-tuned on a labeled training dataset curated by expert manual clinical coders at the CSU Veterinary Teaching Hospital. This study was largely inspired by VetTag (2019), an automatic veterinary diagnosis coding tool developed by researchers at Stanford University using a subset of the CSU labeled training dataset. It was of interest to improve upon the original model by implementing better training practices, considering a wider variety of models, and employing the model for more practically significant tasks.
Veterinary medical records represent a large data resource for application to veterinary and One Health clinical research efforts. Use of the data is limited by interoperability challenges including inconsistent data formats and data siloing. Clinical coding using standardized medical terminologies enhances the quality of medical records and facilitates their interoperability with veterinary and human health records from other sites. Previous studies, such as DeepTag and VetTag, have evaluated the application of Natural Language Processing (NLP) to automate veterinary diagnosis coding, employing long short-term memory (LSTM) and transformer models to infer a subset of Systemized Nomenclature of Medicine - Clinical Terms (SNOMED-CT) diagnosis codes from free-text clinical notes. This study expands on these efforts by incorporating all 7,739 distinct SNOMED-CT diagnosis codes recognized by the Colorado State University (CSU) Veterinary Teaching Hospital (VTH) and by leveraging the increasing availability of pre-trained large language models (LLMs). Ten freely-available pre-trained LLMs (GatorTron, MedicalAI ClinicalBERT, medAlpaca, VetBERT, PetBERT, BERT, BERT Large, RoBERTa, GPT-2, and GPT-2 XL) were fine-tuned on the free-text notes from 246,473 manually-coded veterinary patient visits included in the CSU VTH's electronic health records (EHR), which resulted in superior performance relative to previous efforts. The most accurate results were obtained when expansive labeled data were used to fine-tune relatively large clinical LLMs, but the study also showed that comparable results can be obtained using more limited resources and non-clinical LLMs. The results of this study contribute to the improvement of the quality of veterinary EHR by investigating accessible methods for automated coding and support both animal and human health research by paving the way for more integrated and comprehensive health databases that span species and institutions.
Existing state-of-the-art human and veterinary clinical LLMs and general knowledge LLMs were leveraged by fine-tuning them for veterinary clinical coding to 7,739 SNOMED-CT diagnoses. All models were fine-tuned under a consistent training framework.
Loss Function: Binary Cross-Entropy with Logits
Optimizer: AdamW
Batch Size: 32
Initial Learning Rate: $5*10^{-8}$
Pleateau Learning Rate: $3*10^{-5}$
Warmup Steps: 5,000
Early Stopping: 5 epoch patience based on validation loss
Maximum Epochs: 50
Several Python scripts are involved in the fine-tuning pipeline for this study:
preprocess.py: Handles the cleaning of raw clinical text, data preprocessing, and tokenization. Raw text in the form a .csv file is inputted and a set of .pt PyTorch tensors are outputted. Data are split into training/valiation/testing partitions which are saved separately.
models.py: Defines custom PyTorch model classes for each foundational model. The raw model weights are downloaded from HuggingFace, portions of each model are set to allow for fine-tuning, and additional custom layers are appended to each model (e.g., classifier layers).
trainer.py: Executes a fine-tuning procedure using previously preprocessed data and defined model classes. A fine-tuning procedure is initialized by executing a slurm script named as {model name}_(server name).slurm. The configuration of these slurm scripts is custom to CSU's computing resources.
evaluate.py: Performs auxiliary plotting tasks to evaluate model performance after fine-tuning is complete. The functions in this script are called by the fine-tuning code.
| Model | Pre-Training | Parameters | Transformer Blocks | Self-Attention Heads | Embedding Dimension |
|---|---|---|---|---|---|
| GatorTron (2022) | Univ. of Florida (2.9M notes), MIMIC-III, PubMed, WikiText (91B total words) | 3.9B | 48 | 40 | 2,560 |
| MedicalAI ClinicalBERT (2023) | Zhongshan Hospital, Qingpu Hospital (1.2B total words) | 135M | 6 | 8 | 768 |
| medAlpaca (2023) | Anki flashcards, Stack Exchange, Wikidoc, other Q&A | 6.6B | 32 | 32 | 4,096 |
| VetBERT (2020) | VetCompass (15M total notes, 1.3B total tokens) | 108M | 12 | 12 | 768 |
| PetBERT (2023) | UK Vet EHRs (5.1M total notes, 500M total words) | 108M | 12 | 12 | 768 |
| BERT (2018) | BooksCorpus (800M words), Wikipedia (2.5B words) | 108M | 12 | 12 | 768 |
| BERT Large (2018) | Same as BERT | 335M | 24 | 16 | 1,024 |
| RoBERTa (2019) | Same as BERT with CC-News (63M articles), OpenWebText, Stories | 125M | 12 | 12 | 768 |
| GPT-2 (2019) | WebText (8M documents) | 124M | 12 | 12 | 768 |
| GPT-2 XL (2019) | Same as GPT-2 | 1.6B | 48 | 25 | 1,600 |
| DeBERTa V3 (2021) | Wikipedia, BookCorpus, OpenWebText, Stories | 184M | 12 | 12 | 768 |
| ModernBERT (2024) | Web documents, code, scientific literature (2T tokens) | 149M | 22 | 12 | 768 |
| Clinical ModernBERT (2025) | MIMIC-IV, PubMed, Clinical Codes (e.g., ICD) (13B tokens) | 136M | 22 | 12 | 768 |
| Model | F1 | Precision | Recall | EM | Fine-Tuning Time |
|---|---|---|---|---|---|
| GatorTron | 76.9±0.14 | 81.5±1.74 | 74.4±1.14 | 52.2±0.66 | 23.7±0.14 hrs |
| MedicalAI ClinicalBERT | 69.5±1.50 | 79.2±1.15 | 64.4±1.49 | 45.2±1.79 | 1.6±0.25 hrs |
| medAlpaca | 74.0±0.14 | 81.3±0.72 | 70.4±0.50 | 48.3±0.38 | 18.5±2.94 hrs |
| VetBERT | 72.8±0.50 | 79.8±0.50 | 68.9±0.76 | 48.4±0.14 | 3.2±1.23 hrs |
| PetBERT | 71.6±2.58 | 79.4±0.76 | 67.4±4.31 | 47.6±2.35 | 3.2±1.03 hrs |
| BERT | 72.2±1.55 | 79.9±0.90 | 67.9±2.23 | 47.7±1.25 | 3.0±0.63 hrs |
| BERT Large | 70.3±5.76 | 78.2±2.41 | 66.0±7.16 | 45.9±4.86 | 8.1±5.22 hrs |
| RoBERTa | 71.5±0.50 | 79.2±1.08 | 67.3±1.27 | 47.0±0.38 | 3.7±1.74 hrs |
| GPT-2 Small | 70.6±1.24 | 79.6±0.29 | 65.8±1.79 | 45.7±1.38 | 4.3±0.38 hrs |
| GPT-2 XL | 73.7±3.31 | 80.5±1.97 | 70.1±3.71 | 48.4±3.37 | 18.5±6.35 hrs |
| DeBERTa V3 | 68.2±3.31 | 77.3±3.58 | 63.7±2.88 | 44.9±1.86 | 7.9±1.17 hrs |
| ModernBERT | 64.1±4.34 | 75.7±1.14 | 58.8±5.65 | 40.4±4.26 | 5.0±0.14 hrs |
| Clinical ModernBERT | 70.8±0.63 | 79.4±0.38 | 66.4±0.86 | 46.8±0.87 | 6.9±0.43 hrs |
The data used to fine-tune foundational models for this study are propriety medical records that are the property of Colorado State University; they cannot be shared publicly. Once proper approvals are obtained, we hope to be able to share fine-tuned model weights publicly under the control of a data usage agreement.
@software{CSU_NLPEHR_2024,
author = {Mayla Boguslav and Adam Kiehl and David Kott},
title = {Fine-Tuning Foundational Models To Code Diagnoses From Veterinary Health Records},
url = {https://github.com/adam-kiehl/DiagnosisCoding},
date = {2025-06-17},
note = {GitHub repository},
organization = {Colorado State University}
}
@misc{boguslav2024finetuningfoundationalmodelscode,
title={Fine-tuning foundational models to code diagnoses from veterinary health records},
author={Mayla R. Boguslav and Adam Kiehl and David Kott and G. Joseph Strecker and Tracy Webb and Nadia Saklou and Terri Ward and Michael Kirby},
year={2024},
eprint={2410.15186},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2410.15186},
}
13 commits
7 commits
Python
87.9%
Shell
12.1%
The primary purpose of this study is to develop a fast and reliable LLM-based tool for the automatic diagnosis coding of Colorado State University (CSU) veterinary medical summaries to 7,739 SNOMED-CT codes. To achieve this, a variety of human and veterinary clinical, and non-clinical foundational LLMs were fine-tuned on a labeled training dataset curated by expert manual clinical coders at the CSU Veterinary Teaching Hospital. This study was largely inspired by VetTag (2019), an automatic veterinary diagnosis coding tool developed by researchers at Stanford University using a subset of the CSU labeled training dataset. It was of interest to improve upon the original model by implementing better training practices, considering a wider variety of models, and employing the model for more practically significant tasks.
Veterinary medical records represent a large data resource for application to veterinary and One Health clinical research efforts. Use of the data is limited by interoperability challenges including inconsistent data formats and data siloing. Clinical coding using standardized medical terminologies enhances the quality of medical records and facilitates their interoperability with veterinary and human health records from other sites. Previous studies, such as DeepTag and VetTag, have evaluated the application of Natural Language Processing (NLP) to automate veterinary diagnosis coding, employing long short-term memory (LSTM) and transformer models to infer a subset of Systemized Nomenclature of Medicine - Clinical Terms (SNOMED-CT) diagnosis codes from free-text clinical notes. This study expands on these efforts by incorporating all 7,739 distinct SNOMED-CT diagnosis codes recognized by the Colorado State University (CSU) Veterinary Teaching Hospital (VTH) and by leveraging the increasing availability of pre-trained large language models (LLMs). Ten freely-available pre-trained LLMs (GatorTron, MedicalAI ClinicalBERT, medAlpaca, VetBERT, PetBERT, BERT, BERT Large, RoBERTa, GPT-2, and GPT-2 XL) were fine-tuned on the free-text notes from 246,473 manually-coded veterinary patient visits included in the CSU VTH's electronic health records (EHR), which resulted in superior performance relative to previous efforts. The most accurate results were obtained when expansive labeled data were used to fine-tune relatively large clinical LLMs, but the study also showed that comparable results can be obtained using more limited resources and non-clinical LLMs. The results of this study contribute to the improvement of the quality of veterinary EHR by investigating accessible methods for automated coding and support both animal and human health research by paving the way for more integrated and comprehensive health databases that span species and institutions.
Existing state-of-the-art human and veterinary clinical LLMs and general knowledge LLMs were leveraged by fine-tuning them for veterinary clinical coding to 7,739 SNOMED-CT diagnoses. All models were fine-tuned under a consistent training framework.
Loss Function: Binary Cross-Entropy with Logits
Optimizer: AdamW
Batch Size: 32
Initial Learning Rate: $5*10^{-8}$
Pleateau Learning Rate: $3*10^{-5}$
Warmup Steps: 5,000
Early Stopping: 5 epoch patience based on validation loss
Maximum Epochs: 50
Several Python scripts are involved in the fine-tuning pipeline for this study:
preprocess.py: Handles the cleaning of raw clinical text, data preprocessing, and tokenization. Raw text in the form a .csv file is inputted and a set of .pt PyTorch tensors are outputted. Data are split into training/valiation/testing partitions which are saved separately.
models.py: Defines custom PyTorch model classes for each foundational model. The raw model weights are downloaded from HuggingFace, portions of each model are set to allow for fine-tuning, and additional custom layers are appended to each model (e.g., classifier layers).
trainer.py: Executes a fine-tuning procedure using previously preprocessed data and defined model classes. A fine-tuning procedure is initialized by executing a slurm script named as {model name}_(server name).slurm. The configuration of these slurm scripts is custom to CSU's computing resources.
evaluate.py: Performs auxiliary plotting tasks to evaluate model performance after fine-tuning is complete. The functions in this script are called by the fine-tuning code.
| Model | Pre-Training | Parameters | Transformer Blocks | Self-Attention Heads | Embedding Dimension |
|---|---|---|---|---|---|
| GatorTron (2022) | Univ. of Florida (2.9M notes), MIMIC-III, PubMed, WikiText (91B total words) | 3.9B | 48 | 40 | 2,560 |
| MedicalAI ClinicalBERT (2023) | Zhongshan Hospital, Qingpu Hospital (1.2B total words) | 135M | 6 | 8 | 768 |
| medAlpaca (2023) | Anki flashcards, Stack Exchange, Wikidoc, other Q&A | 6.6B | 32 | 32 | 4,096 |
| VetBERT (2020) | VetCompass (15M total notes, 1.3B total tokens) | 108M | 12 | 12 | 768 |
| PetBERT (2023) | UK Vet EHRs (5.1M total notes, 500M total words) | 108M | 12 | 12 | 768 |
| BERT (2018) | BooksCorpus (800M words), Wikipedia (2.5B words) | 108M | 12 | 12 | 768 |
| BERT Large (2018) | Same as BERT | 335M | 24 | 16 | 1,024 |
| RoBERTa (2019) | Same as BERT with CC-News (63M articles), OpenWebText, Stories | 125M | 12 | 12 | 768 |
| GPT-2 (2019) | WebText (8M documents) | 124M | 12 | 12 | 768 |
| GPT-2 XL (2019) | Same as GPT-2 | 1.6B | 48 | 25 | 1,600 |
| DeBERTa V3 (2021) | Wikipedia, BookCorpus, OpenWebText, Stories | 184M | 12 | 12 | 768 |
| ModernBERT (2024) | Web documents, code, scientific literature (2T tokens) | 149M | 22 | 12 | 768 |
| Clinical ModernBERT (2025) | MIMIC-IV, PubMed, Clinical Codes (e.g., ICD) (13B tokens) | 136M | 22 | 12 | 768 |
| Model | F1 | Precision | Recall | EM | Fine-Tuning Time |
|---|---|---|---|---|---|
| GatorTron | 76.9±0.14 | 81.5±1.74 | 74.4±1.14 | 52.2±0.66 | 23.7±0.14 hrs |
| MedicalAI ClinicalBERT | 69.5±1.50 | 79.2±1.15 | 64.4±1.49 | 45.2±1.79 | 1.6±0.25 hrs |
| medAlpaca | 74.0±0.14 | 81.3±0.72 | 70.4±0.50 | 48.3±0.38 | 18.5±2.94 hrs |
| VetBERT | 72.8±0.50 | 79.8±0.50 | 68.9±0.76 | 48.4±0.14 | 3.2±1.23 hrs |
| PetBERT | 71.6±2.58 | 79.4±0.76 | 67.4±4.31 | 47.6±2.35 | 3.2±1.03 hrs |
| BERT | 72.2±1.55 | 79.9±0.90 | 67.9±2.23 | 47.7±1.25 | 3.0±0.63 hrs |
| BERT Large | 70.3±5.76 | 78.2±2.41 | 66.0±7.16 | 45.9±4.86 | 8.1±5.22 hrs |
| RoBERTa | 71.5±0.50 | 79.2±1.08 | 67.3±1.27 | 47.0±0.38 | 3.7±1.74 hrs |
| GPT-2 Small | 70.6±1.24 | 79.6±0.29 | 65.8±1.79 | 45.7±1.38 | 4.3±0.38 hrs |
| GPT-2 XL | 73.7±3.31 | 80.5±1.97 | 70.1±3.71 | 48.4±3.37 | 18.5±6.35 hrs |
| DeBERTa V3 | 68.2±3.31 | 77.3±3.58 | 63.7±2.88 | 44.9±1.86 | 7.9±1.17 hrs |
| ModernBERT | 64.1±4.34 | 75.7±1.14 | 58.8±5.65 | 40.4±4.26 | 5.0±0.14 hrs |
| Clinical ModernBERT | 70.8±0.63 | 79.4±0.38 | 66.4±0.86 | 46.8±0.87 | 6.9±0.43 hrs |
The data used to fine-tune foundational models for this study are propriety medical records that are the property of Colorado State University; they cannot be shared publicly. Once proper approvals are obtained, we hope to be able to share fine-tuned model weights publicly under the control of a data usage agreement.
@software{CSU_NLPEHR_2024,
author = {Mayla Boguslav and Adam Kiehl and David Kott},
title = {Fine-Tuning Foundational Models To Code Diagnoses From Veterinary Health Records},
url = {https://github.com/adam-kiehl/DiagnosisCoding},
date = {2025-06-17},
note = {GitHub repository},
organization = {Colorado State University}
}
@misc{boguslav2024finetuningfoundationalmodelscode,
title={Fine-tuning foundational models to code diagnoses from veterinary health records},
author={Mayla R. Boguslav and Adam Kiehl and David Kott and G. Joseph Strecker and Tracy Webb and Nadia Saklou and Terri Ward and Michael Kirby},
year={2024},
eprint={2410.15186},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2410.15186},
}
13 commits
7 commits
Python
87.9%
Shell
12.1%