User-Defined Functions/User-defined function examples
This page collects examples of function implementations for educational purposes, to illustrate various concepts related to parameter declaration, syntax, array abstraction, etc.
Non-negative matrix factorization
Also known as positive matrix factorization.
Given a source matrix of non-negative values, [math]\displaystyle{ A }[/math], decompose it into two matrices [math]\displaystyle{ W }[/math] and [math]\displaystyle{ H }[/math] so that [math]\displaystyle{ A=WH }[/math]. These new matrices have a new latent dimension not shared by A, which is usually smaller that either index of A.
Function NMF( a : NonNegative [I,J] ; I,J,K : Index ; iterations : number atom=30) ::=
Local w[I, K] := Random(over:I,K);
Local h[K, J] := Random(over:K,J);
Local ϵ := 1e-9; { small epsilon to avoid division by zero }
For n:=1..iterations Do (
{ Update H based on W }
h := Relu(h * (Sum(w*a, I) / Max([ϵ,Sum( w * Sum(w*h, K),I)])));
{ Update W based on H }
w := Relu(w * (Sum(a*h, J) / Max([ϵ,Sum( Sum(w*h,K) * h, J)])));
);
_(w,h)
Download: Non-Negative Matrix Factorization.ana
Comments
Enable comment auto-refresher