MLPRegression
Single hidden-layer MLP regressor implemented in PyTorch.
A Multi-layer Perceptron (MLP) is a feedforward neural network composed of an input layer, one hidden layer of configurable width, and a single linear output neuron. The hidden layer uses a configurable activation function (ReLU, tanh, sigmoid, or identity). The network is trained by minimising the mean squared error using the Adam optimiser and mini-batch gradient descent via backpropagation.
Key hyperparameters include hidden_size (number of neurons in the hidden
layer), activation, learning_rate, epochs, batch_size, and
device (CPU or GPU). The model also supports per-epoch and per-step metric
logging. The implementation uses PyTorch (torch.nn).
References
- [1] Rumelhart, D.E., Hinton, G.E., & Williams, R.J. (1986). "Learning representations by back-propagating errors." Nature, 323(6088), 533-536. https://doi.org/10.1038/323533a0
- [2] Kingma, D.P. & Ba, J. (2015). "Adam: A Method for Stochastic Optimization." ICLR 2015. https://arxiv.org/abs/1412.6980
Parameters
- hidden_size : integer, default=
5 - Number of neurons in the hidden layer.
- activation : string, default=
relu - Activation function.
- learning_rate : number, default=
0.001 - Initial learning rate for the optimizer.
- epochs : integer, default=
5 - Total number of training passes over the dataset.
- batch_size, default=
32 - Number of samples per gradient update during training. If greater than dataset size or None, uses full dataset.
- device : string, default=
CPU - Hardware device (CPU/GPU).
- log_train_every_n_epochs, default=
1 - Log metrics for train split every n epochs during training. If None, it won't log per epoch.
- log_train_every_n_steps, default=
None - Log metrics for train split every n steps during training. If None, it won't log per step.
- log_validation_every_n_epochs, default=
1 - Log metrics for validation split every n epochs during training. If None, it won't log per epoch.
- log_validation_every_n_steps, default=
None - Log metrics for validation split every n steps during training. If None, it won't log per step.
Methods
load(filename: str) -> 'MLPRegression'
MLPRegressionRestore an MLPRegression instance from a saved checkpoint.
Parameters
- filename : str
- Path to the checkpoint file saved by
save.
Returns
- MLPRegression
- The restored model instance with loaded weights.
predict(self, x: 'DashAIDataset') -> 'ndarray'
MLPRegressionGenerate regression predictions for the input dataset.
Parameters
- x : DashAIDataset
- The input features to predict on.
Returns
- ndarray
- Predicted continuous values as a 1-D NumPy array.
save(self, filename: str) -> None
MLPRegressionSave the trained model weights and configuration to disk.
Parameters
- filename : str
- Path where the model checkpoint will be saved.
train(self, x_train: 'DashAIDataset', y_train: 'DashAIDataset', x_validation: 'DashAIDataset' = None, y_validation: 'DashAIDataset' = None) -> 'MLPRegression'
MLPRegressionTrain the MLP regressor using Adam optimiser and MSE loss.
Parameters
- x_train : DashAIDataset
- The input features for training.
- y_train : DashAIDataset
- The regression targets for training.
- x_validation : DashAIDataset, optional
- Input features for validation metric logging. Defaults to None.
- y_validation : DashAIDataset, optional
- Target values for validation metric logging. Defaults to None.
Returns
- MLPRegression
- The trained model instance (self).
calculate_metrics(self, split: DashAI.back.core.enums.metrics.SplitEnum = <SplitEnum.VALIDATION: 'validation'>, level: DashAI.back.core.enums.metrics.LevelEnum = <LevelEnum.LAST: 'last'>, log_index: int = None, x_data: 'DashAIDataset' = None, y_data: 'DashAIDataset' = None)
BaseModelCalculate and save metrics for a given data split and level.
Parameters
- split : SplitEnum
- The data split to evaluate (TRAIN, VALIDATION, or TEST). Defaults to SplitEnum.VALIDATION.
- level : LevelEnum
- The metric granularity level (LAST, TRIAL, STEP, or BATCH). Defaults to LevelEnum.LAST.
- log_index : int, optional
- Explicit step index for the metric entry. If None, the next step index is computed automatically. Defaults to None.
- x_data : DashAIDataset, optional
- Input features. If None, the dataset stored in the model for the given split is used. Defaults to None.
- y_data : DashAIDataset, optional
- Target labels. If None, the labels stored in the model for the given split are used. Defaults to None.
get_metadata(cls) -> Dict[str, Any]
BaseModelGet metadata values for the current model.
Returns
- Dict[str, Any]
- Dictionary containing UI metadata such as the model icon used in the DashAI frontend.
get_schema(cls) -> dict
ConfigObjectGenerates the component related Json Schema.
Returns
- dict
- Dictionary representing the Json Schema of the component.
prepare_dataset(self, dataset: 'DashAIDataset', is_fit: bool = False) -> 'DashAIDataset'
BaseModelHook for model-specific preprocessing of input features.
Parameters
- dataset : DashAIDataset
- The input dataset to preprocess.
- is_fit : bool
- Whether the call is part of a fitting phase. Defaults to False.
Returns
- DashAIDataset
- The preprocessed dataset ready to be fed into the model.
prepare_output(self, dataset: 'DashAIDataset', is_fit: bool = False) -> 'DashAIDataset'
BaseModelHook for model-specific preprocessing of output targets.
Parameters
- dataset : DashAIDataset
- The output dataset (target labels) to preprocess.
- is_fit : bool
- Whether the call is part of a fitting phase. Defaults to False.
Returns
- DashAIDataset
- The preprocessed output dataset.
validate_and_transform(self, raw_data: dict) -> dict
ConfigObjectIt takes the data given by the user to initialize the model and returns it with all the objects that the model needs to work.
Parameters
- raw_data : dict
- A dictionary with the data provided by the user to initialize the model.
Returns
- dict
- A validated dictionary with the necessary objects.