How it looks in pure PHP

A minimal one-neuron backpropagation example in PHP

A minimal example of a single neuron. Everything we just computed by hand is now written as code. There is no extra logic in the program – the same formulas are simply expressed in PHP.

 
<?php

include 'code.php';

// Data and initial parameters.
$x = 2;          // input
$w = 0.5;        // weight
$y = 1;          // expected result
$lr = 0.1;

// 1. Forward pass
$z = $w * $x;
$a = sigmoid($z);
$loss = pow($a - $y, 2);

// 2. Backward pass
// Chain rule:
// dL/dw = dL/da × da/dz × dz/dw
$dL_da = 2 * ($a - $y);
$da_dz = sigmoid_derivative($a);
$dz_dw = $x;

$gradient = $dL_da * $da_dz * $dz_dw;

// 3. Weight update with gradient descent.
$w = $w - $lr * $gradient;

echo 
'Loss: ' . $loss . PHP_EOL;
echo 
'Gradient: ' . $gradient . PHP_EOL;
echo 
'New weight: ' . $w;

Example:

x = 2
w = 0.5
y = 1
lr = 0.1
Result: Memory: 0.001 Mb Time running: < 0.001 sec.
Loss: 0.072329488128513
Gradient: -0.21150837113707
New weight: 0.52115083711371

This is backpropagation – in a little more than ten lines of PHP. Most machine-learning libraries do exactly the same thing, only automatically and for millions of parameters at once.