adtzlr / tensortrax

Differentiable Tensors based on NumPy Arrays

Home Page:https://tensortrax.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mixed partial derivatives

adtzlr opened this issue · comments

For a given function

$$f = f(x, y)$$

try to implement

$$∂(∂f / ∂x) / ∂y$$

I fear that with the current concept this is only possible by combining the unknowns, evaluate the hessian and split the result.

This is possible via manual scripting. It is realized by deactivating one dual component per variable.

import numpy as np

import tensortrax as tr
import tensortrax.math as tm

x = (np.eye(3).ravel() + np.arange(9)).reshape(3, 3)
y = (np.eye(3).ravel() + np.arange(10, 19)).reshape(3, 3)

fun = lambda x, y: tm.trace(x) * tm.linalg.det(y)

r = tr.Tensor(x)
r.init(hessian=True, Δx=False)

s = tr.Tensor(y)
s.init(hessian=True, δx=False)

f = fun(r, s)
dfdxdy = tr.Δδ(f)

Dfdxdy = np.einsum(
    "ij...,kl...->ijkl...", np.eye(3), np.linalg.det(y) * np.linalg.inv(y).T
)

assert np.allclose(dfdxdy, Dfdxdy)