pyopmnearwell.ml.nn module
Transform ensemble data into datasets and train neural networks.
- pyopmnearwell.ml.nn.build_model(hp: HyperParameters, ninputs: int, noutputs: int, lr_tune: float = 0.1) Module
Build and compile a FCNN with the given hyperparameters.
- Args:
hp (keras_tuner.Hyperparameters): Hyperparameters object. ninputs (int): Number of inputs. noutputs (int): Number of outputs.
- Returns:
tf.Module: The built neural network model.
- pyopmnearwell.ml.nn.get_FCNN(ninputs: int, noutputs: int, depth: int = 5, hidden_dim: int = 10, saved_model: str | None = None, activation: Literal['sigmoid', 'relu', 'tanh'] = 'sigmoid', kernel_initializer: Literal['glorot_normal', 'glorot_uniform'] = 'glorot_normal', normalization: bool = False) Model
Return a fully connected neural network with the specified architecture.
- Args:
ninputs (int): Number of inputs to the model. noutputs (int): Number of outputs from the model. depth (int, optional): Number of hidden layers in the model. Defaults to 5. hidden_dim (int, optional): Number of neurons in each hidden layer. Defaults to
- saved_model (str, optional): Path to a saved model to load weights from.
Defaults to None.
- activation (Literal[“sigmoid”, “relu”, “tanh”], optional): Activation function
to use in the hidden layers. Defaults to “sigmoid”.
- kernel_initializer (Literal[“glorot_normal”, “glorot_uniform”], optional):
Weight initialization method to use in the hidden layers. Defaults to “glorot_normal”.
- normalization (bool, optional): Whether to use batch normalization in the model.
Defaults to False.
- Returns:
keras.Model: A fully connected neural network.
- pyopmnearwell.ml.nn.get_GRU(ninputs: int, noutputs: int, units: int = 20, saved_model: str | None = None, activation: Literal['sigmoid', 'relu', 'tanh'] = 'tanh', kernel_initializer: Literal['glorot_normal', 'glorot_uniform'] = 'glorot_uniform') Model
Return a recurrent neural network with the specified architecture.
- Args:
ninputs (int): Number of inputs to the model. noutputs (int): Number of outputs from the model. units (int, optional): Size of internal model state. Defaults to 20. hidden_dim (int, optional): Number of neurons in each hidden layer. Defaults to
- saved_model (str, optional): Path to a saved model to load weights from.
Defaults to None.
- activation (Literal[“sigmoid”, “relu”, “tanh”], optional): Activation function
to use in the hidden layers. Defaults to “sigmoid”.
- kernel_initializer (Literal[“glorot_normal”, “glorot_uniform”], optional):
Weight initialization method to use in the hidden layers. Defaults to “glorot_normal”.
- Returns:
keras.Model: A fully connected neural network.
- pyopmnearwell.ml.nn.get_LSTM(ninputs: int, noutputs: int, units: int = 20, saved_model: str | None = None, activation: Literal['sigmoid', 'relu', 'tanh'] = 'tanh', kernel_initializer: Literal['glorot_normal', 'glorot_uniform'] = 'glorot_uniform') Model
Return a recurrent neural network with the specified architecture.
- Args:
ninputs (int): Number of inputs to the model. noutputs (int): Number of outputs from the model. units (int, optional): Size of internal model state. Defaults to 20. hidden_dim (int, optional): Number of neurons in each hidden layer. Defaults to
- saved_model (str, optional): Path to a saved model to load weights from.
Defaults to None.
- activation (Literal[“sigmoid”, “relu”, “tanh”], optional): Activation function
to use in the hidden layers. Defaults to “sigmoid”.
- kernel_initializer (Literal[“glorot_normal”, “glorot_uniform”], optional):
Weight initialization method to use in the hidden layers. Defaults to “glorot_normal”.
- Returns:
keras.Model: A fully connected neural network.
- pyopmnearwell.ml.nn.get_RNN(ninputs: int, noutputs: int, units: int = 20, saved_model: str | None = None, activation: Literal['sigmoid', 'relu', 'tanh'] = 'tanh', kernel_initializer: Literal['glorot_normal', 'glorot_uniform'] = 'glorot_uniform') Model
Return a recurrent neural network with the specified architecture.
- Args:
ninputs (int): Number of inputs to the model. noutputs (int): Number of outputs from the model. units (int, optional): Size of internal model state. Defaults to 20. hidden_dim (int, optional): Number of neurons in each hidden layer. Defaults to
- saved_model (str, optional): Path to a saved model to load weights from.
Defaults to None.
- activation (Literal[“sigmoid”, “relu”, “tanh”], optional): Activation function
to use in the hidden layers. Defaults to “sigmoid”.
- kernel_initializer (Literal[“glorot_normal”, “glorot_uniform”], optional):
Weight initialization method to use in the hidden layers. Defaults to “glorot_normal”.
- Returns:
keras.Model: A fully connected neural network.
- pyopmnearwell.ml.nn.handle_zeros_in_scale(scale: Tensor | ndarray) ndarray
Set scales of near constant features to 1.
Note: This behavior is in line with sklearn.preprocessing.MinMaxScaler.
- Args:
scale (ArrayLike): The scale array.
- Returns:
np.ndarray: The modified scale array.
- pyopmnearwell.ml.nn.save_tune_results(tuner: Tuner, savepath: str | Path) None
- pyopmnearwell.ml.nn.scale_and_evaluate(model: Model, model_input: Tensor | ndarray, scalingsfile: str | Path) Tensor
Scale the input, evaluate with the model and scale the output.
- Args:
model (tf.keras.Model): A Keras model to evaluate the input with. model_input (ArrayLike): Input tensor. Can be a batch. scalingsfile (str | pathlib.Path): The path to the CSV file containing the
scaling parameters for MinMaxScaling.
- Returns:
tf.Tensor: The model’s output, scaled back to the original range.
- Raises:
FileNotFoundError: If
scalingsfile
does not exist. ValueError: Ifscalingsfile
contains an invalid row.
- pyopmnearwell.ml.nn.scale_and_prepare_dataset(dsfile: str | Path, feature_names: list[str], savepath: str | Path, train_split: float = 0.9, val_split: float | None = 0.1, test_split: float | None = None, shuffle: Literal['first', 'last', 'false'] = 'first', feature_range: tuple[float, float] = (-1, 1), target_range: tuple[float, float] = (-1, 1), scale: bool = True, **kwargs) tuple[tuple[ndarray, ndarray], tuple[ndarray, ndarray]] | tuple[tuple[ndarray, ndarray], tuple[ndarray, ndarray], tuple[ndarray, ndarray]]
Scale, shuffle and split a dataset.
- Args:
dsfile (str | pathlib.Path): Dataset file. feature_names (list[str]): List of feature names. savepath (pathlib.Path): Savepath for the scaling values. train_split (float, optional): Train split. Defaults to 0.9. val_split (float, optional): Val split. Defaults to 0.1. test_split (float, optional): Test split. Defaults to None. shuffle (Literal[“first”, “last”, “false”], optional): Options for shuffling the
dataset: - “first”: The dataset gets shuffled before the split. - “last”: The dataset gets shuffled after the split. - “false”: The dataset does not get shuffled. Defaults to “first”.
- feature_range (tuple[float, float], optional): Target range of feature scaling.
Defaults to (-1, 1).
- target_range (tuple[float, float], optional): Target range of target scaling.
Defaults to (-1, 1)
scale (bool, optional): Whether to scale the dataset. Defaults to True.
- Returns:
tuple[tuple[np.ndarray, np.ndarray], tuple[np.ndarray, np.ndarray]] | tuple[
tuple[np.ndarray, np.ndarray], tuple[np.ndarray, np.ndarray], tuple[np.ndarray, np.ndarray],
- ]: Tuple of scaled and split dataset. Includes test set only if
test_split > 0
.
- pyopmnearwell.ml.nn.train(model: Model, train_data: tuple[Tensor | ndarray, Tensor | ndarray], val_data: tuple[Tensor | ndarray, Tensor | ndarray], savepath: str | Path, lr: float = 0.1, epochs: int = 500, bs: int = 64, patience: int = 100, lr_patience: int = 10, kerasify: bool = True, loss_func: Literal['mse', 'MeanAbsolutePercentageError', 'MeanSquaredLogarithmicError'] = 'mse', recompile_model: bool = True, **kwargs) None
Train a tensorflow model on the provided training data and save the best model.
- Args:
model (tf.Module): Model to be trained. train_data (tuple[ArrayLike, ArrayLike]): Training features and targets. val_data (tuple[ArrayLike, ArrayLike]): Validation features and targets. savepath (pathlib.Path): Savepath for models and logging. lr (float, optional): Initial learning rate. Defaults to 0.1. epochs (_type_, optional): Training epochs. Defaults to 500. bs (int, optional): Batch size. Defaults to 64. patience (int, optional): Number of epochs without improvement before early
stopping. Defaults to 100.
- lr_patience (int, optional): Number of epochs without improvement before lr
decay. Defaults to 10.
- kerasify (bool, optional): Export the best model with kerasify after training.
Defaults to True.
- loss_func (Literal[“mse”, “MeanAbsolutePercentageError”,
“MeanSquaredLogarithmicError”], optional): Loss function. Defaults to “mse”.
- recompile_model (bool, optional): Whether to recompile the model before
training. Can e.g., be set to false, if the model is built and compiled by a different function. Defaults to True.
**kwargs: Get passed to the
model.fit()
method.- Returns:
None
- pyopmnearwell.ml.nn.tune(ninputs: int, noutputs: int, train_data: tuple[Tensor | ndarray, Tensor | ndarray], val_data: tuple[Tensor | ndarray, Tensor | ndarray], savepath: str | Path, objective: Literal['loss', 'val_loss'] = 'val_loss', max_trials: int = 5, executions_per_trial: int = 1, sample_weight: Tensor | ndarray = array([1.]), lr_tune: float = 0.1, **kwargs) tuple[Model, Tuner]
Tune the hyperparameters of a neural network model using random search.
- Args:
ninputs (int): Number of input features to the model. noutputs (int): Number of output features to the model. train_data (tuple[ArrayLike, ArrayLike]): Tuple of training input and target
data.
- val_data (tuple[ArrayLike, ArrayLike],): Tuple of validation input and target
data.
- objective (Literal[“loss”, “val_loss”], optional): Objective for search.
Defaults to
"val_loss"
.
max_trials (int): Default is 5. executions_per_trial (int): Default is 1. sample_weight:(ArrayLike): Default is
np.array([1.0])
. **kwargs: Get passed to the tuner’s search method.- Returns:
tf.Module: The model compiled with the best hyperparameters. keras_tuner.Tuner: The tuner.
- Raises:
ValueError: If train_data or val_data is not a tuple of two tensors.