on master help sign in
Docs
f9c282bHuw Campbell
grenade.cabal+11−2
@@ -5,11 +5,20 @@ license-file: LICENSE
5author: Huw Campbell <huw.campbell@gmail.com>
5author: Huw Campbell <huw.campbell@gmail.com>
6maintainer: Huw Campbell <huw.campbell@gmail.com>
6maintainer: Huw Campbell <huw.campbell@gmail.com>
7copyright: (c) 2016-2017 Huw Campbell.
7copyright: (c) 2016-2017 Huw Campbell.
8−synopsis: grenade
8+synopsis: Practical Deep Learning in Haskell
9category: System
9category: System
10cabal-version: >= 1.8
10cabal-version: >= 1.8
11build-type: Simple
11build-type: Simple
12−description: grenade.
12+description:
13+ Grenade is a composable, dependently typed, practical, and fast
14+ recurrent neural network library for concise and precise
15+ specifications of complex networks in Haskell.
16+ .
17+ Grenade provides an API for composing layers of a neural network
18+ into a sequence parallel graph in a type safe manner.
19+ .
20+ Documentation and examples are available on github
21+ <https://github.com/HuwCampbell/grenade>.
13
22
14extra-source-files:
23extra-source-files:
15 cbits/im2col.h
24 cbits/im2col.h
src/Grenade.hs+2−1
@@ -29,6 +29,7 @@ It provides an expressive type level API for the construction
29of complex neural network architectures. Backing this API is and
29of complex neural network architectures. Backing this API is and
30implementation written using BLAS and LAPACK, mostly provided by
30implementation written using BLAS and LAPACK, mostly provided by
31the hmatrix library.
31the hmatrix library.
32+
32-}
33-}
33
34
34{- $example
35{- $example
@@ -45,7 +46,7 @@ is a simple example which runs a logistic regression.
45> randomMyNet = randomNetwork
46> randomMyNet = randomNetwork
46
47
47The function `randomMyNet` witnesses the `CreatableNetwork`
48The function `randomMyNet` witnesses the `CreatableNetwork`
48−constraint of the neural network, that is it ensures the network
49+constraint of the neural network, that is, it ensures the network
49can be built, and hence, that the architecture is sound.
50can be built, and hence, that the architecture is sound.
50-}
51-}
51
52
src/Grenade/Core/Layer.hs+18−2
@@ -7,14 +7,30 @@
7{-# LANGUAGE RankNTypes #-}
7{-# LANGUAGE RankNTypes #-}
8{-# LANGUAGE FlexibleInstances #-}
8{-# LANGUAGE FlexibleInstances #-}
9{-|
9{-|
10−Module : Grenade.Core.Network
10+Module : Grenade.Core.Layer
11−Description : Core definition a simple neural etwork
11+Description : Defines the Layer Classes
12Copyright : (c) Huw Campbell, 2016-2017
12Copyright : (c) Huw Campbell, 2016-2017
13License : BSD2
13License : BSD2
14Stability : experimental
14Stability : experimental
15
15
16This module defines what a Layer is in a Grenade
16This module defines what a Layer is in a Grenade
17neural network.
17neural network.
18+
19+There are two classes of interest: `UpdateLayer` and `Layer`.
20+
21+`UpdateLayer` is required for all types which are used as a layer
22+in a network. Having no shape information, this class is agnotostic
23+to the input and output data of the layer.
24+
25+An instance of `Layer` on the other hand is required for usage in
26+a neural network, but also specifies the shapes of data that the
27+network can transform. Multiple instance of `Layer` are permitted
28+for a single type, to transform different shapes. The `Reshape` layer
29+for example can act as a flattening layer, and its inverse, projecting
30+a 1D shape up to 2 or 3 dimensions.
31+
32+Instances of `Layer` should be as strict as possible, and not emit
33+runtime errors.
18-}
34-}
19module Grenade.Core.Layer (
35module Grenade.Core.Layer (
20 Layer (..)
36 Layer (..)
src/Grenade/Core/LearningParameters.hs+7
@@ -1,3 +1,10 @@
1+{-|
2+Module : Grenade.Core.LearningParameters
3+Description : Stochastic gradient descent learning parameters
4+Copyright : (c) Huw Campbell, 2016-2017
5+License : BSD2
6+Stability : experimental
7+-}
1module Grenade.Core.LearningParameters (
8module Grenade.Core.LearningParameters (
2 -- | This module contains learning algorithm specific
9 -- | This module contains learning algorithm specific
3 -- code. Currently, this module should be consifered
10 -- code. Currently, this module should be consifered
src/Grenade/Core/Network.hs+5−4
@@ -9,7 +9,7 @@
9{-# LANGUAGE FlexibleInstances #-}
9{-# LANGUAGE FlexibleInstances #-}
10{-|
10{-|
11Module : Grenade.Core.Network
11Module : Grenade.Core.Network
12−Description : Core definition a simple neural etwork
12+Description : Core definition of a Neural Network
13Copyright : (c) Huw Campbell, 2016-2017
13Copyright : (c) Huw Campbell, 2016-2017
14License : BSD2
14License : BSD2
15Stability : experimental
15Stability : experimental
@@ -91,7 +91,6 @@ data Tapes :: [*] -> [Shape] -> * where
91--
91--
92-- This gives the output, and the Wengert tape required for back
92-- This gives the output, and the Wengert tape required for back
93-- propagation.
93-- propagation.
94−--
95runNetwork :: forall layers shapes.
94runNetwork :: forall layers shapes.
96 Network layers shapes
95 Network layers shapes
97 -> S (Head shapes)
96 -> S (Head shapes)
@@ -119,7 +118,6 @@ runNetwork =
119--
118--
120-- Gives the gradients for the layer, and the gradient across the
119-- Gives the gradients for the layer, and the gradient across the
121-- input (which may not be required).
120-- input (which may not be required).
122−--
123runGradient :: forall layers shapes.
121runGradient :: forall layers shapes.
124 Network layers shapes
122 Network layers shapes
125 -> Tapes layers shapes
123 -> Tapes layers shapes
@@ -179,12 +177,15 @@ instance (SingI i, SingI o, Layer x i o, Serialize x, Serialize (Network xs (o '
179
177
180-- | Ultimate composition.
178-- | Ultimate composition.
181--
179--
182−-- This allows a complete network to be treated as a layer in a bigger network.
180+-- This allows a complete network to be treated as a layer in a larger network.
183instance CreatableNetwork sublayers subshapes => UpdateLayer (Network sublayers subshapes) where
181instance CreatableNetwork sublayers subshapes => UpdateLayer (Network sublayers subshapes) where
184 type Gradient (Network sublayers subshapes) = Gradients sublayers
182 type Gradient (Network sublayers subshapes) = Gradients sublayers
185 runUpdate = applyUpdate
183 runUpdate = applyUpdate
186 createRandom = randomNetwork
184 createRandom = randomNetwork
187
185
186+-- | Ultimate composition.
187+--
188+-- This allows a complete network to be treated as a layer in a larger network.
188instance (CreatableNetwork sublayers subshapes, i ~ (Head subshapes), o ~ (Last subshapes)) => Layer (Network sublayers subshapes) i o where
189instance (CreatableNetwork sublayers subshapes, i ~ (Head subshapes), o ~ (Last subshapes)) => Layer (Network sublayers subshapes) i o where
189 type Tape (Network sublayers subshapes) i o = Tapes sublayers subshapes
190 type Tape (Network sublayers subshapes) i o = Tapes sublayers subshapes
190 runForwards = runNetwork
191 runForwards = runNetwork
src/Grenade/Core/Runner.hs+2−5
@@ -1,14 +1,11 @@
1{-# LANGUAGE DataKinds #-}
1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-|
3{-|
4−Module : Grenade.Core.Shape
4+Module : Grenade.Core.Runner
5−Description : Core definition of the Shapes of data we understand
5+Description : Functions to perform training and backpropagation
6Copyright : (c) Huw Campbell, 2016-2017
6Copyright : (c) Huw Campbell, 2016-2017
7License : BSD2
7License : BSD2
8Stability : experimental
8Stability : experimental
9−
10−This module defines simple back propagation and training functions
11−for a network.
12-}
9-}
13module Grenade.Core.Runner (
10module Grenade.Core.Runner (
14 train
11 train
src/Grenade/Core/Shape.hs+2−3
@@ -9,13 +9,12 @@
9{-# LANGUAGE RankNTypes #-}
9{-# LANGUAGE RankNTypes #-}
10{-|
10{-|
11Module : Grenade.Core.Shape
11Module : Grenade.Core.Shape
12−Description : Core definition of the Shapes of data we understand
12+Description : Dependently typed shapes of data which are passed between layers of a network
13Copyright : (c) Huw Campbell, 2016-2017
13Copyright : (c) Huw Campbell, 2016-2017
14License : BSD2
14License : BSD2
15Stability : experimental
15Stability : experimental
16
16
17−This module defines the core data types for the shapes of data that
17+
18−are understood by Grenade.
19-}
18-}
20module Grenade.Core.Shape (
19module Grenade.Core.Shape (
21 Shape (..)
20 Shape (..)
src/Grenade/Layers/Concat.hs+1−1
@@ -15,7 +15,7 @@ Copyright : (c) Huw Campbell, 2016-2017
15License : BSD2
15License : BSD2
16Stability : experimental
16Stability : experimental
17
17
18−This module provides the concatenation layer, whic used to run two separate layers in parallel and combine their outputs.
18+This module provides the concatenation layer, which runs two chilld layers in parallel and combines their outputs.
19-}
19-}
20module Grenade.Layers.Concat (
20module Grenade.Layers.Concat (
21 Concat (..)
21 Concat (..)
src/Grenade/Layers/Convolution.hs+10
@@ -7,6 +7,16 @@
7{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE FlexibleInstances #-}
8{-# LANGUAGE FlexibleInstances #-}
9{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE FlexibleContexts #-}
10+{-|
11+Module : Grenade.Layers.Convolution
12+Description : Convolution layer
13+Copyright : (c) Huw Campbell, 2016-2017
14+License : BSD2
15+Stability : experimental
16+
17+This module provides the Convolution layer, which is critical in many computer vision tasks.
18+
19+-}
10module Grenade.Layers.Convolution (
20module Grenade.Layers.Convolution (
11 Convolution (..)
21 Convolution (..)
12 , Convolution' (..)
22 , Convolution' (..)
src/Grenade/Layers/Crop.hs+7
@@ -5,6 +5,13 @@
5{-# LANGUAGE TypeFamilies #-}
5{-# LANGUAGE TypeFamilies #-}
6{-# LANGUAGE FlexibleContexts #-}
6{-# LANGUAGE FlexibleContexts #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8+{-|
9+Module : Grenade.Layers.Crop
10+Description : Cropping layer
11+Copyright : (c) Huw Campbell, 2016-2017
12+License : BSD2
13+Stability : experimental
14+-}
8module Grenade.Layers.Crop (
15module Grenade.Layers.Crop (
9 Crop (..)
16 Crop (..)
10 ) where
17 ) where
src/Grenade/Layers/Deconvolution.hs+39−30
@@ -7,6 +7,20 @@
7{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE FlexibleInstances #-}
8{-# LANGUAGE FlexibleInstances #-}
9{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE FlexibleContexts #-}
10+{-|
11+Module : Grenade.Layers.Deconvolution
12+Description : Deconvolution layer
13+Copyright : (c) Huw Campbell, 2016-2017
14+License : BSD2
15+Stability : experimental
16+
17+A deconvolution layer is in many ways a convolution layer in reverse.
18+It learns a kernel to apply to each pixel location, spreading it out
19+into a larger layer.
20+
21+This layer is important for image generation tasks, such as GANs on
22+images.
23+-}
10module Grenade.Layers.Deconvolution (
24module Grenade.Layers.Deconvolution (
11 Deconvolution (..)
25 Deconvolution (..)
12 , Deconvolution' (..)
26 , Deconvolution' (..)
@@ -30,42 +44,37 @@ import Grenade.Layers.Internal.Convolution
30import Grenade.Layers.Internal.Update
44import Grenade.Layers.Internal.Update
31
45
32-- | A Deconvolution layer for a neural network.
46-- | A Deconvolution layer for a neural network.
33−-- This uses the im2col Deconvolution trick popularised by Caffe, which essentially turns the
47+-- This uses the im2col Convolution trick popularised by Caffe.
34−-- many, many, many, many loop Deconvolution into a single matrix multiplication.
35−--
36−-- The Deconvolution layer takes all of the kernels for the Deconvolution, which are flattened
37−-- and then put into columns in the matrix.
38--
48--
39−-- The kernel size dictates which input and output sizes will "fit". Fitting the equation:
49+-- The Deconvolution layer is a way of spreading out a single response
40−-- `out = (in - kernel) / stride + 1` for both dimensions.
50+-- into a larger image, and is useful in generating images.
41--
51--
42−-- One probably shouldn't build their own layer, but rather use the randomDeconvolution function.
43data Deconvolution :: Nat -- Number of channels, for the first layer this could be RGB for instance.
52data Deconvolution :: Nat -- Number of channels, for the first layer this could be RGB for instance.
44− -> Nat -- Number of filters, this is the number of channels output by the layer.
53+ -> Nat -- Number of filters, this is the number of channels output by the layer.
45− -> Nat -- The number of rows in the kernel filter
54+ -> Nat -- The number of rows in the kernel filter
46− -> Nat -- The number of column in the kernel filter
55+ -> Nat -- The number of column in the kernel filter
47− -> Nat -- The row stride of the Deconvolution filter
56+ -> Nat -- The row stride of the Deconvolution filter
48− -> Nat -- The columns stride of the Deconvolution filter
57+ -> Nat -- The columns stride of the Deconvolution filter
49− -> * where
58+ -> * where
50 Deconvolution :: ( KnownNat channels
59 Deconvolution :: ( KnownNat channels
51− , KnownNat filters
60+ , KnownNat filters
52− , KnownNat kernelRows
61+ , KnownNat kernelRows
53− , KnownNat kernelColumns
62+ , KnownNat kernelColumns
54− , KnownNat strideRows
63+ , KnownNat strideRows
55− , KnownNat strideColumns
64+ , KnownNat strideColumns
56− , KnownNat kernelFlattened
65+ , KnownNat kernelFlattened
57− , kernelFlattened ~ (kernelRows * kernelColumns * filters))
66+ , kernelFlattened ~ (kernelRows * kernelColumns * filters))
58− => !(L kernelFlattened channels) -- The kernel filter weights
67+ => !(L kernelFlattened channels) -- The kernel filter weights
59− -> !(L kernelFlattened channels) -- The last kernel update (or momentum)
68+ -> !(L kernelFlattened channels) -- The last kernel update (or momentum)
60− -> Deconvolution channels filters kernelRows kernelColumns strideRows strideColumns
69+ -> Deconvolution channels filters kernelRows kernelColumns strideRows strideColumns
61
70
62data Deconvolution' :: Nat -- Number of channels, for the first layer this could be RGB for instance.
71data Deconvolution' :: Nat -- Number of channels, for the first layer this could be RGB for instance.
63− -> Nat -- Number of filters, this is the number of channels output by the layer.
72+ -> Nat -- Number of filters, this is the number of channels output by the layer.
64− -> Nat -- The number of rows in the kernel filter
73+ -> Nat -- The number of rows in the kernel filter
65− -> Nat -- The number of column in the kernel filter
74+ -> Nat -- The number of column in the kernel filter
66− -> Nat -- The row stride of the Deconvolution filter
75+ -> Nat -- The row stride of the Deconvolution filter
67− -> Nat -- The columns stride of the Deconvolution filter
76+ -> Nat -- The columns stride of the Deconvolution filter
68− -> * where
77+ -> * where
69 Deconvolution' :: ( KnownNat channels
78 Deconvolution' :: ( KnownNat channels
70 , KnownNat filters
79 , KnownNat filters
71 , KnownNat kernelRows
80 , KnownNat kernelRows
src/Grenade/Layers/Merge.hs+1−1
@@ -10,7 +10,7 @@
10{-# LANGUAGE StandaloneDeriving #-}
10{-# LANGUAGE StandaloneDeriving #-}
11{-|
11{-|
12Module : Grenade.Core.Network
12Module : Grenade.Core.Network
13−Description : Core definition a simple neural etwork
13+Description : Merging layer for parallel network composition
14Copyright : (c) Huw Campbell, 2016-2017
14Copyright : (c) Huw Campbell, 2016-2017
15License : BSD2
15License : BSD2
16Stability : experimental
16Stability : experimental
src/Grenade/Recurrent.hs+22
@@ -1,7 +1,29 @@
1module Grenade.Recurrent (
1module Grenade.Recurrent (
2+ -- | This is an empty module which simply re-exports public definitions
3+ -- for recurrent networks in Grenade.
4+
5+ -- * Exported modules
6+ --
7+ -- | The core types and runners for Recurrent Networks.
2 module Grenade.Recurrent.Core
8 module Grenade.Recurrent.Core
9+
10+ -- | The recurrent neural network layer zoo
3 , module Grenade.Recurrent.Layers
11 , module Grenade.Recurrent.Layers
12+
13+ -- * Overview of recurrent Networks
14+ -- $recurrent
15+
4 ) where
16 ) where
5
17
6import Grenade.Recurrent.Core
18import Grenade.Recurrent.Core
7import Grenade.Recurrent.Layers
19import Grenade.Recurrent.Layers
20+
21+{- $recurrent
22+There are two ways in which deep learning libraries choose to represent
23+recurrent Neural Networks, as an unrolled graph, or at a first class
24+level. Grenade chooses the latter representation, and provides a network
25+type which is specifically suited for recurrent neural networks.
26+
27+Currently grenade supports two layers, a basic recurrent layer, and an
28+LSTM layer.
29+-}
test/test.hs+16−1
@@ -1,4 +1,4 @@
1−import Disorder.Core.Main
1+import Control.Monad
2
2
3import qualified Test.Grenade.Layers.Pooling
3import qualified Test.Grenade.Layers.Pooling
4import qualified Test.Grenade.Layers.Convolution
4import qualified Test.Grenade.Layers.Convolution
@@ -11,6 +11,9 @@ import qualified Test.Grenade.Layers.Internal.Pooling
11
11
12import qualified Test.Grenade.Recurrent.Layers.LSTM
12import qualified Test.Grenade.Recurrent.Layers.LSTM
13
13
14+import System.Exit
15+import System.IO
16+
14main :: IO ()
17main :: IO ()
15main =
18main =
16 disorderMain [
19 disorderMain [
@@ -25,3 +28,15 @@ main =
25
28
26 , Test.Grenade.Recurrent.Layers.LSTM.tests
29 , Test.Grenade.Recurrent.Layers.LSTM.tests
27 ]
30 ]
31+
32+disorderMain :: [IO Bool] -> IO ()
33+disorderMain tests = do
34+ lineBuffer
35+ rs <- sequence tests
36+ unless (and rs) exitFailure
37+
38+
39+lineBuffer :: IO ()
40+lineBuffer = do
41+ hSetBuffering stdout LineBuffering
42+ hSetBuffering stderr LineBuffering