Model

class seawrd.model.DNNManager(model: Sequential, history: History | None, version: int, model_name: str)

Bases: object

A class to manage the creation, saving, and loading of a Deep Neural Network (DNN) model using Keras.

clone_model() Sequential

Creates a clone of the current model with the same architecture and normalisation weights. This is useful for creating a new instance of the model for further training or evaluation without affecting the original model.

Returns:

A new Keras Sequential model that is a clone of the current model.

Return type:

keras.Sequential

static compile_from_config(model: Sequential, compile_config: CompileConfig) None

Compiles the current model using the instance’s CompileConfig. This allows for dynamic compilation of the model with different loss functions, optimisers, and metrics.

Parameters:

model (keras.Sequential) – The Keras model to be compiled.

create_model_name() str

Creates a model name based on the architecture of the model.

Returns:

The generated model name in the format “R(num_layers x num_neurons)_{num_inputs}i_{num_outputs}o”.

Return type:

str

classmethod from_config(model_config: ModelConfig, input_shape: tuple[int, ...], normaliser: Normalization | None = None) DNNManager

Generates a Keras Sequential model based on the provided configuration and creates a DNNManager instance with the generated model, an empty history, and version 0.

Parameters:
  • model_config (ModelConfig) – An instance of the ModelConfig dataclass containing model parameters such as number of layers, number of neurons, input shape, number of outputs, etc.

  • input_shape (tuple[int, ...]) – The shape of the input features.

  • normaliser (keras.layers.Normalization | None, optional) – The normalisation layer to be applied to the input features, by default None

Returns:

The DNNManager instance with the generated model, an empty history, and version 0.

Return type:

DNNManager

Raises:

ValueError – If normalisation is to be used but the normaliser is not provided.

classmethod from_previous_model(model_dir: Path | str, model_name: str, version: int | None = None) DNNManager

Loads a previously saved Keras model from the specified path and creates a DNNManager instance with the loaded model, history, and version.

Parameters:
  • model_dir (Path | str) – The directory containing the model files.

  • model_name (str) – The name of the model.

  • version (int | None, optional) – The version number of the model to load, by default None (loads the latest version).

Returns:

The DNNManager instance with the loaded model, history, and version.

Return type:

DNNManager

get_latest_version(model_dir: Path | str, model_name: str, pattern: Pattern | str | None = None) int

Finds the latest version of a model from the specified directory.

Parameters:
  • model_dir (Path | str) – The directory containing the model files.

  • model_name (str) – The name of the model.

  • pattern (re.Pattern | str | None, optional) – The regex pattern to match model files, by default None

Returns:

The latest version number of the model, or 0 if none exists.

Return type:

int

get_model_info() dict[str, Any]

Retrieves information about the model, including its name, number of parameters, and training history.

Returns:

A dictionary containing the model’s name, number of parameters, and training history.

Return type:

dict[str, Any]

get_model_version(model_dir: Path | str, model_name: str, version: int | None = None) tuple[Model, History | None, int]

Retrieves a specific version of the model and its training history.

Parameters:
  • model_dir (Path | str) – The directory containing the saved model.

  • model_name (str) – The name of the model.

  • version (int | None, optional) – The version number of the model to load. If None, loads the latest version, by default None

Returns:

  • keras.Model – The loaded Keras model.

  • keras.callbacks.History | None – The training history of the model, if available. If not available, returns None.

  • int – The version number of the loaded model.

Raises:

FileNotFoundError – If no saved model is found for the specified model name.

load_model_version(model_dir: Path | str, model_name: str, version: int | None = None)

Loads a specific version of the model and its training history into the DNNManager.

Parameters:
  • model_dir (Path | str) – The directory containing the saved model.

  • model_name (str) – The name of the model.

  • version (int | None, optional) – The version number of the model to load. If None, loads the latest version, by default None

save_model_version(model: Model, history: History, model_dir: Path | str, model_name: str, version: int) dict[str, Path]

Save a specific version of the model and its training history.

Parameters:
  • model (keras.Model) – The model to save.

  • history (keras.callbacks.History) – The training history to save.

  • model_dir (Path | str) – The directory to save the model in.

  • model_name (str) – The name of the model.

  • version (int) – The version number of the model.

Returns:

A dictionary containing the paths for the saved files.

Return type:

dict[str, Path]