@@ -1,8 +1,8 @@
1−{-# LANGUAGE CPP #-}
21{-# LANGUAGE BangPatterns #-}
32{-# LANGUAGE DataKinds #-}
43{-# LANGUAGE GADTs #-}
54{-# LANGUAGE RankNTypes #-}
5+{-# LANGUAGE RecordWildCards #-}
66{-# LANGUAGE TypeOperators #-}
77{-# LANGUAGE TypeFamilies #-}
88{-# LANGUAGE MultiParamTypeClasses #-}
@@ -21,6 +21,8 @@ import Control.Monad.Random ( MonadRandom, getRandom )
2121-- import Data.List ( foldl1' )
2222import Data.Proxy
2323import Data.Serialize
24+import Data.Singletons.TypeLits
25+
2426import Data.Kind (Type)
2527
2628import qualified Numeric.LinearAlgebra as LA
@@ -29,7 +31,6 @@ import Numeric.LinearAlgebra.Static
2931import Grenade.Core
3032import Grenade.Recurrent.Core
3133import Grenade.Layers.Internal.Update
32−import GHC.TypeLits
3334
3435
3536-- | Long Short Term Memory Recurrent unit
@@ -71,7 +72,7 @@ instance (KnownNat i, KnownNat o) => UpdateLayer (LSTM i o) where
7172
7273 -- Run the update function for each group matrix/vector of weights, momentums and gradients.
7374 -- Hmm, maybe the function should be used instead of passing in the learning parameters.
74− runUpdate lp (LSTM w m) g =
75+ runUpdate LearningParameters {..} (LSTM w m) g =
7576 let (wf, wf') = u lstmWf w m g
7677 (uf, uf') = u lstmUf w m g
7778 (bf, bf') = v lstmBf w m g
@@ -88,11 +89,11 @@ instance (KnownNat i, KnownNat o) => UpdateLayer (LSTM i o) where
8889 -- Utility function for updating with the momentum, gradients, and weights.
8990 u :: forall x ix out. (KnownNat ix, KnownNat out) => (x -> (L out ix)) -> x -> x -> x -> ((L out ix), (L out ix))
9091 u e (e -> weights) (e -> momentum) (e -> gradient) =
91− descendMatrix (learningRate lp) (learningMomentum lp) (learningRegulariser lp) weights gradient momentum
92+ descendMatrix learningRate learningMomentum learningRegulariser weights gradient momentum
9293
9394 v :: forall x ix. (KnownNat ix) => (x -> (R ix)) -> x -> x -> x -> ((R ix), (R ix))
9495 v e (e -> weights) (e -> momentum) (e -> gradient) =
95− descendVector (learningRate lp) (learningMomentum lp) (learningRegulariser lp) weights gradient momentum
96+ descendVector learningRate learningMomentum learningRegulariser weights gradient momentum
9697
9798 -- There's a lot of updates here, so to try and minimise the number of data copies
9899 -- we'll create a mutable bucket for each.
@@ -133,18 +134,18 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
133134 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)
134135 -- Forward propagation for the LSTM layer.
135136 -- The size of the cell state is also the size of the output.
136− runRecurrentForwards (LSTM lw _) (S1D cell) (S1D input) =
137+ runRecurrentForwards (LSTM (LSTMWeights {..}) _) (S1D cell) (S1D input) =
137138 let -- Forget state vector
138− f_s = lstmBf lw + lstmWf lw #> input + lstmUf lw #> cell
139+ f_s = lstmBf + lstmWf #> input + lstmUf #> cell
139140 f_t = sigmoid f_s
140141 -- Input state vector
141− i_s = lstmBi lw + lstmWi lw #> input + lstmUi lw #> cell
142+ i_s = lstmBi + lstmWi #> input + lstmUi #> cell
142143 i_t = sigmoid i_s
143144 -- Output state vector
144− o_s = lstmBo lw + lstmWo lw #> input + lstmUo lw #> cell
145+ o_s = lstmBo + lstmWo #> input + lstmUo #> cell
145146 o_t = sigmoid o_s
146147 -- Cell input state vector
147− c_s = lstmBc lw + lstmWc lw #> input
148+ c_s = lstmBc + lstmWc #> input
148149 c_x = tanh c_s
149150 -- Cell state
150151 c_t = f_t * cell + i_t * c_x
@@ -158,7 +159,7 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
158159 --
159160 -- There's a test version using the AD library without hmatrix in the test
160161 -- suite. These should match always.
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') =
162+ 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') =
162163 let -- Reverse Mode AD Derivitives
163164 c_t' = h_t' * o_t + cellGrad
164165
@@ -175,8 +176,8 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
175176 c_s' = tanh' c_s * c_x'
176177
177178 -- The derivatives to pass sideways (recurrent) and downwards
178− cell' = tr (lstmUf lw) #> f_s' + tr (lstmUo lw) #> o_s' + tr (lstmUi lw) #> i_s' + c_t' * f_t
179− input' = tr (lstmWf lw) #> f_s' + tr (lstmWo lw) #> o_s' + tr (lstmWi lw) #> i_s' + tr (lstmWc lw) #> c_s'
179+ cell' = tr lstmUf #> f_s' + tr lstmUo #> o_s' + tr lstmUi #> i_s' + c_t' * f_t
180+ input' = tr lstmWf #> f_s' + tr lstmWo #> o_s' + tr lstmWi #> i_s' + tr lstmWc #> c_s'
180181
181182 -- Calculate the gradient Matricies for the input
182183 lstmWf' = f_s' `outer` input
@@ -235,34 +236,44 @@ tanh' :: (Floating a) => a -> a
235236tanh' t = 1 - s ^ (2 :: Int) where s = tanh t
236237
237238instance (KnownNat i, KnownNat o) => Serialize (LSTM i o) where
238− put (LSTM lw _) = do
239− u (lstmWf lw)
240− u (lstmUf lw)
241− v (lstmBf lw)
242− u (lstmWi lw)
243− u (lstmUi lw)
244− v (lstmBi lw)
245− u (lstmWo lw)
246− u (lstmUo lw)
247− v (lstmBo lw)
248− u (lstmWc lw)
249− v (lstmBc lw)
250− where
251− u :: forall a b. (KnownNat a, KnownNat b) => Putter (L b a)
252− u = putListOf put . LA.toList . LA.flatten . extract
253− v :: forall a. (KnownNat a) => Putter (R a)
254− v = putListOf put . LA.toList . extract
239+ put (LSTM LSTMWeights {..} _) = do
240+ u lstmWf
241+ u lstmUf
242+ v lstmBf
243+ u lstmWi
244+ u lstmUi
245+ v lstmBi
246+ u lstmWo
247+ u lstmUo
248+ v lstmBo
249+ u lstmWc
250+ v lstmBc
251+ where
252+ u :: forall a b. (KnownNat a, KnownNat b) => Putter (L b a)
253+ u = putListOf put . LA.toList . LA.flatten . extract
254+ v :: forall a. (KnownNat a) => Putter (R a)
255+ v = putListOf put . LA.toList . extract
255256
256257 get = do
257− w <- LSTMWeights <$> u <*> u <*> v <*> u <*> u <*> v <*> u <*> u <*> v <*> u <*> v
258− return $ LSTM w (LSTMWeights w0 u0 v0 w0 u0 v0 w0 u0 v0 w0 v0)
259− where
260− u :: forall a b. (KnownNat a, KnownNat b) => Get (L b a)
261− u = let f = fromIntegral $ natVal (Proxy :: Proxy a)
262− in maybe (fail "Vector of incorrect size") return . create . LA.reshape f . LA.fromList =<< getListOf get
263− v :: forall a. (KnownNat a) => Get (R a)
264− v = maybe (fail "Vector of incorrect size") return . create . LA.fromList =<< getListOf get
265−
266− w0 = konst 0
267− u0 = konst 0
268− v0 = konst 0
258+ lstmWf <- u
259+ lstmUf <- u
260+ lstmBf <- v
261+ lstmWi <- u
262+ lstmUi <- u
263+ lstmBi <- v
264+ lstmWo <- u
265+ lstmUo <- u
266+ lstmBo <- v
267+ lstmWc <- u
268+ lstmBc <- v
269+ return $ LSTM (LSTMWeights {..}) (LSTMWeights w0 u0 v0 w0 u0 v0 w0 u0 v0 w0 v0)
270+ where
271+ u :: forall a b. (KnownNat a, KnownNat b) => Get (L b a)
272+ u = let f = fromIntegral $ natVal (Proxy :: Proxy a)
273+ in maybe (fail "Vector of incorrect size") return . create . LA.reshape f . LA.fromList =<< getListOf get
274+ v :: forall a. (KnownNat a) => Get (R a)
275+ v = maybe (fail "Vector of incorrect size") return . create . LA.fromList =<< getListOf get
276+
277+ w0 = konst 0
278+ u0 = konst 0
279+ v0 = konst 0