backprop-0.2.7.2: Heterogeneous automatic differentation
Copyright(c) Justin Le 2023
LicenseBSD3
Maintainerjustin@jle.im
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Numeric.Backprop.Internal

Description

Provides the types and instances used for the graph building/back-propagation for the library.

Do not import this unless you want to mess with the internals and know what you're doing. Otherwise, use Numeric.Backprop, Numeric.Backprop.Op, etc.

Since: 0.2.7.0

Synopsis

Exported for re-use

data BVar s a Source #

A BVar s a is a value of type a that can be "backpropagated".

Functions referring to BVars are tracked by the library and can be automatically differentiated to get their gradients and results.

For simple numeric values, you can use its Num, Fractional, and Floating instances to manipulate them as if they were the numbers they represent.

If a contains items, the items can be accessed and extracted using lenses. A Lens' b a can be used to access an a inside a b, using ^^. (viewVar):

(^.)  ::        a -> Lens' a b ->        b
(^^.) :: BVar s a -> Lens' a b -> BVar s b

There is also ^^? (previewVar), to use a Prism' or Traversal' to extract a target that may or may not be present (which can implement pattern matching), ^^.. (toListOfVar) to use a Traversal' to extract all targets inside a BVar, and .~~ (setVar) to set and update values inside a BVar.

If you have control over your data type definitions, you can also use splitBV and joinBV to manipulate data types by easily extracting fields out of a BVar of data types and creating BVars of data types out of BVars of their fields. See Numeric.Backprop for a tutorial on this use pattern.

For more complex operations, libraries can provide functions on BVars using liftOp and related functions. This is how you can create primitive functions that users can use to manipulate your library's values. See https://backprop.jle.im/08-equipping-your-library.html for a detailed guide.

For example, the hmatrix library has a matrix-vector multiplication function, #> :: L m n -> R n -> L m.

A library could instead provide a function #> :: BVar (L m n) -> BVar (R n) -> BVar (R m), which the user can then use to manipulate their BVars of L m ns and R ns, etc.

See Numeric.Backprop and documentation for liftOp for more information.

Constructors

BV

Since: 0.2.7.1

Fields

Instances

Instances details
IsoHKD (BVar s :: Type -> Type) (a :: Type) Source #

Since: 0.2.6.3

Instance details

Defined in Numeric.Backprop.Internal

Associated Types

type HKD (BVar s :: Type -> Type) (a :: Type) 
Instance details

Defined in Numeric.Backprop.Internal

type HKD (BVar s :: Type -> Type) (a :: Type) = BVar s a

Methods

unHKD :: HKD (BVar s) a -> BVar s a #

toHKD :: BVar s a -> HKD (BVar s) a #

BVGroup s ('[] :: [Type]) (K1 i a :: Type -> Type) (K1 i (BVar s a) :: Type -> Type) Source # 
Instance details

Defined in Numeric.Backprop.Explicit

Methods

gsplitBV :: Rec AddFunc ('[] :: [Type]) -> Rec ZeroFunc ('[] :: [Type]) -> BVar s (K1 i a ()) -> K1 i (BVar s a) ()

gjoinBV :: Rec AddFunc ('[] :: [Type]) -> Rec ZeroFunc ('[] :: [Type]) -> K1 i (BVar s a) () -> BVar s (K1 i a ())

(Backprop a, Reifies s W) => Backprop (BVar s a) Source #

Since: 0.2.2.0

Instance details

Defined in Numeric.Backprop.Internal

Methods

zero :: BVar s a -> BVar s a Source #

add :: BVar s a -> BVar s a -> BVar s a Source #

one :: BVar s a -> BVar s a Source #

(Floating a, Reifies s W) => Floating (BVar s a) Source # 
Instance details

Defined in Numeric.Backprop.Internal

Methods

pi :: BVar s a #

exp :: BVar s a -> BVar s a #

log :: BVar s a -> BVar s a #

sqrt :: BVar s a -> BVar s a #

(**) :: BVar s a -> BVar s a -> BVar s a #

logBase :: BVar s a -> BVar s a -> BVar s a #

sin :: BVar s a -> BVar s a #

cos :: BVar s a -> BVar s a #

tan :: BVar s a -> BVar s a #

asin :: BVar s a -> BVar s a #

acos :: BVar s a -> BVar s a #

atan :: BVar s a -> BVar s a #

sinh :: BVar s a -> BVar s a #

cosh :: BVar s a -> BVar s a #

tanh :: BVar s a -> BVar s a #

asinh :: BVar s a -> BVar s a #

acosh :: BVar s a -> BVar s a #

atanh :: BVar s a -> BVar s a #

log1p :: BVar s a -> BVar s a #

expm1 :: BVar s a -> BVar s a #

log1pexp :: BVar s a -> BVar s a #

log1mexp :: BVar s a -> BVar s a #

(Num a, Reifies s W) => Num (BVar s a) Source # 
Instance details

Defined in Numeric.Backprop.Internal

Methods

(+) :: BVar s a -> BVar s a -> BVar s a #

(-) :: BVar s a -> BVar s a -> BVar s a #

(*) :: BVar s a -> BVar s a -> BVar s a #

negate :: BVar s a -> BVar s a #

abs :: BVar s a -> BVar s a #

signum :: BVar s a -> BVar s a #

fromInteger :: Integer -> BVar s a #

(Fractional a, Reifies s W) => Fractional (BVar s a) Source # 
Instance details

Defined in Numeric.Backprop.Internal

Methods

(/) :: BVar s a -> BVar s a -> BVar s a #

recip :: BVar s a -> BVar s a #

fromRational :: Rational -> BVar s a #

NFData a => NFData (BVar s a) Source #

This will force the value inside, as well.

Instance details

Defined in Numeric.Backprop.Internal

Methods

rnf :: BVar s a -> () #

Eq a => Eq (BVar s a) Source #

Compares the values inside the BVar.

Since: 0.1.5.0

Instance details

Defined in Numeric.Backprop.Internal

Methods

(==) :: BVar s a -> BVar s a -> Bool #

(/=) :: BVar s a -> BVar s a -> Bool #

Ord a => Ord (BVar s a) Source #

Compares the values inside the BVar.

Since: 0.1.5.0

Instance details

Defined in Numeric.Backprop.Internal

Methods

compare :: BVar s a -> BVar s a -> Ordering #

(<) :: BVar s a -> BVar s a -> Bool #

(<=) :: BVar s a -> BVar s a -> Bool #

(>) :: BVar s a -> BVar s a -> Bool #

(>=) :: BVar s a -> BVar s a -> Bool #

max :: BVar s a -> BVar s a -> BVar s a #

min :: BVar s a -> BVar s a -> BVar s a #

type HKD (BVar s :: Type -> Type) (a :: Type) Source # 
Instance details

Defined in Numeric.Backprop.Internal

type HKD (BVar s :: Type -> Type) (a :: Type) = BVar s a

newtype W Source #

An ephemeral Wengert Tape in the environment. Used internally to track of the computational graph of variables.

For the end user, one can just imagine Reifies s W as a required constraint on s that allows backpropagation to work.

Constructors

W

Since: 0.2.7.1

Fields

backpropWithN :: forall (as :: [Type]) b. Rec ZeroFunc as -> (forall s. Reifies s W => Rec (BVar s) as -> BVar s b) -> Rec Identity as -> (b, b -> Rec Identity as) Source #

backpropWithN, but with explicit zero and one.

Note that argument order changed in v0.2.4.

Since: 0.2.0.0

evalBPN :: forall (as :: [Type]) b. (forall s. Reifies s W => Rec (BVar s) as -> BVar s b) -> Rec Identity as -> b Source #

evalBP generalized to multiple inputs of different types. See documentation for backpropN for more details.

constVar :: a -> BVar s a Source #

Lift a value into a BVar representing a constant value.

This value will not be considered an input, and its gradients will not be backpropagated.

liftOp :: forall (as :: [Type]) b s. Reifies s W => Rec AddFunc as -> Op as b -> Rec (BVar s) as -> BVar s b Source #

liftOp, but with explicit add and zero.

liftOp1 :: forall a b s. Reifies s W => AddFunc a -> Op '[a] b -> BVar s a -> BVar s b Source #

liftOp1, but with explicit add and zero.

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 Source #

liftOp2, but with explicit add and zero.

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 Source #

liftOp3, but with explicit add and zero.

viewVar :: forall a b s. Reifies s W => AddFunc a -> ZeroFunc b -> Lens' b a -> BVar s b -> BVar s a Source #

viewVar, but with explicit add and zero.

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 Source #

setVar, but with explicit add and zero.

sequenceVar :: forall t a s. (Reifies s W, Traversable t) => AddFunc a -> ZeroFunc a -> BVar s (t a) -> t (BVar s a) Source #

sequenceVar, but with explicit add and zero.

collectVar :: forall t a s. (Reifies s W, Foldable t, Functor t) => AddFunc a -> ZeroFunc a -> t (BVar s a) -> BVar s (t a) Source #

collectVar, but with explicit add and zero.

previewVar :: forall b a s. Reifies s W => AddFunc a -> ZeroFunc b -> Traversal' b a -> BVar s b -> Maybe (BVar s a) Source #

previewVar, but with explicit add and zero.

toListOfVar :: forall b a s. Reifies s W => AddFunc a -> ZeroFunc b -> Traversal' b a -> BVar s b -> [BVar s a] Source #

toListOfVar, but with explicit add and zero.

coerceVar :: Coercible a b => BVar s a -> BVar s b Source #

Coerce a BVar contents. Useful for things like newtype wrappers.

Since: 0.1.5.2

Func wrappers

newtype ZeroFunc a Source #

"Zero out" all components of a value. For scalar values, this should just be const 0. For vectors and matrices, this should set all components to zero, the additive identity.

Should be idempotent: Applying the function twice is the same as applying it just once.

Each type should ideally only have one ZeroFunc. This coherence constraint is given by the typeclass Backprop.

Since: 0.2.0.0

Constructors

ZF 

Fields

zfNum :: Num a => ZeroFunc a Source #

If a type has a Num instance, this is the canonical ZeroFunc.

Since: 0.2.0.0

zeroFunc :: Backprop a => ZeroFunc a Source #

The canonical ZeroFunc for instances of Backprop.

Since: 0.2.0.0

newtype AddFunc a Source #

Add together two values of a type. To combine contributions of gradients, so should ideally be information-preserving.

See laws for Backprop for the laws this should be expected to preserve. Namely, it should be commutative and associative, with an identity for a valid ZeroFunc.

Each type should ideally only have one AddFunc. This coherence constraint is given by the typeclass Backprop.

Since: 0.2.0.0

Constructors

AF 

Fields

afNum :: Num a => AddFunc a Source #

If a type has a Num instance, this is the canonical AddFunc.

Since: 0.2.0.0

addFunc :: Backprop a => AddFunc a Source #

The canonical AddFunc for instances of Backprop.

Since: 0.2.0.0

newtype OneFunc a Source #

One all components of a value. For scalar values, this should just be const 1. For vectors and matrices, this should set all components to one, the multiplicative identity.

Should be idempotent: Applying the function twice is the same as applying it just once.

Each type should ideally only have one OneFunc. This coherence constraint is given by the typeclass Backprop.

Since: 0.2.0.0

Constructors

OF 

Fields

ofNum :: Num a => OneFunc a Source #

If a type has a Num instance, this is the canonical OneFunc.

Since: 0.2.0.0

oneFunc :: Backprop a => OneFunc a Source #

The canonical OneFunc for instances of Backprop.

Since: 0.2.0.0

Debug

debugSTN :: SomeTapeNode -> String Source #

Debugging string for a SomeTapeMode.

debugIR :: InpRef a -> String Source #

Debugging string for an InpRef.

Only used internally within this module

data TapeNode a where Source #

Since: 0.2.7.1

Constructors

TN 

Fields

data SomeTapeNode where Source #

Since: 0.2.7.1

Constructors

STN 

Fields

data BRef s Source #

Since: 0.2.7.1

Constructors

BRInp !Int 
BRIx !Int 
BRC 

Instances

Instances details
Generic (BRef s) Source # 
Instance details

Defined in Numeric.Backprop.Internal

Associated Types

type Rep (BRef s) 
Instance details

Defined in Numeric.Backprop.Internal

type Rep (BRef s) = D1 ('MetaData "BRef" "Numeric.Backprop.Internal" "backprop-0.2.7.2-4vOLact0bYs25u94sL17w8" 'False) (C1 ('MetaCons "BRInp" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int)) :+: (C1 ('MetaCons "BRIx" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int)) :+: C1 ('MetaCons "BRC" 'PrefixI 'False) (U1 :: Type -> Type)))

Methods

from :: BRef s -> Rep (BRef s) x #

to :: Rep (BRef s) x -> BRef s #

Show (BRef s) Source # 
Instance details

Defined in Numeric.Backprop.Internal

Methods

showsPrec :: Int -> BRef s -> ShowS #

show :: BRef s -> String #

showList :: [BRef s] -> ShowS #

NFData (BRef s) Source # 
Instance details

Defined in Numeric.Backprop.Internal

Methods

rnf :: BRef s -> () #

type Rep (BRef s) Source # 
Instance details

Defined in Numeric.Backprop.Internal

type Rep (BRef s) = D1 ('MetaData "BRef" "Numeric.Backprop.Internal" "backprop-0.2.7.2-4vOLact0bYs25u94sL17w8" 'False) (C1 ('MetaCons "BRInp" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int)) :+: (C1 ('MetaCons "BRIx" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int)) :+: C1 ('MetaCons "BRC" 'PrefixI 'False) (U1 :: Type -> Type)))

data Runner s Source #

Since: 0.2.7.1

Constructors

R 

Fields

data InpRef a where Source #

Since: 0.2.7.2

Constructors

IR 

Fields

initWengert :: IO W Source #

Since: 0.2.7.1

insertNode Source #

Arguments

:: TapeNode a 
-> a

val

-> W 
-> IO (BVar s a) 

Since: 0.2.7.1

bvConst :: BVar s a -> Maybe a Source #

Project out a constant value if the BVar refers to one.

forceBVar :: BVar s a -> () Source #

forceInpRef :: InpRef a -> () Source #

Since: 0.2.7.1

forceSomeTapeNode :: SomeTapeNode -> () Source #

Since: 0.2.7.1

forceTapeNode :: TapeNode a -> () Source #

Since: 0.2.7.1

fillWengert :: forall (as :: [Type]) b. (forall s. Reifies s W => Rec (BVar s) as -> BVar s b) -> Rec Identity as -> IO (Either Int (Int, [SomeTapeNode]), b) Source #

bumpMaybe Source #

Arguments

:: a

val

-> (a -> b -> b)

add

-> (a -> b)

embed

-> Maybe b 
-> Maybe b 

Since: 0.2.7.1

initRunner :: (Int, [SomeTapeNode]) -> (Int, [Maybe (Any :: Type)]) -> ST s (Runner s) Source #

Since: 0.2.7.1

gradRunner Source #

Arguments

:: b

one

-> Runner s 
-> (Int, [SomeTapeNode]) 
-> ST s () 

Since: 0.2.7.1