@@ -1,8 +1,8 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE BangPatterns #-}
23{-# LANGUAGE DataKinds #-}
34{-# LANGUAGE GADTs #-}
45{-# LANGUAGE RankNTypes #-}
5−{-# LANGUAGE RecordWildCards #-}
66{-# LANGUAGE TypeOperators #-}
77{-# LANGUAGE TypeFamilies #-}
88{-# LANGUAGE MultiParamTypeClasses #-}
@@ -21,8 +21,6 @@ import Control.Monad.Random ( MonadRandom, getRandom )
2121-- import Data.List ( foldl1' )
2222import Data.Proxy
2323import Data.Serialize
24−import Data.Singletons.TypeLits
25−
2624import Data.Kind (Type)
2725
2826import qualified Numeric.LinearAlgebra as LA
@@ -31,6 +29,7 @@ import Numeric.LinearAlgebra.Static
3129import Grenade.Core
3230import Grenade.Recurrent.Core
3331import Grenade.Layers.Internal.Update
32+import GHC.TypeLits
3433
3534
3635-- | Long Short Term Memory Recurrent unit
@@ -72,7 +71,7 @@ instance (KnownNat i, KnownNat o) => UpdateLayer (LSTM i o) where
7271
7372 -- Run the update function for each group matrix/vector of weights, momentums and gradients.
7473 -- Hmm, maybe the function should be used instead of passing in the learning parameters.
75− runUpdate LearningParameters {..} (LSTM w m) g =
74+ runUpdate lp (LSTM w m) g =
7675 let (wf, wf') = u lstmWf w m g
7776 (uf, uf') = u lstmUf w m g
7877 (bf, bf') = v lstmBf w m g
@@ -89,11 +88,11 @@ instance (KnownNat i, KnownNat o) => UpdateLayer (LSTM i o) where
8988 -- Utility function for updating with the momentum, gradients, and weights.
9089 u :: forall x ix out. (KnownNat ix, KnownNat out) => (x -> (L out ix)) -> x -> x -> x -> ((L out ix), (L out ix))
9190 u e (e -> weights) (e -> momentum) (e -> gradient) =
92− descendMatrix learningRate learningMomentum learningRegulariser weights gradient momentum
91+ descendMatrix (learningRate lp) (learningMomentum lp) (learningRegulariser lp) weights gradient momentum
9392
9493 v :: forall x ix. (KnownNat ix) => (x -> (R ix)) -> x -> x -> x -> ((R ix), (R ix))
9594 v e (e -> weights) (e -> momentum) (e -> gradient) =
96− descendVector learningRate learningMomentum learningRegulariser weights gradient momentum
95+ descendVector (learningRate lp) (learningMomentum lp) (learningRegulariser lp) weights gradient momentum
9796
9897 -- There's a lot of updates here, so to try and minimise the number of data copies
9998 -- we'll create a mutable bucket for each.
@@ -134,18 +133,18 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
134133 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)
135134 -- Forward propagation for the LSTM layer.
136135 -- The size of the cell state is also the size of the output.
137− runRecurrentForwards (LSTM (LSTMWeights {..}) _) (S1D cell) (S1D input) =
136+ runRecurrentForwards (LSTM lw _) (S1D cell) (S1D input) =
138137 let -- Forget state vector
139− f_s = lstmBf + lstmWf #> input + lstmUf #> cell
138+ f_s = lstmBf lw + lstmWf lw #> input + lstmUf lw #> cell
140139 f_t = sigmoid f_s
141140 -- Input state vector
142− i_s = lstmBi + lstmWi #> input + lstmUi #> cell
141+ i_s = lstmBi lw + lstmWi lw #> input + lstmUi lw #> cell
143142 i_t = sigmoid i_s
144143 -- Output state vector
145− o_s = lstmBo + lstmWo #> input + lstmUo #> cell
144+ o_s = lstmBo lw + lstmWo lw #> input + lstmUo lw #> cell
146145 o_t = sigmoid o_s
147146 -- Cell input state vector
148− c_s = lstmBc + lstmWc #> input
147+ c_s = lstmBc lw + lstmWc lw #> input
149148 c_x = tanh c_s
150149 -- Cell state
151150 c_t = f_t * cell + i_t * c_x
@@ -159,7 +158,7 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
159158 --
160159 -- There's a test version using the AD library without hmatrix in the test
161160 -- suite. These should match always.
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') =
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') =
163162 let -- Reverse Mode AD Derivitives
164163 c_t' = h_t' * o_t + cellGrad
165164
@@ -176,8 +175,8 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
176175 c_s' = tanh' c_s * c_x'
177176
178177 -- The derivatives to pass sideways (recurrent) and downwards
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'
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'
181180
182181 -- Calculate the gradient Matricies for the input
183182 lstmWf' = f_s' `outer` input
@@ -236,44 +235,34 @@ tanh' :: (Floating a) => a -> a
236235tanh' t = 1 - s ^ (2 :: Int) where s = tanh t
237236
238237instance (KnownNat i, KnownNat o) => Serialize (LSTM i o) where
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
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
256255
257256 get = do
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
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