@@ -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,7 +21,7 @@ import Control.Monad.Random ( MonadRandom, getRandom )
2121-- import Data.List ( foldl1' )
2222import Data.Proxy
2323import Data.Serialize
24−import Data.Kind (Type)
24+import Data.Singletons.TypeLits
2525
2626import qualified Numeric.LinearAlgebra as LA
2727import Numeric.LinearAlgebra.Static
@@ -29,7 +29,6 @@ import Numeric.LinearAlgebra.Static
2929import Grenade.Core
3030import Grenade.Recurrent.Core
3131import Grenade.Layers.Internal.Update
32−import GHC.TypeLits
3332
3433
3534-- | Long Short Term Memory Recurrent unit
@@ -37,14 +36,14 @@ import GHC.TypeLits
3736-- This is a Peephole formulation, so the recurrent shape is
3837-- just the cell state, the previous output is not held or used
3938-- at all.
40−data LSTM :: Nat -> Nat -> Type where
39+data LSTM :: Nat -> Nat -> * where
4140 LSTM :: ( KnownNat input
4241 , KnownNat output
4342 ) => !(LSTMWeights input output) -- Weights
4443 -> !(LSTMWeights input output) -- Momentums
4544 -> LSTM input output
4645
47−data LSTMWeights :: Nat -> Nat -> Type where
46+data LSTMWeights :: Nat -> Nat -> * where
4847 LSTMWeights :: ( KnownNat input
4948 , KnownNat output
5049 ) => {
@@ -71,7 +70,7 @@ instance (KnownNat i, KnownNat o) => UpdateLayer (LSTM i o) where
7170
7271 -- Run the update function for each group matrix/vector of weights, momentums and gradients.
7372 -- Hmm, maybe the function should be used instead of passing in the learning parameters.
74− runUpdate lp (LSTM w m) g =
73+ runUpdate LearningParameters {..} (LSTM w m) g =
7574 let (wf, wf') = u lstmWf w m g
7675 (uf, uf') = u lstmUf w m g
7776 (bf, bf') = v lstmBf w m g
@@ -88,11 +87,11 @@ instance (KnownNat i, KnownNat o) => UpdateLayer (LSTM i o) where
8887 -- Utility function for updating with the momentum, gradients, and weights.
8988 u :: forall x ix out. (KnownNat ix, KnownNat out) => (x -> (L out ix)) -> x -> x -> x -> ((L out ix), (L out ix))
9089 u e (e -> weights) (e -> momentum) (e -> gradient) =
91− descendMatrix (learningRate lp) (learningMomentum lp) (learningRegulariser lp) weights gradient momentum
90+ descendMatrix learningRate learningMomentum learningRegulariser weights gradient momentum
9291
9392 v :: forall x ix. (KnownNat ix) => (x -> (R ix)) -> x -> x -> x -> ((R ix), (R ix))
9493 v e (e -> weights) (e -> momentum) (e -> gradient) =
95− descendVector (learningRate lp) (learningMomentum lp) (learningRegulariser lp) weights gradient momentum
94+ descendVector learningRate learningMomentum learningRegulariser weights gradient momentum
9695
9796 -- There's a lot of updates here, so to try and minimise the number of data copies
9897 -- we'll create a mutable bucket for each.
@@ -133,18 +132,18 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
133132 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)
134133 -- Forward propagation for the LSTM layer.
135134 -- The size of the cell state is also the size of the output.
136− runRecurrentForwards (LSTM lw _) (S1D cell) (S1D input) =
135+ runRecurrentForwards (LSTM (LSTMWeights {..}) _) (S1D cell) (S1D input) =
137136 let -- Forget state vector
138− f_s = lstmBf lw + lstmWf lw #> input + lstmUf lw #> cell
137+ f_s = lstmBf + lstmWf #> input + lstmUf #> cell
139138 f_t = sigmoid f_s
140139 -- Input state vector
141− i_s = lstmBi lw + lstmWi lw #> input + lstmUi lw #> cell
140+ i_s = lstmBi + lstmWi #> input + lstmUi #> cell
142141 i_t = sigmoid i_s
143142 -- Output state vector
144− o_s = lstmBo lw + lstmWo lw #> input + lstmUo lw #> cell
143+ o_s = lstmBo + lstmWo #> input + lstmUo #> cell
145144 o_t = sigmoid o_s
146145 -- Cell input state vector
147− c_s = lstmBc lw + lstmWc lw #> input
146+ c_s = lstmBc + lstmWc #> input
148147 c_x = tanh c_s
149148 -- Cell state
150149 c_t = f_t * cell + i_t * c_x
@@ -158,7 +157,7 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
158157 --
159158 -- There's a test version using the AD library without hmatrix in the test
160159 -- 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') =
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') =
162161 let -- Reverse Mode AD Derivitives
163162 c_t' = h_t' * o_t + cellGrad
164163
@@ -175,8 +174,8 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
175174 c_s' = tanh' c_s * c_x'
176175
177176 -- 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'
177+ cell' = tr lstmUf #> f_s' + tr lstmUo #> o_s' + tr lstmUi #> i_s' + c_t' * f_t
178+ input' = tr lstmWf #> f_s' + tr lstmWo #> o_s' + tr lstmWi #> i_s' + tr lstmWc #> c_s'
180179
181180 -- Calculate the gradient Matricies for the input
182181 lstmWf' = f_s' `outer` input
@@ -235,34 +234,44 @@ tanh' :: (Floating a) => a -> a
235234tanh' t = 1 - s ^ (2 :: Int) where s = tanh t
236235
237236instance (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
237+ put (LSTM LSTMWeights {..} _) = do
238+ u lstmWf
239+ u lstmUf
240+ v lstmBf
241+ u lstmWi
242+ u lstmUi
243+ v lstmBi
244+ u lstmWo
245+ u lstmUo
246+ v lstmBo
247+ u lstmWc
248+ v lstmBc
249+ where
250+ u :: forall a b. (KnownNat a, KnownNat b) => Putter (L b a)
251+ u = putListOf put . LA.toList . LA.flatten . extract
252+ v :: forall a. (KnownNat a) => Putter (R a)
253+ v = putListOf put . LA.toList . extract
255254
256255 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
256+ lstmWf <- u
257+ lstmUf <- u
258+ lstmBf <- v
259+ lstmWi <- u
260+ lstmUi <- u
261+ lstmBi <- v
262+ lstmWo <- u
263+ lstmUo <- u
264+ lstmBo <- v
265+ lstmWc <- u
266+ lstmBc <- v
267+ return $ LSTM (LSTMWeights {..}) (LSTMWeights w0 u0 v0 w0 u0 v0 w0 u0 v0 w0 v0)
268+ where
269+ u :: forall a b. (KnownNat a, KnownNat b) => Get (L b a)
270+ 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
272+ v :: forall a. (KnownNat a) => Get (R a)
273+ v = maybe (fail "Vector of incorrect size") return . create . LA.fromList =<< getListOf get
274+
275+ w0 = konst 0
276+ u0 = konst 0
277+ v0 = konst 0