autodiff icon indicating copy to clipboard operation
autodiff copied to clipboard

Custom Matrix Ops

Open maka89 opened this issue 3 years ago • 0 comments

Hi! Is it possible to write custom operations for autodiff in the reverse mode?

Consider for instance the matrix inverse. Backpropagating through this operation explicitly would create a very long computational graph and be impractical. But you can write a "matrix operation" that does this easily(see below, hope python syntax is ok).

class Inverse:

    def forward(self,X):
        self.inp=X
        self.out=np.linalg.inv(X)
        return self.out
    def backward(self,err):
        return -np.dot(self.out.T,np.dot(err,self.out.T))

maka89 avatar Dec 09 '22 13:12 maka89