Skip to content

Model

pamiq_core.model.InferenceModel

Bases: ABC

Base interface class for model to infer in InferenceThread.

Needed for multi-thread training and inference in parallel.

infer abstractmethod

infer(*args: Any, **kwds: Any) -> Any

Perform inference using a model.

PARAMETER DESCRIPTION
*args

Positional arguments required for inference.

TYPE: Any DEFAULT: ()

**kwds

Keyword arguments required for inference.

TYPE: Any DEFAULT: {}

Returns: Any: The result of the inference.

Source code in src/pamiq_core/model/interface.py
@abstractmethod
def infer(self, *args: Any, **kwds: Any) -> Any:
    """Perform inference using a model.

    Args:
        *args: Positional arguments required for inference.
        **kwds: Keyword arguments required for inference.
    Returns:
        Any: The result of the inference.
    """
    raise NotImplementedError

__call__

__call__(*args: Any, **kwds: Any) -> Any

Perform inference using a model.

PARAMETER DESCRIPTION
*args

Positional arguments required for inference.

TYPE: Any DEFAULT: ()

**kwds

Keyword arguments required for inference.

TYPE: Any DEFAULT: {}

Returns: Any: The result of the inference.

Source code in src/pamiq_core/model/interface.py
def __call__(self, *args: Any, **kwds: Any) -> Any:
    """Perform inference using a model.

    Args:
        *args:  Positional arguments required for inference.
        **kwds: Keyword arguments required for inference.
    Returns:
        Any: The result of the inference.
    """
    return self.infer(*args, **kwds)

pamiq_core.model.TrainingModel

TrainingModel(has_inference_model: bool = True, inference_thread_only: bool = False)

Bases: ABC, PersistentStateMixin

Base interface class to train model in TrainingThread.

Needed for multi-thread training and inference in parallel.

Initialize the TrainingModel.

PARAMETER DESCRIPTION
has_inference_model

Whether to have inference model.

TYPE: bool DEFAULT: True

inference_thread_only

Whether it is an inference thread only.

TYPE: bool DEFAULT: False

Source code in src/pamiq_core/model/interface.py
def __init__(
    self, has_inference_model: bool = True, inference_thread_only: bool = False
):
    """Initialize the TrainingModel.

    Args:
        has_inference_model: Whether to have inference model.
        inference_thread_only: Whether it is an inference thread only.
    """
    if (not has_inference_model) and (inference_thread_only):
        raise ValueError
    self.has_inference_model = has_inference_model
    self.inference_thread_only = inference_thread_only

inference_model property

inference_model: T

Get inference model.

forward abstractmethod

forward(*args: Any, **kwds: Any) -> Any

Forward path of model.

PARAMETER DESCRIPTION
*args

Positional arguments required for forward path.

TYPE: Any DEFAULT: ()

**kwds

Keyword arguments required for forward path.

TYPE: Any DEFAULT: {}

Returns: Result of forward path of the model.

Source code in src/pamiq_core/model/interface.py
@abstractmethod
def forward(self, *args: Any, **kwds: Any) -> Any:
    """Forward path of model.

    Args:
        *args: Positional arguments required for forward path.
        **kwds: Keyword arguments required for forward path.
    Returns:
        Result of forward path of the model.
    """
    pass

__call__

__call__(*args: Any, **kwds: Any) -> Any

Calls forward method.

Source code in src/pamiq_core/model/interface.py
def __call__(self, *args: Any, **kwds: Any) -> Any:
    """Calls `forward` method."""
    return self.forward(*args, **kwds)

sync

sync() -> None

Synchronizes parameters of training model to self._inference_model if needed.

Source code in src/pamiq_core/model/interface.py
def sync(self) -> None:
    """Synchronizes parameters of training model to self._inference_model
    if needed."""
    if self._need_sync:
        self.sync_impl(self.inference_model)

sync_impl abstractmethod

sync_impl(inference_model: T) -> None

Copies params of training model to self._inference_model if needed.

PARAMETER DESCRIPTION
inference_model

InferenceModel to sync.

TYPE: T

Source code in src/pamiq_core/model/interface.py
@abstractmethod
def sync_impl(self, inference_model: T) -> None:
    """Copies params of training model to self._inference_model if needed.

    Args:
        inference_model: InferenceModel to sync.
    """
    pass