@@ -3,6 +3,7 @@
33{-# LANGUAGE DataKinds #-}
44{-# LANGUAGE GADTs #-}
55{-# LANGUAGE RankNTypes #-}
6+{-# LANGUAGE RecordWildCards #-}
67{-# LANGUAGE TypeOperators #-}
78{-# LANGUAGE TypeFamilies #-}
89{-# LANGUAGE MultiParamTypeClasses #-}
@@ -21,7 +22,11 @@ import Control.Monad.Random ( MonadRandom, getRandom )
2122-- import Data.List ( foldl1' )
2223import Data.Proxy
2324import Data.Serialize
25+import Data.Singletons.TypeLits
26+
27+#if MIN_VERSION_base(4,9,0)
2428import Data.Kind (Type)
29+#endif
2530
2631import qualified Numeric.LinearAlgebra as LA
2732import Numeric.LinearAlgebra.Static
@@ -29,7 +34,6 @@ import Numeric.LinearAlgebra.Static
2934import Grenade.Core
3035import Grenade.Recurrent.Core
3136import Grenade.Layers.Internal.Update
32−import GHC.TypeLits
3337
3438
3539-- | Long Short Term Memory Recurrent unit
@@ -71,7 +75,7 @@ instance (KnownNat i, KnownNat o) => UpdateLayer (LSTM i o) where
7175
7276 -- Run the update function for each group matrix/vector of weights, momentums and gradients.
7377 -- Hmm, maybe the function should be used instead of passing in the learning parameters.
74− runUpdate lp (LSTM w m) g =
78+ runUpdate LearningParameters {..} (LSTM w m) g =
7579 let (wf, wf') = u lstmWf w m g
7680 (uf, uf') = u lstmUf w m g
7781 (bf, bf') = v lstmBf w m g
@@ -88,11 +92,11 @@ instance (KnownNat i, KnownNat o) => UpdateLayer (LSTM i o) where
8892 -- Utility function for updating with the momentum, gradients, and weights.
8993 u :: forall x ix out. (KnownNat ix, KnownNat out) => (x -> (L out ix)) -> x -> x -> x -> ((L out ix), (L out ix))
9094 u e (e -> weights) (e -> momentum) (e -> gradient) =
91− descendMatrix (learningRate lp) (learningMomentum lp) (learningRegulariser lp) weights gradient momentum
95+ descendMatrix learningRate learningMomentum learningRegulariser weights gradient momentum
9296
9397 v :: forall x ix. (KnownNat ix) => (x -> (R ix)) -> x -> x -> x -> ((R ix), (R ix))
9498 v e (e -> weights) (e -> momentum) (e -> gradient) =
95− descendVector (learningRate lp) (learningMomentum lp) (learningRegulariser lp) weights gradient momentum
99+ descendVector learningRate learningMomentum learningRegulariser weights gradient momentum
96100
97101 -- There's a lot of updates here, so to try and minimise the number of data copies
98102 -- we'll create a mutable bucket for each.
@@ -133,18 +137,18 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
133137 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)
134138 -- Forward propagation for the LSTM layer.
135139 -- The size of the cell state is also the size of the output.
136− runRecurrentForwards (LSTM lw _) (S1D cell) (S1D input) =
140+ runRecurrentForwards (LSTM (LSTMWeights {..}) _) (S1D cell) (S1D input) =
137141 let -- Forget state vector
138− f_s = lstmBf lw + lstmWf lw #> input + lstmUf lw #> cell
142+ f_s = lstmBf + lstmWf #> input + lstmUf #> cell
139143 f_t = sigmoid f_s
140144 -- Input state vector
141− i_s = lstmBi lw + lstmWi lw #> input + lstmUi lw #> cell
145+ i_s = lstmBi + lstmWi #> input + lstmUi #> cell
142146 i_t = sigmoid i_s
143147 -- Output state vector
144− o_s = lstmBo lw + lstmWo lw #> input + lstmUo lw #> cell
148+ o_s = lstmBo + lstmWo #> input + lstmUo #> cell
145149 o_t = sigmoid o_s
146150 -- Cell input state vector
147− c_s = lstmBc lw + lstmWc lw #> input
151+ c_s = lstmBc + lstmWc #> input
148152 c_x = tanh c_s
149153 -- Cell state
150154 c_t = f_t * cell + i_t * c_x
@@ -158,7 +162,7 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
158162 --
159163 -- There's a test version using the AD library without hmatrix in the test
160164 -- 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') =
165+ 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') =
162166 let -- Reverse Mode AD Derivitives
163167 c_t' = h_t' * o_t + cellGrad
164168
@@ -175,8 +179,8 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
175179 c_s' = tanh' c_s * c_x'
176180
177181 -- 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'
182+ cell' = tr lstmUf #> f_s' + tr lstmUo #> o_s' + tr lstmUi #> i_s' + c_t' * f_t
183+ input' = tr lstmWf #> f_s' + tr lstmWo #> o_s' + tr lstmWi #> i_s' + tr lstmWc #> c_s'
180184
181185 -- Calculate the gradient Matricies for the input
182186 lstmWf' = f_s' `outer` input
@@ -235,34 +239,44 @@ tanh' :: (Floating a) => a -> a
235239tanh' t = 1 - s ^ (2 :: Int) where s = tanh t
236240
237241instance (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
242+ put (LSTM LSTMWeights {..} _) = do
243+ u lstmWf
244+ u lstmUf
245+ v lstmBf
246+ u lstmWi
247+ u lstmUi
248+ v lstmBi
249+ u lstmWo
250+ u lstmUo
251+ v lstmBo
252+ u lstmWc
253+ v lstmBc
254+ where
255+ u :: forall a b. (KnownNat a, KnownNat b) => Putter (L b a)
256+ u = putListOf put . LA.toList . LA.flatten . extract
257+ v :: forall a. (KnownNat a) => Putter (R a)
258+ v = putListOf put . LA.toList . extract
255259
256260 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
261+ lstmWf <- u
262+ lstmUf <- u
263+ lstmBf <- v
264+ lstmWi <- u
265+ lstmUi <- u
266+ lstmBi <- v
267+ lstmWo <- u
268+ lstmUo <- u
269+ lstmBo <- v
270+ lstmWc <- u
271+ lstmBc <- v
272+ return $ LSTM (LSTMWeights {..}) (LSTMWeights w0 u0 v0 w0 u0 v0 w0 u0 v0 w0 v0)
273+ where
274+ u :: forall a b. (KnownNat a, KnownNat b) => Get (L b a)
275+ u = let f = fromIntegral $ natVal (Proxy :: Proxy a)
276+ in maybe (fail "Vector of incorrect size") return . create . LA.reshape f . LA.fromList =<< getListOf get
277+ v :: forall a. (KnownNat a) => Get (R a)
278+ v = maybe (fail "Vector of incorrect size") return . create . LA.fromList =<< getListOf get
279+
280+ w0 = konst 0
281+ u0 = konst 0
282+ v0 = konst 0