Calculating the index of a tensor

For my neural net classes, i have a Tensor class that acts as value storage. It actually has only a one-dimensional array, the multidimensionality is achieved with calculating an index value out of a desired number of values, the number of values being the dimension of the Tensor.

Let's say we have a \$n\$ dimensional tensor \$T\$, so each tensor dimension has a certain value dimension \$d_i\$ . For example for a \$3\times 4\times 5\$ Tensor, \$\vec{d}$$ = \{3,4,5\}\$ .

First we need a one dimensional array \$\vec a\$ that has enough values to hold all the values of the Tensor, so \$\vec a\$ needs to hold $n_v = \prod_{i=0}^{n}$$ d_i$ values.

If we have a location \$\vec l\$ in the Tensor, we need to obtain the location \$l_a\$ in the array \$\vec a\$.
In a 2-dimensional setting, it's:
$l_a = l_1 + d_1l_2$ .
I guess that in a 3-dimensional setting, it is: 
$l_a = l_1 + d_1 \cdot (l_2 + d_2l_3) = l_1 + d_1l_2 + d_1d_2l_3$ ,
So in a $n$-dimensional setting, it'd be:
$l_a = l_1 + d_1l_2 + d_1d_2l_3 + \dots + l_n \cdot d_1 \cdot (\dots) \cdot d_{n-1}  = l_1 + \sum_{i=2}^{n}  (\prod_{j =1}^{i-1} d_j) \cdot l_i$


Kommentare

Beliebte Posts aus diesem Blog

Using a midi keyboard with minimum latency in Ubuntu 16.04