File size: 26,820 Bytes
7934b29 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 98 99 100 101 102 103 104 105 106 107 108 109 110 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 156 157 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 221 222 223 224 225 226 227 228 229 230 231 232 233 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 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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 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 567 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 608 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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"BRANCH = 'r1.17.0'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "o_0K1lsW1dj9"
},
"outputs": [],
"source": [
"\"\"\"\n",
"You can run either this notebook locally (if you have all the dependencies and a GPU) or on Google Colab.\n",
"\n",
"Instructions for setting up Colab are as follows:\n",
"1. Open a new Python 3 notebook.\n",
"2. Import this notebook from GitHub (File -> Upload Notebook -> \"GITHUB\" tab -> copy/paste GitHub URL)\n",
"3. Connect to an instance with a GPU (Runtime -> Change runtime type -> select \"GPU\" for hardware accelerator)\n",
"4. Run this cell to set up dependencies.\n",
"\"\"\"\n",
"# If you're using Google Colab and not running locally, run this cell\n",
"\n",
"# install NeMo\n",
"\n",
"!python -m pip install git+https://github.com/NVIDIA/NeMo.git@$BRANCH#egg=nemo_toolkit[nlp]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
},
"scrolled": true
},
"outputs": [],
"source": [
"# If you're not using Colab, you might need to upgrade jupyter notebook to avoid the following error:\n",
"# 'ImportError: IProgress not found. Please update jupyter and ipywidgets.'\n",
"\n",
"! pip install ipywidgets\n",
"! jupyter nbextension enable --py widgetsnbextension\n",
"\n",
"# Please restart the kernel after running this cell"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "dzqD2WDFOIN-"
},
"outputs": [],
"source": [
"from nemo.collections import nlp as nemo_nlp\n",
"from nemo.utils.exp_manager import exp_manager\n",
"\n",
"import os\n",
"import wget\n",
"import torch\n",
"import pytorch_lightning as pl\n",
"from omegaconf import OmegaConf"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "daYw_Xll2ZR9"
},
"source": [
"In this tutorial, we are going to describe how to finetune BioMegatron - a [BERT](https://arxiv.org/abs/1810.04805)-like [Megatron-LM](https://arxiv.org/pdf/1909.08053.pdf) model pre-trained on large biomedical text corpus ([PubMed](https://pubmed.ncbi.nlm.nih.gov/) abstracts and full-text commercial use collection) - on [RE: Text mining chemical-protein interactions (CHEMPROT)](https://biocreative.bioinformatics.udel.edu/tasks/biocreative-vi/track-5/).\n",
"\n",
"The model size of Megatron-LM can be larger than BERT, up to multi-billion parameters, compared to 345 million parameters of BERT-large.\n",
"There are some alternatives of BioMegatron, most notably [BioBERT](https://arxiv.org/abs/1901.08746). Compared to BioBERT BioMegatron is larger by model size and pre-trained on larger text corpus.\n",
"\n",
"A more general tutorial of using BERT-based models, including Megatron-LM, for downstream natural language processing tasks can be found [here](https://github.com/NVIDIA/NeMo/blob/stable/tutorials/nlp/01_Pretrained_Language_Models_for_Downstream_Tasks.ipynb)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task Description\n",
"**Relation Extraction (RE)** can be regarded as a type of sentence classification.\n",
"\n",
"The task is to classify the relation of a [GENE] and [CHEMICAL] in a sentence, for example like the following:\n",
"```html\n",
"14967461.T1.T22\t<@CHEMICAL$> inhibitors currently under investigation include the small molecules <@GENE$> (Iressa, ZD1839) and erlotinib (Tarceva, OSI-774), as well as monoclonal antibodies such as cetuximab (IMC-225, Erbitux).\t<CPR:4>\n",
"14967461.T2.T22\t<@CHEMICAL$> inhibitors currently under investigation include the small molecules gefitinib (<@GENE$>, ZD1839) and erlotinib (Tarceva, OSI-774), as well as monoclonal antibodies such as cetuximab (IMC-225, Erbitux).\t<CPR:4>\n",
"```\n",
"to one of the following class:\n",
"\n",
"| Relation Class | Relations |\n",
"| ----------- | ----------- |\n",
"| CPR:3 | Upregulator and activator |\n",
"| CPR:4 | Downregulator and inhibitor |\n",
"| CPR:5 | Agonist |\n",
"| CPR:6 | Antagonist |\n",
"| CPR:9 | Substrate and product of |"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "ZnuziSwJ1yEB"
},
"source": [
"# Datasets\n",
"\n",
"Details of ChemProt Relation Extraction task and the original data can be found on the [BioCreative VI website](https://biocreative.bioinformatics.udel.edu/tasks/biocreative-vi/track-5/)\n",
"\n",
"ChemProt dataset pre-processed for easier consumption can be downloaded from [here](https://github.com/arwhirang/recursive_chemprot/blob/master/Demo/tree_LSTM/data/chemprot-data_treeLSTM.zip) or [here](https://github.com/ncbi-nlp/BLUE_Benchmark/releases/download/0.1/bert_data.zip)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "--wJ2891aIIE"
},
"outputs": [],
"source": [
"TASK = 'ChemProt'\n",
"DATA_DIR = os.path.join(os.getcwd(), 'DATA_DIR')\n",
"RE_DATA_DIR = os.path.join(DATA_DIR, 'RE')\n",
"WORK_DIR = os.path.join(os.getcwd(), 'WORK_DIR')\n",
"MODEL_CONFIG = 'text_classification_config.yaml'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"os.makedirs(DATA_DIR, exist_ok=True)\n",
"os.makedirs(os.path.join(DATA_DIR, 'RE'), exist_ok=True)\n",
"os.makedirs(WORK_DIR, exist_ok=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# download the dataset\n",
"wget.download('https://github.com/arwhirang/recursive_chemprot/blob/master/Demo/tree_LSTM/data/chemprot-data_treeLSTM.zip?raw=true',\n",
" os.path.join(DATA_DIR, 'data_re.zip'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!unzip -o {DATA_DIR}/data_re.zip -d {RE_DATA_DIR}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "qB0oLE4R9EhJ"
},
"outputs": [],
"source": [
"! ls -l $RE_DATA_DIR"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre-process dataset\n",
"Let's convert the dataset into the format that is compatible for [NeMo text-classification module](https://github.com/NVIDIA/NeMo/blob/stable/examples/nlp/text_classification/text_classification_with_bert.py)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"wget.download(f'https://raw.githubusercontent.com/NVIDIA/NeMo/{BRANCH}/examples/nlp/text_classification/data/import_datasets.py')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"! python import_datasets.py --dataset_name=chemprot --source_data_dir={RE_DATA_DIR} --target_data_dir={RE_DATA_DIR}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# let's take a look at the training data \n",
"! head -n 5 {RE_DATA_DIR}/train.tsv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# let's check the label mapping\n",
"! cat {RE_DATA_DIR}/label_mapping.tsv"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is not necessary to have the mapping exactly like this - it can be different.\n",
"We use the same [mapping used by BioBERT](https://github.com/dmis-lab/biobert/blob/master/run_re.py#L438) so that comparison can be more straightforward."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "_whKCxfTMo6Y"
},
"source": [
"# Model configuration\n",
"\n",
"Now, let's take a closer look at the model's configuration and learn to train the model.\n",
"\n",
"The model is defined in a config file which declares multiple important sections. They are:\n",
"- **model**: All arguments that are related to the Model - language model, a classifier, optimizer and schedulers, datasets and any other related information\n",
"\n",
"- **trainer**: Any argument to be passed to PyTorch Lightning"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "T1gA8PsJ13MJ"
},
"outputs": [],
"source": [
"# download the model's configuration file \n",
"MODEL_CONFIG = 'text_classification_config.yaml'\n",
"config_dir = WORK_DIR + '/configs/'\n",
"os.makedirs(config_dir, exist_ok=True)\n",
"if not os.path.exists(config_dir + MODEL_CONFIG):\n",
" print('Downloading config file...')\n",
" wget.download(f'https://raw.githubusercontent.com/NVIDIA/NeMo/{BRANCH}/examples/nlp/text_classification/conf/' + MODEL_CONFIG, config_dir)\n",
"else:\n",
" print ('config file is already exists')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "mX3KmWMvSUQw"
},
"outputs": [],
"source": [
"# this line will print the entire config of the model\n",
"config_path = f'{WORK_DIR}/configs/{MODEL_CONFIG}'\n",
"print(config_path)\n",
"config = OmegaConf.load(config_path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"config.model.train_ds.file_path = os.path.join(RE_DATA_DIR, 'train.tsv')\n",
"config.model.validation_ds.file_path = os.path.join(RE_DATA_DIR, 'dev.tsv')\n",
"config.model.task_name = 'chemprot'\n",
"# Note: these are small batch-sizes - increase as appropriate to available GPU capacity\n",
"config.model.train_ds.batch_size=8\n",
"config.model.validation_ds.batch_size=8\n",
"config.model.dataset.num_classes=6"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(OmegaConf.to_yaml(config))"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "ZCgWzNBkaQLZ"
},
"source": [
"# Model Training\n",
"## Setting up Data within the config\n",
"\n",
"Among other things, the config file contains dictionaries called **dataset**, **train_ds** and **validation_ds**. These are configurations used to setup the Dataset and DataLoaders of the corresponding config.\n",
"\n",
"We assume that both training and evaluation files are located in the same directory, and use the default names mentioned during the data download step. \n",
"So, to start model training, we simply need to specify `model.dataset.data_dir`, like we are going to do below.\n",
"\n",
"Also notice that some config lines, including `model.dataset.data_dir`, have `???` in place of paths, this means that values for these fields are required to be specified by the user.\n",
"\n",
"Let's now add the data directory path, task name and output directory for saving predictions to the config."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "LQHCJN-ZaoLp"
},
"outputs": [],
"source": [
"config.model.task_name = TASK\n",
"config.model.output_dir = WORK_DIR\n",
"config.model.dataset.data_dir = RE_DATA_DIR"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "nB96-3sTc3yk"
},
"source": [
"## Building the PyTorch Lightning Trainer\n",
"\n",
"NeMo models are primarily PyTorch Lightning modules - and therefore are entirely compatible with the PyTorch Lightning ecosystem.\n",
"\n",
"Let's first instantiate a Trainer object"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "1tG4FzZ4Ui60"
},
"outputs": [],
"source": [
"print(\"Trainer config - \\n\")\n",
"print(OmegaConf.to_yaml(config.trainer))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "knF6QeQQdMrH"
},
"outputs": [],
"source": [
"# lets modify some trainer configs\n",
"# checks if we have GPU available and uses it\n",
"accelerator = 'gpu' if torch.cuda.is_available() else 'cpu'\n",
"config.trainer.devices = 1\n",
"config.trainer.accelerator = accelerator\n",
"\n",
"# for PyTorch Native AMP set precision=16\n",
"config.trainer.precision = 16 if torch.cuda.is_available() else 32\n",
"\n",
"# remove distributed training flags\n",
"config.trainer.strategy = None\n",
"\n",
"trainer = pl.Trainer(**config.trainer)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "8IlEMdVxdr6p"
},
"source": [
"## Setting up a NeMo Experiment\n",
"\n",
"NeMo has an experiment manager that handles logging and checkpointing for us, so let's use it:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "8uztqGAmdrYt"
},
"outputs": [],
"source": [
"config.exp_manager.exp_dir = WORK_DIR\n",
"exp_dir = exp_manager(trainer, config.get(\"exp_manager\", None))\n",
"\n",
"# the exp_dir provides a path to the current experiment for easy access\n",
"exp_dir = str(exp_dir)\n",
"exp_dir"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "8tjLhUvL_o7_"
},
"source": [
"To load the pretrained BERT LM model, we can get the list of names by following command "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from nemo.collections.nlp.models.language_modeling.megatron_bert_model import MegatronBertModel\n",
"print([model.pretrained_model_name for model in MegatronBertModel.list_available_models()])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can change the `model.language_mode` config to use it\n",
"```python\n",
"# add the specified above model parameters to the config\n",
"config.model.language_model.pretrained_model_name = MODEL_NAME\n",
"```\n",
"\n",
"In this notebook, we will use 'biomegatron345m_biovocab_30k_cased', which is BioMegatron, [Megatron-LM BERT](https://arxiv.org/abs/1909.08053) pre-trained on [PubMed](https://pubmed.ncbi.nlm.nih.gov/) biomedical text corpus."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "RK2xglXyAUOO"
},
"outputs": [],
"source": [
"# add the specified above model parameters to the config\n",
"# config.model.language_model.pretrained_model_name = PRETRAINED_BERT_MODEL\n",
"config.model.language_model.lm_checkpoint = None\n",
"config.model.language_model.pretrained_model_name = 'biomegatron345m_biovocab_30k_cased'\n",
"config.model.tokenizer.tokenizer_name=None"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "fzNZNAVRjDD-"
},
"source": [
"Now, we are ready to initialize our model. During the model initialization call, the dataset and data loaders we'll be prepared for training and evaluation.\n",
"Also, the pretrained BERT model will be downloaded, note it can take up to a few minutes depending on the size of the chosen BERT model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "NgsGLydWo-6-"
},
"outputs": [],
"source": [
"model = nemo_nlp.models.TextClassificationModel(cfg=config.model, trainer=trainer)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "kQ592Tx4pzyB"
},
"source": [
"## Monitoring training progress\n",
"Optionally, you can create a Tensorboard visualization to monitor training progress.\n",
"If you're not using Colab, refer to [https://www.tensorflow.org/tensorboard/tensorboard_in_notebooks](https://www.tensorflow.org/tensorboard/tensorboard_in_notebooks) if you're facing issues with running the cell below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "mTJr16_pp0aS"
},
"outputs": [],
"source": [
"try:\n",
" from google import colab\n",
" COLAB_ENV = True\n",
"except (ImportError, ModuleNotFoundError):\n",
" COLAB_ENV = False\n",
"\n",
"# Load the TensorBoard notebook extension\n",
"if COLAB_ENV:\n",
" %load_ext tensorboard\n",
" %tensorboard --logdir {exp_dir}\n",
"else:\n",
" print(\"To use tensorboard, please use this notebook in a Google Colab environment.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "hUvnSpyjp0Dh"
},
"outputs": [],
"source": [
"# start model training\n",
"trainer.fit(model)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "ref1qSonGNhP"
},
"source": [
"## Training Script\n",
"\n",
"If you have NeMo installed locally, you can also train the model with `examples/nlp/text_classification/text_classification_with_bert.py.`\n",
"\n",
"To run training script, use:\n",
"\n",
"```bash\n",
"python text_classification_with_bert.py \\\n",
" model.dataset.data_dir=PATH_TO_DATA_DIR \\\n",
" model.task_name=TASK \\\n",
" exp_manager.exp_dir=EXP_DIR \\\n",
" model.language_model.pretrained_model_name=biomegatron345m_biovocab_30k_cased \\\n",
" model.tokenizer.library=megatron \n",
" ```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The training could take several minutes and the results should look something like:\n",
"\n",
"```\n",
"precision recall f1-score support\n",
" \n",
" 0 0.7328 0.8348 0.7805 115\n",
" 1 0.9402 0.9291 0.9346 7950\n",
" 2 0.8311 0.9146 0.8708 199\n",
" 3 0.6400 0.6302 0.6351 457\n",
" 4 0.8002 0.8317 0.8156 1093\n",
" 5 0.7228 0.7518 0.7370 548\n",
" \n",
" accuracy 0.8949 10362\n",
" macro avg 0.7778 0.8153 0.7956 10362\n",
" weighted avg 0.8963 0.8949 0.8954 10362\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "Relation_Extraction-BioMegatron.ipynb",
"private_outputs": true,
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"metadata": {
"collapsed": false
},
"source": []
}
}
},
"nbformat": 4,
"nbformat_minor": 1
} |