backprop-0.2.0.0: Heterogeneous automatic differentation (backpropagation)

Copyright(c) Justin Le 2018
LicenseBSD3
Maintainerjustin@jle.im
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Numeric.Backprop.Class

Contents

Description

Provides the Backprop typeclass, a class for values that can be used for backpropagation.

This class replaces the old (version 0.1) API relying on Num.

Since: 0.2.0.0

Synopsis

Backpropagatable types

class Backprop a where Source #

Class of values that can be backpropagated in general.

For instances of Num, these methods can be given by zeroNum, addNum, and oneNum. There are also generic options given in Numeric.Backprop.Class for functors, IsList instances, and Generic instances.

instance Backprop Double where
    zero = zeroNum
    add = addNum
    one = oneNum

If you leave the body of an instance declaration blank, GHC Generics will be used to derive instances if the type has a single constructor and each field is an instance of Backprop.

To ensure that backpropagation works in a sound way, should obey the laws:

identity

Also implies preservation of information, making zipWith (+) an illegal implementation for lists and vectors.

This is only expected to be true up to potential "extra zeroes" in x and y in the result.

commutativity
associativity
idempotence

Note that not all values in the backpropagation process needs all of these methods: Only the "final result" needs one, for example. These are all grouped under one typeclass for convenience in defining instances, and also to talk about sensible laws. For fine-grained control, use the "explicit" versions of library functions (for example, in Numeric.Backprop.Explicit) instead of Backprop based ones.

This typeclass replaces the reliance on Num of the previous API (v0.1). Num is strictly more powerful than Backprop, and is a stronger constraint on types than is necessary for proper backpropagating. In particular, fromInteger is a problem for many types, preventing useful backpropagation for lists, variable-length vectors (like Data.Vector) and variable-size matrices from linear algebra libraries like hmatrix and accelerate.

Since: 0.2.0.0

Methods

zero :: a -> 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:

Should be as lazy as possible. This behavior is observed for all instances provided by this library.

See zeroNum for a pre-built definition for instances of Num and zeroFunctor for a definition for instances of Functor. If left blank, will automatically be genericZero, a pre-built definition for instances of Generic whose fields are all themselves instances of Backprop.

add :: a -> a -> a Source #

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

Should be as strict as possible. This behavior is observed for all instances provided by this library.

See addNum for a pre-built definition for instances of Num and addFunctor for a definition for instances of Functor. If left blank, will automatically be genericAdd, a pre-built definition for instances of Generic with one constructor whose fields are all themselves instances of Backprop.

one :: a -> 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:

Should be as lazy as possible. This behavior is observed for all instances provided by this library.

See oneNum for a pre-built definition for instances of Num and oneFunctor for a definition for instances of Functor. If left blank, will automatically be genericOne, a pre-built definition for instances of Generic whose fields are all themselves instances of Backprop.

zero :: (Generic a, GZero (Rep a)) => a -> 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:

Should be as lazy as possible. This behavior is observed for all instances provided by this library.

See zeroNum for a pre-built definition for instances of Num and zeroFunctor for a definition for instances of Functor. If left blank, will automatically be genericZero, a pre-built definition for instances of Generic whose fields are all themselves instances of Backprop.

add :: (Generic a, GAdd (Rep a)) => a -> a -> a Source #

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

Should be as strict as possible. This behavior is observed for all instances provided by this library.

See addNum for a pre-built definition for instances of Num and addFunctor for a definition for instances of Functor. If left blank, will automatically be genericAdd, a pre-built definition for instances of Generic with one constructor whose fields are all themselves instances of Backprop.

one :: (Generic a, GOne (Rep a)) => a -> 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:

Should be as lazy as possible. This behavior is observed for all instances provided by this library.

See oneNum for a pre-built definition for instances of Num and oneFunctor for a definition for instances of Functor. If left blank, will automatically be genericOne, a pre-built definition for instances of Generic whose fields are all themselves instances of Backprop.

Instances

Backprop Double Source # 
Backprop Float Source # 
Backprop Int Source # 

Methods

zero :: Int -> Int Source #

add :: Int -> Int -> Int Source #

one :: Int -> Int Source #

Backprop Integer Source # 
Backprop () Source #

add is strict, but zero and one are lazy in their arguments.

Methods

zero :: () -> () Source #

add :: () -> () -> () Source #

one :: () -> () Source #

Backprop Void Source # 

Methods

zero :: Void -> Void Source #

add :: Void -> Void -> Void Source #

one :: Void -> Void Source #

Backprop a => Backprop [a] Source #

add assumes the shorter list has trailing zeroes, and the result has the length of the longest input.

Methods

zero :: [a] -> [a] Source #

add :: [a] -> [a] -> [a] Source #

one :: [a] -> [a] Source #

Backprop a => Backprop (Maybe a) Source #

Nothing is treated the same as Just 0. However, zero, add, and one preserve Nothing if all inputs are also Nothing.

Methods

zero :: Maybe a -> Maybe a Source #

add :: Maybe a -> Maybe a -> Maybe a Source #

one :: Maybe a -> Maybe a Source #

Integral a => Backprop (Ratio a) Source # 

Methods

zero :: Ratio a -> Ratio a Source #

add :: Ratio a -> Ratio a -> Ratio a Source #

one :: Ratio a -> Ratio a Source #

RealFloat a => Backprop (Complex a) Source # 

Methods

zero :: Complex a -> Complex a Source #

add :: Complex a -> Complex a -> Complex a Source #

one :: Complex a -> Complex a Source #

Backprop a => Backprop (NonEmpty a) Source #

add assumes the shorter list has trailing zeroes, and the result has the length of the longest input.

Backprop a => Backprop (Identity a) Source # 
Backprop a => Backprop (IntMap a) Source #

zero and one replace all current values, and add merges keys from both maps, adding in the case of double-occurrences.

Methods

zero :: IntMap a -> IntMap a Source #

add :: IntMap a -> IntMap a -> IntMap a Source #

one :: IntMap a -> IntMap a Source #

Backprop a => Backprop (Seq a) Source #

add assumes the shorter sequence has trailing zeroes, and the result has the length of the longest input.

Methods

zero :: Seq a -> Seq a Source #

add :: Seq a -> Seq a -> Seq a Source #

one :: Seq a -> Seq a Source #

Backprop a => Backprop (I a) Source # 

Methods

zero :: I a -> I a Source #

add :: I a -> I a -> I a Source #

one :: I a -> I a Source #

(Unbox a, Backprop a) => Backprop (Vector a) Source # 

Methods

zero :: Vector a -> Vector a Source #

add :: Vector a -> Vector a -> Vector a Source #

one :: Vector a -> Vector a Source #

(Storable a, Backprop a) => Backprop (Vector a) Source # 

Methods

zero :: Vector a -> Vector a Source #

add :: Vector a -> Vector a -> Vector a Source #

one :: Vector a -> Vector a Source #

(Prim a, Backprop a) => Backprop (Vector a) Source # 

Methods

zero :: Vector a -> Vector a Source #

add :: Vector a -> Vector a -> Vector a Source #

one :: Vector a -> Vector a Source #

Backprop a => Backprop (Vector a) Source # 

Methods

zero :: Vector a -> Vector a Source #

add :: Vector a -> Vector a -> Vector a Source #

one :: Vector a -> Vector a Source #

(Backprop a, Backprop b) => Backprop (a, b) Source #

add is strict

Methods

zero :: (a, b) -> (a, b) Source #

add :: (a, b) -> (a, b) -> (a, b) Source #

one :: (a, b) -> (a, b) Source #

Backprop (Proxy * a) Source #

add is strict, but zero and one are lazy in their arguments.

Methods

zero :: Proxy * a -> Proxy * a Source #

add :: Proxy * a -> Proxy * a -> Proxy * a Source #

one :: Proxy * a -> Proxy * a Source #

(Backprop a, Ord k) => Backprop (Map k a) Source #

zero and one replace all current values, and add merges keys from both maps, adding in the case of double-occurrences.

Methods

zero :: Map k a -> Map k a Source #

add :: Map k a -> Map k a -> Map k a Source #

one :: Map k a -> Map k a Source #

(Backprop a, Backprop b, Backprop c) => Backprop (a, b, c) Source #

add is strict

Methods

zero :: (a, b, c) -> (a, b, c) Source #

add :: (a, b, c) -> (a, b, c) -> (a, b, c) Source #

one :: (a, b, c) -> (a, b, c) Source #

ListC ((<$>) * Constraint Backprop ((<$>) * * f as)) => Backprop (Prod * f as) Source # 

Methods

zero :: Prod * f as -> Prod * f as Source #

add :: Prod * f as -> Prod * f as -> Prod * f as Source #

one :: Prod * f as -> Prod * f as Source #

MaybeC ((<$>) * Constraint Backprop ((<$>) * * f a)) => Backprop (Option * f a) Source # 

Methods

zero :: Option * f a -> Option * f a Source #

add :: Option * f a -> Option * f a -> Option * f a Source #

one :: Option * f a -> Option * f a Source #

(Backprop a, Backprop b, Backprop c, Backprop d) => Backprop (a, b, c, d) Source #

add is strict

Methods

zero :: (a, b, c, d) -> (a, b, c, d) Source #

add :: (a, b, c, d) -> (a, b, c, d) -> (a, b, c, d) Source #

one :: (a, b, c, d) -> (a, b, c, d) Source #

(Backprop a, Backprop b, Backprop c, Backprop d, Backprop e) => Backprop (a, b, c, d, e) Source #

add is strict

Methods

zero :: (a, b, c, d, e) -> (a, b, c, d, e) Source #

add :: (a, b, c, d, e) -> (a, b, c, d, e) -> (a, b, c, d, e) Source #

one :: (a, b, c, d, e) -> (a, b, c, d, e) Source #

Derived methods

zeroNum :: Num a => a -> a Source #

zero for instances of Num.

Is lazy in its argument.

addNum :: Num a => a -> a -> a Source #

add for instances of Num.

oneNum :: Num a => a -> a Source #

one for instances of Num.

Is lazy in its argument.

zeroVec :: (Vector v a, Backprop a) => v a -> v a Source #

zero for instances of Vector.

addVec :: (Vector v a, Backprop a) => v a -> v a -> v a Source #

add for instances of Vector. Automatically pads the end of the shorter vector with zeroes.

oneVec :: (Vector v a, Backprop a) => v a -> v a Source #

one for instances of Vector.

zeroFunctor :: (Functor f, Backprop a) => f a -> f a Source #

zero for Functor instances.

addIsList :: (IsList a, Backprop (Item a)) => a -> a -> a Source #

add for instances of IsList. Automatically pads the end of the "shorter" value with zeroes.

addAsList Source #

Arguments

:: Backprop b 
=> (a -> [b])

convert to list (should form isomorphism)

-> ([b] -> a)

convert from list (should form isomorphism)

-> a 
-> a 
-> a 

add for types that are isomorphic to a list. Automatically pads the end of the "shorter" value with zeroes.

oneFunctor :: (Functor f, Backprop a) => f a -> f a Source #

one for instances of Functor.

genericZero :: (Generic a, GZero (Rep a)) => a -> a Source #

zero using GHC Generics; works if all fields are instances of Backprop.

genericAdd :: (Generic a, GAdd (Rep a)) => a -> a -> a Source #

add using GHC Generics; works if all fields are instances of Backprop, but only for values with single constructors.

genericOne :: (Generic a, GOne (Rep a)) => a -> a Source #

one using GHC Generics; works if all fields are instaces of Backprop.

Generics

class GZero f where Source #

Helper class for automatically deriving zero using GHC Generics.

Minimal complete definition

gzero

Methods

gzero :: f t -> f t Source #

Instances

GZero (V1 *) Source # 

Methods

gzero :: V1 * t -> V1 * t Source #

GZero (U1 *) Source # 

Methods

gzero :: U1 * t -> U1 * t Source #

Backprop a => GZero (K1 * i a) Source # 

Methods

gzero :: K1 * i a t -> K1 * i a t Source #

(GZero f, GZero g) => GZero ((:+:) * f g) Source # 

Methods

gzero :: (* :+: f) g t -> (* :+: f) g t Source #

(GZero f, GZero g) => GZero ((:*:) * f g) Source # 

Methods

gzero :: (* :*: f) g t -> (* :*: f) g t Source #

GZero f => GZero (M1 * i c f) Source # 

Methods

gzero :: M1 * i c f t -> M1 * i c f t Source #

GZero f => GZero ((:.:) * * f g) Source # 

Methods

gzero :: (* :.: *) f g t -> (* :.: *) f g t Source #

class GAdd f where Source #

Helper class for automatically deriving add using GHC Generics.

Minimal complete definition

gadd

Methods

gadd :: f t -> f t -> f t Source #

Instances

GAdd (V1 *) Source # 

Methods

gadd :: V1 * t -> V1 * t -> V1 * t Source #

GAdd (U1 *) Source # 

Methods

gadd :: U1 * t -> U1 * t -> U1 * t Source #

Backprop a => GAdd (K1 * i a) Source # 

Methods

gadd :: K1 * i a t -> K1 * i a t -> K1 * i a t Source #

(GAdd f, GAdd g) => GAdd ((:*:) * f g) Source # 

Methods

gadd :: (* :*: f) g t -> (* :*: f) g t -> (* :*: f) g t Source #

GAdd f => GAdd (M1 * i c f) Source # 

Methods

gadd :: M1 * i c f t -> M1 * i c f t -> M1 * i c f t Source #

GAdd f => GAdd ((:.:) * * f g) Source # 

Methods

gadd :: (* :.: *) f g t -> (* :.: *) f g t -> (* :.: *) f g t Source #

class GOne f where Source #

Helper class for automatically deriving one using GHC Generics.

Minimal complete definition

gone

Methods

gone :: f t -> f t Source #

Instances

GOne (V1 *) Source # 

Methods

gone :: V1 * t -> V1 * t Source #

GOne (U1 *) Source # 

Methods

gone :: U1 * t -> U1 * t Source #

Backprop a => GOne (K1 * i a) Source # 

Methods

gone :: K1 * i a t -> K1 * i a t Source #

(GOne f, GOne g) => GOne ((:+:) * f g) Source # 

Methods

gone :: (* :+: f) g t -> (* :+: f) g t Source #

(GOne f, GOne g) => GOne ((:*:) * f g) Source # 

Methods

gone :: (* :*: f) g t -> (* :*: f) g t Source #

GOne f => GOne (M1 * i c f) Source # 

Methods

gone :: M1 * i c f t -> M1 * i c f t Source #

GOne f => GOne ((:.:) * * f g) Source # 

Methods

gone :: (* :.: *) f g t -> (* :.: *) f g t Source #