packages feed

backprop 0.2.0.0 → 0.2.1.0

raw patch · 12 files changed

+366/−44 lines, 12 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Numeric.Backprop: (^^?!) :: forall b a s. (Backprop a, Reifies s W) => BVar s b -> Traversal' b a -> BVar s a
+ Numeric.Backprop: ABP :: f a -> ABP f a
+ Numeric.Backprop: NumBP :: a -> NumBP a
+ Numeric.Backprop: [runABP] :: ABP f a -> f a
+ Numeric.Backprop: [runNumBP] :: NumBP a -> a
+ Numeric.Backprop: newtype ABP f a
+ Numeric.Backprop: newtype NumBP a
+ Numeric.Backprop.Class: ABP :: f a -> ABP f a
+ Numeric.Backprop.Class: NumBP :: a -> NumBP a
+ Numeric.Backprop.Class: [runABP] :: ABP f a -> f a
+ Numeric.Backprop.Class: [runNumBP] :: NumBP a -> a
+ Numeric.Backprop.Class: instance (Data.Data.Data (f a), Data.Typeable.Internal.Typeable * a, Data.Typeable.Internal.Typeable (* -> *) f) => Data.Data.Data (Numeric.Backprop.Class.ABP f a)
+ Numeric.Backprop.Class: instance (GHC.Base.Applicative f, GHC.Float.Floating a) => GHC.Float.Floating (Numeric.Backprop.Class.ABP f a)
+ Numeric.Backprop.Class: instance (GHC.Base.Applicative f, GHC.Num.Num a) => GHC.Num.Num (Numeric.Backprop.Class.ABP f a)
+ Numeric.Backprop.Class: instance (GHC.Base.Applicative f, GHC.Real.Fractional a) => GHC.Real.Fractional (Numeric.Backprop.Class.ABP f a)
+ Numeric.Backprop.Class: instance (GHC.Base.Applicative f, Numeric.Backprop.Class.Backprop a) => Numeric.Backprop.Class.Backprop (Numeric.Backprop.Class.ABP f a)
+ Numeric.Backprop.Class: instance Control.DeepSeq.NFData (f a) => Control.DeepSeq.NFData (Numeric.Backprop.Class.ABP f a)
+ Numeric.Backprop.Class: instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Numeric.Backprop.Class.NumBP a)
+ Numeric.Backprop.Class: instance Data.Data.Data a => Data.Data.Data (Numeric.Backprop.Class.NumBP a)
+ Numeric.Backprop.Class: instance Data.Foldable.Foldable Numeric.Backprop.Class.NumBP
+ Numeric.Backprop.Class: instance Data.Foldable.Foldable f => Data.Foldable.Foldable (Numeric.Backprop.Class.ABP f)
+ Numeric.Backprop.Class: instance Data.Traversable.Traversable Numeric.Backprop.Class.NumBP
+ Numeric.Backprop.Class: instance Data.Traversable.Traversable f => Data.Traversable.Traversable (Numeric.Backprop.Class.ABP f)
+ Numeric.Backprop.Class: instance GHC.Base.Applicative Numeric.Backprop.Class.NumBP
+ Numeric.Backprop.Class: instance GHC.Base.Applicative f => GHC.Base.Applicative (Numeric.Backprop.Class.ABP f)
+ Numeric.Backprop.Class: instance GHC.Base.Functor Numeric.Backprop.Class.NumBP
+ Numeric.Backprop.Class: instance GHC.Base.Functor f => GHC.Base.Functor (Numeric.Backprop.Class.ABP f)
+ Numeric.Backprop.Class: instance GHC.Base.Monad Numeric.Backprop.Class.NumBP
+ Numeric.Backprop.Class: instance GHC.Base.Monad m => GHC.Base.Monad (Numeric.Backprop.Class.ABP m)
+ Numeric.Backprop.Class: instance GHC.Classes.Eq (f a) => GHC.Classes.Eq (Numeric.Backprop.Class.ABP f a)
+ Numeric.Backprop.Class: instance GHC.Classes.Eq a => GHC.Classes.Eq (Numeric.Backprop.Class.NumBP a)
+ Numeric.Backprop.Class: instance GHC.Classes.Ord (f a) => GHC.Classes.Ord (Numeric.Backprop.Class.ABP f a)
+ Numeric.Backprop.Class: instance GHC.Classes.Ord a => GHC.Classes.Ord (Numeric.Backprop.Class.NumBP a)
+ Numeric.Backprop.Class: instance GHC.Float.Floating a => GHC.Float.Floating (Numeric.Backprop.Class.NumBP a)
+ Numeric.Backprop.Class: instance GHC.Generics.Generic (Numeric.Backprop.Class.ABP f a)
+ Numeric.Backprop.Class: instance GHC.Generics.Generic (Numeric.Backprop.Class.NumBP a)
+ Numeric.Backprop.Class: instance GHC.Num.Num a => GHC.Num.Num (Numeric.Backprop.Class.NumBP a)
+ Numeric.Backprop.Class: instance GHC.Num.Num a => Numeric.Backprop.Class.Backprop (Numeric.Backprop.Class.NumBP a)
+ Numeric.Backprop.Class: instance GHC.Read.Read (f a) => GHC.Read.Read (Numeric.Backprop.Class.ABP f a)
+ Numeric.Backprop.Class: instance GHC.Read.Read a => GHC.Read.Read (Numeric.Backprop.Class.NumBP a)
+ Numeric.Backprop.Class: instance GHC.Real.Fractional a => GHC.Real.Fractional (Numeric.Backprop.Class.NumBP a)
+ Numeric.Backprop.Class: instance GHC.Show.Show (f a) => GHC.Show.Show (Numeric.Backprop.Class.ABP f a)
+ Numeric.Backprop.Class: instance GHC.Show.Show a => GHC.Show.Show (Numeric.Backprop.Class.NumBP a)
+ Numeric.Backprop.Class: instance Numeric.Backprop.Class.Backprop GHC.Natural.Natural
+ Numeric.Backprop.Class: newtype ABP f a
+ Numeric.Backprop.Class: newtype NumBP a
+ Numeric.Backprop.Explicit: ABP :: f a -> ABP f a
+ Numeric.Backprop.Explicit: NumBP :: a -> NumBP a
+ Numeric.Backprop.Explicit: [runABP] :: ABP f a -> f a
+ Numeric.Backprop.Explicit: [runNumBP] :: NumBP a -> a
+ Numeric.Backprop.Explicit: newtype ABP f a
+ Numeric.Backprop.Explicit: newtype NumBP a
+ Numeric.Backprop.Explicit: ofFunctor :: (Backprop a, Functor f) => OneFunc (f a)
+ Numeric.Backprop.Explicit: zfFunctor :: (Backprop a, Functor f) => ZeroFunc (f a)
+ Numeric.Backprop.Num: (^^?!) :: forall b a s. (Num a, Reifies s W) => BVar s b -> Traversal' b a -> BVar s a
+ Prelude.Backprop: fromIntegral :: (Backprop a, Integral a, Backprop b, Integral b, Reifies s W) => BVar s a -> BVar s b
+ Prelude.Backprop: realToFrac :: (Backprop a, Fractional a, Real a, Backprop b, Fractional b, Real b, Reifies s W) => BVar s a -> BVar s b
+ Prelude.Backprop.Explicit: fromIntegral :: (Integral a, Integral b, Reifies s W) => AddFunc a -> ZeroFunc b -> BVar s a -> BVar s b
+ Prelude.Backprop.Explicit: realToFrac :: (Fractional a, Real a, Fractional b, Real b, Reifies s W) => AddFunc a -> ZeroFunc b -> BVar s a -> BVar s b
+ Prelude.Backprop.Num: fromIntegral :: (Integral a, Integral b, Reifies s W) => BVar s a -> BVar s b
+ Prelude.Backprop.Num: realToFrac :: (Fractional a, Real a, Fractional b, Real b, Reifies s W) => BVar s a -> BVar s b

Files

CHANGELOG.md view
@@ -1,6 +1,29 @@ Changelog ========= +Version 0.2.1.0+---------------++*May 8, 2018*++<https://github.com/mstksg/backprop/releases/tag/v0.2.1.0>++*   Added `ABP` newtype wrapper to *Numeric.Backprop.Class* (re-exported from+    *Numeric.Backprop* and *Numeric.Backprop.Explicit*) to give free `Backprop`+    instances for Applicative actions.+*   Added `NumBP` newtype wrapper to *Numeric.Backprop.Class* (re-exported in+    the same places as `ABP`) to give free `Backprop` instances for `Num`+    instances.+*   Added `^^?!` (unsafe access) to *Numeric.Backprop* and+    *Numeric.Backprop.Num*.+*   `Backprop` instance for `Natural` from *Numeric.Natural*.  Should actually+    be safe, unlike its `Num` instance!+*   `zfFunctor` and `ofFunctor` for instances of `Functor` for+    *Numeric.Backprop.Explicit*.+*   `realToFrac` and `fromIntegral` to *Prelude* modules+*   `T2` and `T3` patterns for *Numeric.Backprop*, for conveniently+    constructing and deconstructing tuples.+ Version 0.2.0.0 --------------- @@ -85,7 +108,7 @@     applying isomorphisms to `BVar`s.  Helpful for use with constructors and     deconstructors. *   `opIso2` and `opIso3` added to *Numeric.Backprop.Op*, for convenience.-*   `T0` (Unit with numeric instances) added to *Numeric.Backprop.Tuple".+*   `T0` (Unit with numeric instances) added to *Numeric.Backprop.Tuple*.  *Internal* 
README.md view
@@ -1,6 +1,8 @@ backprop ======== +[![Join the chat at https://gitter.im/haskell-backprop/Lobby](https://badges.gitter.im/haskell-backprop/Lobby.svg)](https://gitter.im/haskell-backprop/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)+ [![backprop on Hackage](https://img.shields.io/hackage/v/backprop.svg?maxAge=2592000)](https://hackage.haskell.org/package/backprop) [![Build Status](https://travis-ci.org/mstksg/backprop.svg?branch=master)](https://travis-ci.org/mstksg/backprop) @@ -25,6 +27,11 @@ [hackage]: http://hackage.haskell.org/package/backprop [docs]: https://mstksg.github.io/backprop +If you want to provide *backprop* for users of your library, see this **[guide+to equipping your library with backprop][library]**.++[library]: https://github.com/mstksg/backprop/wiki/Equipping-your-Library-with-Backprop+ MNIST Digit Classifier Example ------------------------------ @@ -252,4 +259,7 @@  4.  Some open questions: -    a. Is it possible to support constructors with existential types?+    a.  Is it possible to support constructors with existential types?++    b.  How to support "monadic" operations that depend on results of previous+        operations? (`ApBP` already exists for situations that don't)
backprop.cabal view
@@ -1,11 +1,11 @@--- This file has been generated from package.yaml by hpack version 0.21.2.+-- This file has been generated from package.yaml by hpack version 0.28.2. -- -- see: https://github.com/sol/hpack ----- hash: 0ba2801ba9787e38a25a6b3a1f48172558ed7066726034c01b1f3b01b9ee17fa+-- hash: 2877842d9cf55116566216ea0e7a25477c1df3557ff9e0d96a97d815dc772ac8  name:           backprop-version:        0.2.0.0+version:        0.2.1.0 synopsis:       Heterogeneous automatic differentation (backpropagation) description:    Write your functions to compute your result, and the library will                 automatically generate functions to compute your gradient.@@ -25,7 +25,6 @@ tested-with:    GHC >= 8.0 build-type:     Simple cabal-version:  >= 1.10- extra-source-files:     Build.hs     CHANGELOG.md
src/Numeric/Backprop.hs view
@@ -1,8 +1,9 @@-{-# LANGUAGE DataKinds         #-}-{-# LANGUAGE FlexibleContexts  #-}-{-# LANGUAGE GADTs             #-}-{-# LANGUAGE PatternSynonyms   #-}-{-# LANGUAGE RankNTypes        #-}+{-# LANGUAGE DataKinds        #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE GADTs            #-}+{-# LANGUAGE PatternSynonyms  #-}+{-# LANGUAGE RankNTypes       #-}+{-# LANGUAGE ViewPatterns     #-}  -- | -- Module      : Numeric.Backprop@@ -61,7 +62,7 @@  module Numeric.Backprop (     -- * Types-    BVar, W, Backprop(..)+    BVar, W, Backprop(..), ABP(..), NumBP(..)     -- * Running   , backprop, E.evalBP, gradBP, backpropWith     -- ** Multiple inputs@@ -69,10 +70,11 @@   , backpropN, E.evalBPN, gradBPN, backpropWithN, Every     -- * Manipulating 'BVar'   , E.constVar, E.auto, E.coerceVar-  , (^^.), (.~~), (^^?), (^^..)+  , (^^.), (.~~), (^^?), (^^..), (^^?!)   , viewVar, setVar   , sequenceVar, collectVar   , previewVar, toListOfVar+  , pattern T2, pattern T3     -- ** With Isomorphisms   , isoVar, isoVar2, isoVar3, isoVarN     -- ** With 'Op's#liftops#@@ -99,6 +101,7 @@   , Reifies   ) where +import           Data.Maybe import           Data.Reflection import           Data.Type.Index import           Data.Type.Length@@ -442,6 +445,23 @@ v ^^? t = previewVar t v {-# INLINE (^^?) #-} +-- | An *UNSAFE* version of 'previewVar' assuming that it is there.+--+-- Is undefined if the 'Traversal' hits no targets.+--+-- Is essentially '^^?' with 'fromJust', or '^^..' with 'head'.+--+-- @since 0.2.1.0+(^^?!)+    :: forall b a s. (Backprop a, Reifies s W)+    => BVar s b+    -> Traversal' b a+    -> BVar s a+v ^^?! t = fromMaybe (error e) (previewVar t v)+  where+    e = "Numeric.Backprop.^^?!: Empty traversal"+{-# INLINE (^^?!) #-}+ -- | Using a 'Traversal'', extract a single value /inside/ a 'BVar', if it -- exists.  If more than one traversal target exists, returns te first. -- Meant to evoke parallels to 'preview' from lens.  Really only intended@@ -606,7 +626,7 @@     -> (b -> a)     -> BVar s a     -> BVar s b-isoVar f g = liftOp1 (opIso f g)+isoVar = E.isoVar E.addFunc E.zeroFunc {-# INLINE isoVar #-}  -- | Convert the values inside two 'BVar's using a given isomorphism.@@ -620,7 +640,7 @@     -> BVar s a     -> BVar s b     -> BVar s c-isoVar2 f g = liftOp2 (opIso2 f g)+isoVar2 = E.isoVar2 E.addFunc E.addFunc E.zeroFunc {-# INLINE isoVar2 #-}  -- | Convert the values inside three 'BVar's using a given isomorphism.@@ -635,7 +655,7 @@     -> BVar s b     -> BVar s c     -> BVar s d-isoVar3 f g = liftOp3 (opIso3 f g)+isoVar3 = E.isoVar3 E.addFunc E.addFunc E.addFunc E.zeroFunc {-# INLINE isoVar3 #-}  -- | Convert the values inside a tuple of 'BVar's using a given@@ -649,5 +669,33 @@     -> (b -> Tuple as)     -> Prod (BVar s) as     -> BVar s b-isoVarN f g = liftOp (opIsoN f g)+isoVarN = E.isoVarN E.addFuncs E.zeroFunc {-# INLINE isoVarN #-}++-- | Useful pattern for constructing and deconstructing 'BVar's of+-- two-tuples.+--+-- @since 0.2.1.0+pattern T2+    :: (Backprop a, Backprop b, Reifies s W)+    => BVar s a+    -> BVar s b+    -> BVar s (a, b)+pattern T2 x y <- (\xy -> (xy ^^. _1, xy ^^. _2) -> (x, y))+  where+    T2 = isoVar2 (,) id++-- | Useful pattern for constructing and deconstructing 'BVar's+-- three-tuples.+--+-- @since 0.2.1.0+pattern T3+    :: (Backprop a, Backprop b, Backprop c, Reifies s W)+    => BVar s a+    -> BVar s b+    -> BVar s c+    -> BVar s (a, b, c)+pattern T3 x y z <- (\xyz -> (xyz ^^. _1, xyz ^^. _2, xyz ^^. _3) -> (x, y, z))+  where+    T3 = isoVar3 (,,) id+
src/Numeric/Backprop/Class.hs view
@@ -1,11 +1,18 @@-{-# LANGUAGE BangPatterns         #-}-{-# LANGUAGE DefaultSignatures    #-}-{-# LANGUAGE EmptyCase            #-}-{-# LANGUAGE FlexibleContexts     #-}-{-# LANGUAGE GADTs                #-}-{-# LANGUAGE LambdaCase           #-}-{-# LANGUAGE TypeOperators        #-}-{-# LANGUAGE UndecidableInstances #-}+{-# LANGUAGE BangPatterns               #-}+{-# LANGUAGE DefaultSignatures          #-}+{-# LANGUAGE DeriveDataTypeable         #-}+{-# LANGUAGE DeriveFoldable             #-}+{-# LANGUAGE DeriveFunctor              #-}+{-# LANGUAGE DeriveGeneric              #-}+{-# LANGUAGE DeriveTraversable          #-}+{-# LANGUAGE EmptyCase                  #-}+{-# LANGUAGE FlexibleContexts           #-}+{-# LANGUAGE GADTs                      #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE LambdaCase                 #-}+{-# LANGUAGE ScopedTypeVariables        #-}+{-# LANGUAGE TypeOperators              #-}+{-# LANGUAGE UndecidableInstances       #-}  -- | -- Module      : Numeric.Backprop.Class@@ -31,15 +38,20 @@   , zeroVec, addVec, oneVec   , zeroFunctor, addIsList, addAsList, oneFunctor   , genericZero, genericAdd, genericOne+  -- * Newtype+  , ABP(..), NumBP(..)   -- * Generics   , GZero(..), GAdd(..), GOne(..)   ) where +import           Control.Applicative+import           Control.DeepSeq+import           Data.Coerce import           Data.Complex+import           Data.Data import           Data.Foldable hiding        (toList) import           Data.Functor.Identity import           Data.List.NonEmpty          (NonEmpty(..))-import           Data.Proxy import           Data.Ratio import           Data.Type.Combinator hiding ((:.:), Comp1) import           Data.Type.Option@@ -47,6 +59,7 @@ import           Data.Void import           GHC.Exts import           GHC.Generics+import           Numeric.Natural import           Type.Family.List import qualified Data.IntMap                 as IM import qualified Data.Map                    as M@@ -276,10 +289,127 @@ oneFunctor = fmap one {-# INLINE oneFunctor #-} +-- | A newtype wrapper over an instance of 'Num' that gives a free+-- 'Backprop' instance.+--+-- Useful for things like /DerivingVia/, or for avoiding orphan instances.+--+-- @since 0.2.1.0+newtype NumBP a = NumBP { runNumBP :: a }+  deriving (Show, Read, Eq, Ord, Typeable, Data, Generic, Functor, Foldable, Traversable, Num, Fractional, Floating) +instance NFData a => NFData (NumBP a) +instance Applicative NumBP where+    pure    = NumBP+    f <*> x = NumBP $ (runNumBP f) (runNumBP x) +instance Monad NumBP where+    return = NumBP+    x >>= f = f (runNumBP x) +instance Num a => Backprop (NumBP a) where+    zero = coerce (zeroNum :: a -> a)+    add = coerce (addNum :: a -> a -> a)+    one = coerce (oneNum :: a -> a)++-- | A newtype wrapper over an @f a@ for @'Applicative' f@ that gives+-- a free 'Backprop' instance (as well as 'Num' etc. instances).+--+-- Useful for performing backpropagation over functions that require some+-- monadic context (like 'IO') to perform.+--+-- @since 0.2.1.0+newtype ABP f a = ABP { runABP :: f a }+  deriving (Show, Read, Eq, Ord, Typeable, Data, Generic, Functor, Foldable, Traversable)++instance NFData (f a) => NFData (ABP f a)++instance Applicative f => Applicative (ABP f) where+    pure = ABP . pure+    {-# INLINE pure #-}+    f <*> x = ABP $ ($) <$> runABP f <*> runABP x+    {-# INLINE (<*>) #-}++instance Monad m => Monad (ABP m) where+    return = ABP . return+    {-# INLINE return #-}+    x >>= f = ABP $ do+      x' <- runABP x+      runABP $ f x'+    {-# INLINE (>>=) #-}++instance (Applicative f, Backprop a) => Backprop (ABP f a) where+    zero = fmap zero+    {-# INLINE zero #-}+    add  = liftA2 add+    {-# INLINE add #-}+    one  = fmap one+    {-# INLINE one #-}++instance (Applicative f, Num a) => Num (ABP f a) where+    (+) = liftA2 (+)+    {-# INLINE (+) #-}+    (-) = liftA2 (-)+    {-# INLINE (-) #-}+    (*) = liftA2 (*)+    {-# INLINE (*) #-}+    negate = fmap negate+    {-# INLINE negate #-}+    abs = fmap abs+    {-# INLINE abs #-}+    signum = fmap signum+    {-# INLINE signum #-}+    fromInteger = pure . fromInteger+    {-# INLINE fromInteger #-}++instance (Applicative f, Fractional a) => Fractional (ABP f a) where+    (/) = liftA2 (/)+    {-# INLINE (/) #-}+    recip = fmap recip+    {-# INLINE recip #-}+    fromRational = pure . fromRational+    {-# INLINE fromRational #-}++instance (Applicative f, Floating a) => Floating (ABP f a) where+    pi  = pure pi+    {-# INLINE pi #-}+    exp = fmap exp+    {-# INLINE exp #-}+    log = fmap log+    {-# INLINE log #-}+    sqrt = fmap sqrt+    {-# INLINE sqrt #-}+    (**) = liftA2 (**)+    {-# INLINE (**) #-}+    logBase = liftA2 logBase+    {-# INLINE logBase #-}+    sin = fmap sin+    {-# INLINE sin #-}+    cos = fmap cos+    {-# INLINE cos #-}+    tan = fmap tan+    {-# INLINE tan #-}+    asin = fmap asin+    {-# INLINE asin #-}+    acos = fmap acos+    {-# INLINE acos #-}+    atan = fmap atan+    {-# INLINE atan #-}+    sinh = fmap sinh+    {-# INLINE sinh #-}+    cosh = fmap cosh+    {-# INLINE cosh #-}+    tanh = fmap tanh+    {-# INLINE tanh #-}+    asinh = fmap asinh+    {-# INLINE asinh #-}+    acosh = fmap acosh+    {-# INLINE acosh #-}+    atanh = fmap atanh+    {-# INLINE atanh #-}++ -- | Helper class for automatically deriving 'zero' using GHC Generics. class GZero f where     gzero :: f t -> f t@@ -388,6 +518,15 @@     {-# INLINE one #-}  instance Backprop Integer where+    zero = zeroNum+    {-# INLINE zero #-}+    add  = addNum+    {-# INLINE add #-}+    one  = oneNum+    {-# INLINE one #-}++-- | @since 0.2.1.0+instance Backprop Natural where     zero = zeroNum     {-# INLINE zero #-}     add  = addNum
src/Numeric/Backprop/Explicit.hs view
@@ -29,11 +29,11 @@  module Numeric.Backprop.Explicit (     -- * Types-    BVar, W, Backprop(..)+    BVar, W, Backprop(..), ABP(..), NumBP(..)     -- * Explicit 'zero', 'add', and 'one'-  , ZeroFunc(..), zfNum, zfNums, zeroFunc, zeroFuncs+  , ZeroFunc(..), zfNum, zfNums, zeroFunc, zeroFuncs, zfFunctor   , AddFunc(..), afNum, afNums, addFunc, addFuncs-  , OneFunc(..), ofNum, ofNums, oneFunc, oneFuncs+  , OneFunc(..), ofNum, ofNums, oneFunc, oneFuncs, ofFunctor     -- * Running   , backprop, evalBP, gradBP, backpropWith     -- ** Multiple inputs@@ -41,7 +41,6 @@   , backpropN, evalBPN, gradBPN, backpropWithN, Every     -- * Manipulating 'BVar'   , constVar, auto, coerceVar-  -- , (^^.), (.~~), (^^?), (^^..)   , viewVar, setVar   , sequenceVar, collectVar   , previewVar, toListOfVar@@ -89,6 +88,13 @@ zfNums :: (Every Num as, Known Length as) => Prod ZeroFunc as zfNums = map1 (\i -> zfNum \\ every @_ @Num i) indices +-- | 'zeroFunc' for instances of 'Functor'+--+-- @since 0.2.1.0+zfFunctor :: (Backprop a, Functor f) => ZeroFunc (f a)+zfFunctor = ZF zeroFunctor+{-# INLINE zfFunctor #-}+ -- | 'ZeroFunc's for every item in a type level list based on their -- 'Num' instances --@@ -102,6 +108,13 @@ -- @since 0.2.0.0 ofNums :: (Every Num as, Known Length as) => Prod OneFunc as ofNums = map1 (\i -> ofNum \\ every @_ @Num i) indices++-- | 'OneFunc' for instances of 'Functor'+--+-- @since 0.2.1.0+ofFunctor :: (Backprop a, Functor f) => OneFunc (f a)+ofFunctor = OF oneFunctor+{-# INLINE ofFunctor #-}  -- | The canonical 'ZeroFunc' for instances of 'Backprop'. --
src/Numeric/Backprop/Internal.hs view
@@ -762,6 +762,6 @@     stuff    = evalState (traverseOf t (state . const go) xs)       where         go :: [a] -> (a,  [a])-        go []     = error "asList"+        go []     = error "Numeric.Backprop.Internal: unexpected shape involved in gradient computation"         go (y:ys) = (y, ys) {-# INLINE ixt #-}
src/Numeric/Backprop/Num.hs view
@@ -1,8 +1,8 @@-{-# LANGUAGE DataKinds         #-}-{-# LANGUAGE FlexibleContexts  #-}-{-# LANGUAGE GADTs             #-}-{-# LANGUAGE PatternSynonyms   #-}-{-# LANGUAGE RankNTypes        #-}+{-# LANGUAGE DataKinds        #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE GADTs            #-}+{-# LANGUAGE PatternSynonyms  #-}+{-# LANGUAGE RankNTypes       #-}  -- | -- Module      : Numeric.Backprop.Num@@ -57,7 +57,7 @@   , backpropN, E.evalBPN, gradBPN, backpropWithN, Every     -- * Manipulating 'BVar'   , E.constVar, E.auto, E.coerceVar-  , (^^.), (.~~), (^^?), (^^..)+  , (^^.), (.~~), (^^?), (^^..), (^^?!)   , viewVar, setVar   , sequenceVar, collectVar   , previewVar, toListOfVar@@ -87,6 +87,7 @@   , Reifies   ) where +import           Data.Maybe import           Data.Reflection import           Data.Type.Index import           Data.Type.Length@@ -280,6 +281,22 @@ v ^^? t = previewVar t v {-# INLINE (^^?) #-} +-- | 'Numeric.Backprop.^^?!', but with 'Num' constraints instead of+-- 'Backprop' constraints.+--+-- Like 'Numeric.Backprop.^^?!', is *UNSAFE*.+--+-- @since 0.2.1.0+(^^?!)+    :: forall b a s. (Num a, Reifies s W)+    => BVar s b+    -> Traversal' b a+    -> BVar s a+v ^^?! t = fromMaybe (error e) (previewVar t v)+  where+    e = "Numeric.Backprop.Num.^^?!: Empty traversal"+{-# INLINE (^^?!) #-}+ -- | 'Numeric.Backprop.previewVar', but with 'Num' constraints instead of -- 'Backprop' constraints. --@@ -385,7 +402,7 @@     -> (b -> a)     -> BVar s a     -> BVar s b-isoVar f g = liftOp1 (opIso f g)+isoVar = E.isoVar E.afNum E.zfNum {-# INLINE isoVar #-}  -- | 'Numeric.Backprop.isoVar', but with 'Num' constraints instead of@@ -397,7 +414,7 @@     -> BVar s a     -> BVar s b     -> BVar s c-isoVar2 f g = liftOp2 (opIso2 f g)+isoVar2 = E.isoVar2 E.afNum E.afNum E.zfNum {-# INLINE isoVar2 #-}  -- | 'Numeric.Backprop.isoVar3', but with 'Num' constraints instead of@@ -410,7 +427,7 @@     -> BVar s b     -> BVar s c     -> BVar s d-isoVar3 f g = liftOp3 (opIso3 f g)+isoVar3 = E.isoVar3 E.afNum E.afNum E.afNum E.zfNum {-# INLINE isoVar3 #-}  -- | 'Numeric.Backprop.isoVarN', but with 'Num' constraints instead of@@ -421,6 +438,5 @@     -> (b -> Tuple as)     -> Prod (BVar s) as     -> BVar s b-isoVarN f g = liftOp (opIsoN f g)+isoVarN = E.isoVarN E.afNums E.zfNum {-# INLINE isoVarN #-}-
src/Numeric/Backprop/Op.hs view
@@ -355,7 +355,7 @@ -- -- @since 0.1.3.0 noGrad1 :: (a -> b) -> Op '[a] b-noGrad1 f = op1 (\x -> (f x, error "noGrad: no gradient defined"))+noGrad1 f = op1 (\x -> (f x, \_ -> error "noGrad1: no gradient defined")) {-# INLINE noGrad1 #-}  -- | Create an 'Op' with no gradient.  Can be evaluated with 'evalOp',  but@@ -372,7 +372,7 @@ -- -- @since 0.1.3.0 noGrad :: (Tuple as -> b) -> Op as b-noGrad f = Op (\xs -> (f xs, error "noGrads: no gradient defined"))+noGrad f = Op (\xs -> (f xs, \_ -> error "noGrad: no gradient defined")) {-# INLINE noGrad #-}  -- | An 'Op' that just returns whatever it receives.  The identity
src/Prelude/Backprop.hs view
@@ -39,6 +39,8 @@   , liftA2   , liftA3   -- * Misc+  , fromIntegral+  , realToFrac   , coerce   ) where @@ -195,3 +197,25 @@     -> BVar s b coerce = coerceVar {-# INLINE coerce #-}++-- | Lifted conversion between two 'P.Integral' instances.+--+-- @since 0.2.1.0+fromIntegral+    :: (Backprop a, P.Integral a, Backprop b, P.Integral b, Reifies s W)+    => BVar s a+    -> BVar s b+fromIntegral = liftOp1 . op1 $ \x ->+    (P.fromIntegral x, P.fromIntegral)+{-# INLINE fromIntegral #-}++-- | Lifted conversion between two 'Fractional' and 'P.Real' instances.+--+-- @since 0.2.1.0+realToFrac+    :: (Backprop a, Fractional a, P.Real a, Backprop b, Fractional b, P.Real b, Reifies s W)+    => BVar s a+    -> BVar s b+realToFrac = liftOp1 . op1 $ \x ->+    (P.realToFrac x, P.realToFrac)+{-# INLINE realToFrac #-}
src/Prelude/Backprop/Explicit.hs view
@@ -30,6 +30,8 @@   , liftA2   , liftA3   -- * Misc+  , fromIntegral+  , realToFrac   , coerce   ) where @@ -222,4 +224,28 @@ coerce = coerceVar {-# INLINE coerce #-} +-- | Lifted conversion between two 'P.Integral' instances.+--+-- @since 0.2.1.0+fromIntegral+    :: (P.Integral a, P.Integral b, Reifies s W)+    => AddFunc a+    -> ZeroFunc b+    -> BVar s a+    -> BVar s b+fromIntegral af zf = liftOp1 af zf . op1 $ \x ->+    (P.fromIntegral x, P.fromIntegral)+{-# INLINE fromIntegral #-} +-- | Lifted conversion between two 'Fractional' and 'P.Real' instances.+--+-- @since 0.2.1.0+realToFrac+    :: (Fractional a, P.Real a, Fractional b, P.Real b, Reifies s W)+    => AddFunc a+    -> ZeroFunc b+    -> BVar s a+    -> BVar s b+realToFrac af zf = liftOp1 af zf . op1 $ \x ->+    (P.realToFrac x, P.realToFrac)+{-# INLINE realToFrac #-}
src/Prelude/Backprop/Num.hs view
@@ -30,6 +30,8 @@   , liftA2   , liftA3   -- * Misc+  , fromIntegral+  , realToFrac   , coerce   ) where @@ -185,3 +187,25 @@     -> BVar s b coerce = coerceVar {-# INLINE coerce #-}++-- | Lifted conversion between two 'P.Integral' instances.+--+-- @since 0.2.1.0+fromIntegral+    :: (P.Integral a, P.Integral b, Reifies s W)+    => BVar s a+    -> BVar s b+fromIntegral = liftOp1 . op1 $ \x ->+    (P.fromIntegral x, P.fromIntegral)+{-# INLINE fromIntegral #-}++-- | Lifted conversion between two 'Fractional' and 'P.Real' instances.+--+-- @since 0.2.1.0+realToFrac+    :: (Fractional a, P.Real a, Fractional b, P.Real b, Reifies s W)+    => BVar s a+    -> BVar s b+realToFrac = liftOp1 . op1 $ \x ->+    (P.realToFrac x, P.realToFrac)+{-# INLINE realToFrac #-}