A factorization machine scores a feature vector with a bias, linear terms and all pairwise interactions, each interaction weight factorised as an inner product of -dimensional vectors (rows of , shape (D, K)):
. Implement:
fm_naive(x, mu, w, V): the double loop over pairs, .
fm_fast(x, mu, w, V): the same value in by rewriting the pairwise sum through the per-factor sums and . Also accept a batch: if x is 2-D (B, D), return an array of B scores.
fm_grads(x, mu, w, V): for a single 1-D x, a dict with the gradients of : "mu" (a float), "w" (shape (D,)) and "V" (shape (D, K)).
Scalar outputs are Python floats.