diff --git a/pypots/classification/template/core.py b/pypots/classification/template/core.py new file mode 100644 index 00000000..157cc4d9 --- /dev/null +++ b/pypots/classification/template/core.py @@ -0,0 +1,42 @@ +""" +The implementation of YourNewModel for the partially-observed time-series classification task. + +Refer to the paper "Your paper citation". + +""" + +# Created by Your Name TODO: modify the author information. +# License: BSD-3-Clause + +import torch.nn as nn + +# from ...nn.modules import some_modules + + +# TODO: define your new model here. +# It could be a neural network model or a non-neural network algorithm (e.g. written in numpy). +# Your model should be implemented with PyTorch and subclass torch.nn.Module if it is a neural network. +# Note that your main algorithm is defined in this class, and this class usually won't be exposed to users. +class _YourNewModel(nn.Module): + def __init__(self): + super().__init__() + + # TODO: define your model's components here. If modules in pypots.nn.modules can be reused in your model, + # you can import them and use them here. AND if you think the modules you implemented can be reused by + # other models, you can also consider to contribute them to pypots.nn.modules + self.embedding = nn.Module + self.submodule = nn.Module + self.backbone = nn.Module + + def forward(self, inputs: dict) -> dict: + # TODO: define your model's forward propagation process here. + # The input is a dict, and the output `results` should also be a dict. + output = self.backbone() # replace this with your model's process + + # TODO: `results` must contains the key `loss` which is will be used for + # backward propagation to update the model. + loss = None + results = { + "loss": loss, + } + return results diff --git a/pypots/classification/template/data.py b/pypots/classification/template/data.py index c391740e..3c4ca97e 100644 --- a/pypots/classification/template/data.py +++ b/pypots/classification/template/data.py @@ -1,7 +1,7 @@ """ Dataset class for YourNewModel. -TODO: modify the above description with your model's information. +TODO: modify the above description for your model's dataset class. """ @@ -10,17 +10,26 @@ from typing import Union, Iterable -from ...data.base import BaseDataset +from ...data.dataset import BaseDataset +# TODO: define your new dataset class here. Remove or add arguments as needed. class DatasetForYourNewModel(BaseDataset): def __init__( self, data: Union[dict, str], - return_labels: bool = True, - file_type: str = "h5py", + return_X_ori: bool, + return_X_pred: bool, + return_y: bool, + file_type: str = "hdf5", ): - super().__init__(data, return_labels, file_type) + super().__init__( + data=data, + return_X_ori=return_X_ori, + return_X_pred=return_X_pred, + return_y=return_y, + file_type=file_type, + ) def _fetch_data_from_array(self, idx: int) -> Iterable: raise NotImplementedError diff --git a/pypots/classification/template/model.py b/pypots/classification/template/model.py index 584fe1d7..40f6b252 100644 --- a/pypots/classification/template/model.py +++ b/pypots/classification/template/model.py @@ -3,6 +3,8 @@ Refer to the paper "Your paper citation". +TODO: modify the above description with your model's information. + """ # Created by Your Name TODO: modify the author information. @@ -10,40 +12,19 @@ from typing import Union, Optional -import numpy as np import torch -import torch.nn as nn + +from .core import _YourNewModel # TODO: import the base class from the classification package in PyPOTS. # Here I suppose this is a neural-network classification model. # You should make your model inherent BaseClassifier if it is not a NN. # from ..base import BaseClassifier from ..base import BaseNNClassifier - from ...optim.adam import Adam from ...optim.base import Optimizer -# TODO: define your new model here. -# It could be a neural network model or a non-neural network algorithm (e.g. written in numpy). -# Your model should be implemented with PyTorch and subclass torch.nn.Module if it is a neural network. -# Note that your main algorithm is defined in this class, and this class usually won't be exposed to users. -class _YourNewModel(nn.Module): - def __init__(self): - super().__init__() - - def forward(self, inputs: dict) -> dict: - # TODO: define your model's forward propagation process here. - # The input is a dict, and the output `results` should also be a dict. - # `results` must contains the key `loss` which is will be used for backward propagation to update the model. - - loss = None - results = { - "loss": loss, - } - return results - - # TODO: define your new model's wrapper here. # It should be a subclass of a base class defined in PyPOTS task packages (e.g. # BaseNNClassifier of PyPOTS classification task package). It has to implement all abstract methods of the base class. @@ -53,13 +34,13 @@ def __init__( self, # TODO: add your model's hyper-parameters here n_classes: int, - batch_size: int, - epochs: int, - patience: int, - num_workers: int = 0, + batch_size: int = 32, + epochs: int = 100, + patience: Optional[int] = None, optimizer: Optional[Optimizer] = Adam(), + num_workers: int = 0, device: Optional[Union[str, torch.device, list]] = None, - saving_path: str = None, + saving_path: Optional[str] = None, model_saving_strategy: Optional[str] = "best", ): super().__init__( @@ -76,9 +57,11 @@ def __init__( # TODO: set up your model's hyper-parameters here # set up the model - self.model = _YourNewModel() - self.model = self.model.to(self.device) + self.model = _YourNewModel( + # pass the arguments to your model + ) self._print_model_size() + self._send_model_to_given_device() # set up the optimizer self.optimizer = optimizer @@ -97,13 +80,13 @@ def fit( self, train_set: Union[dict, str], val_set: Optional[Union[dict, str]] = None, - file_type: str = "h5py", + file_type: str = "hdf5", ) -> None: raise NotImplementedError def predict( self, test_set: Union[dict, str], - file_type: str = "h5py", + file_type: str = "hdf5", ) -> dict: raise NotImplementedError diff --git a/pypots/classification/template/module.py b/pypots/classification/template/module.py deleted file mode 100644 index fa20e4cd..00000000 --- a/pypots/classification/template/module.py +++ /dev/null @@ -1,13 +0,0 @@ -""" -The implementation of the modules for YourNewModel. - -Refer to the paper "Your paper citation". - -""" - -# Created by Your Name TODO: modify the author information. -# License: BSD-3-Clause - - -# TODO: this file is not necessary. If your new model has customized layers or modules, please put them here. -# Otherwise, please delete this modules.py file, don't commit it to the repository. diff --git a/pypots/clustering/template/core.py b/pypots/clustering/template/core.py new file mode 100644 index 00000000..524a7a3c --- /dev/null +++ b/pypots/clustering/template/core.py @@ -0,0 +1,42 @@ +""" +The implementation of YourNewModel for the partially-observed time-series clustering task. + +Refer to the paper "Your paper citation". + +""" + +# Created by Your Name TODO: modify the author information. +# License: BSD-3-Clause + +import torch.nn as nn + +# from ...nn.modules import some_modules + + +# TODO: define your new model here. +# It could be a neural network model or a non-neural network algorithm (e.g. written in numpy). +# Your model should be implemented with PyTorch and subclass torch.nn.Module if it is a neural network. +# Note that your main algorithm is defined in this class, and this class usually won't be exposed to users. +class _YourNewModel(nn.Module): + def __init__(self): + super().__init__() + + # TODO: define your model's components here. If modules in pypots.nn.modules can be reused in your model, + # you can import them and use them here. AND if you think the modules you implemented can be reused by + # other models, you can also consider to contribute them to pypots.nn.modules + self.embedding = nn.Module + self.submodule = nn.Module + self.backbone = nn.Module + + def forward(self, inputs: dict) -> dict: + # TODO: define your model's forward propagation process here. + # The input is a dict, and the output `results` should also be a dict. + output = self.backbone() # replace this with your model's process + + # TODO: `results` must contains the key `loss` which is will be used for + # backward propagation to update the model. + loss = None + results = { + "loss": loss, + } + return results diff --git a/pypots/clustering/template/data.py b/pypots/clustering/template/data.py index c391740e..3c4ca97e 100644 --- a/pypots/clustering/template/data.py +++ b/pypots/clustering/template/data.py @@ -1,7 +1,7 @@ """ Dataset class for YourNewModel. -TODO: modify the above description with your model's information. +TODO: modify the above description for your model's dataset class. """ @@ -10,17 +10,26 @@ from typing import Union, Iterable -from ...data.base import BaseDataset +from ...data.dataset import BaseDataset +# TODO: define your new dataset class here. Remove or add arguments as needed. class DatasetForYourNewModel(BaseDataset): def __init__( self, data: Union[dict, str], - return_labels: bool = True, - file_type: str = "h5py", + return_X_ori: bool, + return_X_pred: bool, + return_y: bool, + file_type: str = "hdf5", ): - super().__init__(data, return_labels, file_type) + super().__init__( + data=data, + return_X_ori=return_X_ori, + return_X_pred=return_X_pred, + return_y=return_y, + file_type=file_type, + ) def _fetch_data_from_array(self, idx: int) -> Iterable: raise NotImplementedError diff --git a/pypots/clustering/template/model.py b/pypots/clustering/template/model.py index 06a4a38b..65bc8aa9 100644 --- a/pypots/clustering/template/model.py +++ b/pypots/clustering/template/model.py @@ -3,6 +3,8 @@ Refer to the paper "Your paper citation". +TODO: modify the above description with your model's information. + """ # Created by Your Name TODO: modify the author information. @@ -10,40 +12,19 @@ from typing import Union, Optional -import numpy as np import torch -import torch.nn as nn + +from .core import _YourNewModel # TODO: import the base class from the clustering package in PyPOTS. # Here I suppose this is a neural-network clustering model. # You should make your model inherent BaseClusterer if it is not a NN. # from ..base import BaseClusterer from ..base import BaseNNClusterer - from ...optim.adam import Adam from ...optim.base import Optimizer -# TODO: define your new model here. -# It could be a neural network model or a non-neural network algorithm (e.g. written in numpy). -# Your model should be implemented with PyTorch and subclass torch.nn.Module if it is a neural network. -# Note that your main algorithm is defined in this class, and this class usually won't be exposed to users. -class _YourNewModel(nn.Module): - def __init__(self): - super().__init__() - - def forward(self, inputs: dict) -> dict: - # TODO: define your model's forward propagation process here. - # The input is a dict, and the output `results` should also be a dict. - # `results` must contains the key `loss` which is will be used for backward propagation to update the model. - - loss = None - results = { - "loss": loss, - } - return results - - # TODO: define your new model's wrapper here. # It should be a subclass of a base class defined in PyPOTS task packages (e.g. # BaseNNClusterer of PyPOTS clustering task package), and it has to implement all abstract methods of the base class. @@ -53,13 +34,13 @@ def __init__( self, # TODO: add your model's hyper-parameters here n_clusters: int, - batch_size: int, - epochs: int, - patience: int, - num_workers: int = 0, + batch_size: int = 32, + epochs: int = 100, + patience: Optional[int] = None, optimizer: Optional[Optimizer] = Adam(), + num_workers: int = 0, device: Optional[Union[str, torch.device, list]] = None, - saving_path: str = None, + saving_path: Optional[str] = None, model_saving_strategy: Optional[str] = "best", ): super().__init__( @@ -76,9 +57,11 @@ def __init__( # TODO: set up your model's hyper-parameters here # set up the model - self.model = _YourNewModel() - self.model = self.model.to(self.device) + self.model = _YourNewModel( + # pass the arguments to your model + ) self._print_model_size() + self._send_model_to_given_device() # set up the optimizer self.optimizer = optimizer @@ -97,13 +80,13 @@ def fit( self, train_set: Union[dict, str], val_set: Optional[Union[dict, str]] = None, - file_type: str = "h5py", + file_type: str = "hdf5", ) -> None: raise NotImplementedError def predict( self, test_set: Union[dict, str], - file_type: str = "h5py", + file_type: str = "hdf5", ) -> dict: raise NotImplementedError diff --git a/pypots/clustering/template/module.py b/pypots/clustering/template/module.py deleted file mode 100644 index fa20e4cd..00000000 --- a/pypots/clustering/template/module.py +++ /dev/null @@ -1,13 +0,0 @@ -""" -The implementation of the modules for YourNewModel. - -Refer to the paper "Your paper citation". - -""" - -# Created by Your Name TODO: modify the author information. -# License: BSD-3-Clause - - -# TODO: this file is not necessary. If your new model has customized layers or modules, please put them here. -# Otherwise, please delete this modules.py file, don't commit it to the repository. diff --git a/pypots/forecasting/template/core.py b/pypots/forecasting/template/core.py new file mode 100644 index 00000000..55cf3ada --- /dev/null +++ b/pypots/forecasting/template/core.py @@ -0,0 +1,42 @@ +""" +The implementation of YourNewModel for the partially-observed time-series forecasting task. + +Refer to the paper "Your paper citation". + +""" + +# Created by Your Name TODO: modify the author information. +# License: BSD-3-Clause + +import torch.nn as nn + +# from ...nn.modules import some_modules + + +# TODO: define your new model here. +# It could be a neural network model or a non-neural network algorithm (e.g. written in numpy). +# Your model should be implemented with PyTorch and subclass torch.nn.Module if it is a neural network. +# Note that your main algorithm is defined in this class, and this class usually won't be exposed to users. +class _YourNewModel(nn.Module): + def __init__(self): + super().__init__() + + # TODO: define your model's components here. If modules in pypots.nn.modules can be reused in your model, + # you can import them and use them here. AND if you think the modules you implemented can be reused by + # other models, you can also consider to contribute them to pypots.nn.modules + self.embedding = nn.Module + self.submodule = nn.Module + self.backbone = nn.Module + + def forward(self, inputs: dict) -> dict: + # TODO: define your model's forward propagation process here. + # The input is a dict, and the output `results` should also be a dict. + output = self.backbone() # replace this with your model's process + + # TODO: `results` must contains the key `loss` which is will be used for + # backward propagation to update the model. + loss = None + results = { + "loss": loss, + } + return results diff --git a/pypots/forecasting/template/data.py b/pypots/forecasting/template/data.py index c391740e..3c4ca97e 100644 --- a/pypots/forecasting/template/data.py +++ b/pypots/forecasting/template/data.py @@ -1,7 +1,7 @@ """ Dataset class for YourNewModel. -TODO: modify the above description with your model's information. +TODO: modify the above description for your model's dataset class. """ @@ -10,17 +10,26 @@ from typing import Union, Iterable -from ...data.base import BaseDataset +from ...data.dataset import BaseDataset +# TODO: define your new dataset class here. Remove or add arguments as needed. class DatasetForYourNewModel(BaseDataset): def __init__( self, data: Union[dict, str], - return_labels: bool = True, - file_type: str = "h5py", + return_X_ori: bool, + return_X_pred: bool, + return_y: bool, + file_type: str = "hdf5", ): - super().__init__(data, return_labels, file_type) + super().__init__( + data=data, + return_X_ori=return_X_ori, + return_X_pred=return_X_pred, + return_y=return_y, + file_type=file_type, + ) def _fetch_data_from_array(self, idx: int) -> Iterable: raise NotImplementedError diff --git a/pypots/forecasting/template/model.py b/pypots/forecasting/template/model.py index 099c617b..890c3fde 100644 --- a/pypots/forecasting/template/model.py +++ b/pypots/forecasting/template/model.py @@ -3,6 +3,8 @@ Refer to the paper "Your paper citation". +TODO: modify the above description with your model's information. + """ # Created by Your Name TODO: modify the author information. @@ -10,40 +12,19 @@ from typing import Union, Optional -import numpy as np import torch -import torch.nn as nn + +from .core import _YourNewModel # TODO: import the base class from the forecasting package in PyPOTS. # Here I suppose this is a neural-network forecasting model. # You should make your model inherent BaseForecaster if it is not a NN. # from ..base import BaseForecaster from ..base import BaseNNForecaster - from ...optim.adam import Adam from ...optim.base import Optimizer -# TODO: define your new model here. -# It could be a neural network model or a non-neural network algorithm (e.g. written in numpy). -# Your model should be implemented with PyTorch and subclass torch.nn.Module if it is a neural network. -# Note that your main algorithm is defined in this class, and this class usually won't be exposed to users. -class _YourNewModel(nn.Module): - def __init__(self): - super().__init__() - - def forward(self, inputs: dict) -> dict: - # TODO: define your model's forward propagation process here. - # The input is a dict, and the output `results` should also be a dict. - # `results` must contains the key `loss` which is will be used for backward propagation to update the model. - - loss = None - results = { - "loss": loss, - } - return results - - # TODO: define your new model's wrapper here. # It should be a subclass of a base class defined in PyPOTS task packages (e.g. # BaseNNForecaster of PyPOTS forecasting task package), and it has to implement all abstract methods of the base class. @@ -52,13 +33,13 @@ class YourNewModel(BaseNNForecaster): def __init__( self, # TODO: add your model's hyper-parameters here - batch_size: int, - epochs: int, - patience: int, - num_workers: int = 0, + batch_size: int = 32, + epochs: int = 100, + patience: Optional[int] = None, optimizer: Optional[Optimizer] = Adam(), + num_workers: int = 0, device: Optional[Union[str, torch.device, list]] = None, - saving_path: str = None, + saving_path: Optional[str] = None, model_saving_strategy: Optional[str] = "best", ): super().__init__( @@ -74,9 +55,11 @@ def __init__( # TODO: set up your model's hyper-parameters here # set up the model - self.model = _YourNewModel() - self.model = self.model.to(self.device) + self.model = _YourNewModel( + # pass the arguments to your model + ) self._print_model_size() + self._send_model_to_given_device() # set up the optimizer self.optimizer = optimizer @@ -95,13 +78,13 @@ def fit( self, train_set: Union[dict, str], val_set: Optional[Union[dict, str]] = None, - file_type: str = "h5py", + file_type: str = "hdf5", ) -> None: raise NotImplementedError def predict( self, test_set: Union[dict, str], - file_type: str = "h5py", + file_type: str = "hdf5", ) -> dict: raise NotImplementedError diff --git a/pypots/forecasting/template/module.py b/pypots/forecasting/template/module.py deleted file mode 100644 index fa20e4cd..00000000 --- a/pypots/forecasting/template/module.py +++ /dev/null @@ -1,13 +0,0 @@ -""" -The implementation of the modules for YourNewModel. - -Refer to the paper "Your paper citation". - -""" - -# Created by Your Name TODO: modify the author information. -# License: BSD-3-Clause - - -# TODO: this file is not necessary. If your new model has customized layers or modules, please put them here. -# Otherwise, please delete this modules.py file, don't commit it to the repository. diff --git a/pypots/imputation/template/core.py b/pypots/imputation/template/core.py new file mode 100644 index 00000000..4bfd259d --- /dev/null +++ b/pypots/imputation/template/core.py @@ -0,0 +1,42 @@ +""" +The implementation of YourNewModel for the partially-observed time-series imputation task. + +Refer to the paper "Your paper citation". + +""" + +# Created by Your Name TODO: modify the author information. +# License: BSD-3-Clause + +import torch.nn as nn + +# from ...nn.modules import some_modules + + +# TODO: define your new model here. +# It could be a neural network model or a non-neural network algorithm (e.g. written in numpy). +# Your model should be implemented with PyTorch and subclass torch.nn.Module if it is a neural network. +# Note that your main algorithm is defined in this class, and this class usually won't be exposed to users. +class _YourNewModel(nn.Module): + def __init__(self): + super().__init__() + + # TODO: define your model's components here. If modules in pypots.nn.modules can be reused in your model, + # you can import them and use them here. AND if you think the modules you implemented can be reused by + # other models, you can also consider to contribute them to pypots.nn.modules + self.embedding = nn.Module + self.submodule = nn.Module + self.backbone = nn.Module + + def forward(self, inputs: dict) -> dict: + # TODO: define your model's forward propagation process here. + # The input is a dict, and the output `results` should also be a dict. + output = self.backbone() # replace this with your model's process + + # TODO: `results` must contains the key `loss` which is will be used for + # backward propagation to update the model. + loss = None + results = { + "loss": loss, + } + return results diff --git a/pypots/imputation/template/data.py b/pypots/imputation/template/data.py index c391740e..3c4ca97e 100644 --- a/pypots/imputation/template/data.py +++ b/pypots/imputation/template/data.py @@ -1,7 +1,7 @@ """ Dataset class for YourNewModel. -TODO: modify the above description with your model's information. +TODO: modify the above description for your model's dataset class. """ @@ -10,17 +10,26 @@ from typing import Union, Iterable -from ...data.base import BaseDataset +from ...data.dataset import BaseDataset +# TODO: define your new dataset class here. Remove or add arguments as needed. class DatasetForYourNewModel(BaseDataset): def __init__( self, data: Union[dict, str], - return_labels: bool = True, - file_type: str = "h5py", + return_X_ori: bool, + return_X_pred: bool, + return_y: bool, + file_type: str = "hdf5", ): - super().__init__(data, return_labels, file_type) + super().__init__( + data=data, + return_X_ori=return_X_ori, + return_X_pred=return_X_pred, + return_y=return_y, + file_type=file_type, + ) def _fetch_data_from_array(self, idx: int) -> Iterable: raise NotImplementedError diff --git a/pypots/imputation/template/model.py b/pypots/imputation/template/model.py index 496782bf..170e140b 100644 --- a/pypots/imputation/template/model.py +++ b/pypots/imputation/template/model.py @@ -3,6 +3,8 @@ Refer to the paper "Your paper citation". +TODO: modify the above description with your model's information. + """ # Created by Your Name TODO: modify the author information. @@ -10,40 +12,19 @@ from typing import Union, Optional -import numpy as np import torch -import torch.nn as nn + +from .core import _YourNewModel # TODO: import the base class from the imputation package in PyPOTS. # Here I suppose this is a neural-network imputation model. # You should make your model inherent BaseImputer if it is not a NN. # from ..base import BaseImputer from ..base import BaseNNImputer - from ...optim.adam import Adam from ...optim.base import Optimizer -# TODO: define your new model here. -# It could be a neural network model or a non-neural network algorithm (e.g. written in numpy). -# Your model should be implemented with PyTorch and subclass torch.nn.Module if it is a neural network. -# Note that your main algorithm is defined in this class, and this class usually won't be exposed to users. -class _YourNewModel(nn.Module): - def __init__(self): - super().__init__() - - def forward(self, inputs: dict) -> dict: - # TODO: define your model's forward propagation process here. - # The input is a dict, and the output `results` should also be a dict. - # `results` must contains the key `loss` which is will be used for backward propagation to update the model. - - loss = None - results = { - "loss": loss, - } - return results - - # TODO: define your new model's wrapper here. # It should be a subclass of a base class defined in PyPOTS task packages (e.g. # BaseNNImputer of PyPOTS imputation task package), and it has to implement all abstract methods of the base class. @@ -52,13 +33,13 @@ class YourNewModel(BaseNNImputer): def __init__( self, # TODO: add your model's hyper-parameters here - batch_size: int, - epochs: int, - patience: int, - num_workers: int = 0, + batch_size: int = 32, + epochs: int = 100, + patience: Optional[int] = None, optimizer: Optional[Optimizer] = Adam(), + num_workers: int = 0, device: Optional[Union[str, torch.device, list]] = None, - saving_path: str = None, + saving_path: Optional[str] = None, model_saving_strategy: Optional[str] = "best", ): super().__init__( @@ -74,9 +55,11 @@ def __init__( # TODO: set up your model's hyper-parameters here # set up the model - self.model = _YourNewModel() - self.model = self.model.to(self.device) + self.model = _YourNewModel( + # pass the arguments to your model + ) self._print_model_size() + self._send_model_to_given_device() # set up the optimizer self.optimizer = optimizer @@ -95,13 +78,13 @@ def fit( self, train_set: Union[dict, str], val_set: Optional[Union[dict, str]] = None, - file_type: str = "h5py", + file_type: str = "hdf5", ) -> None: raise NotImplementedError def predict( self, test_set: Union[dict, str], - file_type: str = "h5py", + file_type: str = "hdf5", ) -> dict: raise NotImplementedError diff --git a/pypots/imputation/template/module.py b/pypots/imputation/template/module.py deleted file mode 100644 index fa20e4cd..00000000 --- a/pypots/imputation/template/module.py +++ /dev/null @@ -1,13 +0,0 @@ -""" -The implementation of the modules for YourNewModel. - -Refer to the paper "Your paper citation". - -""" - -# Created by Your Name TODO: modify the author information. -# License: BSD-3-Clause - - -# TODO: this file is not necessary. If your new model has customized layers or modules, please put them here. -# Otherwise, please delete this modules.py file, don't commit it to the repository.