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.

Example of code:

 
<?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];