Let say I have a matrix W of shape (n_words, model_dim) where n_words is the number of words in a sentence and model_dim is the dimension of the space where the word vectors are represented. What is the fastest way to compute the moving average of these vectors ?
For example, with a window size of 2 (window length = 5), I could have something like this (which raises an error TypeError: JAX 'Tracer' objects do not support item assignment):
from jax import random
import jax.numpy as jnp
# Fake word vectors (17 words vectors of dimension 32)
W = random.normal(random.PRNGKey(0), shape=(17, 32))
ws = 2 # window size
N = W.shape[0] # number of words
new_W = jnp.zeros(W.shape)
for i in range(N):
window = W[max(0, i-ws):min(N, i+ws+1)]
n = window.shape[0]
for j in range(n):
new_W[i] += W[j] / n
I guess there is a faster solution with jnp.convolve but I'm not familiar with it.