Solution Module¶
Reference to the neural_network.solution.py module.
solution
¶
Defines sklearn.neural_network solution interoperability.
This module provides functionality for interacting with scikit-learn's neural network models.
| CLASS | DESCRIPTION |
|---|---|
MLPRegressorSolution |
A Pydantic model representation of scikit-learn's MLPRegressor. |
Variables
Loss An annotated type for handling loss values in scikit-learn models.
Loss
module-attribute
¶
Loss = Annotated[
Any,
BeforeValidator(lambda x: x),
PlainSerializer(lambda x: b64encode(dumps(x))),
]
Annotated type for serialization and validation of scikit-learn loss objects.
This type encodes a pickle serialized representation of a loss object as a base64 string, to ensure that loss objects can be safely serialized and deserialized.
MLPRegressorSolution
¶
Bases: BaseModel
MLP Regressor scikit-learn model representation.
You can import the MLPRegressorSolution class directly from neural_network:
This class provides a Pydantic model representation of scikit-learn's MLPRegressor model, enabling serialization, deserialization, and conversion between model formats.
| PARAMETER | DESCRIPTION |
|---|---|
|
The current loss computed with the loss function.
TYPE:
|
|
The minimum loss reached by the solver throughout fitting.
TYPE:
|
|
Loss value evaluated at the end of each training step.
TYPE:
|
|
The score at each iteration on a held-out validation set. |
|
The best validation score (i.e. R2 score) that triggered the early stopping.
TYPE:
|
|
The number of training samples seen by the solver during fitting.
TYPE:
|
|
The ith element in the list represents the weight matrix corresponding to layer i. |
|
The ith element in the list represents the bias vector corresponding to layer i + 1. |
|
Number of features seen during fit.
TYPE:
|
|
Names of features seen during fit.
TYPE:
|
|
The number of iterations the solver has run.
TYPE:
|
|
Number of layers.
TYPE:
|
|
Number of outputs.
TYPE:
|
|
Name of the output activation function.
TYPE:
|
Examples:
>>> from sklearn.neural_network import MLPRegressor
>>> from nextmv_sklearn.neural_network import MLPRegressorSolution
>>>
>>> # Create and train a sklearn MLPRegressor
>>> regressor = MLPRegressor(hidden_layer_sizes=(100, 50), max_iter=500)
>>> regressor.fit(X_train, y_train)
>>>
>>> # Convert to MLPRegressorSolution for serialization
>>> solution = MLPRegressorSolution.from_model(regressor)
>>> solution_dict = solution.to_dict()
>>>
>>> # Later, recreate the solution and convert back to sklearn model
>>> restored_solution = MLPRegressorSolution.from_dict(solution_dict["attributes"])
>>> restored_model = restored_solution.to_model()
model_config
class-attribute
instance-attribute
¶
loss_
class-attribute
instance-attribute
¶
loss_: float = 0.0
The current loss computed with the loss function.
best_loss_
class-attribute
instance-attribute
¶
best_loss_: float = 0.0
The minimum loss reached by the solver throughout fitting.
loss_curve_
class-attribute
instance-attribute
¶
loss_curve_: list[float64] = None
Loss value evaluated at the end of each training step.
validation_scores_
class-attribute
instance-attribute
¶
The score at each iteration on a held-out validation set.
best_validation_score_
class-attribute
instance-attribute
¶
best_validation_score_: float | None = None
The best validation score (i.e. R2 score) that triggered the early stopping.
t_
class-attribute
instance-attribute
¶
t_: int = 0
The number of training samples seen by the solver during fitting.
coefs_
class-attribute
instance-attribute
¶
The ith element in the list represents the weight matrix corresponding to layer i.
intercepts_
class-attribute
instance-attribute
¶
The ith element in the list represents the bias vector corresponding to layer i + 1.
n_features_in_
class-attribute
instance-attribute
¶
n_features_in_: int = 0
Number of features seen during fit.
feature_names_in_
class-attribute
instance-attribute
¶
feature_names_in_: ndarray = None
Names of features seen during fit.
n_iter_
class-attribute
instance-attribute
¶
n_iter_: int = 0
The number of iterations the solver has run.
out_activation_
class-attribute
instance-attribute
¶
out_activation_: str = None
Name of the output activation function.
from_dict
classmethod
¶
from_dict(data: dict[str, Any]) -> MLPRegressorSolution
Creates a MLPRegressorSolution instance from a dictionary.
| PARAMETER | DESCRIPTION |
|---|---|
|
Dictionary containing the model attributes. |
| RETURNS | DESCRIPTION |
|---|---|
MLPRegressorSolution
|
Instance of MLPRegressorSolution. |
Examples:
>>> solution_dict = {"loss_": 0.15, "n_layers_": 3, "n_outputs_": 1}
>>> solution = MLPRegressorSolution.from_dict(solution_dict)
Source code in nextmv-py/nextmv-scikit-learn/nextmv_sklearn/neural_network/solution.py
from_model
classmethod
¶
from_model(model: MLPRegressor) -> MLPRegressorSolution
Creates a MLPRegressorSolution instance from a scikit-learn MLPRegressor model.
| PARAMETER | DESCRIPTION |
|---|---|
|
scikit-learn MLPRegressor model.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MLPRegressorSolution
|
Instance of MLPRegressorSolution. |
Examples:
>>> from sklearn.neural_network import MLPRegressor
>>> regressor = MLPRegressor().fit(X, y)
>>> solution = MLPRegressorSolution.from_model(regressor)
Source code in nextmv-py/nextmv-scikit-learn/nextmv_sklearn/neural_network/solution.py
to_dict
¶
to_dict() -> dict
Convert a data model instance to a dict with associated class info.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Dictionary containing class information and model attributes. |
Examples:
>>> solution = MLPRegressorSolution.from_model(regressor)
>>> solution_dict = solution.to_dict()
>>> print(solution_dict["class"]["name"])
'MLPRegressorSolution'
Source code in nextmv-py/nextmv-scikit-learn/nextmv_sklearn/neural_network/solution.py
to_model
¶
Transforms the MLPRegressorSolution instance into a scikit-learn MLPRegressor model.
| RETURNS | DESCRIPTION |
|---|---|
MLPRegressor
|
scikit-learn MLPRegressor model. |
Examples:
>>> solution = MLPRegressorSolution.from_dict(solution_data)
>>> model = solution.to_model()
>>> predictions = model.predict(X_test)