on d118c5e help sign in
33 files changed+219−50
since splitsnapshots
unifiedsplit
examples/grenade-examples.cabal+5−6 examples/main/shakespeare.hs+7−2 grenade.cabal+1−2 src/Grenade/Core/Layer.hs+2−1 src/Grenade/Core/Network.hs+9−2 src/Grenade/Core/Runner.hs+1−2 src/Grenade/Core/Shape.hs+31−16 src/Grenade/Layers/Concat.hs+4 src/Grenade/Layers/Convolution.hs+11 src/Grenade/Layers/Crop.hs+11 src/Grenade/Layers/Deconvolution.hs+11 src/Grenade/Layers/Dropout.hs+5−1 src/Grenade/Layers/Elu.hs+4 src/Grenade/Layers/FullyConnected.hs+8−1 src/Grenade/Layers/Logit.hs+4 src/Grenade/Layers/Merge.hs+4 src/Grenade/Layers/Pad.hs+11 src/Grenade/Layers/Pooling.hs+11 src/Grenade/Layers/Relu.hs+4 src/Grenade/Layers/Reshape.hs+5 src/Grenade/Layers/Sinusoid.hs+4 src/Grenade/Layers/Softmax.hs+4 src/Grenade/Layers/Tanh.hs+4 src/Grenade/Layers/Trivial.hs+4 src/Grenade/Recurrent/Core/Layer.hs+2−1 src/Grenade/Recurrent/Core/Network.hs+12−1 src/Grenade/Recurrent/Core/Runner.hs+1−1 src/Grenade/Recurrent/Layers/BasicRecurrent.hs+7 src/Grenade/Recurrent/Layers/ConcatRecurrent.hs+10 src/Grenade/Recurrent/Layers/LSTM.hs+15−1 src/Grenade/Utils/OneHot.hs+1−1 stack.yaml+1−7 stack.yaml.lock+5−5
examples/grenade-examples.cabal+5−6
@@ -26,7 +26,7 @@ executable feedforward
2626 , bytestring
2727 , cereal
2828 , either
29− , optparse-applicative >= 0.13 && < 0.18
29+ , optparse-applicative >= 0.13 && < 0.16
3030 , text == 1.2.*
3131 , mtl >= 2.2.1 && < 2.3
3232 , hmatrix
@@ -42,7 +42,7 @@ executable mnist
4242 , grenade
4343 , attoparsec
4444 , either
45− , optparse-applicative >= 0.13 && < 0.18
45+ , optparse-applicative >= 0.13 && < 0.16
4646 , text == 1.2.*
4747 , mtl >= 2.2.1 && < 2.3
4848 , hmatrix >= 0.18 && < 0.21
@@ -61,7 +61,7 @@ executable gan-mnist
6161 , bytestring
6262 , cereal
6363 , either
64− , optparse-applicative >= 0.13 && < 0.18
64+ , optparse-applicative >= 0.13 && < 0.16
6565 , text == 1.2.*
6666 , mtl >= 2.2.1 && < 2.3
6767 , hmatrix >= 0.18 && < 0.21
@@ -78,7 +78,7 @@ executable recurrent
7878 , grenade
7979 , attoparsec
8080 , either
81− , optparse-applicative >= 0.13 && < 0.18
81+ , optparse-applicative >= 0.13 && < 0.16
8282 , text == 1.2.*
8383 , mtl >= 2.2.1 && < 2.3
8484 , hmatrix >= 0.18 && < 0.21
@@ -96,14 +96,13 @@ executable shakespeare
9696 , bytestring
9797 , cereal
9898 , either
99− , optparse-applicative >= 0.13 && < 0.18
99+ , optparse-applicative >= 0.13 && < 0.16
100100 , text == 1.2.*
101101 , mtl >= 2.2.1 && < 2.3
102102 , hmatrix >= 0.18 && < 0.21
103103 , transformers
104104 , semigroups
105105 , singletons
106− , singletons-base
107106 , vector
108107 , MonadRandom
109108 , containers
examples/main/shakespeare.hs+7−2
@@ -14,15 +14,22 @@ import Data.Char ( isUpper, toUpper, toLower )
1414import Data.List ( foldl' )
1515import Data.Maybe ( fromMaybe )
1616
17+#if ! MIN_VERSION_base(4,13,0)
18+import Data.Semigroup ( (<>) )
19+#endif
1720
1821import qualified Data.Vector as V
1922import Data.Vector ( Vector )
2023
2124import qualified Data.Map as M
25+#if ! MIN_VERSION_base(4,13,0)
26+import Data.Proxy ( Proxy (..) )
27+#endif
2228
2329import qualified Data.ByteString as B
2430import Data.Serialize
2531
32+import Data.Singletons.Prelude
2633import GHC.TypeLits
2734
2835import Numeric.LinearAlgebra.Static ( konst )
@@ -34,8 +41,6 @@ import Grenade.Recurrent
3441import Grenade.Utils.OneHot
3542
3643import System.IO.Unsafe ( unsafeInterleaveIO )
37−import Data.Proxy
38−import Prelude.Singletons
3944
4045-- The defininition for our natural language recurrent network.
4146-- This network is able to learn and generate simple words in
grenade.cabal+1−2
@@ -38,7 +38,7 @@ source-repository head
3838library
3939 build-depends:
4040 base >= 4.8 && < 5
41− , bytestring >= 0.10.0
41+ , bytestring == 0.10.*
4242 , containers >= 0.5 && < 0.7
4343 , cereal >= 0.5 && < 0.6
4444 , deepseq >= 1.4 && < 1.5
@@ -48,7 +48,6 @@ library
4848 -- Versions of singletons are *tightly* coupled with the
4949 -- GHC version so its fine to drop version bounds.
5050 , singletons
51− , singletons-base
5251 , vector >= 0.11 && < 0.13
5352
5453 ghc-options:
src/Grenade/Core/Layer.hs+2−1
@@ -48,6 +48,7 @@ import Data.Kind (Type)
4848
4949import Grenade.Core.Shape
5050import Grenade.Core.LearningParameters
51+import Control.DeepSeq (NFData)
5152
5253-- | Class for updating a layer. All layers implement this, as it
5354-- describes how to create and update the layer.
@@ -73,7 +74,7 @@ class UpdateLayer x where
7374-- need to implement it for all shapes, only ones which are
7475-- appropriate.
7576--
76−class UpdateLayer x => Layer x (i :: Shape) (o :: Shape) where
77+class (UpdateLayer x, NFData x) => Layer x (i :: Shape) (o :: Shape) where
7778 -- | The Wengert tape for this layer. Includes all that is required
7879 -- to generate the back propagated gradients efficiently. As a
7980 -- default, `S i` is fine.
src/Grenade/Core/Network.hs+9−2
@@ -34,6 +34,7 @@ module Grenade.Core.Network (
3434import Control.Monad.Random ( MonadRandom )
3535
3636import Data.Singletons
37+import Data.Singletons.Prelude
3738import Data.Serialize
3839
3940#if MIN_VERSION_base(4,9,0)
@@ -43,7 +44,7 @@ import Data.Kind (Type)
4344import Grenade.Core.Layer
4445import Grenade.Core.LearningParameters
4546import Grenade.Core.Shape
46−import Prelude.Singletons
47+import Control.DeepSeq
4748
4849-- | Type of a network.
4950--
@@ -68,6 +69,12 @@ instance Show (Network '[] '[i]) where
6869instance (Show x, Show (Network xs rs)) => Show (Network (x ': xs) (i ': rs)) where
6970 show (x :~> xs) = show x ++ "\n~>\n" ++ show xs
7071
72+instance NFData (Network '[] '[i]) where
73+ rnf NNil = ()
74+
75+instance NFData (Network xs rs) => NFData (Network (x ': xs) (i ': rs)) where
76+ rnf (x :~> xs) = x `deepseq` xs `deepseq` ()
77+
7178-- | Gradient of a network.
7279--
7380-- Parameterised on the layers of the network.
@@ -191,7 +198,7 @@ instance CreatableNetwork sublayers subshapes => UpdateLayer (Network sublayers
191198-- | Ultimate composition.
192199--
193200-- This allows a complete network to be treated as a layer in a larger network.
194−instance (CreatableNetwork sublayers subshapes, i ~ (Head subshapes), o ~ (Last subshapes)) => Layer (Network sublayers subshapes) i o where
201+instance (CreatableNetwork sublayers subshapes, i ~ (Head subshapes), o ~ (Last subshapes), NFData (Network sublayers subshapes)) => Layer (Network sublayers subshapes) i o where
195202 type Tape (Network sublayers subshapes) i o = Tapes sublayers subshapes
196203 runForwards = runNetwork
197204 runBackwards = runGradient
src/Grenade/Core/Runner.hs+1−2
@@ -13,12 +13,11 @@ module Grenade.Core.Runner (
1313 , runNet
1414 ) where
1515
16+import Data.Singletons.Prelude
1617
1718import Grenade.Core.LearningParameters
1819import Grenade.Core.Network
1920import Grenade.Core.Shape
20−import Data.Singletons
21−import Prelude.Singletons
2221
2322-- | Perform reverse automatic differentiation on the network
2423-- for the current input and expected output.
src/Grenade/Core/Shape.hs+31−16
@@ -21,20 +21,35 @@ Stability : experimental
2121module Grenade.Core.Shape (
2222 S (..)
2323 , Shape (..)
24+#if MIN_VERSION_singletons(2,6,0)
2425 , SShape (..)
26+#else
27+ , Sing (..)
28+#endif
29+
2530 , randomOfShape
2631 , fromStorable
2732 ) where
2833
2934import Control.DeepSeq (NFData (..))
3035import Control.Monad.Random ( MonadRandom, getRandom )
36+
37+#if MIN_VERSION_base(4,13,0)
3138import Data.Kind (Type)
39+#endif
3240import Data.Proxy
3341import Data.Serialize
3442import Data.Singletons
43+import Data.Singletons.TypeLits
3544import Data.Vector.Storable ( Vector )
3645import qualified Data.Vector.Storable as V
46+
47+#if MIN_VERSION_base(4,11,0)
48+import GHC.TypeLits hiding (natVal)
49+#else
3750import GHC.TypeLits
51+#endif
52+
3853import qualified Numeric.LinearAlgebra.Static as H
3954import Numeric.LinearAlgebra.Static
4055import qualified Numeric.LinearAlgebra as NLA
@@ -84,9 +99,9 @@ deriving instance Show (S n)
8499type instance Sing = SShape
85100
86101data SShape :: Shape -> Type where
87− D1Sing :: KnownNat a => SShape ('D1 a)
88− D2Sing :: (KnownNat a, KnownNat b) => SShape ('D2 a b)
89− D3Sing :: (KnownNat (a * c), KnownNat a, KnownNat b, KnownNat c) => SShape ('D3 a b c)
102+ D1Sing :: Sing a -> SShape ('D1 a)
103+ D2Sing :: Sing a -> Sing b -> SShape ('D2 a b)
104+ D3Sing :: KnownNat (a * c) => Sing a -> Sing b -> Sing c -> SShape ('D3 a b c)
90105#else
91106data instance Sing (n :: Shape) where
92107 D1Sing :: Sing a -> Sing ('D1 a)
@@ -95,11 +110,11 @@ data instance Sing (n :: Shape) where
95110#endif
96111
97112instance KnownNat a => SingI ('D1 a) where
98− sing = D1Sing
113+ sing = D1Sing sing
99114instance (KnownNat a, KnownNat b) => SingI ('D2 a b) where
100− sing = D2Sing
115+ sing = D2Sing sing sing
101116instance (KnownNat a, KnownNat b, KnownNat c, KnownNat (a * c)) => SingI ('D3 a b c) where
102− sing = D3Sing
117+ sing = D3Sing sing sing sing
103118
104119instance SingI x => Num (S x) where
105120 (+) = n2 (+)
@@ -148,13 +163,13 @@ randomOfShape :: forall x m. ( MonadRandom m, SingI x ) => m (S x)
148163randomOfShape = do
149164 seed :: Int <- getRandom
150165 return $ case (sing :: Sing x) of
151− D1Sing ->
166+ D1Sing SNat ->
152167 S1D (randomVector seed Uniform * 2 - 1)
153168
154− D2Sing ->
169+ D2Sing SNat SNat ->
155170 S2D (uniformSample seed (-1) 1)
156171
157− D3Sing ->
172+ D3Sing SNat SNat SNat ->
158173 S3D (uniformSample seed (-1) 1)
159174
160175-- | Generate a shape from a Storable Vector.
@@ -162,13 +177,13 @@ randomOfShape = do
162177-- Returns Nothing if the vector is of the wrong size.
163178fromStorable :: forall x. SingI x => Vector Double -> Maybe (S x)
164179fromStorable xs = case sing :: Sing x of
165− D1Sing ->
180+ D1Sing SNat ->
166181 S1D <$> H.create xs
167182
168− D2Sing ->
183+ D2Sing SNat SNat ->
169184 S2D <$> mkL xs
170185
171− D3Sing ->
186+ D3Sing SNat SNat SNat ->
172187 S3D <$> mkL xs
173188 where
174189 mkL :: forall rows columns. (KnownNat rows, KnownNat columns)
@@ -205,13 +220,13 @@ n2 f (S2D x) (S2D y) = S2D (f x y)
205220n2 f (S3D x) (S3D y) = S3D (f x y)
206221
207222-- Helper function for creating the number instances
208−nk :: forall x. (SingI x) => Double -> S x
223+nk :: forall x. SingI x => Double -> S x
209224nk x = case (sing :: Sing x) of
210− D1Sing ->
225+ D1Sing SNat ->
211226 S1D (konst x)
212227
213− D2Sing ->
228+ D2Sing SNat SNat ->
214229 S2D (konst x)
215230
216− D3Sing ->
231+ D3Sing SNat SNat SNat ->
217232 S3D (konst x)
src/Grenade/Layers/Concat.hs+4
@@ -35,6 +35,7 @@ import Data.Kind (Type)
3535import Grenade.Core
3636
3737import Numeric.LinearAlgebra.Static ( row, (===), splitRows, unrow, (#), split, R )
38+import Control.DeepSeq
3839
3940-- | A Concatentating Layer.
4041--
@@ -52,6 +53,9 @@ import Numeric.LinearAlgebra.Static ( row, (===), splitRows, unrow, (#
5253data Concat :: Shape -> Type -> Shape -> Type -> Type where
5354 Concat :: x -> y -> Concat m x n y
5455
56+instance (NFData x, NFData y) => NFData (Concat m x n y) where
57+ rnf (Concat x y) = rnf x `deepseq` rnf y `deepseq` ()
58+
5559instance (Show x, Show y) => Show (Concat m x n y) where
5660 show (Concat x y) = "Concat\n" ++ show x ++ "\n" ++ show y
5761
src/Grenade/Layers/Convolution.hs+11
@@ -28,9 +28,16 @@ import Control.Monad.Random hiding ( fromList )
2828import Data.Maybe
2929import Data.Proxy
3030import Data.Serialize
31+import Data.Singletons.TypeLits
3132
33+#if MIN_VERSION_base(4,11,0)
34+import GHC.TypeLits hiding (natVal)
35+#else
3236import GHC.TypeLits
37+#endif
38+#if MIN_VERSION_base(4,9,0)
3339import Data.Kind (Type)
40+#endif
3441
3542import Numeric.LinearAlgebra hiding ( uniformSample, konst )
3643import qualified Numeric.LinearAlgebra as LA
@@ -39,6 +46,7 @@ import Numeric.LinearAlgebra.Static hiding ((|||), build, toRows)
3946import Grenade.Core
4047import Grenade.Layers.Internal.Convolution
4148import Grenade.Layers.Internal.Update
49+import Control.DeepSeq
4250
4351-- | A convolution layer for a neural network.
4452-- This uses the im2col convolution trick popularised by Caffe, which essentially turns the
@@ -106,6 +114,9 @@ instance Show (Convolution c f k k' s s') where
106114 px = (fmap . fmap . fmap) render ms
107115 in unlines $ foldl1 (zipWith (\a' b' -> a' ++ " | " ++ b')) $ px
108116
117+instance NFData (Convolution c f k k' s s') where
118+ rnf (Convolution a b) = rnf a `deepseq` rnf b `deepseq` ()
119+
109120randomConvolution :: ( MonadRandom m
110121 , KnownNat channels
111122 , KnownNat filters
src/Grenade/Layers/Crop.hs+11
@@ -21,15 +21,23 @@ module Grenade.Layers.Crop (
2121import Data.Maybe
2222import Data.Proxy
2323import Data.Serialize
24+import Data.Singletons.TypeLits
2425
26+#if MIN_VERSION_base(4,11,0)
27+import GHC.TypeLits hiding (natVal)
28+#else
2529import GHC.TypeLits
30+#endif
31+#if MIN_VERSION_base(4,9,0)
2632import Data.Kind (Type)
33+#endif
2734
2835import Grenade.Core
2936import Grenade.Layers.Internal.Pad
3037
3138import Numeric.LinearAlgebra (konst, subMatrix, diagBlock)
3239import Numeric.LinearAlgebra.Static (extract, create)
40+import Control.DeepSeq
3341
3442-- | A cropping layer for a neural network.
3543data Crop :: Nat
@@ -38,6 +46,9 @@ data Crop :: Nat
3846 -> Nat -> Type where
3947 Crop :: Crop cropLeft cropTop cropRight cropBottom
4048
49+instance NFData (Crop cropLeft cropTop cropRight cropBottom) where
50+ rnf Crop = ()
51+
4152instance Show (Crop cropLeft cropTop cropRight cropBottom) where
4253 show Crop = "Crop"
4354
src/Grenade/Layers/Deconvolution.hs+11
@@ -32,9 +32,16 @@ import Control.Monad.Random hiding ( fromList )
3232import Data.Maybe
3333import Data.Proxy
3434import Data.Serialize
35+import Data.Singletons.TypeLits
3536
37+#if MIN_VERSION_base(4,11,0)
38+import GHC.TypeLits hiding (natVal)
39+#else
3640import GHC.TypeLits
41+#endif
42+#if MIN_VERSION_base(4,9,0)
3743import Data.Kind (Type)
44+#endif
3845
3946import Numeric.LinearAlgebra hiding ( uniformSample, konst )
4047import qualified Numeric.LinearAlgebra as LA
@@ -43,6 +50,7 @@ import Numeric.LinearAlgebra.Static hiding ((|||), build, toRows)
4350import Grenade.Core
4451import Grenade.Layers.Internal.Convolution
4552import Grenade.Layers.Internal.Update
53+import Control.DeepSeq
4654
4755-- | A Deconvolution layer for a neural network.
4856-- This uses the im2col Convolution trick popularised by Caffe.
@@ -87,6 +95,9 @@ data Deconvolution' :: Nat -- Number of channels, for the first layer this could
8795 => !(L kernelFlattened channels) -- The kernel filter gradient
8896 -> Deconvolution' channels filters kernelRows kernelColumns strideRows strideColumns
8997
98+instance NFData (Deconvolution c f k k' s s') where
99+ rnf (Deconvolution a b) = rnf a `deepseq` rnf b `deepseq` ()
100+
90101instance Show (Deconvolution c f k k' s s') where
91102 show (Deconvolution a _) = renderConv a
92103 where
src/Grenade/Layers/Dropout.hs+5−1
@@ -11,6 +11,7 @@ import Control.Monad.Random hiding (fromList)
1111
1212import GHC.TypeLits
1313import Grenade.Core
14+import Control.DeepSeq
1415
1516-- Dropout layer help to reduce overfitting.
1617-- Idea here is that the vector is a shape of 1s and 0s, which we multiply the input by.
@@ -20,7 +21,10 @@ import Grenade.Core
2021data Dropout = Dropout {
2122 dropoutRate :: Double
2223 , dropoutSeed :: Int
23− } deriving Show
24+ } deriving (Show)
25+
26+instance NFData Dropout where
27+ rnf (Dropout r s) = rnf r `deepseq` rnf s `deepseq` ()
2428
2529instance UpdateLayer Dropout where
2630 type Gradient Dropout = ()
src/Grenade/Layers/Elu.hs+4
@@ -19,6 +19,7 @@ import GHC.TypeLits
1919import Grenade.Core
2020
2121import qualified Numeric.LinearAlgebra.Static as LAS
22+import Control.DeepSeq
2223
2324-- | An exponential linear unit.
2425-- A layer which can act between any shape of the same dimension, acting as a
@@ -26,6 +27,9 @@ import qualified Numeric.LinearAlgebra.Static as LAS
2627data Elu = Elu
2728 deriving Show
2829
30+instance NFData Elu where
31+ rnf Elu = ()
32+
2933instance UpdateLayer Elu where
3034 type Gradient Elu = ()
3135 runUpdate _ _ _ = Elu
src/Grenade/Layers/FullyConnected.hs+8−1
@@ -13,6 +13,7 @@ import Control.Monad.Random hiding (fromList)
1313
1414import Data.Proxy
1515import Data.Serialize
16+import Data.Singletons.TypeLits
1617
1718import qualified Numeric.LinearAlgebra as LA
1819import Numeric.LinearAlgebra.Static
@@ -20,7 +21,7 @@ import Numeric.LinearAlgebra.Static
2021import Grenade.Core
2122
2223import Grenade.Layers.Internal.Update
23−import GHC.TypeLits
24+import Control.DeepSeq
2425
2526-- | A basic fully connected (or inner product) neural network layer.
2627data FullyConnected i o = FullyConnected
@@ -31,6 +32,12 @@ data FullyConnected' i o = FullyConnected'
3132 !(R o) -- Bias
3233 !(L o i) -- Activations
3334
35+instance NFData (FullyConnected' i o) where
36+ rnf (FullyConnected' a b) = a `deepseq` b `deepseq` ()
37+
38+instance NFData (FullyConnected i o) where
39+ rnf (FullyConnected a b) = a `deepseq` b `deepseq` ()
40+
3441instance Show (FullyConnected i o) where
3542 show FullyConnected {} = "FullyConnected"
3643
src/Grenade/Layers/Logit.hs+4
@@ -19,6 +19,7 @@ import Data.Serialize
1919import Data.Singletons
2020
2121import Grenade.Core
22+import Control.DeepSeq
2223
2324-- | A Logit layer.
2425--
@@ -28,6 +29,9 @@ import Grenade.Core
2829data Logit = Logit
2930 deriving Show
3031
32+instance NFData Logit where
33+ rnf Logit = ()
34+
3135instance UpdateLayer Logit where
3236 type Gradient Logit = ()
3337 runUpdate _ _ _ = Logit
src/Grenade/Layers/Merge.hs+4
@@ -29,6 +29,7 @@ import Data.Kind (Type)
2929#endif
3030
3131import Grenade.Core
32+import Control.DeepSeq
3233
3334-- | A Merging layer.
3435--
@@ -37,6 +38,9 @@ import Grenade.Core
3738data Merge :: Type -> Type -> Type where
3839 Merge :: x -> y -> Merge x y
3940
41+instance (NFData x, NFData y) => NFData (Merge x y) where
42+ rnf (Merge x y) = rnf x `deepseq` rnf y `deepseq` ()
43+
4044instance (Show x, Show y) => Show (Merge x y) where
4145 show (Merge x y) = "Merge\n" ++ show x ++ "\n" ++ show y
4246
src/Grenade/Layers/Pad.hs+11
@@ -21,15 +21,23 @@ module Grenade.Layers.Pad (
2121import Data.Maybe
2222import Data.Proxy
2323import Data.Serialize
24+import Data.Singletons.TypeLits
2425
26+#if MIN_VERSION_base(4,11,0)
27+import GHC.TypeLits hiding (natVal)
28+#else
2529import GHC.TypeLits
30+#endif
31+#if MIN_VERSION_base(4,9,0)
2632import Data.Kind (Type)
33+#endif
2734
2835import Grenade.Core
2936import Grenade.Layers.Internal.Pad
3037
3138import Numeric.LinearAlgebra (konst, subMatrix, diagBlock)
3239import Numeric.LinearAlgebra.Static (extract, create)
40+import Control.DeepSeq
3341
3442-- | A padding layer for a neural network.
3543--
@@ -40,6 +48,9 @@ data Pad :: Nat
4048 -> Nat -> Type where
4149 Pad :: Pad padLeft padTop padRight padBottom
4250
51+instance NFData (Pad padLeft padTop padRight padBottom) where
52+ rnf Pad = ()
53+
4354instance Show (Pad padLeft padTop padRight padBottom) where
4455 show Pad = "Pad"
4556
src/Grenade/Layers/Pooling.hs+11
@@ -22,14 +22,22 @@ module Grenade.Layers.Pooling (
2222import Data.Maybe
2323import Data.Proxy
2424import Data.Serialize
25+import Data.Singletons.TypeLits
2526
27+#if MIN_VERSION_base(4,11,0)
28+import GHC.TypeLits hiding (natVal)
29+#else
2630import GHC.TypeLits
31+#endif
32+#if MIN_VERSION_base(4,9,0)
2733import Data.Kind (Type)
34+#endif
2835
2936import Grenade.Core
3037import Grenade.Layers.Internal.Pooling
3138
3239import Numeric.LinearAlgebra.Static as LAS hiding ((|||), build, toRows)
40+import Control.DeepSeq
3341
3442-- | A pooling layer for a neural network.
3543--
@@ -42,6 +50,9 @@ import Numeric.LinearAlgebra.Static as LAS hiding ((|||), build, toRow
4250data Pooling :: Nat -> Nat -> Nat -> Nat -> Type where
4351 Pooling :: Pooling kernelRows kernelColumns strideRows strideColumns
4452
53+instance NFData (Pooling k k' s s') where
54+ rnf Pooling = ()
55+
4556instance Show (Pooling k k' s s') where
4657 show Pooling = "Pooling"
4758
src/Grenade/Layers/Relu.hs+4
@@ -19,6 +19,7 @@ import GHC.TypeLits
1919import Grenade.Core
2020
2121import qualified Numeric.LinearAlgebra.Static as LAS
22+import Control.DeepSeq
2223
2324-- | A rectifying linear unit.
2425-- A layer which can act between any shape of the same dimension, acting as a
@@ -26,6 +27,9 @@ import qualified Numeric.LinearAlgebra.Static as LAS
2627data Relu = Relu
2728 deriving Show
2829
30+instance NFData Relu where
31+ rnf Relu = ()
32+
2933instance UpdateLayer Relu where
3034 type Gradient Relu = ()
3135 runUpdate _ _ _ = Relu
src/Grenade/Layers/Reshape.hs+5
@@ -17,12 +17,14 @@ module Grenade.Layers.Reshape (
1717
1818import Data.Serialize
1919
20+import Data.Singletons.TypeLits
2021import GHC.TypeLits
2122
2223import Numeric.LinearAlgebra.Static
2324import Numeric.LinearAlgebra.Data as LA ( flatten )
2425
2526import Grenade.Core
27+import Control.DeepSeq
2628
2729-- | Reshape Layer
2830--
@@ -35,6 +37,9 @@ import Grenade.Core
3537data Reshape = Reshape
3638 deriving Show
3739
40+instance NFData Reshape where
41+ rnf Reshape = ()
42+
3843instance UpdateLayer Reshape where
3944 type Gradient Reshape = ()
4045 runUpdate _ _ _ = Reshape
src/Grenade/Layers/Sinusoid.hs+4
@@ -18,12 +18,16 @@ import Data.Serialize
1818import Data.Singletons
1919
2020import Grenade.Core
21+import Control.DeepSeq
2122
2223-- | A Sinusoid layer.
2324-- A layer which can act between any shape of the same dimension, performing a sin function.
2425data Sinusoid = Sinusoid
2526 deriving Show
2627
28+instance NFData Sinusoid where
29+ rnf Sinusoid = ()
30+
2731instance UpdateLayer Sinusoid where
2832 type Gradient Sinusoid = ()
2933 runUpdate _ _ _ = Sinusoid
src/Grenade/Layers/Softmax.hs+4
@@ -22,6 +22,7 @@ import GHC.TypeLits
2222import Grenade.Core
2323
2424import Numeric.LinearAlgebra.Static as LAS
25+import Control.DeepSeq
2526
2627-- | A Softmax layer
2728--
@@ -33,6 +34,9 @@ import Numeric.LinearAlgebra.Static as LAS
3334data Softmax = Softmax
3435 deriving Show
3536
37+instance NFData Softmax where
38+ rnf Softmax = ()
39+
3640instance UpdateLayer Softmax where
3741 type Gradient Softmax = ()
3842 runUpdate _ _ _ = Softmax
src/Grenade/Layers/Tanh.hs+4
@@ -18,12 +18,16 @@ import Data.Serialize
1818import Data.Singletons
1919
2020import Grenade.Core
21+import Control.DeepSeq
2122
2223-- | A Tanh layer.
2324-- A layer which can act between any shape of the same dimension, performing a tanh function.
2425data Tanh = Tanh
2526 deriving Show
2627
28+instance NFData Tanh where
29+ rnf Tanh = ()
30+
2731instance UpdateLayer Tanh where
2832 type Gradient Tanh = ()
2933 runUpdate _ _ _ = Tanh
src/Grenade/Layers/Trivial.hs+4
@@ -17,6 +17,7 @@ module Grenade.Layers.Trivial (
1717import Data.Serialize
1818
1919import Grenade.Core
20+import Control.DeepSeq
2021
2122-- | A Trivial layer.
2223--
@@ -25,6 +26,9 @@ import Grenade.Core
2526data Trivial = Trivial
2627 deriving Show
2728
29+instance NFData Trivial where
30+ rnf Trivial = ()
31+
2832instance Serialize Trivial where
2933 put _ = return ()
3034 get = return Trivial
src/Grenade/Recurrent/Core/Layer.hs+2−1
@@ -14,6 +14,7 @@ import Data.Kind (Type)
1414#endif
1515
1616import Grenade.Core
17+import Control.DeepSeq (NFData)
1718
1819-- | Class for a recurrent layer.
1920-- It's quite similar to a normal layer but for the input and output
@@ -22,7 +23,7 @@ class UpdateLayer x => RecurrentUpdateLayer x where
2223 -- | Shape of data that is passed between each subsequent run of the layer
2324 type RecurrentShape x :: Type
2425
25−class (RecurrentUpdateLayer x, Num (RecurrentShape x)) => RecurrentLayer x (i :: Shape) (o :: Shape) where
26+class (RecurrentUpdateLayer x, Num (RecurrentShape x), NFData x) => RecurrentLayer x (i :: Shape) (o :: Shape) where
2627 -- | Wengert Tape
2728 type RecTape x i o :: Type
2829 -- | Used in training and scoring. Take the input from the previous
src/Grenade/Recurrent/Core/Network.hs+12−1
@@ -29,13 +29,17 @@ module Grenade.Recurrent.Core.Network (
2929
3030
3131import Control.Monad.Random ( MonadRandom )
32+import Data.Singletons ( SingI )
33+import Data.Singletons.Prelude ( Head, Last )
3234import Data.Serialize
3335
36+#if MIN_VERSION_base(4,9,0)
3437import Data.Kind (Type)
38+#endif
3539
3640import Grenade.Core
3741import Grenade.Recurrent.Core.Layer
38−import Prelude.Singletons
42+import Control.DeepSeq
3943
4044-- | Witness type to say indicate we're building up with a normal feed
4145-- forward layer.
@@ -195,6 +199,12 @@ applyRecurrentUpdate rate (layer :~@> rest) (gradient ://> grest)
195199applyRecurrentUpdate _ RNil RGNil
196200 = RNil
197201
202+instance NFData (RecurrentNetwork '[] '[i]) where
203+ rnf RNil = ()
204+
205+instance NFData (RecurrentNetwork xs rs) => NFData (RecurrentNetwork (x ': xs) (i ': rs)) where
206+ rnf (layer :~~> rest) = rnf layer `deepseq` rnf rest `deepseq` ()
207+ rnf (layer :~@> rest) = rnf layer `deepseq` rnf rest `deepseq` ()
198208
199209instance Show (RecurrentNetwork '[] '[i]) where
200210 show RNil = "NNil"
@@ -322,6 +332,7 @@ instance CreatableRecurrent sublayers subshapes => RecurrentUpdateLayer (Recurre
322332instance ( CreatableRecurrent sublayers subshapes
323333 , i ~ (Head subshapes), o ~ (Last subshapes)
324334 , Num (RecurrentShape (RecurrentNetwork sublayers subshapes))
335+ , NFData (RecurrentNetwork sublayers subshapes)
325336 ) => RecurrentLayer (RecurrentNetwork sublayers subshapes) i o where
326337 type RecTape (RecurrentNetwork sublayers subshapes) i o = RecurrentTape sublayers subshapes
327338 runRecurrentForwards = runRecurrent
src/Grenade/Recurrent/Core/Runner.hs+1−1
@@ -17,10 +17,10 @@ module Grenade.Recurrent.Core.Runner (
1717 ) where
1818
1919import Data.List ( foldl' )
20+import Data.Singletons.Prelude
2021import Grenade.Core
2122
2223import Grenade.Recurrent.Core.Network
23−import Prelude.Singletons
2424
2525type RecurrentGradients layers = [RecurrentGradient layers]
2626
src/Grenade/Recurrent/Layers/BasicRecurrent.hs+7
@@ -16,8 +16,11 @@ module Grenade.Recurrent.Layers.BasicRecurrent (
1616
1717import Control.Monad.Random ( MonadRandom, getRandom )
1818
19+import Data.Singletons.TypeLits
1920
21+#if MIN_VERSION_base(4,9,0)
2022import Data.Kind (Type)
23+#endif
2124
2225import Numeric.LinearAlgebra.Static
2326
@@ -25,6 +28,7 @@ import GHC.TypeLits
2528
2629import Grenade.Core
2730import Grenade.Recurrent.Core
31+import Control.DeepSeq
2832
2933data BasicRecurrent :: Nat -- Input layer size
3034 -> Nat -- Output layer size
@@ -39,6 +43,9 @@ data BasicRecurrent :: Nat -- Input layer size
3943 -> !(L output matrixCols) -- Momentum
4044 -> BasicRecurrent input output
4145
46+instance NFData (BasicRecurrent input output) where
47+ rnf (BasicRecurrent b bm a aM) = rnf b `deepseq` rnf bm `deepseq` rnf a `deepseq` rnf aM `deepseq` ()
48+
4249data BasicRecurrent' :: Nat -- Input layer size
4350 -> Nat -- Output layer size
4451 -> Type where
src/Grenade/Recurrent/Layers/ConcatRecurrent.hs+10
@@ -36,6 +36,7 @@ import Grenade.Core
3636import Grenade.Recurrent.Core
3737
3838import Numeric.LinearAlgebra.Static ( (#), split, R )
39+import Control.DeepSeq
3940
4041-- | A Concatentating Layer.
4142--
@@ -55,6 +56,15 @@ data ConcatRecurrent :: Shape -> Type -> Shape -> Type -> Type where
5556 ConcatRecRight :: x -> y -> ConcatRecurrent m (FeedForward x) n (Recurrent y)
5657 ConcatRecBoth :: x -> y -> ConcatRecurrent m (Recurrent x) n (Recurrent y)
5758
59+instance (NFData x, NFData y) => NFData (ConcatRecurrent m (Recurrent x) n (Recurrent y)) where
60+ rnf (ConcatRecBoth x y) = rnf x `deepseq` rnf y `deepseq` ()
61+
62+instance (NFData x, NFData y) => NFData (ConcatRecurrent m (Recurrent x) n (FeedForward y)) where
63+ rnf (ConcatRecLeft x y) = rnf x `deepseq` rnf y `deepseq` ()
64+
65+instance (NFData x, NFData y) => NFData (ConcatRecurrent m (FeedForward x) n (Recurrent y)) where
66+ rnf (ConcatRecRight x y) = rnf x `deepseq` rnf y `deepseq` ()
67+
5868instance (Show x, Show y) => Show (ConcatRecurrent m (p x) n (q y)) where
5969 show (ConcatRecLeft x y) = "ConcatRecLeft\n" ++ show x ++ "\n" ++ show y
6070 show (ConcatRecRight x y) = "ConcatRecRight\n" ++ show x ++ "\n" ++ show y
src/Grenade/Recurrent/Layers/LSTM.hs+15−1
@@ -21,7 +21,11 @@ import Control.Monad.Random ( MonadRandom, getRandom )
2121-- import Data.List ( foldl1' )
2222import Data.Proxy
2323import Data.Serialize
24+import Data.Singletons.TypeLits
25+
26+#if MIN_VERSION_base(4,9,0)
2427import Data.Kind (Type)
28+#endif
2529
2630import qualified Numeric.LinearAlgebra as LA
2731import Numeric.LinearAlgebra.Static
@@ -29,7 +33,7 @@ import Numeric.LinearAlgebra.Static
2933import Grenade.Core
3034import Grenade.Recurrent.Core
3135import Grenade.Layers.Internal.Update
32−import GHC.TypeLits
36+import Control.DeepSeq
3337
3438
3539-- | Long Short Term Memory Recurrent unit
@@ -61,6 +65,16 @@ data LSTMWeights :: Nat -> Nat -> Type where
6165 , lstmBc :: !(R output) -- Bias Cell (b_c)
6266 } -> LSTMWeights input output
6367
68+instance NFData (LSTMWeights input output) where
69+ rnf (LSTMWeights wf uf bf wi ui bi wo uo bo wc bc) =
70+ rnf wf `deepseq` rnf uf `deepseq` rnf bf `deepseq`
71+ rnf wi `deepseq` rnf ui `deepseq` rnf bi `deepseq`
72+ rnf wo `deepseq` rnf uo `deepseq` rnf bo `deepseq`
73+ rnf wc `deepseq` rnf bc `deepseq` ()
74+
75+instance NFData (LSTM input output) where
76+ rnf (LSTM w m) = rnf w `deepseq` rnf m `deepseq` ()
77+
6478instance Show (LSTM i o) where
6579 show LSTM {} = "LSTM"
6680
src/Grenade/Utils/OneHot.hs+1−1
@@ -22,6 +22,7 @@ import Data.Map ( Map )
2222import qualified Data.Map as M
2323
2424import Data.Proxy
25+import Data.Singletons.TypeLits
2526
2627import Data.Vector ( Vector )
2728import qualified Data.Vector as V
@@ -30,7 +31,6 @@ import qualified Data.Vector.Storable as VS
3031import Numeric.LinearAlgebra ( maxIndex )
3132import Numeric.LinearAlgebra.Devel
3233import Numeric.LinearAlgebra.Static
33−import GHC.TypeLits
3434
3535import Grenade.Core.Shape
3636
stack.yaml+1−7
@@ -20,7 +20,7 @@
2020#
2121# resolver: ./custom-snapshot.yaml
2222# resolver: https://example.com/snapshots/2018-01-01.yaml
23−resolver: lts-20.18
23+resolver: lts-15.5
2424
2525# User packages to be built.
2626# Various formats can be used as shown in the example below.
@@ -70,9 +70,3 @@ extra-deps:
7070#
7171# Allow a newer minor version of GHC than the snapshot specifies
7272# compiler-check: newer-minor
73−
74−nix:
75− enable: true
76− packages:
77− - blas
78− - lapack
stack.yaml.lock+5−5
@@ -7,13 +7,13 @@ packages:
77- completed:
88 hackage: typelits-witnesses-0.3.0.3@sha256:2d9df4ac6ff3077bfd2bf659e4b495e157723ac5b45c519762853f55df5c16db,2738
99 pantry-tree:
10− sha256: 6a42a462f98e94933b6e9721acd912c6c6b6a4743635efd15cc5871908c816a0
1110 size: 469
11+ sha256: 6a42a462f98e94933b6e9721acd912c6c6b6a4743635efd15cc5871908c816a0
1212 original:
1313 hackage: typelits-witnesses-0.3.0.3
1414snapshots:
1515- completed:
16− sha256: 9fa4bece7acfac1fc7930c5d6e24606004b09e80aa0e52e9f68b148201008db9
17− size: 649606
18− url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/20/18.yaml
19− original: lts-20.18
16+ size: 491372
17+ url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/15/5.yaml
18+ sha256: 1b549cfff328040c382a70a84a2087aac8dab6d778bf92f32a93a771a1980dfc
19+ original: lts-15.5