on master help sign in
GHC 8.4 compatibility
49a4280Claude Heiland-Allen
grenade.cabal+2
@@ -58,6 +58,8 @@ library
58 if impl(ghc < 8.0)
58 if impl(ghc < 8.0)
59 ghc-options: -fno-warn-incomplete-patterns
59 ghc-options: -fno-warn-incomplete-patterns
60
60
61+ if impl(ghc >= 8.6)
62+ default-extensions: NoStarIsType
61
63
62 exposed-modules:
64 exposed-modules:
63 Grenade
65 Grenade
src/Grenade/Core/Layer.hs+4−2
@@ -41,6 +41,8 @@ import Control.Monad.Random ( MonadRandom )
41
41
42import Data.List ( foldl' )
42import Data.List ( foldl' )
43
43
44+import Data.Kind (Type)
45+
44import Grenade.Core.Shape
46import Grenade.Core.Shape
45import Grenade.Core.LearningParameters
47import Grenade.Core.LearningParameters
46
48
@@ -50,7 +52,7 @@ import Grenade.Core.LearningParameters
50class UpdateLayer x where
52class UpdateLayer x where
51 -- | The type for the gradient for this layer.
53 -- | The type for the gradient for this layer.
52 -- Unit if there isn't a gradient to pass back.
54 -- Unit if there isn't a gradient to pass back.
53− type Gradient x :: *
55+ type Gradient x :: Type
54
56
55 -- | Update a layer with its gradient and learning parameters
57 -- | Update a layer with its gradient and learning parameters
56 runUpdate :: LearningParameters -> x -> Gradient x -> x
58 runUpdate :: LearningParameters -> x -> Gradient x -> x
@@ -72,7 +74,7 @@ class UpdateLayer x => Layer x (i :: Shape) (o :: Shape) where
72 -- | The Wengert tape for this layer. Includes all that is required
74 -- | The Wengert tape for this layer. Includes all that is required
73 -- to generate the back propagated gradients efficiently. As a
75 -- to generate the back propagated gradients efficiently. As a
74 -- default, `S i` is fine.
76 -- default, `S i` is fine.
75− type Tape x i o :: *
77+ type Tape x i o :: Type
76
78
77 -- | Used in training and scoring. Take the input from the previous
79 -- | Used in training and scoring. Take the input from the previous
78 -- layer, and give the output from this layer.
80 -- layer, and give the output from this layer.
src/Grenade/Core/Network.hs+6−4
@@ -36,6 +36,8 @@ import Data.Singletons
36import Data.Singletons.Prelude
36import Data.Singletons.Prelude
37import Data.Serialize
37import Data.Serialize
38
38
39+import Data.Kind (Type)
40+
39import Grenade.Core.Layer
41import Grenade.Core.Layer
40import Grenade.Core.LearningParameters
42import Grenade.Core.LearningParameters
41import Grenade.Core.Shape
43import Grenade.Core.Shape
@@ -48,7 +50,7 @@ import Grenade.Core.Shape
48--
50--
49-- Can be considered to be a heterogeneous list of layers which are able to
51-- Can be considered to be a heterogeneous list of layers which are able to
50-- transform the data shapes of the network.
52-- transform the data shapes of the network.
51−data Network :: [*] -> [Shape] -> * where
53+data Network :: [Type] -> [Shape] -> Type where
52 NNil :: SingI i
54 NNil :: SingI i
53 => Network '[] '[i]
55 => Network '[] '[i]
54
56
@@ -66,7 +68,7 @@ instance (Show x, Show (Network xs rs)) => Show (Network (x ': xs) (i ': rs)) wh
66-- | Gradient of a network.
68-- | Gradient of a network.
67--
69--
68-- Parameterised on the layers of the network.
70-- Parameterised on the layers of the network.
69−data Gradients :: [*] -> * where
71+data Gradients :: [Type] -> Type where
70 GNil :: Gradients '[]
72 GNil :: Gradients '[]
71
73
72 (:/>) :: UpdateLayer x
74 (:/>) :: UpdateLayer x
@@ -77,7 +79,7 @@ data Gradients :: [*] -> * where
77-- | Wegnert Tape of a network.
79-- | Wegnert Tape of a network.
78--
80--
79-- Parameterised on the layers and shapes of the network.
81-- Parameterised on the layers and shapes of the network.
80−data Tapes :: [*] -> [Shape] -> * where
82+data Tapes :: [Type] -> [Shape] -> Type where
81 TNil :: SingI i
83 TNil :: SingI i
82 => Tapes '[] '[i]
84 => Tapes '[] '[i]
83
85
@@ -152,7 +154,7 @@ applyUpdate _ NNil GNil
152
154
153-- | A network can easily be created by hand with (:~>), but an easy way to
155-- | A network can easily be created by hand with (:~>), but an easy way to
154-- initialise a random network is with the randomNetwork.
156-- initialise a random network is with the randomNetwork.
155−class CreatableNetwork (xs :: [*]) (ss :: [Shape]) where
157+class CreatableNetwork (xs :: [Type]) (ss :: [Shape]) where
156 -- | Create a network with randomly initialised weights.
158 -- | Create a network with randomly initialised weights.
157 --
159 --
158 -- Calls to this function will not compile if the type of the neural
160 -- Calls to this function will not compile if the type of the neural
src/Grenade/Core/Shape.hs−1
@@ -8,7 +8,6 @@
8{-# LANGUAGE FlexibleContexts #-}
8{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE ScopedTypeVariables #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE RankNTypes #-}
10{-# LANGUAGE RankNTypes #-}
11−{-# LANGUAGE NoStarIsType #-}
12{-# LANGUAGE UndecidableInstances #-}
11{-# LANGUAGE UndecidableInstances #-}
13{-|
12{-|
14Module : Grenade.Core.Shape
13Module : Grenade.Core.Shape
src/Grenade/Layers/Concat.hs−1
@@ -8,7 +8,6 @@
8{-# LANGUAGE FlexibleInstances #-}
8{-# LANGUAGE FlexibleInstances #-}
9{-# LANGUAGE ScopedTypeVariables #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE StandaloneDeriving #-}
10{-# LANGUAGE StandaloneDeriving #-}
11−{-# LANGUAGE NoStarIsType #-}
12{-# LANGUAGE UndecidableInstances #-}
11{-# LANGUAGE UndecidableInstances #-}
13{-|
12{-|
14Module : Grenade.Layers.Concat
13Module : Grenade.Layers.Concat
src/Grenade/Layers/Convolution.hs−1
@@ -8,7 +8,6 @@
8{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
9{-# LANGUAGE FlexibleInstances #-}
9{-# LANGUAGE FlexibleInstances #-}
10{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE FlexibleContexts #-}
11−{-# LANGUAGE NoStarIsType #-}
12{-# LANGUAGE UndecidableInstances #-}
11{-# LANGUAGE UndecidableInstances #-}
13{-|
12{-|
14Module : Grenade.Layers.Convolution
13Module : Grenade.Layers.Convolution
src/Grenade/Layers/Crop.hs−1
@@ -6,7 +6,6 @@
6{-# LANGUAGE TypeFamilies #-}
6{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE FlexibleContexts #-}
7{-# LANGUAGE FlexibleContexts #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
9−{-# LANGUAGE NoStarIsType #-}
10{-# LANGUAGE UndecidableInstances #-}
9{-# LANGUAGE UndecidableInstances #-}
11{-|
10{-|
12Module : Grenade.Layers.Crop
11Module : Grenade.Layers.Crop
src/Grenade/Layers/Deconvolution.hs−1
@@ -8,7 +8,6 @@
8{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
9{-# LANGUAGE FlexibleInstances #-}
9{-# LANGUAGE FlexibleInstances #-}
10{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE FlexibleContexts #-}
11−{-# LANGUAGE NoStarIsType #-}
12{-# LANGUAGE UndecidableInstances #-}
11{-# LANGUAGE UndecidableInstances #-}
13{-|
12{-|
14Module : Grenade.Layers.Deconvolution
13Module : Grenade.Layers.Deconvolution
src/Grenade/Layers/Merge.hs+3−1
@@ -23,13 +23,15 @@ import Data.Serialize
23
23
24import Data.Singletons
24import Data.Singletons
25
25
26+import Data.Kind (Type)
27+
26import Grenade.Core
28import Grenade.Core
27
29
28-- | A Merging layer.
30-- | A Merging layer.
29--
31--
30-- Similar to Concat layer, except sums the activations instead of creating a larger
32-- Similar to Concat layer, except sums the activations instead of creating a larger
31-- shape.
33-- shape.
32−data Merge :: * -> * -> * where
34+data Merge :: Type -> Type -> Type where
33 Merge :: x -> y -> Merge x y
35 Merge :: x -> y -> Merge x y
34
36
35instance (Show x, Show y) => Show (Merge x y) where
37instance (Show x, Show y) => Show (Merge x y) where
src/Grenade/Layers/Pad.hs−1
@@ -6,7 +6,6 @@
6{-# LANGUAGE TypeFamilies #-}
6{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE FlexibleContexts #-}
8{-# LANGUAGE FlexibleContexts #-}
9−{-# LANGUAGE NoStarIsType #-}
10{-# LANGUAGE UndecidableInstances #-}
9{-# LANGUAGE UndecidableInstances #-}
11{-|
10{-|
12Module : Grenade.Core.Pad
11Module : Grenade.Core.Pad
src/Grenade/Layers/Pooling.hs−1
@@ -7,7 +7,6 @@
7{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE TypeFamilies #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
9{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE FlexibleContexts #-}
10−{-# LANGUAGE NoStarIsType #-}
11{-# LANGUAGE UndecidableInstances #-}
10{-# LANGUAGE UndecidableInstances #-}
12{-|
11{-|
13Module : Grenade.Core.Pooling
12Module : Grenade.Core.Pooling
src/Grenade/Layers/Reshape.hs−1
@@ -3,7 +3,6 @@
3{-# LANGUAGE TypeFamilies #-}
3{-# LANGUAGE TypeFamilies #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE FlexibleContexts #-}
6−{-# LANGUAGE NoStarIsType #-}
7{-# LANGUAGE UndecidableInstances #-}
6{-# LANGUAGE UndecidableInstances #-}
8{-|
7{-|
9Module : Grenade.Layers.Reshape
8Module : Grenade.Layers.Reshape
src/Grenade/Recurrent/Core/Layer.hs+4−2
@@ -8,6 +8,8 @@ module Grenade.Recurrent.Core.Layer (
8 , RecurrentUpdateLayer (..)
8 , RecurrentUpdateLayer (..)
9 ) where
9 ) where
10
10
11+import Data.Kind (Type)
12+
11import Grenade.Core
13import Grenade.Core
12
14
13-- | Class for a recurrent layer.
15-- | Class for a recurrent layer.
@@ -15,11 +17,11 @@ import Grenade.Core
15-- of an extra recurrent data shape.
17-- of an extra recurrent data shape.
16class UpdateLayer x => RecurrentUpdateLayer x where
18class UpdateLayer x => RecurrentUpdateLayer x where
17 -- | Shape of data that is passed between each subsequent run of the layer
19 -- | Shape of data that is passed between each subsequent run of the layer
18− type RecurrentShape x :: *
20+ type RecurrentShape x :: Type
19
21
20class (RecurrentUpdateLayer x, Num (RecurrentShape x)) => RecurrentLayer x (i :: Shape) (o :: Shape) where
22class (RecurrentUpdateLayer x, Num (RecurrentShape x)) => RecurrentLayer x (i :: Shape) (o :: Shape) where
21 -- | Wengert Tape
23 -- | Wengert Tape
22− type RecTape x i o :: *
24+ type RecTape x i o :: Type
23 -- | Used in training and scoring. Take the input from the previous
25 -- | Used in training and scoring. Take the input from the previous
24 -- layer, and give the output from this layer.
26 -- layer, and give the output from this layer.
25 runRecurrentForwards :: x -> RecurrentShape x -> S i -> (RecTape x i o, RecurrentShape x, S o)
27 runRecurrentForwards :: x -> RecurrentShape x -> S i -> (RecTape x i o, RecurrentShape x, S o)
src/Grenade/Recurrent/Core/Network.hs+10−8
@@ -32,18 +32,20 @@ import Data.Singletons ( SingI )
32import Data.Singletons.Prelude ( Head, Last )
32import Data.Singletons.Prelude ( Head, Last )
33import Data.Serialize
33import Data.Serialize
34
34
35+import Data.Kind (Type)
36+
35import Grenade.Core
37import Grenade.Core
36import Grenade.Recurrent.Core.Layer
38import Grenade.Recurrent.Core.Layer
37
39
38-- | Witness type to say indicate we're building up with a normal feed
40-- | Witness type to say indicate we're building up with a normal feed
39-- forward layer.
41-- forward layer.
40−data FeedForward :: * -> *
42+data FeedForward :: Type -> Type
41-- | Witness type to say indicate we're building up with a recurrent layer.
43-- | Witness type to say indicate we're building up with a recurrent layer.
42−data Recurrent :: * -> *
44+data Recurrent :: Type -> Type
43
45
44-- | Type of a recurrent neural network.
46-- | Type of a recurrent neural network.
45--
47--
46−-- The [*] type specifies the types of the layers.
48+-- The [Type] type specifies the types of the layers.
47--
49--
48-- The [Shape] type specifies the shapes of data passed between the layers.
50-- The [Shape] type specifies the shapes of data passed between the layers.
49--
51--
@@ -52,7 +54,7 @@ data Recurrent :: * -> *
52--
54--
53-- Often, to make the definitions more concise, one will use a type alias
55-- Often, to make the definitions more concise, one will use a type alias
54-- for these empty data types.
56-- for these empty data types.
55−data RecurrentNetwork :: [*] -> [Shape] -> * where
57+data RecurrentNetwork :: [Type] -> [Shape] -> Type where
56 RNil :: SingI i
58 RNil :: SingI i
57 => RecurrentNetwork '[] '[i]
59 => RecurrentNetwork '[] '[i]
58
60
@@ -71,7 +73,7 @@ infixr 5 :~@>
71-- | Gradient of a network.
73-- | Gradient of a network.
72--
74--
73-- Parameterised on the layers of the network.
75-- Parameterised on the layers of the network.
74−data RecurrentGradient :: [*] -> * where
76+data RecurrentGradient :: [Type] -> Type where
75 RGNil :: RecurrentGradient '[]
77 RGNil :: RecurrentGradient '[]
76
78
77 (://>) :: UpdateLayer x
79 (://>) :: UpdateLayer x
@@ -81,7 +83,7 @@ data RecurrentGradient :: [*] -> * where
81
83
82-- | Recurrent inputs (sideways shapes on an imaginary unrolled graph)
84-- | Recurrent inputs (sideways shapes on an imaginary unrolled graph)
83-- Parameterised on the layers of a Network.
85-- Parameterised on the layers of a Network.
84−data RecurrentInputs :: [*] -> * where
86+data RecurrentInputs :: [Type] -> Type where
85 RINil :: RecurrentInputs '[]
87 RINil :: RecurrentInputs '[]
86
88
87 (:~~+>) :: (UpdateLayer x, Fractional (RecurrentInputs xs))
89 (:~~+>) :: (UpdateLayer x, Fractional (RecurrentInputs xs))
@@ -95,7 +97,7 @@ data RecurrentInputs :: [*] -> * where
95--
97--
96-- We index on the time step length as well, to ensure
98-- We index on the time step length as well, to ensure
97-- that that all Tape lengths are the same.
99-- that that all Tape lengths are the same.
98−data RecurrentTape :: [*] -> [Shape] -> * where
100+data RecurrentTape :: [Type] -> [Shape] -> Type where
99 TRNil :: SingI i
101 TRNil :: SingI i
100 => RecurrentTape '[] '[i]
102 => RecurrentTape '[] '[i]
101
103
@@ -204,7 +206,7 @@ instance (Show x, Show (RecurrentNetwork xs rs)) => Show (RecurrentNetwork (Recu
204
206
205-- | A network can easily be created by hand with (:~~>) and (:~@>), but an easy way to initialise a random
207-- | A network can easily be created by hand with (:~~>) and (:~@>), but an easy way to initialise a random
206-- recurrent network and a set of random inputs for it is with the randomRecurrent.
208-- recurrent network and a set of random inputs for it is with the randomRecurrent.
207−class CreatableRecurrent (xs :: [*]) (ss :: [Shape]) where
209+class CreatableRecurrent (xs :: [Type]) (ss :: [Shape]) where
208 -- | Create a network of the types requested
210 -- | Create a network of the types requested
209 randomRecurrent :: MonadRandom m => m (RecurrentNetwork xs ss)
211 randomRecurrent :: MonadRandom m => m (RecurrentNetwork xs ss)
210
212
src/Grenade/Recurrent/Layers/BasicRecurrent.hs+4−2
@@ -18,6 +18,8 @@ import Control.Monad.Random ( MonadRandom, getRandom )
18
18
19import Data.Singletons.TypeLits
19import Data.Singletons.TypeLits
20
20
21+import Data.Kind (Type)
22+
21import Numeric.LinearAlgebra.Static
23import Numeric.LinearAlgebra.Static
22
24
23import GHC.TypeLits
25import GHC.TypeLits
@@ -27,7 +29,7 @@ import Grenade.Recurrent.Core
27
29
28data BasicRecurrent :: Nat -- Input layer size
30data BasicRecurrent :: Nat -- Input layer size
29 -> Nat -- Output layer size
31 -> Nat -- Output layer size
30− -> * where
32+ -> Type where
31 BasicRecurrent :: ( KnownNat input
33 BasicRecurrent :: ( KnownNat input
32 , KnownNat output
34 , KnownNat output
33 , KnownNat matrixCols
35 , KnownNat matrixCols
@@ -40,7 +42,7 @@ data BasicRecurrent :: Nat -- Input layer size
40
42
41data BasicRecurrent' :: Nat -- Input layer size
43data BasicRecurrent' :: Nat -- Input layer size
42 -> Nat -- Output layer size
44 -> Nat -- Output layer size
43− -> * where
45+ -> Type where
44 BasicRecurrent' :: ( KnownNat input
46 BasicRecurrent' :: ( KnownNat input
45 , KnownNat output
47 , KnownNat output
46 , KnownNat matrixCols
48 , KnownNat matrixCols
src/Grenade/Recurrent/Layers/ConcatRecurrent.hs+3−1
@@ -27,6 +27,8 @@ import Data.Serialize
27import Data.Singletons
27import Data.Singletons
28import GHC.TypeLits
28import GHC.TypeLits
29
29
30+import Data.Kind (Type)
31+
30import Grenade.Core
32import Grenade.Core
31import Grenade.Recurrent.Core
33import Grenade.Recurrent.Core
32
34
@@ -45,7 +47,7 @@ import Numeric.LinearAlgebra.Static ( (#), split, R )
45--
47--
46-- 3D images become 3D images with more channels. The sizes must be the same, one can use Pad
48-- 3D images become 3D images with more channels. The sizes must be the same, one can use Pad
47-- and Crop layers to ensure this is the case.
49-- and Crop layers to ensure this is the case.
48−data ConcatRecurrent :: Shape -> * -> Shape -> * -> * where
50+data ConcatRecurrent :: Shape -> Type -> Shape -> Type -> Type where
49 ConcatRecLeft :: x -> y -> ConcatRecurrent m (Recurrent x) n (FeedForward y)
51 ConcatRecLeft :: x -> y -> ConcatRecurrent m (Recurrent x) n (FeedForward y)
50 ConcatRecRight :: x -> y -> ConcatRecurrent m (FeedForward x) n (Recurrent y)
52 ConcatRecRight :: x -> y -> ConcatRecurrent m (FeedForward x) n (Recurrent y)
51 ConcatRecBoth :: x -> y -> ConcatRecurrent m (Recurrent x) n (Recurrent y)
53 ConcatRecBoth :: x -> y -> ConcatRecurrent m (Recurrent x) n (Recurrent y)
src/Grenade/Recurrent/Layers/LSTM.hs+4−2
@@ -23,6 +23,8 @@ import Data.Proxy
23import Data.Serialize
23import Data.Serialize
24import Data.Singletons.TypeLits
24import Data.Singletons.TypeLits
25
25
26+import Data.Kind (Type)
27+
26import qualified Numeric.LinearAlgebra as LA
28import qualified Numeric.LinearAlgebra as LA
27import Numeric.LinearAlgebra.Static
29import Numeric.LinearAlgebra.Static
28
30
@@ -36,14 +38,14 @@ import Grenade.Layers.Internal.Update
36-- This is a Peephole formulation, so the recurrent shape is
38-- This is a Peephole formulation, so the recurrent shape is
37-- just the cell state, the previous output is not held or used
39-- just the cell state, the previous output is not held or used
38-- at all.
40-- at all.
39−data LSTM :: Nat -> Nat -> * where
41+data LSTM :: Nat -> Nat -> Type where
40 LSTM :: ( KnownNat input
42 LSTM :: ( KnownNat input
41 , KnownNat output
43 , KnownNat output
42 ) => !(LSTMWeights input output) -- Weights
44 ) => !(LSTMWeights input output) -- Weights
43 -> !(LSTMWeights input output) -- Momentums
45 -> !(LSTMWeights input output) -- Momentums
44 -> LSTM input output
46 -> LSTM input output
45
47
46−data LSTMWeights :: Nat -> Nat -> * where
48+data LSTMWeights :: Nat -> Nat -> Type where
47 LSTMWeights :: ( KnownNat input
49 LSTMWeights :: ( KnownNat input
48 , KnownNat output
50 , KnownNat output
49 ) => {
51 ) => {