-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from WenjieDu/dev
Merge `dev` into `main`
- Loading branch information
Showing
22 changed files
with
344 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,13 @@ | |
# Created by Wenjie Du <[email protected]> | ||
# License: GPL-v3 | ||
|
||
|
||
from .__version__ import version as __version__ | ||
|
||
__all__ = [ | ||
"data", | ||
"imputation", | ||
"classification", | ||
"clustering", | ||
"forecasting", | ||
"utils", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,15 @@ | |
|
||
# Created by Wenjie Du <[email protected]> | ||
# License: GLP-v3 | ||
|
||
import os | ||
from abc import ABC | ||
|
||
import numpy as np | ||
import torch | ||
from torch.utils.tensorboard import SummaryWriter | ||
|
||
from pypots.utils.logging import logger | ||
from pypots.utils.files import create_dir_if_not_exist | ||
|
||
|
||
class BaseModel(ABC): | ||
|
@@ -24,7 +28,7 @@ def __init__(self, device): | |
if torch.cuda.is_available() and torch.cuda.device_count() > 0 | ||
else "cpu" | ||
) | ||
print("No given device, using default device:", self.device) | ||
logger.info(f"No given device, using default device: {self.device}") | ||
else: | ||
self.device = device | ||
|
||
|
@@ -136,21 +140,41 @@ def save_logs_to_tensorboard(self, saving_path): | |
# tb_summary_writer = SummaryWriter(saving_path) | ||
# tb_summary_writer.add_custom_scalars(self.logger) | ||
# tb_summary_writer.close() | ||
# print(f'Log saved successfully to {saving_path}.') | ||
# logger.info(f'Log saved successfully to {saving_path}.') | ||
|
||
def save_model(self, saving_path): | ||
def save_model(self, saving_dir, name, overwrite=False): | ||
"""Save the model to a disk file. | ||
A .pypots extension will be appended to the filename if it does not already have one. | ||
Please note that such an extension is not necessary, but to indicate the saved model is from PyPOTS framework so people can distinguish. | ||
Parameters | ||
---------- | ||
saving_path : str, | ||
The given path to save the model. | ||
saving_dir : str, | ||
The given directory to save the model. | ||
name : str, | ||
The file name of the model to be saved. | ||
overwrite : bool, | ||
""" | ||
name = name + ".pypots" if name.split(".")[-1] != "pypots" else name | ||
saving_path = os.path.join(saving_dir, name) | ||
if os.path.exists(saving_path): | ||
if overwrite: | ||
logger.warning( | ||
f"File {saving_path} exists. Argument `overwrite` is True. Overwriting now..." | ||
) | ||
else: | ||
logger.error(f"File {saving_path} exists. Saving operation aborted.") | ||
return | ||
try: | ||
create_dir_if_not_exist(saving_dir) | ||
torch.save(self.model, saving_path) | ||
logger.info(f"Saved successfully to {saving_path}.") | ||
except Exception as e: | ||
print(e) | ||
print(f"Saved successfully to {saving_path}.") | ||
raise RuntimeError(f'{e} Failed to save the model to "{saving_path}"!') | ||
|
||
def load_model(self, model_path): | ||
"""Load the saved model from a disk file. | ||
|
@@ -174,7 +198,7 @@ def load_model(self, model_path): | |
self.model = loaded_model.model | ||
except Exception as e: | ||
raise e | ||
print(f"Model loaded successfully from {model_path}.") | ||
logger.info(f"Model loaded successfully from {model_path}.") | ||
|
||
|
||
class BaseNNModel(BaseModel): | ||
|
@@ -202,6 +226,6 @@ def __init__( | |
def _print_model_size(self): | ||
"""Print the number of trainable parameters in the initialized NN model.""" | ||
num_params = sum(p.numel() for p in self.model.parameters() if p.requires_grad) | ||
print( | ||
logger.info( | ||
f"Model initialized successfully. Number of the trainable parameters: {num_params}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.