From 09a76b82e181212b5db0ef0c5d938d9d723c7aec Mon Sep 17 00:00:00 2001 From: Abdallah Ahmed Date: Thu, 10 Dec 2020 15:57:13 +0200 Subject: [PATCH 01/10] Add support for tensorboard. This commit add support for tensorboard and edits the following: 1. config.yml: add boolean flag wether to save in tensorboard or not 2. main.py and train.py: main writers for tensorboard 3. requirments.txt: added the tensorboard to be installed with the requirments (no version specified yet!) --- config.yaml | 1 + main.py | 1 + pytorch_ner/train.py | 12 ++++++++++++ requirements.txt | 1 + 4 files changed, 15 insertions(+) diff --git a/config.yaml b/config.yaml index f770aa6..fa337cb 100644 --- a/config.yaml +++ b/config.yaml @@ -49,6 +49,7 @@ optimizer: train: n_epoch: 10 verbose: true + tensorboard: ture save: path_to_folder: 'models/test_main/' diff --git a/main.py b/main.py index 289d551..79b0674 100644 --- a/main.py +++ b/main.py @@ -202,6 +202,7 @@ def main(path_to_config: str): device=device, n_epoch=config["train"]["n_epoch"], verbose=config["train"]["verbose"], + tensorboard=config["train"]["tensorboard"] ) # SAVE MODEL diff --git a/pytorch_ner/train.py b/pytorch_ner/train.py index 60e1d52..6eed059 100644 --- a/pytorch_ner/train.py +++ b/pytorch_ner/train.py @@ -7,6 +7,7 @@ import torch.optim as optim from torch.utils.data import DataLoader from tqdm import tqdm +from torch.utils.tensorboard import SummaryWriter from pytorch_ner.metrics import calculate_metrics from pytorch_ner.utils import to_numpy @@ -53,6 +54,8 @@ def train_loop( labels.to(device), lengths.to(device), ) + tokens, lengths = tokens.long(), lengths.long() + labels = labels.long() mask = masking(lengths) @@ -107,6 +110,8 @@ def validate_loop( labels.to(device), lengths.to(device), ) + tokens, lengths = tokens.long(), lengths.long() + labels = labels.long() mask = masking(lengths) @@ -144,12 +149,15 @@ def train( optimizer: optim.Optimizer, device: torch.device, n_epoch: int, + tensorboard: bool, testloader: Optional[DataLoader] = None, verbose: bool = True, ): """ Training / validation loop for n_epoch with final testing. """ + # Writer will output to ./runs/ directory by default + writer = SummaryWriter() for epoch in range(n_epoch): @@ -183,6 +191,10 @@ def train( print(f"val {metric_name}: {np.mean(metric_list)}") print() + if tensorboard: + writer.add_scalar('Loss/train', np.mean((train_metrics['loss'])), epoch) + writer.add_scalar('Loss/val', np.mean((val_metrics['loss'])), epoch) + if testloader is not None: test_metrics = validate_loop( diff --git a/requirements.txt b/requirements.txt index b72a218..9942175 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,6 +24,7 @@ scipy==1.5.4 six==1.15.0 sklearn==0.0 smart-open==4.0.0 +tensorboard threadpoolctl==2.1.0 toml==0.10.2 torch==1.6.0 From 3ae99229533077170d06f620eb34d607cb19e047 Mon Sep 17 00:00:00 2001 From: Abdallah Ahmed Date: Thu, 10 Dec 2020 16:05:59 +0200 Subject: [PATCH 02/10] Add support for tensorboard. This commit add support for tensorboard and edits the following: 1. config.yml: add boolean flag wether to save in tensorboard or not 2. main.py and train.py: main writers for tensorboard 3. requirments.txt: added the tensorboard to be installed with the requirments (no version specified yet!) --- pytorch_ner/train.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pytorch_ner/train.py b/pytorch_ner/train.py index 6eed059..27ab0d1 100644 --- a/pytorch_ner/train.py +++ b/pytorch_ner/train.py @@ -54,8 +54,6 @@ def train_loop( labels.to(device), lengths.to(device), ) - tokens, lengths = tokens.long(), lengths.long() - labels = labels.long() mask = masking(lengths) @@ -110,8 +108,6 @@ def validate_loop( labels.to(device), lengths.to(device), ) - tokens, lengths = tokens.long(), lengths.long() - labels = labels.long() mask = masking(lengths) From 438ccef10a0f4f11cb715baba6afd4e56358b40d Mon Sep 17 00:00:00 2001 From: Abdallah Ahmed Date: Thu, 10 Dec 2020 16:26:39 +0200 Subject: [PATCH 03/10] Solves error while testing train. The added clip-grad feature required sending one more argument to the train function. This commits solves "TypeError: train() missing 1 required positional argument: 'clip_grad'" in test_train.py file --- tests/test_train.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_train.py b/tests/test_train.py index 4158166..f770369 100644 --- a/tests/test_train.py +++ b/tests/test_train.py @@ -74,6 +74,7 @@ criterion=criterion, optimizer=optimizer, device=device, + clip_grad=0.1, n_epoch=5, verbose=False, ) From 2a4f9238938c59e344fc6378730d6884263246bd Mon Sep 17 00:00:00 2001 From: Abdallah Ahmed Date: Thu, 10 Dec 2020 16:27:30 +0200 Subject: [PATCH 04/10] Solves Code Format Check with Black --- pytorch_ner/train.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytorch_ner/train.py b/pytorch_ner/train.py index 27ab0d1..bbbb61c 100644 --- a/pytorch_ner/train.py +++ b/pytorch_ner/train.py @@ -188,8 +188,8 @@ def train( print() if tensorboard: - writer.add_scalar('Loss/train', np.mean((train_metrics['loss'])), epoch) - writer.add_scalar('Loss/val', np.mean((val_metrics['loss'])), epoch) + writer.add_scalar("Loss/train", np.mean((train_metrics["loss"])), epoch) + writer.add_scalar("Loss/val", np.mean((val_metrics["loss"])), epoch) if testloader is not None: From 3375d6eb664ba9f9f3a0db360f3e7ba0745eae75 Mon Sep 17 00:00:00 2001 From: Abdallah Ahmed Date: Thu, 10 Dec 2020 16:36:05 +0200 Subject: [PATCH 05/10] Solve code format check with isort --- pytorch_ner/train.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_ner/train.py b/pytorch_ner/train.py index bbbb61c..ffd9208 100644 --- a/pytorch_ner/train.py +++ b/pytorch_ner/train.py @@ -6,8 +6,8 @@ import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader -from tqdm import tqdm from torch.utils.tensorboard import SummaryWriter +from tqdm import tqdm from pytorch_ner.metrics import calculate_metrics from pytorch_ner.utils import to_numpy From f738f447c95bb30a1f20cc55bbdc33191d721827 Mon Sep 17 00:00:00 2001 From: Abdallah Ahmed Date: Thu, 10 Dec 2020 16:41:07 +0200 Subject: [PATCH 06/10] Revert "Solves error while testing train." This reverts commit 438ccef10a0f4f11cb715baba6afd4e56358b40d. --- tests/test_train.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_train.py b/tests/test_train.py index f770369..4158166 100644 --- a/tests/test_train.py +++ b/tests/test_train.py @@ -74,7 +74,6 @@ criterion=criterion, optimizer=optimizer, device=device, - clip_grad=0.1, n_epoch=5, verbose=False, ) From ff9a05b55b72d0e27692fe3dff7d585171a45456 Mon Sep 17 00:00:00 2001 From: Abdallah Ahmed Date: Thu, 10 Dec 2020 16:49:57 +0200 Subject: [PATCH 07/10] Solves test_train.py missing argument --- main.py | 2 +- tests/test_train.py | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index 79b0674..2ef2727 100644 --- a/main.py +++ b/main.py @@ -202,7 +202,7 @@ def main(path_to_config: str): device=device, n_epoch=config["train"]["n_epoch"], verbose=config["train"]["verbose"], - tensorboard=config["train"]["tensorboard"] + tensorboard=config["train"]["tensorboard"], ) # SAVE MODEL diff --git a/tests/test_train.py b/tests/test_train.py index 4158166..53963dc 100644 --- a/tests/test_train.py +++ b/tests/test_train.py @@ -8,11 +8,8 @@ from torch.utils.data import DataLoader from pytorch_ner.dataset import NERCollator, NERDataset -from pytorch_ner.prepare_data import ( - get_label2idx, - get_token2idx, - prepare_conll_data_format, -) +from pytorch_ner.prepare_data import (get_label2idx, get_token2idx, + prepare_conll_data_format) from pytorch_ner.train import train, validate_loop from tests.test_nn_modules.test_architecture import model_bilstm as model @@ -76,6 +73,7 @@ device=device, n_epoch=5, verbose=False, + tensorboard=True, ) From 61fe52f8ca56a870a49d7b88585458791c8ded4a Mon Sep 17 00:00:00 2001 From: Abdallah Ahmed Date: Thu, 10 Dec 2020 16:53:42 +0200 Subject: [PATCH 08/10] Solves Code Format Check with Black --- tests/test_train.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_train.py b/tests/test_train.py index 53963dc..ee1dab2 100644 --- a/tests/test_train.py +++ b/tests/test_train.py @@ -8,8 +8,11 @@ from torch.utils.data import DataLoader from pytorch_ner.dataset import NERCollator, NERDataset -from pytorch_ner.prepare_data import (get_label2idx, get_token2idx, - prepare_conll_data_format) +from pytorch_ner.prepare_data import ( + get_label2idx, + get_token2idx, + prepare_conll_data_format, +) from pytorch_ner.train import train, validate_loop from tests.test_nn_modules.test_architecture import model_bilstm as model From ffc4e9d2491d61b887937fa6e2d6bd3c1a8c71eb Mon Sep 17 00:00:00 2001 From: Dani El-Ayyass Date: Tue, 19 Jul 2022 12:12:52 +0300 Subject: [PATCH 09/10] Update config.yaml --- config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.yaml b/config.yaml index fa337cb..d4f838b 100644 --- a/config.yaml +++ b/config.yaml @@ -49,7 +49,7 @@ optimizer: train: n_epoch: 10 verbose: true - tensorboard: ture + tensorboard: true save: path_to_folder: 'models/test_main/' From 77fb5b1c6b5032efc1f9422d732c9eb8fe52c545 Mon Sep 17 00:00:00 2001 From: Dani El-Ayyass Date: Tue, 19 Jul 2022 12:14:10 +0300 Subject: [PATCH 10/10] Update requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 9942175..5ca8bc0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,7 +24,7 @@ scipy==1.5.4 six==1.15.0 sklearn==0.0 smart-open==4.0.0 -tensorboard +tensorboard==2.9.0 threadpoolctl==2.1.0 toml==0.10.2 torch==1.6.0