23 karma · joined March 18, 2016
F# provides all of them, is shipped with .Net Core 3.0 and also works great on Linux.
For example if you have f_{i,j} = (x_i)^2, then the derivative (of some loss) w.r.t. x will be: dx_i = \sum_j df_{i,j} 2 x_i. The sum is needed because the argument x does not depend on the index j and thus x_i is affected by all j elements of df_{i,j}.
Another example would be: f_i = (x_ii)^2, i.e. taking the squares of the diagonal of the matrix x. Here the derivative is x_{i,j} = kronecker_{i=j} 2 df_i x_ii because off-diagonal elements have zero derivatives.
For such simple expressions it's trivial, but for complex expressions it's error-prone when you do it by hand.
https://github.com/surban/TensorAlgDiff/raw/master/elemdiff....
Our system takes a tensor algebra (we call it element-wise defined tensor) and outputs expressions for the derivatives w.r.t. all of its arguments. The expression may contain sums and the indices of the argument tensors can be any linear combination of the function indices (see our example for more details). It correctly handles the cases where an index does not appear in an argument, appears twice, appears as (i+j) etc.
Code to play with at https://github.com/surban/TensorAlgDiff