neural-network-base- Yet Another High Performance and Extendable Neural Network in Haskell

Copyright(c) 2016 Jiasen Wu
LicenseBSD-style (see the file LICENSE)
MaintainerJiasen Wu <>
Safe HaskellSafe



This module defines an abstract interface for neural network and a protocol for its backends to follow.



class Component a where Source #

Abstraction of a neural network component

Minimal complete definition

forwardT, output, backward

Associated Types

type Run a :: * -> * Source #

execution environment

type Inp a Source #

the type of input and in-error

type Out a Source #

the type of output and out-error

data Trace a Source #

the trace of a forward propagation


forwardT :: a -> Inp a -> Run a (Trace a) Source #

Forward propagation

forward :: Applicative (Run a) => a -> Inp a -> Run a (Out a) Source #

Forward propagation

output :: Trace a -> Out a Source #

extract the output value from the trace

backward :: a -> Trace a -> Out a -> Float -> Run a (a, Inp a) Source #

Backward propagation

learn Source #


:: (Component n, Monad (Run n)) 
=> (Out n -> Out n -> Run n (Out n))

derivative of the error function

-> Float

learning rate

-> n

neuron network

-> (Inp n, Out n)

input and expect output

-> Run n n

updated network

By giving a way to measure the error, learn can update the neural network component.

relu :: (Num a, Ord a) => a -> a Source #

default RELU and derivative of RELU

relu' :: (Num a, Ord a) => a -> a Source #

default RELU and derivative of RELU

cost' :: (Num a, Ord a) => a -> a -> a Source #

default derivative of error measurement

class Backend b s where Source #

Abstraction of backend to carry out the specification

Minimal complete definition

witness, compile

Associated Types

type Env b :: * -> * Source #

environment to compile the specification

type ConvertFromSpec s :: * Source #

result type of compile


witness :: b -> s -> Dict (Monad (Env b), Monad (Run (ConvertFromSpec s)), Component (ConvertFromSpec s), RunInEnv (Run (ConvertFromSpec s)) (Env b)) Source #

necessary constraints of the resulting type

compile :: b -> s -> Env b (ConvertFromSpec s) Source #

compile the specification to runnable component.

class (Monad r, Monad e) => RunInEnv r e where Source #

Lifting from one monad to another. It is not necessary that the Env and Run maps to the same execution environment, but the Run one should be able to be lifted to Env one.

Minimal complete definition



run :: r a -> e a Source #

data a :++ b infixr 0 Source #

Specification: stacking layer


a :++ b infixr 0 

data SpecIn1D Source #

Specification: 1D input


In1D Int

dimension of input

data SpecIn2D Source #

Specification: 2D input


In2D Int Int

dimension of input

data SpecReshape2DAs1D Source #

Specification: reshaping layer



data SpecFullConnect Source #

Specification: full connection layer


FullConnect Int

number of neurals

data SpecConvolution Source #

Specification: convolution layer


Convolution Int Int Int

number of output channels, size of kernel, size of padding

data SpecMaxPooling Source #

Specification: max pooling layer


MaxPooling Int