Solution¶
This section documents the solution components of the neural_network 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.
TYPE:
|
|
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.
TYPE:
|
|
The ith element in the list represents the bias vector corresponding to layer i + 1.
TYPE:
|
|
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()
best_loss_
class-attribute
instance-attribute
¶
The minimum loss reached by the solver throughout fitting.
best_validation_score_
class-attribute
instance-attribute
¶
The best validation score (i.e. R2 score) that triggered the early stopping.
coefs_
class-attribute
instance-attribute
¶
coefs_: list[ndarray] = None
The ith element in the list represents the weight matrix corresponding to layer i.
feature_names_in_
class-attribute
instance-attribute
¶
feature_names_in_: ndarray = None
Names of features seen during fit.
from_dict
classmethod
¶
from_dict(data: dict[str, Any]) -> MLPRegressorSolution
Creates a MLPRegressorSolution instance from a dictionary.
PARAMETER | DESCRIPTION |
---|---|
|
Dictionary containing the model attributes.
TYPE:
|
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-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-scikit-learn/nextmv_sklearn/neural_network/solution.py
intercepts_
class-attribute
instance-attribute
¶
intercepts_: list[ndarray] = None
The ith element in the list represents the bias vector corresponding to layer i + 1.
loss_
class-attribute
instance-attribute
¶
The current loss computed with the loss function.
loss_curve_
class-attribute
instance-attribute
¶
Loss value evaluated at the end of each training step.
model_config
class-attribute
instance-attribute
¶
n_features_in_
class-attribute
instance-attribute
¶
Number of features seen during fit.
n_iter_
class-attribute
instance-attribute
¶
The number of iterations the solver has run.
out_activation_
class-attribute
instance-attribute
¶
Name of the output activation function.
t_
class-attribute
instance-attribute
¶
The number of training samples seen by the solver during fitting.
to_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-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)
Source code in nextmv-scikit-learn/nextmv_sklearn/neural_network/solution.py
validation_scores_
class-attribute
instance-attribute
¶
The score at each iteration on a held-out validation set.