A minimal PHP example (intuitive)

A simple convolution

PHP is not used for deep learning in production, but it is excellent for understanding.

Example of code:

 
<?php

function convolve($image, $kernel) {
    
$h = count($image);
    
$w = count($image[0]);

    
$kh = count($kernel);
    
$kw = count($kernel[0]);

    
$output = [];

    for (
$i = 0; $i <= $h - $kh; $i++) {
        for (
$j = 0; $j <= $w - $kw; $j++) {

            
$sum = 0;

            for (
$m = 0; $m < $kh; $m++) {
                for (
$n = 0; $n < $kw; $n++) {
                    
$sum += $image[$i + $m][$j + $n] * $kernel[$m][$n];
                }
            }

            
$output[$i][$j] = $sum;
        }
    }

    return 
$output;
}