Skip to content

Commit

Permalink
refactor: update the templates;
Browse files Browse the repository at this point in the history
  • Loading branch information
WenjieDu committed Apr 16, 2024
1 parent be497bc commit 4e9e34b
Show file tree
Hide file tree
Showing 16 changed files with 284 additions and 200 deletions.
42 changes: 42 additions & 0 deletions pypots/classification/template/core.py
Original file line number Diff line number Diff line change
@@ -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 <Your contact email> 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
19 changes: 14 additions & 5 deletions pypots/classification/template/data.py
Original file line number Diff line number Diff line change
@@ -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.
"""

Expand All @@ -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
Expand Down
47 changes: 15 additions & 32 deletions pypots/classification/template/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,28 @@
Refer to the paper "Your paper citation".
TODO: modify the above description with your model's information.
"""

# Created by Your Name <Your contact email> TODO: modify the author information.
# License: BSD-3-Clause

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.
Expand All @@ -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__(
Expand All @@ -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
Expand All @@ -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
13 changes: 0 additions & 13 deletions pypots/classification/template/module.py

This file was deleted.

42 changes: 42 additions & 0 deletions pypots/clustering/template/core.py
Original file line number Diff line number Diff line change
@@ -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 <Your contact email> 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
19 changes: 14 additions & 5 deletions pypots/clustering/template/data.py
Original file line number Diff line number Diff line change
@@ -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.
"""

Expand All @@ -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
Expand Down
47 changes: 15 additions & 32 deletions pypots/clustering/template/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,28 @@
Refer to the paper "Your paper citation".
TODO: modify the above description with your model's information.
"""

# Created by Your Name <Your contact email> TODO: modify the author information.
# License: BSD-3-Clause

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.
Expand All @@ -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__(
Expand All @@ -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
Expand All @@ -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
13 changes: 0 additions & 13 deletions pypots/clustering/template/module.py

This file was deleted.

Loading

0 comments on commit 4e9e34b

Please sign in to comment.