MultilayerPerceptronClassifier (Neural Network) classifier implementation.
Classes
class algorithms.neural.multilayer_perceptron.MultilayerPerceptronClassifier(Classifier)
Multilayer Perceptron (Neural Network) classifier.
__init__( self, hidden_layers: List[int] = None, learning_rate: float = 0.1, momentum: float = 0.9, max_epochs: int = 500, validation_threshold: int = 20, decay: bool = True, activation: str = 'relu', random_state: Optional[int] = None, )
Overview
The Multilayer Perceptron trains by iteratively adjusting weights through forward and backward passes:
- Initialize weights using Xavier initialization for each layer
- Standardize input features to zero mean and unit variance
- Perform a forward pass: propagate inputs through hidden layers
- Compute the cross-entropy loss between predictions and targets
- Perform a backward pass (backpropagation): compute gradients of the
- Update weights using gradient descent with momentum
- Optionally decay the learning rate over epochs
-
Stop early if the loss does not improve for
validation_threshold
Theory
For a network with L layers, the forward pass computes activations layer by layer. For hidden layer l:
where g is the activation function (sigmoid or ReLU). The output layer uses softmax:
The network is trained by minimizing the cross-entropy loss:
Weights are updated using momentum-based gradient descent:
where \mu is the momentum and \eta is the learning rate.
Parameters
hidden_layers
learning_rate
momentum
max_epochs
validation_threshold
decay
activation
random_state
Attributes
weights_
biases_
classes_
fit.
Notes
Complexity:
- Training: O(n \cdot E \cdot \sum_{l=1}^{L} d_l \cdot d_{l+1})
- Prediction: O(n \cdot \sum_{l=1}^{L} d_l \cdot d_{l+1}) per
When to use MultilayerPerceptronClassifier:
- When the decision boundary is non-linear
- When you need a flexible model that can approximate complex functions
- For problems like XOR that are not linearly separable
- When sufficient training data is available to fit the network parameters
- As a lightweight alternative to deep learning frameworks
References
Train a Multilayer Perceptron on the XOR problem:
>>> from tuiml.algorithms.neural import MultilayerPerceptronClassifier
>>> import numpy as np
>>> X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
>>> y = np.array([0, 1, 1, 0]) # XOR problem
>>> clf = MultilayerPerceptronClassifier(hidden_layers=[5], activation='relu')
>>> clf.fit(X, y)
MultilayerPerceptronClassifier(layers=[5])
Methods
get_parameter_schema
(cls) -> Dict[str, Dict[str, Any]]
get_capabilities
(cls) -> List[str]
get_complexity
(cls) -> str
get_references
(cls) -> List[str]
fit
(self, X: np.ndarray, y: np.ndarray) -> 'MultilayerPerceptronClassifier'
fit
(self, X: np.ndarray, y: np.ndarray) -> 'MultilayerPerceptronClassifier'
Fit the MultilayerPerceptronClassifier classifier.
Parameters
X
y
Returns
self
partial_fit
(self, X: np.ndarray, y: np.ndarray, classes: Optional[np.ndarray]=None) -> 'MultilayerPerceptronClassifier'
partial_fit
(self, X: np.ndarray, y: np.ndarray, classes: Optional[np.ndarray]=None) -> 'MultilayerPerceptronClassifier'
Incrementally fit the MultilayerPerceptronClassifier classifier on a batch of samples.
Parameters
X
y
classes
Returns
self
__repr__
(self) -> str
class algorithms.neural.multilayer_perceptron.MultilayerPerceptronRegressor(Regressor)
Multilayer Perceptron (Neural Network) regressor.
__init__( self, hidden_layers: List[int] = None, learning_rate: float = 0.1, momentum: float = 0.9, max_epochs: int = 500, validation_threshold: int = 20, decay: bool = True, activation: str = 'relu', random_state: Optional[int] = None, )
Overview
The Multilayer Perceptron regressor trains by iteratively adjusting weights through forward and backward passes:
- Initialize weights using Xavier initialization for each layer
- Standardize input features and target values to zero mean and unit variance
- Perform a forward pass: propagate inputs through hidden layers
- Compute the MSE loss between predictions and targets
- Perform a backward pass (backpropagation): compute gradients of the
- Update weights using gradient descent with momentum
- Optionally decay the learning rate over epochs
-
Stop early if the loss does not improve for
validation_threshold
Theory
For a network with L layers, the forward pass computes activations layer by layer. For hidden layer l:
where g is the activation function (sigmoid or ReLU). The output layer uses a linear activation (identity function):
The network is trained by minimizing the mean squared error loss:
Parameters
hidden_layers
learning_rate
momentum
max_epochs
validation_threshold
decay
activation
random_state
Attributes
weights_
biases_
Notes
Complexity:
- Training: O(n \cdot E \cdot \sum_{l=1}^{L} d_l \cdot d_{l+1})
- Prediction: O(n \cdot \sum_{l=1}^{L} d_l \cdot d_{l+1})
- When the relationship between features and target is non-linear
- When you need a flexible model for continuous value prediction
- When sufficient training data is available to fit the network
- As a lightweight alternative to deep learning frameworks
References
See Also
Train a Multilayer Perceptron regressor on a simple problem:
>>> from tuiml.algorithms.neural import MultilayerPerceptronRegressor
>>> import numpy as np
>>> X = np.array([[1], [2], [3], [4], [5]])
>>> y = np.array([1.0, 4.0, 9.0, 16.0, 25.0])
>>> reg = MultilayerPerceptronRegressor(hidden_layers=[10], activation='relu')
>>> reg.fit(X, y)
MultilayerPerceptronRegressor(layers=[10])
Methods
fit
(self, X: np.ndarray, y: np.ndarray) -> 'MultilayerPerceptronRegressor'
fit
(self, X: np.ndarray, y: np.ndarray) -> 'MultilayerPerceptronRegressor'
Fit the MultilayerPerceptronRegressor.
Parameters
X
y
Returns
self
partial_fit
(self, X: np.ndarray, y: np.ndarray) -> 'MultilayerPerceptronRegressor'
partial_fit
(self, X: np.ndarray, y: np.ndarray) -> 'MultilayerPerceptronRegressor'
Incrementally fit the MultilayerPerceptronRegressor regressor on a batch of samples.
Parameters
X
y
Returns
self