# (Almost) Differentiable Indexing of Tensors

#### Indexing tensors is at the core of numeros ML applications. In this post, I describe a strategy to index a tensor when we need to compute the gradient with respect to the index.

When indexing a tensor, we extract the portion of the tensor specified by the index. Using a programming-oriented notation, $\mathbf a[i]$ will be the i-th element of the tensor $\mathbf a$.

During differentiation, we compute the rate of change of a function with respect to its input (i.e. the derivative). In the case of indexing, this equates to *How would my output look if I take a tiny step from my current index?* The long story is that functions need to respect specific properties to be differentiable, and indexing does not respect them. The short one is that indices are integer values, so an *infinitesimally* small step from the current index would result in an ill-defined operation (which memory cell corresponds to $i+\mathrm{d}i$?).

With these ideas in mind, let’s explore a trick to index a tensor in a differentiable way. To do this, we consider the mathematical description of the indexing operation:

where $\mathbf a$ is the input tensor and $\mathbf w$ is the one-hot encoding of the index. That is, $\mathbf w$ is constrained (or quantized) to rows of the $N\times N$ identity matrix ($I_N$). Therefore, for any $\widehat{\mathbf w} \in \mathbb{R}^N$,

where $\mathbf{Q}(\cdot)$ is the quantization operation that snaps $\widehat{\mathbf w}$ to the closest row of $I_N$. There are many alternatives for the function $\mathbf{Q}(\cdot)$, and they depend on the definition of “closest.” Let’s define

That is, $\mathbf{Q}(\mathbf x)$ maps each vector $\mathbf x$ to the row of the identity matrix with the $1$ located in the position of the highest entry of $\mathbf x$. The rate of change of $\mathbf{Q}(\mathbf x)$ with respect to the entries of $\mathbf x$ will be zero because the output is constant for a given input (imagine a step function whose derivative is an impulse).

Let’s say we want to optimize an objective, $\mathcal{L}$, using gradient descent. To do this, we compute the gradient of $\mathcal{L}$ with respect to the weights of our model (in our case, the indexing vector, $\widehat{\mathbf w}$) and update the weights after each iteration as:

Since $\nabla_{\widehat{\mathbf w}} \mathcal{L} = \nabla_{\mathbf Q} \mathcal{L}\cdot \mathbf{J} (\mathbf{Q}) = 0$ we don’t update the weights. We can, however, use the Straight-Throught Estimation (STE) of the gradients. That is, we compute the gradient as if we did not quantize the vector

and use this gradient for the update in Eq. $5$.

In code, the indexing operation with STE would look like this for one-dimensional vectors:

1
2

def index_ste(T: Tensor, idx: Tensor) -> Tensor:
return T + (torch.eye(T.shape[0])[T.argmax()] - T).detach()

The role of `T`

in the return value is to instruct autograd to propagate the gradients through `T`

without the need to specify a custom backward pass.