@@ -1,8 +1,8 @@
1{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE GADTs #-}
4{-# LANGUAGE GADTs #-}
4{-# LANGUAGE RankNTypes #-}
5{-# LANGUAGE RankNTypes #-}
5−{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE TypeOperators #-}
6{-# LANGUAGE TypeOperators #-}
7{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE TypeFamilies #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
@@ -21,7 +21,7 @@ import Control.Monad.Random ( MonadRandom, getRandom )
21-- import Data.List ( foldl1' )
21-- import Data.List ( foldl1' )
22import Data.Proxy
22import Data.Proxy
23import Data.Serialize
23import Data.Serialize
24−import Data.Singletons.TypeLits
24+import Data.Kind (Type)
26import qualified Numeric.LinearAlgebra as LA
26import qualified Numeric.LinearAlgebra as LA
27import Numeric.LinearAlgebra.Static
27import Numeric.LinearAlgebra.Static
@@ -29,6 +29,7 @@ import Numeric.LinearAlgebra.Static
29import Grenade.Core
29import Grenade.Core
30import Grenade.Recurrent.Core
30import Grenade.Recurrent.Core
31import Grenade.Layers.Internal.Update
31import Grenade.Layers.Internal.Update
34-- | Long Short Term Memory Recurrent unit
35-- | Long Short Term Memory Recurrent unit
@@ -36,14 +37,14 @@ import Grenade.Layers.Internal.Update
36-- This is a Peephole formulation, so the recurrent shape is
37-- This is a Peephole formulation, so the recurrent shape is
37-- just the cell state, the previous output is not held or used
38-- just the cell state, the previous output is not held or used
39−data LSTM :: Nat -> Nat -> * where
40+data LSTM :: Nat -> Nat -> Type where
40 LSTM :: ( KnownNat input
41 LSTM :: ( KnownNat input
41 , KnownNat output
42 , KnownNat output
42 ) => !(LSTMWeights input output) -- Weights
43 ) => !(LSTMWeights input output) -- Weights
43 -> !(LSTMWeights input output) -- Momentums
44 -> !(LSTMWeights input output) -- Momentums
44 -> LSTM input output
45 -> LSTM input output
46−data LSTMWeights :: Nat -> Nat -> * where
47+data LSTMWeights :: Nat -> Nat -> Type where
47 LSTMWeights :: ( KnownNat input
48 LSTMWeights :: ( KnownNat input
48 , KnownNat output
49 , KnownNat output
@@ -70,7 +71,7 @@ instance (KnownNat i, KnownNat o) => UpdateLayer (LSTM i o) where
71 -- Run the update function for each group matrix/vector of weights, momentums and gradients.
72 -- Run the update function for each group matrix/vector of weights, momentums and gradients.
72 -- Hmm, maybe the function should be used instead of passing in the learning parameters.
73 -- Hmm, maybe the function should be used instead of passing in the learning parameters.
73− runUpdate LearningParameters {..} (LSTM w m) g =
74+ runUpdate lp (LSTM w m) g =
74 let (wf, wf') = u lstmWf w m g
75 let (wf, wf') = u lstmWf w m g
75 (uf, uf') = u lstmUf w m g
76 (uf, uf') = u lstmUf w m g
76 (bf, bf') = v lstmBf w m g
77 (bf, bf') = v lstmBf w m g
@@ -87,11 +88,11 @@ instance (KnownNat i, KnownNat o) => UpdateLayer (LSTM i o) where
87 -- Utility function for updating with the momentum, gradients, and weights.
88 -- Utility function for updating with the momentum, gradients, and weights.
88 u :: forall x ix out. (KnownNat ix, KnownNat out) => (x -> (L out ix)) -> x -> x -> x -> ((L out ix), (L out ix))
89 u :: forall x ix out. (KnownNat ix, KnownNat out) => (x -> (L out ix)) -> x -> x -> x -> ((L out ix), (L out ix))
89 u e (e -> weights) (e -> momentum) (e -> gradient) =
90 u e (e -> weights) (e -> momentum) (e -> gradient) =
90− descendMatrix learningRate learningMomentum learningRegulariser weights gradient momentum
91+ descendMatrix (learningRate lp) (learningMomentum lp) (learningRegulariser lp) weights gradient momentum
92 v :: forall x ix. (KnownNat ix) => (x -> (R ix)) -> x -> x -> x -> ((R ix), (R ix))
93 v :: forall x ix. (KnownNat ix) => (x -> (R ix)) -> x -> x -> x -> ((R ix), (R ix))
93 v e (e -> weights) (e -> momentum) (e -> gradient) =
94 v e (e -> weights) (e -> momentum) (e -> gradient) =
94− descendVector learningRate learningMomentum learningRegulariser weights gradient momentum
95+ descendVector (learningRate lp) (learningMomentum lp) (learningRegulariser lp) weights gradient momentum
96 -- There's a lot of updates here, so to try and minimise the number of data copies
97 -- There's a lot of updates here, so to try and minimise the number of data copies
97 -- we'll create a mutable bucket for each.
98 -- we'll create a mutable bucket for each.
@@ -132,18 +133,18 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
132 type RecTape (LSTM i o) ('D1 i) ('D1 o) = (R o, R i, R o, R o, R o, R o, R o, R o, R o, R o, R o)
133 type RecTape (LSTM i o) ('D1 i) ('D1 o) = (R o, R i, R o, R o, R o, R o, R o, R o, R o, R o, R o)
133 -- Forward propagation for the LSTM layer.
134 -- Forward propagation for the LSTM layer.
134 -- The size of the cell state is also the size of the output.
135 -- The size of the cell state is also the size of the output.
135− runRecurrentForwards (LSTM (LSTMWeights {..}) _) (S1D cell) (S1D input) =
136+ runRecurrentForwards (LSTM lw _) (S1D cell) (S1D input) =
136 let -- Forget state vector
137 let -- Forget state vector
137− f_s = lstmBf + lstmWf #> input + lstmUf #> cell
138+ f_s = lstmBf lw + lstmWf lw #> input + lstmUf lw #> cell
138 f_t = sigmoid f_s
139 f_t = sigmoid f_s
139 -- Input state vector
140 -- Input state vector
140− i_s = lstmBi + lstmWi #> input + lstmUi #> cell
141+ i_s = lstmBi lw + lstmWi lw #> input + lstmUi lw #> cell
141 i_t = sigmoid i_s
142 i_t = sigmoid i_s
142 -- Output state vector
143 -- Output state vector
143− o_s = lstmBo + lstmWo #> input + lstmUo #> cell
144+ o_s = lstmBo lw + lstmWo lw #> input + lstmUo lw #> cell
144 o_t = sigmoid o_s
145 o_t = sigmoid o_s
145 -- Cell input state vector
146 -- Cell input state vector
146− c_s = lstmBc + lstmWc #> input
147+ c_s = lstmBc lw + lstmWc lw #> input
147 c_x = tanh c_s
148 c_x = tanh c_s
148 -- Cell state
149 -- Cell state
149 c_t = f_t * cell + i_t * c_x
150 c_t = f_t * cell + i_t * c_x
@@ -157,7 +158,7 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
158 -- There's a test version using the AD library without hmatrix in the test
159 -- There's a test version using the AD library without hmatrix in the test
159 -- suite. These should match always.
160 -- suite. These should match always.
160− runRecurrentBackwards (LSTM (LSTMWeights {..}) _) (cell, input, f_s, f_t, i_s, i_t, o_s, o_t, c_s, c_x, c_t) (S1D cellGrad) (S1D h_t') =
161+ runRecurrentBackwards (LSTM lw _) (cell, input, f_s, f_t, i_s, i_t, o_s, o_t, c_s, c_x, c_t) (S1D cellGrad) (S1D h_t') =
161 let -- Reverse Mode AD Derivitives
162 let -- Reverse Mode AD Derivitives
162 c_t' = h_t' * o_t + cellGrad
163 c_t' = h_t' * o_t + cellGrad
@@ -174,8 +175,8 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
174 c_s' = tanh' c_s * c_x'
175 c_s' = tanh' c_s * c_x'
176 -- The derivatives to pass sideways (recurrent) and downwards
177 -- The derivatives to pass sideways (recurrent) and downwards
177− cell' = tr lstmUf #> f_s' + tr lstmUo #> o_s' + tr lstmUi #> i_s' + c_t' * f_t
178+ cell' = tr (lstmUf lw) #> f_s' + tr (lstmUo lw) #> o_s' + tr (lstmUi lw) #> i_s' + c_t' * f_t
178− input' = tr lstmWf #> f_s' + tr lstmWo #> o_s' + tr lstmWi #> i_s' + tr lstmWc #> c_s'
179+ input' = tr (lstmWf lw) #> f_s' + tr (lstmWo lw) #> o_s' + tr (lstmWi lw) #> i_s' + tr (lstmWc lw) #> c_s'
180 -- Calculate the gradient Matricies for the input
181 -- Calculate the gradient Matricies for the input
181 lstmWf' = f_s' `outer` input
182 lstmWf' = f_s' `outer` input
@@ -234,44 +235,34 @@ tanh' :: (Floating a) => a -> a
234tanh' t = 1 - s ^ (2 :: Int) where s = tanh t
235tanh' t = 1 - s ^ (2 :: Int) where s = tanh t
236instance (KnownNat i, KnownNat o) => Serialize (LSTM i o) where
237instance (KnownNat i, KnownNat o) => Serialize (LSTM i o) where
237− put (LSTM LSTMWeights {..} _) = do
238+ put (LSTM lw _) = do
238− u lstmWf
239+ u (lstmWf lw)
239− u lstmUf
240+ u (lstmUf lw)
240− v lstmBf
241+ v (lstmBf lw)
241− u lstmWi
242+ u (lstmWi lw)
242− u lstmUi
243+ u (lstmUi lw)
243− v lstmBi
244+ v (lstmBi lw)
244− u lstmWo
245+ u (lstmWo lw)
245− u lstmUo
246+ u (lstmUo lw)
246− v lstmBo
247+ v (lstmBo lw)
247− u lstmWc
248+ u (lstmWc lw)
248− v lstmBc
249+ v (lstmBc lw)
250− u :: forall a b. (KnownNat a, KnownNat b) => Putter (L b a)
251+ u :: forall a b. (KnownNat a, KnownNat b) => Putter (L b a)
251− u = putListOf put . LA.toList . LA.flatten . extract
252+ u = putListOf put . LA.toList . LA.flatten . extract
252− v :: forall a. (KnownNat a) => Putter (R a)
253+ v :: forall a. (KnownNat a) => Putter (R a)
253− v = putListOf put . LA.toList . extract
254+ v = putListOf put . LA.toList . extract
256− lstmWf <- u
257+ w <- LSTMWeights <$> u <*> u <*> v <*> u <*> u <*> v <*> u <*> u <*> v <*> u <*> v
267− return $ LSTM (LSTMWeights {..}) (LSTMWeights w0 u0 v0 w0 u0 v0 w0 u0 v0 w0 v0)
258+ return $ LSTM w (LSTMWeights w0 u0 v0 w0 u0 v0 w0 u0 v0 w0 v0)
269− u :: forall a b. (KnownNat a, KnownNat b) => Get (L b a)
260+ u :: forall a b. (KnownNat a, KnownNat b) => Get (L b a)
270− u = let f = fromIntegral $ natVal (Proxy :: Proxy a)
261+ u = let f = fromIntegral $ natVal (Proxy :: Proxy a)
271− in maybe (fail "Vector of incorrect size") return . create . LA.reshape f . LA.fromList =<< getListOf get
262+ in maybe (fail "Vector of incorrect size") return . create . LA.reshape f . LA.fromList =<< getListOf get
272− v :: forall a. (KnownNat a) => Get (R a)
263+ v :: forall a. (KnownNat a) => Get (R a)
273− v = maybe (fail "Vector of incorrect size") return . create . LA.fromList =<< getListOf get
264+ v = maybe (fail "Vector of incorrect size") return . create . LA.fromList =<< getListOf get
275− w0 = konst 0
266+ w0 = konst 0
276− u0 = konst 0
267+ u0 = konst 0
277− v0 = konst 0
268+ v0 = konst 0