Created
July 20, 2019 17:47
-
-
Save ekmett/db3a17792b19f8e00c97bb49e4b54da4 to your computer and use it in GitHub Desktop.
checkpointing with 'ad'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# language RankNTypes #-} | |
{-# language FlexibleContexts #-} | |
{-# language ScopedTypeVariables #-} | |
import Numeric.AD.Internal.Identity | |
import Numeric.AD.Internal.Reverse | |
import Numeric.AD.Internal.Or | |
import Numeric.AD.Mode | |
import Numeric.AD.Mode.Reverse (grad) | |
import Data.Reflection (Reifies) | |
import Data.Foldable (toList) | |
import Data.Proxy | |
-- based on an implementation by Sofus Mortensen | |
grad_cp | |
:: forall f g a. (Traversable f, Traversable g, Num a) | |
=> (forall s. Reifies s Tape => f (Reverse s a) -> g (Reverse s a)) | |
-> (forall s. Reifies s Tape => g (Reverse s a) -> Reverse s a) | |
-> f a -> f a | |
grad_cp f g xs = grad (sum . zipWith ((*) . auto) z' . toList . f) xs where | |
z' = toList $ grad g fxs | |
fxs = reifyTape 0 $ \(_ :: Proxy s) -> primal <$> f (auto <$> xs :: f (Reverse s a)) -- should have an empty tape when done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!