on master help sign in
Docs
commitf9c282bc356b669560f58c0a4538a6f8e6ee7720 authorHuw Campbell <huw.campbell@ambiata.com> dateFri 7 Apr 2017, 22:50 (10 years ago) parent235b767
14 files changed+143−50
grenade.cabal+11−2 src/Grenade.hs+2−1 src/Grenade/Core/Layer.hs+18−2 src/Grenade/Core/LearningParameters.hs+7 src/Grenade/Core/Network.hs+5−4 src/Grenade/Core/Runner.hs+2−5 src/Grenade/Core/Shape.hs+2−3 src/Grenade/Layers/Concat.hs+1−1 src/Grenade/Layers/Convolution.hs+10 src/Grenade/Layers/Crop.hs+7 src/Grenade/Layers/Deconvolution.hs+39−30 src/Grenade/Layers/Merge.hs+1−1 src/Grenade/Recurrent.hs+22 test/test.hs+16−1
grenade.cabal+11−2
@@ -5,11 +5,20 @@ license-file: LICENSE
55author: Huw Campbell <huw.campbell@gmail.com>
66maintainer: Huw Campbell <huw.campbell@gmail.com>
77copyright: (c) 2016-2017 Huw Campbell.
8−synopsis: grenade
8+synopsis: Practical Deep Learning in Haskell
99category: System
1010cabal-version: >= 1.8
1111build-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>.
1322
1423extra-source-files:
1524 cbits/im2col.h
src/Grenade.hs+2−1
@@ -29,6 +29,7 @@ It provides an expressive type level API for the construction
2929of complex neural network architectures. Backing this API is and
3030implementation written using BLAS and LAPACK, mostly provided by
3131the hmatrix library.
32+
3233-}
3334
3435{- $example
@@ -45,7 +46,7 @@ is a simple example which runs a logistic regression.
4546> randomMyNet = randomNetwork
4647
4748The 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
4950can be built, and hence, that the architecture is sound.
5051-}
5152
src/Grenade/Core/Layer.hs+18−2
@@ -7,14 +7,30 @@
77{-# LANGUAGE RankNTypes #-}
88{-# LANGUAGE FlexibleInstances #-}
99{-|
10−Module : Grenade.Core.Network
11−Description : Core definition a simple neural etwork
10+Module : Grenade.Core.Layer
11+Description : Defines the Layer Classes
1212Copyright : (c) Huw Campbell, 2016-2017
1313License : BSD2
1414Stability : experimental
1515
1616This module defines what a Layer is in a Grenade
1717neural 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.
1834-}
1935module Grenade.Core.Layer (
2036 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+-}
18module Grenade.Core.LearningParameters (
29 -- | This module contains learning algorithm specific
310 -- code. Currently, this module should be consifered
src/Grenade/Core/Network.hs+5−4
@@ -9,7 +9,7 @@
99{-# LANGUAGE FlexibleInstances #-}
1010{-|
1111Module : Grenade.Core.Network
12−Description : Core definition a simple neural etwork
12+Description : Core definition of a Neural Network
1313Copyright : (c) Huw Campbell, 2016-2017
1414License : BSD2
1515Stability : experimental
@@ -91,7 +91,6 @@ data Tapes :: [*] -> [Shape] -> * where
9191--
9292-- This gives the output, and the Wengert tape required for back
9393-- propagation.
94−--
9594runNetwork :: forall layers shapes.
9695 Network layers shapes
9796 -> S (Head shapes)
@@ -119,7 +118,6 @@ runNetwork =
119118--
120119-- Gives the gradients for the layer, and the gradient across the
121120-- input (which may not be required).
122−--
123121runGradient :: forall layers shapes.
124122 Network layers shapes
125123 -> Tapes layers shapes
@@ -179,12 +177,15 @@ instance (SingI i, SingI o, Layer x i o, Serialize x, Serialize (Network xs (o '
179177
180178-- | Ultimate composition.
181179--
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.
183181instance CreatableNetwork sublayers subshapes => UpdateLayer (Network sublayers subshapes) where
184182 type Gradient (Network sublayers subshapes) = Gradients sublayers
185183 runUpdate = applyUpdate
186184 createRandom = randomNetwork
187185
186+-- | Ultimate composition.
187+--
188+-- This allows a complete network to be treated as a layer in a larger network.
188189instance (CreatableNetwork sublayers subshapes, i ~ (Head subshapes), o ~ (Last subshapes)) => Layer (Network sublayers subshapes) i o where
189190 type Tape (Network sublayers subshapes) i o = Tapes sublayers subshapes
190191 runForwards = runNetwork
src/Grenade/Core/Runner.hs+2−5
@@ -1,14 +1,11 @@
11{-# LANGUAGE DataKinds #-}
22{-# LANGUAGE FlexibleContexts #-}
33{-|
4−Module : Grenade.Core.Shape
5−Description : Core definition of the Shapes of data we understand
4+Module : Grenade.Core.Runner
5+Description : Functions to perform training and backpropagation
66Copyright : (c) Huw Campbell, 2016-2017
77License : BSD2
88Stability : experimental
9−
10−This module defines simple back propagation and training functions
11−for a network.
129-}
1310module Grenade.Core.Runner (
1411 train
src/Grenade/Core/Shape.hs+2−3
@@ -9,13 +9,12 @@
99{-# LANGUAGE RankNTypes #-}
1010{-|
1111Module : 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
1313Copyright : (c) Huw Campbell, 2016-2017
1414License : BSD2
1515Stability : experimental
1616
17−This module defines the core data types for the shapes of data that
18−are understood by Grenade.
17+
1918-}
2019module Grenade.Core.Shape (
2120 Shape (..)
src/Grenade/Layers/Concat.hs+1−1
@@ -15,7 +15,7 @@ Copyright : (c) Huw Campbell, 2016-2017
1515License : BSD2
1616Stability : experimental
1717
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.
1919-}
2020module Grenade.Layers.Concat (
2121 Concat (..)
src/Grenade/Layers/Convolution.hs+10
@@ -7,6 +7,16 @@
77{-# LANGUAGE MultiParamTypeClasses #-}
88{-# LANGUAGE FlexibleInstances #-}
99{-# 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+-}
1020module Grenade.Layers.Convolution (
1121 Convolution (..)
1222 , Convolution' (..)
src/Grenade/Layers/Crop.hs+7
@@ -5,6 +5,13 @@
55{-# LANGUAGE TypeFamilies #-}
66{-# LANGUAGE FlexibleContexts #-}
77{-# 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+-}
815module Grenade.Layers.Crop (
916 Crop (..)
1017 ) where
src/Grenade/Layers/Deconvolution.hs+39−30
@@ -7,6 +7,20 @@
77{-# LANGUAGE MultiParamTypeClasses #-}
88{-# LANGUAGE FlexibleInstances #-}
99{-# 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+-}
1024module Grenade.Layers.Deconvolution (
1125 Deconvolution (..)
1226 , Deconvolution' (..)
@@ -30,42 +44,37 @@ import Grenade.Layers.Internal.Convolution
3044import Grenade.Layers.Internal.Update
3145
3246-- | A Deconvolution layer for a neural network.
33−-- This uses the im2col Deconvolution trick popularised by Caffe, which essentially turns the
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.
47+-- This uses the im2col Convolution trick popularised by Caffe.
3848--
39−-- The kernel size dictates which input and output sizes will "fit". Fitting the equation:
40−-- `out = (in - kernel) / stride + 1` for both dimensions.
49+-- The Deconvolution layer is a way of spreading out a single response
50+-- into a larger image, and is useful in generating images.
4151--
42−-- One probably shouldn't build their own layer, but rather use the randomDeconvolution function.
4352data 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.
45− -> Nat -- The number of rows in the kernel filter
46− -> Nat -- The number of column in the kernel filter
47− -> Nat -- The row stride of the Deconvolution filter
48− -> Nat -- The columns stride of the Deconvolution filter
49− -> * where
53+ -> Nat -- Number of filters, this is the number of channels output by the layer.
54+ -> Nat -- The number of rows in the kernel filter
55+ -> Nat -- The number of column in the kernel filter
56+ -> Nat -- The row stride of the Deconvolution filter
57+ -> Nat -- The columns stride of the Deconvolution filter
58+ -> * where
5059 Deconvolution :: ( KnownNat channels
51− , KnownNat filters
52− , KnownNat kernelRows
53− , KnownNat kernelColumns
54− , KnownNat strideRows
55− , KnownNat strideColumns
56− , KnownNat kernelFlattened
57− , kernelFlattened ~ (kernelRows * kernelColumns * filters))
58− => !(L kernelFlattened channels) -- The kernel filter weights
59− -> !(L kernelFlattened channels) -- The last kernel update (or momentum)
60− -> Deconvolution channels filters kernelRows kernelColumns strideRows strideColumns
60+ , KnownNat filters
61+ , KnownNat kernelRows
62+ , KnownNat kernelColumns
63+ , KnownNat strideRows
64+ , KnownNat strideColumns
65+ , KnownNat kernelFlattened
66+ , kernelFlattened ~ (kernelRows * kernelColumns * filters))
67+ => !(L kernelFlattened channels) -- The kernel filter weights
68+ -> !(L kernelFlattened channels) -- The last kernel update (or momentum)
69+ -> Deconvolution channels filters kernelRows kernelColumns strideRows strideColumns
6170
6271data 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.
64− -> Nat -- The number of rows in the kernel filter
65− -> Nat -- The number of column in the kernel filter
66− -> Nat -- The row stride of the Deconvolution filter
67− -> Nat -- The columns stride of the Deconvolution filter
68− -> * where
72+ -> Nat -- Number of filters, this is the number of channels output by the layer.
73+ -> Nat -- The number of rows in the kernel filter
74+ -> Nat -- The number of column in the kernel filter
75+ -> Nat -- The row stride of the Deconvolution filter
76+ -> Nat -- The columns stride of the Deconvolution filter
77+ -> * where
6978 Deconvolution' :: ( KnownNat channels
7079 , KnownNat filters
7180 , KnownNat kernelRows
src/Grenade/Layers/Merge.hs+1−1
@@ -10,7 +10,7 @@
1010{-# LANGUAGE StandaloneDeriving #-}
1111{-|
1212Module : Grenade.Core.Network
13−Description : Core definition a simple neural etwork
13+Description : Merging layer for parallel network composition
1414Copyright : (c) Huw Campbell, 2016-2017
1515License : BSD2
1616Stability : experimental
src/Grenade/Recurrent.hs+22
@@ -1,7 +1,29 @@
11module 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.
28 module Grenade.Recurrent.Core
9+
10+ -- | The recurrent neural network layer zoo
311 , module Grenade.Recurrent.Layers
12+
13+ -- * Overview of recurrent Networks
14+ -- $recurrent
15+
416 ) where
517
618import Grenade.Recurrent.Core
719import 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
22
33import qualified Test.Grenade.Layers.Pooling
44import qualified Test.Grenade.Layers.Convolution
@@ -11,6 +11,9 @@ import qualified Test.Grenade.Layers.Internal.Pooling
1111
1212import qualified Test.Grenade.Recurrent.Layers.LSTM
1313
14+import System.Exit
15+import System.IO
16+
1417main :: IO ()
1518main =
1619 disorderMain [
@@ -25,3 +28,15 @@ main =
2528
2629 , Test.Grenade.Recurrent.Layers.LSTM.tests
2730 ]
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