Minimal MLP in PHP: standalone example
One hidden layer and forward pass (standalone run)
A simplified one-hidden-layer MLP without bias terms: this example demonstrates only the forward pass on an input vector.
Example of use
<?php
include 'code.php';
echo 'Minimal MLP (forward-pass only):' . PHP_EOL;
echo '-----------' . PHP_EOL;
$mlp = new SimpleMLP(2, 3, 0.01);
$input = [1.0, 0.5];
$output = $mlp->forward($input);
echo 'Input: [' . implode(', ', array_map(static fn (float $v): string => (string) $v, $input)) . ']' . PHP_EOL;
echo 'Hidden layer: ' . '[' . implode(', ', array_map(static fn (float $v): string => (string) round($v, 2), $mlp->getHidden())) . ']' . PHP_EOL;
echo 'Output: ' . number_format($output, 6, '.', '') . PHP_EOL;
Step-by-step forward-pass visualization
See how the signal flows from inputs to hidden neurons and then to output through each neuron step by step.
zᵢ = wᵢ1 · x1 + wᵢ2 · x2
hᵢ = ReLU(zᵢ)
ŷ = Σ(vᵢ · hᵢ)
Current step
-
x = [x1, x2]
[1.00, 0.50]
Hidden vector h
Output ŷ
0.0000
| # | Stage | Value |
|---|
Result:
Memory: 0.008 Mb
Time running: < 0.001 sec.
Minimal MLP (forward-pass only):
-----------
Input: [1, 0.5]
Hidden layer: [1.28, 0.03, 0.03]
Output: 0.143696