Skip to content

Commit

Permalink
chore: fill missing docstrings (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwanglzu committed Oct 22, 2021
1 parent fddc57d commit 870c5a2
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 3 deletions.
15 changes: 15 additions & 0 deletions finetuner/labeler/__init__.py
Expand Up @@ -20,6 +20,21 @@ def fit(
loss: str = 'CosineSiameseLoss',
**kwargs,
) -> None:
"""Fit the model in an interactive UI.
:param embed_model: The embedding model to fine-tune
:param train_data: Data on which to train the model
:param clear_labels_on_start: If set True, will remove all labeled data.
:param port_expose: The port to expose.
:param runtime_backend: The parallel backend of the runtime inside the Pea, either ``thread`` or ``process``.
:param loss: Which loss to use in training. Supported
losses are:
- ``CosineSiameseLoss`` for Siamese network with cosine distance
- ``EuclideanSiameseLoss`` for Siamese network with eculidean distance
- ``CosineTripletLoss`` for Triplet network with cosine distance
- ``EuclideanTripletLoss`` for Triplet network with eculidean distance
:param kwargs: Additional keyword arguments.
"""
dam_path = tempfile.mkdtemp()

class MyExecutor(FTExecutor):
Expand Down
17 changes: 17 additions & 0 deletions finetuner/tailor/__init__.py
Expand Up @@ -32,6 +32,17 @@ def to_embedding_model(
input_dtype: str = 'float32',
**kwargs
) -> AnyDNN:
"""Convert a general model from :py:attr:`.model` to an embedding model.
:param model: The DNN model to be converted.
:param layer_name: the name of the layer that is used for output embeddings. All layers *after* that layer
will be removed. When set to ``None``, then the last layer listed in :py:attr:`.embedding_layers` will be used.
To see all available names you can check ``name`` field of :py:attr:`.embedding_layers`.
:param output_dim: the dimensionality of the embedding output.
:param freeze: if set, then freeze all weights of the original model.
:param input_size: The input size of the DNN model.
:param input_dtype: The input data type of the DNN model.
"""
ft = _get_tailor_class(model)

return ft(model, input_size, input_dtype).to_embedding_model(
Expand All @@ -44,6 +55,12 @@ def display(
input_size: Optional[Tuple[int, ...]] = None,
input_dtype: str = 'float32',
) -> AnyDNN:
"""Display the model architecture from :py:attr:`.summary` in a table.
:param model: The DNN model to display.
:param input_size: The input size of the DNN model.
:param input_dtype: The input data type of the DNN model.
"""
ft = _get_tailor_class(model)

return ft(model, input_size, input_dtype).display()
16 changes: 16 additions & 0 deletions finetuner/tailor/keras/__init__.py
Expand Up @@ -11,6 +11,12 @@ class KerasTailor(BaseTailor):
"""Tailor class for Keras DNN models."""

def summary(self, skip_identity_layer: bool = False) -> LayerInfoType:
"""Interpret the DNN model and produce model information.
:param skip_identity_layer: If skip identity layer.
:return: The model information stored as dict.
"""

def _get_output_shape(layer):
try:
return layer.output_shape
Expand Down Expand Up @@ -70,6 +76,16 @@ def to_embedding_model(
freeze: bool = False,
) -> AnyDNN:

"""Convert a general model from :py:attr:`.model` to an embedding model.
:param layer_name: the name of the layer that is used for output embeddings. All layers *after* that layer
will be removed. When set to ``None``, then the last layer listed in :py:attr:`.embedding_layers` will be used.
To see all available names you can check ``name`` field of :py:attr:`.embedding_layers`.
:param output_dim: the dimensionality of the embedding output.
:param freeze: if set, then freeze all weights of the original model.
:return: Converted embedding model.
"""

if layer_name:
_all_embed_layers = {l['name']: l for l in self.embedding_layers}
try:
Expand Down
14 changes: 14 additions & 0 deletions finetuner/tailor/paddle/__init__.py
Expand Up @@ -19,6 +19,11 @@ class PaddleTailor(BaseTailor):
"""

def summary(self, skip_identity_layer: bool = False) -> LayerInfoType:
"""Interpret the DNN model and produce model information.
:param skip_identity_layer: If skip identity layer.
:return: The model information stored as dict.
"""
if not self._input_size:
raise ValueError(
f'{self.__class__} requires a valid `input_size`, but receiving {self._input_size}'
Expand Down Expand Up @@ -134,6 +139,15 @@ def to_embedding_model(
output_dim: Optional[int] = None,
freeze: bool = False,
) -> AnyDNN:
"""Convert a general model from :py:attr:`.model` to an embedding model.
:param layer_name: the name of the layer that is used for output embeddings. All layers *after* that layer
will be removed. When set to ``None``, then the last layer listed in :py:attr:`.embedding_layers` will be used.
To see all available names you can check ``name`` field of :py:attr:`.embedding_layers`.
:param output_dim: the dimensionality of the embedding output.
:param freeze: if set, then freeze all weights of the original model.
:return: Converted embedding model.
"""
model = copy.deepcopy(self._model)

if layer_name:
Expand Down
14 changes: 14 additions & 0 deletions finetuner/tailor/pytorch/__init__.py
Expand Up @@ -16,6 +16,11 @@ class PytorchTailor(BaseTailor):
"""Tailor class for PyTorch DNN models"""

def summary(self, skip_identity_layer: bool = False) -> LayerInfoType:
"""Interpret the DNN model and produce model information.
:param skip_identity_layer: If skip identity layer.
:return: The model information stored as dict.
"""
if not self._input_size:
raise ValueError(
f'{self.__class__} requires a valid `input_size`, but receiving {self._input_size}'
Expand Down Expand Up @@ -129,6 +134,15 @@ def to_embedding_model(
output_dim: Optional[int] = None,
freeze: bool = False,
) -> AnyDNN:
"""Convert a general model from :py:attr:`.model` to an embedding model.
:param layer_name: the name of the layer that is used for output embeddings. All layers *after* that layer
will be removed. When set to ``None``, then the last layer listed in :py:attr:`.embedding_layers` will be used.
To see all available names you can check ``name`` field of :py:attr:`.embedding_layers`.
:param output_dim: the dimensionality of the embedding output.
:param freeze: if set, then freeze all weights of the original model.
:return: Converted embedding model.
"""

model = copy.deepcopy(self._model)

Expand Down
6 changes: 6 additions & 0 deletions finetuner/tuner/__init__.py
Expand Up @@ -44,6 +44,12 @@ def fit(
:param eval_data: Data on which to evaluate the model at the end of each epoch
:param epochs: Number of epochs to train the model
:param batch_size: The batch size to use for training and evaluation
:param loss: Which loss to use in training. Supported
losses are:
- ``CosineSiameseLoss`` for Siamese network with cosine distance
- ``EuclideanSiameseLoss`` for Siamese network with eculidean distance
- ``CosineTripletLoss`` for Triplet network with cosine distance
- ``EuclideanTripletLoss`` for Triplet network with eculidean distance
:param learning_rate: Learning rate to use in training
:param optimizer: Which optimizer to use in training. Supported
values/optimizers are:
Expand Down
2 changes: 1 addition & 1 deletion finetuner/tuner/keras/losses.py
Expand Up @@ -34,7 +34,7 @@ def call(self, inputs, **kwargs):


class EuclideanSiameseLoss(BaseLoss, Layer):
"""Computes the loss for a siamese network using cosine distance.
"""Computes the loss for a siamese network using eculidean distance.
This loss is also known as contrastive loss.
Expand Down
2 changes: 1 addition & 1 deletion finetuner/tuner/paddle/losses.py
Expand Up @@ -37,7 +37,7 @@ def forward(


class EuclideanSiameseLoss(BaseLoss, nn.Layer):
"""Computes the loss for a siamese network using cosine distance.
"""Computes the loss for a siamese network using eculidean distance.
This loss is also known as contrastive loss.
Expand Down
2 changes: 1 addition & 1 deletion finetuner/tuner/pytorch/losses.py
Expand Up @@ -37,7 +37,7 @@ def forward(


class EuclideanSiameseLoss(BaseLoss, nn.Module):
"""Computes the loss for a siamese network using cosine distance.
"""Computes the loss for a siamese network using eculidean distance.
This loss is also known as contrastive loss.
Expand Down

0 comments on commit 870c5a2

Please sign in to comment.