{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
{-# OPTIONS_HADDOCK not-home #-}
module Numeric.Backprop.Num (
BVar, W
, backprop, E.evalBP, gradBP, backpropWith
, E.evalBP0
, backprop2, E.evalBP2, gradBP2, backpropWith2
, backpropN, E.evalBPN, gradBPN, backpropWithN
, E.constVar, E.auto, E.coerceVar
, (^^.), (.~~), (%~~), (^^?), (^^..), (^^?!)
, viewVar, setVar, overVar
, sequenceVar, collectVar
, previewVar, toListOfVar
, isoVar, isoVar2, isoVar3, isoVarN
, liftOp
, liftOp1, liftOp2, liftOp3
, Op(..)
, op0, opConst, idOp
, bpOp
, op1, op2, op3
, opCoerce, opTup, opIso, opIsoN, opLens
, noGrad1, noGrad
, Reifies
) where
import Data.Functor.Identity
import Data.Maybe
import Data.Reflection
import Data.Vinyl
import Lens.Micro
import Numeric.Backprop.Explicit (BVar, W)
import Numeric.Backprop.Op
import qualified Numeric.Backprop.Explicit as E
backpropN
:: (RPureConstrained Num as, Num b)
=> (forall s. Reifies s W => Rec (BVar s) as -> BVar s b)
-> Rec Identity as
-> (b, Rec Identity as)
backpropN :: forall (as :: [*]) b.
(RPureConstrained Num as, Num b) =>
(forall s. Reifies s W => Rec (BVar s) as -> BVar s b)
-> Rec Identity as -> (b, Rec Identity as)
backpropN = forall (as :: [*]) b.
Rec ZeroFunc as
-> OneFunc b
-> (forall s. Reifies s W => Rec (BVar s) as -> BVar s b)
-> Rec Identity as
-> (b, Rec Identity as)
E.backpropN forall (as :: [*]). RPureConstrained Num as => Rec ZeroFunc as
E.zfNums forall a. Num a => OneFunc a
E.ofNum
{-# INLINE backpropN #-}
backpropWithN
:: RPureConstrained Num as
=> (forall s. Reifies s W => Rec (BVar s) as -> BVar s b)
-> Rec Identity as
-> (b, b -> Rec Identity as)
backpropWithN :: forall (as :: [*]) b.
RPureConstrained Num as =>
(forall s. Reifies s W => Rec (BVar s) as -> BVar s b)
-> Rec Identity as -> (b, b -> Rec Identity as)
backpropWithN = forall (as :: [*]) b.
Rec ZeroFunc as
-> (forall s. Reifies s W => Rec (BVar s) as -> BVar s b)
-> Rec Identity as
-> (b, b -> Rec Identity as)
E.backpropWithN forall (as :: [*]). RPureConstrained Num as => Rec ZeroFunc as
E.zfNums
{-# INLINE backpropWithN #-}
backprop
:: (Num a, Num b)
=> (forall s. Reifies s W => BVar s a -> BVar s b)
-> a
-> (b, a)
backprop :: forall a b.
(Num a, Num b) =>
(forall s. Reifies s W => BVar s a -> BVar s b) -> a -> (b, a)
backprop = forall a b.
ZeroFunc a
-> OneFunc b
-> (forall s. Reifies s W => BVar s a -> BVar s b)
-> a
-> (b, a)
E.backprop forall a. Num a => ZeroFunc a
E.zfNum forall a. Num a => OneFunc a
E.ofNum
{-# INLINE backprop #-}
backpropWith
:: Num a
=> (forall s. Reifies s W => BVar s a -> BVar s b)
-> a
-> (b, b -> a)
backpropWith :: forall a b.
Num a =>
(forall s. Reifies s W => BVar s a -> BVar s b) -> a -> (b, b -> a)
backpropWith = forall a b.
ZeroFunc a
-> (forall s. Reifies s W => BVar s a -> BVar s b)
-> a
-> (b, b -> a)
E.backpropWith forall a. Num a => ZeroFunc a
E.zfNum
{-# INLINE backpropWith #-}
gradBP
:: (Num a, Num b)
=> (forall s. Reifies s W => BVar s a -> BVar s b)
-> a
-> a
gradBP :: forall a b.
(Num a, Num b) =>
(forall s. Reifies s W => BVar s a -> BVar s b) -> a -> a
gradBP = forall a b.
ZeroFunc a
-> OneFunc b
-> (forall s. Reifies s W => BVar s a -> BVar s b)
-> a
-> a
E.gradBP forall a. Num a => ZeroFunc a
E.zfNum forall a. Num a => OneFunc a
E.ofNum
{-# INLINE gradBP #-}
gradBPN
:: (RPureConstrained Num as, Num b)
=> (forall s. Reifies s W => Rec (BVar s) as -> BVar s b)
-> Rec Identity as
-> Rec Identity as
gradBPN :: forall (as :: [*]) b.
(RPureConstrained Num as, Num b) =>
(forall s. Reifies s W => Rec (BVar s) as -> BVar s b)
-> Rec Identity as -> Rec Identity as
gradBPN = forall (as :: [*]) b.
Rec ZeroFunc as
-> OneFunc b
-> (forall s. Reifies s W => Rec (BVar s) as -> BVar s b)
-> Rec Identity as
-> Rec Identity as
E.gradBPN forall (as :: [*]). RPureConstrained Num as => Rec ZeroFunc as
E.zfNums forall a. Num a => OneFunc a
E.ofNum
{-# INLINE gradBPN #-}
backprop2
:: (Num a, Num b, Num c)
=> (forall s. Reifies s W => BVar s a -> BVar s b -> BVar s c)
-> a
-> b
-> (c, (a, b))
backprop2 :: forall a b c.
(Num a, Num b, Num c) =>
(forall s. Reifies s W => BVar s a -> BVar s b -> BVar s c)
-> a -> b -> (c, (a, b))
backprop2 = forall a b c.
ZeroFunc a
-> ZeroFunc b
-> OneFunc c
-> (forall s. Reifies s W => BVar s a -> BVar s b -> BVar s c)
-> a
-> b
-> (c, (a, b))
E.backprop2 forall a. Num a => ZeroFunc a
E.zfNum forall a. Num a => ZeroFunc a
E.zfNum forall a. Num a => OneFunc a
E.ofNum
{-# INLINE backprop2 #-}
backpropWith2
:: (Num a, Num b)
=> (forall s. Reifies s W => BVar s a -> BVar s b -> BVar s c)
-> a
-> b
-> (c, c -> (a, b))
backpropWith2 :: forall a b c.
(Num a, Num b) =>
(forall s. Reifies s W => BVar s a -> BVar s b -> BVar s c)
-> a -> b -> (c, c -> (a, b))
backpropWith2 = forall a b c.
ZeroFunc a
-> ZeroFunc b
-> (forall s. Reifies s W => BVar s a -> BVar s b -> BVar s c)
-> a
-> b
-> (c, c -> (a, b))
E.backpropWith2 forall a. Num a => ZeroFunc a
E.zfNum forall a. Num a => ZeroFunc a
E.zfNum
{-# INLINE backpropWith2 #-}
gradBP2
:: (Num a, Num b, Num c)
=> (forall s. Reifies s W => BVar s a -> BVar s b -> BVar s c)
-> a
-> b
-> (a, b)
gradBP2 :: forall a b c.
(Num a, Num b, Num c) =>
(forall s. Reifies s W => BVar s a -> BVar s b -> BVar s c)
-> a -> b -> (a, b)
gradBP2 = forall a b c.
ZeroFunc a
-> ZeroFunc b
-> OneFunc c
-> (forall s. Reifies s W => BVar s a -> BVar s b -> BVar s c)
-> a
-> b
-> (a, b)
E.gradBP2 forall a. Num a => ZeroFunc a
E.zfNum forall a. Num a => ZeroFunc a
E.zfNum forall a. Num a => OneFunc a
E.ofNum
{-# INLINE gradBP2 #-}
bpOp
:: RPureConstrained Num as
=> (forall s. Reifies s W => Rec (BVar s) as -> BVar s b)
-> Op as b
bpOp :: forall (as :: [*]) b.
RPureConstrained Num as =>
(forall s. Reifies s W => Rec (BVar s) as -> BVar s b) -> Op as b
bpOp = forall (as :: [*]) b.
Rec ZeroFunc as
-> (forall s. Reifies s W => Rec (BVar s) as -> BVar s b)
-> Op as b
E.bpOp forall (as :: [*]). RPureConstrained Num as => Rec ZeroFunc as
E.zfNums
{-# INLINE bpOp #-}
(^^.)
:: forall b a s. (Num a, Num b, Reifies s W)
=> BVar s b
-> Lens' b a
-> BVar s a
BVar s b
x ^^. :: forall b a s.
(Num a, Num b, Reifies s W) =>
BVar s b -> Lens' b a -> BVar s a
^^. Lens' b a
l = forall b a s.
(Num a, Num b, Reifies s W) =>
Lens' b a -> BVar s b -> BVar s a
viewVar Lens' b a
l BVar s b
x
infixl 8 ^^.
{-# INLINE (^^.) #-}
viewVar
:: forall b a s. (Num a, Num b, Reifies s W)
=> Lens' b a
-> BVar s b
-> BVar s a
viewVar :: forall b a s.
(Num a, Num b, Reifies s W) =>
Lens' b a -> BVar s b -> BVar s a
viewVar = forall a b s.
Reifies s W =>
AddFunc a -> ZeroFunc b -> Lens' b a -> BVar s b -> BVar s a
E.viewVar forall a. Num a => AddFunc a
E.afNum forall a. Num a => ZeroFunc a
E.zfNum
{-# INLINE viewVar #-}
(.~~)
:: (Num a, Num b, Reifies s W)
=> Lens' b a
-> BVar s a
-> BVar s b
-> BVar s b
Lens' b a
l .~~ :: forall a b s.
(Num a, Num b, Reifies s W) =>
Lens' b a -> BVar s a -> BVar s b -> BVar s b
.~~ BVar s a
x = forall a b s.
(Num a, Num b, Reifies s W) =>
Lens' b a -> BVar s a -> BVar s b -> BVar s b
setVar Lens' b a
l BVar s a
x
infixl 8 .~~
{-# INLINE (.~~) #-}
setVar
:: forall a b s. (Num a, Num b, Reifies s W)
=> Lens' b a
-> BVar s a
-> BVar s b
-> BVar s b
setVar :: forall a b s.
(Num a, Num b, Reifies s W) =>
Lens' b a -> BVar s a -> BVar s b -> BVar s b
setVar = forall a b s.
Reifies s W =>
AddFunc a
-> AddFunc b
-> ZeroFunc a
-> Lens' b a
-> BVar s a
-> BVar s b
-> BVar s b
E.setVar forall a. Num a => AddFunc a
E.afNum forall a. Num a => AddFunc a
E.afNum forall a. Num a => ZeroFunc a
E.zfNum
{-# INLINE setVar #-}
(%~~)
:: (Num a, Num b, Reifies s W)
=> Lens' b a
-> (BVar s a -> BVar s a)
-> BVar s b
-> BVar s b
Lens' b a
l %~~ :: forall a b s.
(Num a, Num b, Reifies s W) =>
Lens' b a -> (BVar s a -> BVar s a) -> BVar s b -> BVar s b
%~~ BVar s a -> BVar s a
f = forall a b s.
(Num a, Num b, Reifies s W) =>
Lens' b a -> (BVar s a -> BVar s a) -> BVar s b -> BVar s b
overVar Lens' b a
l BVar s a -> BVar s a
f
infixr 4 %~~
{-# INLINE (%~~) #-}
overVar
:: (Num a, Num b, Reifies s W)
=> Lens' b a
-> (BVar s a -> BVar s a)
-> BVar s b
-> BVar s b
overVar :: forall a b s.
(Num a, Num b, Reifies s W) =>
Lens' b a -> (BVar s a -> BVar s a) -> BVar s b -> BVar s b
overVar = forall s a b.
Reifies s W =>
AddFunc a
-> AddFunc b
-> ZeroFunc a
-> ZeroFunc b
-> Lens' b a
-> (BVar s a -> BVar s a)
-> BVar s b
-> BVar s b
E.overVar forall a. Num a => AddFunc a
E.afNum forall a. Num a => AddFunc a
E.afNum forall a. Num a => ZeroFunc a
E.zfNum forall a. Num a => ZeroFunc a
E.zfNum
{-# INLINE overVar #-}
(^^?)
:: forall b a s. (Num b, Num a, Reifies s W)
=> BVar s b
-> Traversal' b a
-> Maybe (BVar s a)
BVar s b
v ^^? :: forall b a s.
(Num b, Num a, Reifies s W) =>
BVar s b -> Traversal' b a -> Maybe (BVar s a)
^^? Traversal' b a
t = forall b a s.
(Num b, Num a, Reifies s W) =>
Traversal' b a -> BVar s b -> Maybe (BVar s a)
previewVar Traversal' b a
t BVar s b
v
infixl 8 ^^?
{-# INLINE (^^?) #-}
(^^?!)
:: forall b a s. (Num b, Num a, Reifies s W)
=> BVar s b
-> Traversal' b a
-> BVar s a
BVar s b
v ^^?! :: forall b a s.
(Num b, Num a, Reifies s W) =>
BVar s b -> Traversal' b a -> BVar s a
^^?! Traversal' b a
t = forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => [Char] -> a
error [Char]
e) (forall b a s.
(Num b, Num a, Reifies s W) =>
Traversal' b a -> BVar s b -> Maybe (BVar s a)
previewVar Traversal' b a
t BVar s b
v)
where
e :: [Char]
e = [Char]
"Numeric.Backprop.Num.^^?!: Empty traversal"
infixl 8 ^^?!
{-# INLINE (^^?!) #-}
previewVar
:: forall b a s. (Num b, Num a, Reifies s W)
=> Traversal' b a
-> BVar s b
-> Maybe (BVar s a)
previewVar :: forall b a s.
(Num b, Num a, Reifies s W) =>
Traversal' b a -> BVar s b -> Maybe (BVar s a)
previewVar = forall b a s.
Reifies s W =>
AddFunc a
-> ZeroFunc b -> Traversal' b a -> BVar s b -> Maybe (BVar s a)
E.previewVar forall a. Num a => AddFunc a
E.afNum forall a. Num a => ZeroFunc a
E.zfNum
{-# INLINE previewVar #-}
(^^..)
:: forall b a s. (Num b, Num a, Reifies s W)
=> BVar s b
-> Traversal' b a
-> [BVar s a]
BVar s b
v ^^.. :: forall b a s.
(Num b, Num a, Reifies s W) =>
BVar s b -> Traversal' b a -> [BVar s a]
^^.. Traversal' b a
t = forall b a s.
(Num b, Num a, Reifies s W) =>
Traversal' b a -> BVar s b -> [BVar s a]
toListOfVar Traversal' b a
t BVar s b
v
{-# INLINE (^^..) #-}
toListOfVar
:: forall b a s. (Num b, Num a, Reifies s W)
=> Traversal' b a
-> BVar s b
-> [BVar s a]
toListOfVar :: forall b a s.
(Num b, Num a, Reifies s W) =>
Traversal' b a -> BVar s b -> [BVar s a]
toListOfVar = forall b a s.
Reifies s W =>
AddFunc a -> ZeroFunc b -> Traversal' b a -> BVar s b -> [BVar s a]
E.toListOfVar forall a. Num a => AddFunc a
E.afNum forall a. Num a => ZeroFunc a
E.zfNum
{-# INLINE toListOfVar #-}
sequenceVar
:: (Traversable t, Num a, Reifies s W)
=> BVar s (t a)
-> t (BVar s a)
sequenceVar :: forall (t :: * -> *) a s.
(Traversable t, Num a, Reifies s W) =>
BVar s (t a) -> t (BVar s a)
sequenceVar = forall (t :: * -> *) a s.
(Reifies s W, Traversable t) =>
AddFunc a -> ZeroFunc a -> BVar s (t a) -> t (BVar s a)
E.sequenceVar forall a. Num a => AddFunc a
E.afNum forall a. Num a => ZeroFunc a
E.zfNum
{-# INLINE sequenceVar #-}
collectVar
:: (Foldable t, Functor t, Num a, Reifies s W)
=> t (BVar s a)
-> BVar s (t a)
collectVar :: forall (t :: * -> *) a s.
(Foldable t, Functor t, Num a, Reifies s W) =>
t (BVar s a) -> BVar s (t a)
collectVar = forall (t :: * -> *) a s.
(Reifies s W, Foldable t, Functor t) =>
AddFunc a -> ZeroFunc a -> t (BVar s a) -> BVar s (t a)
E.collectVar forall a. Num a => AddFunc a
E.afNum forall a. Num a => ZeroFunc a
E.zfNum
{-# INLINE collectVar #-}
liftOp
:: (RPureConstrained Num as, Reifies s W)
=> Op as b
-> Rec (BVar s) as
-> BVar s b
liftOp :: forall (as :: [*]) s b.
(RPureConstrained Num as, Reifies s W) =>
Op as b -> Rec (BVar s) as -> BVar s b
liftOp = forall (as :: [*]) b s.
Reifies s W =>
Rec AddFunc as -> Op as b -> Rec (BVar s) as -> BVar s b
E.liftOp forall (as :: [*]). RPureConstrained Num as => Rec AddFunc as
E.afNums
{-# INLINE liftOp #-}
liftOp1
:: (Num a, Reifies s W)
=> Op '[a] b
-> BVar s a
-> BVar s b
liftOp1 :: forall a s b.
(Num a, Reifies s W) =>
Op '[a] b -> BVar s a -> BVar s b
liftOp1 = forall a b s.
Reifies s W =>
AddFunc a -> Op '[a] b -> BVar s a -> BVar s b
E.liftOp1 forall a. Num a => AddFunc a
E.afNum
{-# INLINE liftOp1 #-}
liftOp2
:: (Num a, Num b, Reifies s W)
=> Op '[a,b] c
-> BVar s a
-> BVar s b
-> BVar s c
liftOp2 :: forall a b s c.
(Num a, Num b, Reifies s W) =>
Op '[a, b] c -> BVar s a -> BVar s b -> BVar s c
liftOp2 = forall a b c s.
Reifies s W =>
AddFunc a
-> AddFunc b -> Op '[a, b] c -> BVar s a -> BVar s b -> BVar s c
E.liftOp2 forall a. Num a => AddFunc a
E.afNum forall a. Num a => AddFunc a
E.afNum
{-# INLINE liftOp2 #-}
liftOp3
:: (Num a, Num b, Num c, Reifies s W)
=> Op '[a,b,c] d
-> BVar s a
-> BVar s b
-> BVar s c
-> BVar s d
liftOp3 :: forall a b c s d.
(Num a, Num b, Num c, Reifies s W) =>
Op '[a, b, c] d -> BVar s a -> BVar s b -> BVar s c -> BVar s d
liftOp3 = forall a b c d s.
Reifies s W =>
AddFunc a
-> AddFunc b
-> AddFunc c
-> Op '[a, b, c] d
-> BVar s a
-> BVar s b
-> BVar s c
-> BVar s d
E.liftOp3 forall a. Num a => AddFunc a
E.afNum forall a. Num a => AddFunc a
E.afNum forall a. Num a => AddFunc a
E.afNum
{-# INLINE liftOp3 #-}
isoVar
:: (Num a, Reifies s W)
=> (a -> b)
-> (b -> a)
-> BVar s a
-> BVar s b
isoVar :: forall a s b.
(Num a, Reifies s W) =>
(a -> b) -> (b -> a) -> BVar s a -> BVar s b
isoVar = forall s a b.
Reifies s W =>
AddFunc a -> (a -> b) -> (b -> a) -> BVar s a -> BVar s b
E.isoVar forall a. Num a => AddFunc a
E.afNum
{-# INLINE isoVar #-}
isoVar2
:: (Num a, Num b, Reifies s W)
=> (a -> b -> c)
-> (c -> (a, b))
-> BVar s a
-> BVar s b
-> BVar s c
isoVar2 :: forall a b s c.
(Num a, Num b, Reifies s W) =>
(a -> b -> c) -> (c -> (a, b)) -> BVar s a -> BVar s b -> BVar s c
isoVar2 = forall s a b c.
Reifies s W =>
AddFunc a
-> AddFunc b
-> (a -> b -> c)
-> (c -> (a, b))
-> BVar s a
-> BVar s b
-> BVar s c
E.isoVar2 forall a. Num a => AddFunc a
E.afNum forall a. Num a => AddFunc a
E.afNum
{-# INLINE isoVar2 #-}
isoVar3
:: (Num a, Num b, Num c, Reifies s W)
=> (a -> b -> c -> d)
-> (d -> (a, b, c))
-> BVar s a
-> BVar s b
-> BVar s c
-> BVar s d
isoVar3 :: forall a b c s d.
(Num a, Num b, Num c, Reifies s W) =>
(a -> b -> c -> d)
-> (d -> (a, b, c)) -> BVar s a -> BVar s b -> BVar s c -> BVar s d
isoVar3 = forall s a b c d.
Reifies s W =>
AddFunc a
-> AddFunc b
-> AddFunc c
-> (a -> b -> c -> d)
-> (d -> (a, b, c))
-> BVar s a
-> BVar s b
-> BVar s c
-> BVar s d
E.isoVar3 forall a. Num a => AddFunc a
E.afNum forall a. Num a => AddFunc a
E.afNum forall a. Num a => AddFunc a
E.afNum
{-# INLINE isoVar3 #-}
isoVarN
:: (RPureConstrained Num as, Reifies s W)
=> (Rec Identity as -> b)
-> (b -> Rec Identity as)
-> Rec (BVar s) as
-> BVar s b
isoVarN :: forall (as :: [*]) s b.
(RPureConstrained Num as, Reifies s W) =>
(Rec Identity as -> b)
-> (b -> Rec Identity as) -> Rec (BVar s) as -> BVar s b
isoVarN = forall s (as :: [*]) b.
Reifies s W =>
Rec AddFunc as
-> (Rec Identity as -> b)
-> (b -> Rec Identity as)
-> Rec (BVar s) as
-> BVar s b
E.isoVarN forall (as :: [*]). RPureConstrained Num as => Rec AddFunc as
E.afNums
{-# INLINE isoVarN #-}