Skip to content

add tensorboard support #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ optimizer:
train:
n_epoch: 10
verbose: true
tensorboard: true

save:
path_to_folder: 'models/test_main/'
Expand Down
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pytorch_ner/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm

from pytorch_ner.metrics import calculate_metrics
Expand Down Expand Up @@ -144,12 +145,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):

Expand Down Expand Up @@ -183,6 +187,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(
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ scipy==1.5.4
six==1.15.0
sklearn==0.0
smart-open==4.0.0
tensorboard==2.9.0
threadpoolctl==2.1.0
toml==0.10.2
torch==1.6.0
Expand Down
1 change: 1 addition & 0 deletions tests/test_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
device=device,
n_epoch=5,
verbose=False,
tensorboard=True,
)


Expand Down