Skip to content

Commit

Permalink
feat: add the argument verbose to control logging;
Browse files Browse the repository at this point in the history
  • Loading branch information
WenjieDu committed Jun 1, 2024
1 parent 4077383 commit 7b6fbc7
Show file tree
Hide file tree
Showing 42 changed files with 183 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pypots/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from torch.utils.tensorboard import SummaryWriter

from .utils.file import create_dir_if_not_exist
from .utils.logging import logger
from .utils.logging import logger, logger_creator


class BaseModel(ABC):
Expand Down Expand Up @@ -43,6 +43,9 @@ class BaseModel(ABC):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
Attributes
----------
model : object, default = None
Expand All @@ -64,6 +67,7 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
saving_strategies = [None, "best", "better", "all"]
assert (
Expand All @@ -73,6 +77,10 @@ def __init__(
self.device = None # set up with _setup_device() below
self.saving_path = None # set up with _setup_path() below
self.model_saving_strategy = model_saving_strategy
self.verbose = verbose

if not self.verbose:
logger_creator.set_level("warning")

self.model = None
self.summary_writer = None
Expand Down Expand Up @@ -442,6 +450,8 @@ class BaseNNModel(BaseModel):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
Attributes
---------
Expand Down Expand Up @@ -475,11 +485,13 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
device,
saving_path,
model_saving_strategy,
verbose,
)

if patience is None:
Expand Down
8 changes: 8 additions & 0 deletions pypots/classification/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class BaseClassifier(BaseModel):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
"""

def __init__(
Expand All @@ -59,11 +61,13 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
device,
saving_path,
model_saving_strategy,
verbose,
)
self.n_classes = n_classes

Expand Down Expand Up @@ -179,6 +183,8 @@ class BaseNNClassifier(BaseNNModel):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
Notes
-----
Expand All @@ -200,6 +206,7 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
batch_size,
Expand All @@ -209,6 +216,7 @@ def __init__(
device,
saving_path,
model_saving_strategy,
verbose,
)
self.n_classes = n_classes

Expand Down
4 changes: 4 additions & 0 deletions pypots/classification/brits/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class BRITS(BaseNNClassifier):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
"""

def __init__(
Expand All @@ -99,6 +101,7 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
n_classes,
Expand All @@ -109,6 +112,7 @@ def __init__(
device,
saving_path,
model_saving_strategy,
verbose,
)

self.n_steps = n_steps
Expand Down
4 changes: 4 additions & 0 deletions pypots/classification/grud/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class GRUD(BaseNNClassifier):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
"""

def __init__(
Expand All @@ -92,6 +94,7 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
n_classes,
Expand All @@ -102,6 +105,7 @@ def __init__(
device,
saving_path,
model_saving_strategy,
verbose,
)

self.n_steps = n_steps
Expand Down
4 changes: 4 additions & 0 deletions pypots/classification/raindrop/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class Raindrop(BaseNNClassifier):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
"""

def __init__(
Expand All @@ -126,6 +128,7 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
n_classes,
Expand All @@ -136,6 +139,7 @@ def __init__(
device,
saving_path,
model_saving_strategy,
verbose,
)

self.n_features = n_features
Expand Down
2 changes: 2 additions & 0 deletions pypots/classification/template/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: Optional[str] = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
n_classes,
Expand All @@ -50,6 +51,7 @@ def __init__(
device,
saving_path,
model_saving_strategy,
verbose,
)
# set up the hyper-parameters
# TODO: set up your model's hyper-parameters here
Expand Down
8 changes: 8 additions & 0 deletions pypots/clustering/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class BaseClusterer(BaseModel):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
"""

def __init__(
Expand All @@ -59,11 +61,13 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
device,
saving_path,
model_saving_strategy,
verbose,
)
self.n_clusters = n_clusters

Expand Down Expand Up @@ -178,6 +182,8 @@ class BaseNNClusterer(BaseNNModel):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
Notes
-----
Expand All @@ -199,6 +205,7 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
batch_size,
Expand All @@ -208,6 +215,7 @@ def __init__(
device,
saving_path,
model_saving_strategy,
verbose,
)
self.n_clusters = n_clusters

Expand Down
4 changes: 4 additions & 0 deletions pypots/clustering/crli/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class CRLI(BaseNNClusterer):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
"""

def __init__(
Expand All @@ -129,6 +131,7 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: Optional[str] = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
n_clusters,
Expand All @@ -139,6 +142,7 @@ def __init__(
device,
saving_path,
model_saving_strategy,
verbose,
)
assert G_steps > 0 and D_steps > 0, "G_steps and D_steps should both >0"

Expand Down
2 changes: 2 additions & 0 deletions pypots/clustering/template/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: Optional[str] = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
n_clusters,
Expand All @@ -50,6 +51,7 @@ def __init__(
device,
saving_path,
model_saving_strategy,
verbose,
)
# set up the hyper-parameters
# TODO: set up your model's hyper-parameters here
Expand Down
4 changes: 4 additions & 0 deletions pypots/clustering/vader/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class VaDER(BaseNNClusterer):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
"""

def __init__(
Expand All @@ -106,6 +108,7 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
n_clusters,
Expand All @@ -116,6 +119,7 @@ def __init__(
device,
saving_path,
model_saving_strategy,
verbose,
)

assert (
Expand Down
10 changes: 10 additions & 0 deletions pypots/forecasting/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,22 @@ class BaseForecaster(BaseModel):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
"""

def __init__(
self,
device: Optional[Union[str, torch.device, list]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
device,
saving_path,
model_saving_strategy,
verbose,
)

@abstractmethod
Expand Down Expand Up @@ -168,6 +172,10 @@ class BaseNNForecaster(BaseNNModel):
The "better" strategy will automatically save the model during training whenever the model performs
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
Notes
-----
Optimizers are necessary for training deep-learning neural networks, but we don't put a parameter ``optimizer``
Expand All @@ -188,6 +196,7 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: str = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
batch_size,
Expand All @@ -197,6 +206,7 @@ def __init__(
device,
saving_path,
model_saving_strategy,
verbose,
)

@abstractmethod
Expand Down
4 changes: 4 additions & 0 deletions pypots/forecasting/csdi/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class CSDI(BaseNNForecaster):
better than in previous epochs.
The "all" strategy will save every model after each epoch training.
verbose :
Whether to print out the training logs during the training process.
"""

def __init__(
Expand Down Expand Up @@ -147,6 +149,7 @@ def __init__(
device: Optional[Union[str, torch.device, list]] = None,
saving_path: Optional[str] = None,
model_saving_strategy: Optional[str] = "best",
verbose: bool = True,
):
super().__init__(
batch_size,
Expand All @@ -156,6 +159,7 @@ def __init__(
device,
saving_path,
model_saving_strategy,
verbose,
)
assert n_pred_features == n_features, (
f"currently n_pred_features of CSDI forecasting model should be equal to n_features, "
Expand Down
Loading

0 comments on commit 7b6fbc7

Please sign in to comment.