martinResearch / MatlabAutoDiff

A matlab implementation of forward automatic differentiation with operator overloading and sparse jacobians
BSD 3-Clause "New" or "Revised" License
39 stars 11 forks source link

Goal

This project implements a Matlab/Octave non-intrusive forward automatic differentiation method, (wikipedia definition here) based on operator overloading. This does not provide backward mode or higher order derivatives. It enables precise and efficient computation of the Jacobian of a function. This contrasts with numerical differentiation (a.k.a finite differences) that is unprecise due to roundoff errors and that cannot exploit the sparsity of the derivatives.

In contrast with most existing automatic differentiation Matlab toolboxes:

It is likely that the speed could be improved by representing Jacobian matrices by their transpose, due to the way Matlab represents internally sparse matrices. The document [1] describes a method similar to the one implemented here and could be a very valuable source to improve the code.

It has been tested on Matlab 2014a and Octave 4.0.0, but the example using the anonymous function @(x)eig(x) does not work on octave as octave does not call the overloaded eig function once anonymized.

Note that backward differentation (a.k.a. gradients back-propagation in deep learning) is going to me much faster than forward differentiation when the dimension of the output is small in comparison to the dimension of the input. Forward differentiation is of interest when solving a non linear least squares for examples through Levenberg-Marquardt minimization where we want to compute the full jacobian matrix of the residuals.

Licence

Free BSD License

Examples

more examples can be found in ./src/AutoDiffExamples.m


        >> f=@(x) [sin(x(1)) cos(x(2)) tan(x(1)*x(2))];
        >> JAD=full(AutoDiffJacobianAutoDiff(f,[1,1]))
        >> JFD=AutoDiffJacobianFiniteDiff(f,[1,1])
        >> JAD+i*JFD % comparing visualy using complex numbers
        >> fprintf('max abs difference = %e\n',full(max(abs(JAD(:)-JFD(:)))));
        ans =

           0.5403 + 0.5403i   0.0000 + 0.0000i
           0.0000 + 0.0000i  -0.8415 - 0.8415i
           3.4255 + 3.4255i   3.4255 + 3.4255i

        max absolute difference = 1.047984e-10
* a simple images denoising example using a total variation (TV) regularization can be found in  [./src/AutoDiffExamples.m](./src/exampleDenoise.m)
```c
        f=@(x) sum(x.^2,3);
        AutoDiffJacobianFiniteDiff(f,ones(2,2,2))
        Inoisy=zeros(100,100)*0.2;
        Inoisy(30:70,30:70)=0.8 
        Inoisy=Inoisy+randn(100,100)*0.3
        imshow(Inoisy)
        epsilon=0.0001

        %define the objective function using an anonymous function
        func=@(I) sum(reshape((Inoisy-I).^2,1,[]))+...
        sum(reshape(sqrt(epsilon+diff(I(:,1:end-1),1,1).^2+...
        diff(I(1:end-1,:),1,2).^2),1,[]))
        func(Inoisy)
        speed=zeros(numel(Inoisy),1);
        % heavy ball gradient descent, far from the best optimization method but simple
        for k=1:500
              [J,f]=AutoDiffJacobian(func,Inoisy);
              speed=0.95*speed-0.05*J';
              Inoisy(:)=Inoisy(:)+0.05*speed;
              imshow(Inoisy);
        end
* [madiff](https://github.com/gaika/madiff)
  Reverse mode AD using operator overloading. Operators like transpose are not coded yet  at the date of july 2016 . This will fail
```c        
        f=@(x)(sum(x'*x))
        f(rand(20,1))
        f_grad = @(x) adiff(f, x);
        f_grad(rand(20,1))

Projects that use this library

Please add a comment in this issue if you which to add you project in this listing. I am very interested in knowing what it has been used for.

References

[1] Forth, Shaun A. An Efficient Overloaded Implementation of Forward Mode Automatic Differentiation in MATLAB ACM Trans. Math. Softw. 2006 pdf