A Mini PHP Example: Attention as Weights

A toy weighted sum of context words

We are not implementing a real transformer in PHP – that would be too heavy for a mini example. But the idea of attention can be shown as a weighted sum: neighboring words receive different weights, and the final vector is assembled from their values.

 
<?php

$context 
= [
    
'API' => 0.6,
    
'authentication' => 0.9,
    
'for' => 0.1,
];

$values = [
    
'API' => [1.00.2],
    
'authentication' => [0.90.8],
    
'for' => [0.10.0],
];

$result = [0.00.0];

foreach (
$context as $word => $weight) {
    
$result[0] += $weight $values[$word][0];
    
$result[1] += $weight $values[$word][1];
}

print_r($result);

Example:

Context weights:
API => 0.6
authentication => 0.9
for => 0.1

Word values:
API => [1, 0.2]
authentication => [0.9, 0.8]
for => [0.1, 0]
Result: Memory: 0.002 Mb Time running: < 0.001 sec.
Array
(
    [0] => 1.42
    [1] => 0.84
)