packages feed

ad 4.2.0.1 → 4.2.1

raw patch · 6 files changed

+42/−9 lines, 6 filesdep −mtldep −template-haskelldep ~transformers

Dependencies removed: mtl, template-haskell

Dependency ranges changed: transformers

Files

.gitignore view
@@ -11,3 +11,4 @@ *.hi *~ *#+*.imports
.travis.yml view
@@ -7,11 +7,14 @@   - travis/cabal-apt-install $mode  install:+  - cabal install packunused packdeps   - cabal configure -flib-Werror $mode-  - cabal build+  - cabal build --ghc-options=-ddump-minimal-imports  script:   - $script+  - packdeps ad.cabal+  - packunused   - hlint src --cpp-define HLINT --cpp-include include  notifications:
CHANGELOG.markdown view
@@ -1,3 +1,7 @@+4.2.1+-----+* Added `stochasticGradientDescent`.+ 4.2 --- * Removed broken `Directed` mode.
ad.cabal view
@@ -1,5 +1,5 @@ name:         ad-version:      4.2.0.1+version:      4.2.1 license:      BSD3 license-File: LICENSE copyright:    (c) Edward Kmett 2010-2014,@@ -109,13 +109,13 @@     data-reify       >= 0.6   && < 0.7,     erf              >= 2.0   && < 2.1,     free             >= 4.6.1 && < 5,-    mtl              >= 2     && < 2.2,     nats             >= 0.1.2 && < 1,     reflection       >= 1.4   && < 2,-    tagged           >= 0.7   && < 1,-    template-haskell,-    transformers     >= 0.3   && < 0.4+    transformers     >= 0.3   && < 0.5 +  if impl(ghc < 7.8)+    build-depends: tagged >= 0.7 && < 1+   exposed-modules:     Numeric.AD     Numeric.AD.Halley@@ -167,8 +167,7 @@     base,     directory,     doctest >= 0.9.0.1 && <= 0.10,-    filepath,-    mtl+    filepath   ghc-options: -Wall -threaded   if impl(ghc<7.6)     ghc-options: -Werror
src/Numeric/AD.hs view
@@ -127,6 +127,7 @@   , gradientAscent   , conjugateGradientDescent   , conjugateGradientAscent+  , stochasticGradientDescent   ) where  import Data.Functor.Compose
src/Numeric/AD/Newton.hs view
@@ -25,6 +25,7 @@   , gradientAscent   , conjugateGradientDescent   , conjugateGradientAscent+  , stochasticGradientDescent   ) where  import Data.Foldable (all, sum)@@ -37,7 +38,7 @@ import Numeric.AD.Internal.Reverse (Reverse, Tape) import Numeric.AD.Internal.Type (AD(..)) import Numeric.AD.Mode-import Numeric.AD.Mode.Reverse as Reverse (gradWith')+import Numeric.AD.Mode.Reverse as Reverse (gradWith, gradWith') import Numeric.AD.Rank1.Kahn as Kahn (Kahn, grad) import qualified Numeric.AD.Rank1.Newton as Rank1 import Prelude hiding (all, mapM, sum)@@ -121,6 +122,30 @@         x1 = fmap (\(xi,gxi) -> xi - eta * gxi) xgx         (fx1, xgx1) = Reverse.gradWith' (,) f x1 {-# INLINE gradientDescent #-}++-- | The 'stochasticGradientDescent' function approximates+-- the true gradient of the constFunction by a gradient at+-- a single example. As the algorithm sweeps through the training +-- set, it performs the update for each training example.+--+-- It uses reverse mode automatic differentiation to compute the gradient+-- The learning rate is constant through out, and is set to 0.001+stochasticGradientDescent :: (Traversable f, Fractional a, Ord a) +  => (forall s. Reifies s Tape => f (Scalar a) -> f (Reverse s a) -> Reverse s a) +  -> [f (Scalar a)]+  -> f a +  -> [f a]+stochasticGradientDescent errorSingle d0 x0 = go xgx0 0.001 dLeft+  where+    dLeft = tail $ cycle d0+    xgx0 = Reverse.gradWith (,) (errorSingle (head d0)) x0+    go xgx !eta d+      | eta ==0       = []+      | otherwise     = x1 : go xgx1 eta (tail d)+      where+        x1 = fmap (\(xi, gxi) -> xi - eta * gxi) xgx+        (_, xgx1) = Reverse.gradWith' (,) (errorSingle (head d)) x1+{-# INLINE stochasticGradientDescent #-}  -- | Perform a gradient descent using reverse mode automatic differentiation to compute the gradient. gradientAscent :: (Traversable f, Fractional a, Ord a) => (forall s. Reifies s Tape => f (Reverse s a) -> Reverse s a) -> f a -> [f a]