on master help sign in
Remove use of RecordWildCards
commit5206c95c423d9755e620f41576470a281ba59c89 authorErik de Castro Lopo <erikd@mega-nerd.com> dateSat 11 Apr 2020, 17:18 (7 years ago) parentf19da0f
8 files changed+92−105
examples/main/shakespeare.hs+12−13 src/Grenade/Layers/Convolution.hs+2−3 src/Grenade/Layers/Deconvolution.hs+2−3 src/Grenade/Layers/FullyConnected.hs+3−4 src/Grenade/Recurrent/Core/Runner.hs+4−5 src/Grenade/Recurrent/Layers/BasicRecurrent.hs+4−5 src/Grenade/Recurrent/Layers/LSTM.hs+40−51 test/Test/Grenade/Recurrent/Layers/LSTM/Reference.hs+25−21
examples/main/shakespeare.hs+12−13
@@ -1,6 +1,5 @@
11{-# LANGUAGE BangPatterns #-}
22{-# LANGUAGE CPP #-}
3−{-# LANGUAGE RecordWildCards #-}
43{-# LANGUAGE DataKinds #-}
54{-# LANGUAGE ScopedTypeVariables #-}
65{-# LANGUAGE TypeOperators #-}
@@ -83,34 +82,34 @@ loadShakespeare path = do
8382 return (V.fromList hot, m, cs)
8483
8584trainSlice :: LearningParameters -> Shakespeare -> Shakespearian -> Vector Int -> Int -> Int -> (Shakespeare, Shakespearian)
86−trainSlice !rate !net !recIns input offset size =
85+trainSlice !lrate !net !recIns input offset size =
8786 let e = fmap (x . oneHot) . V.toList $ V.slice offset size input
8887 in case reverse e of
8988 (o : l : xs) ->
9089 let examples = reverse $ (l, Just o) : ((,Nothing) <$> xs)
91− in trainRecurrent rate net recIns examples
90+ in trainRecurrent lrate net recIns examples
9291 _ -> error "Not enough input"
9392 where
9493 x = fromMaybe (error "Hot variable didn't fit.")
9594
9695runShakespeare :: ShakespeareOpts -> ExceptT String IO ()
97−runShakespeare ShakespeareOpts {..} = do
98− (shakespeare, oneHotMap, oneHotDictionary) <- loadShakespeare trainingFile
96+runShakespeare opts = do
97+ (shakespeare, oneHotMap, oneHotDictionary) <- loadShakespeare $ trainingFile opts
9998 (net0, i0) <- lift $
100− case loadPath of
99+ case loadPath opts of
101100 Just loadFile -> netLoad loadFile
102101 Nothing -> (,0) <$> randomNet
103102
104103 (trained, bestInput) <- lift $ foldM (\(!net, !io) size -> do
105− xs <- take (iterations `div` 10) <$> getRandomRs (0, length shakespeare - size - 1)
106− let (!trained, !bestInput) = foldl' (\(!n, !i) offset -> trainSlice rate n i shakespeare offset size) (net, io) xs
107− results <- take 1000 <$> generateParagraph trained bestInput temperature oneHotMap oneHotDictionary ( S1D $ konst 0)
104+ xs <- take (iterations opts `div` 10) <$> getRandomRs (0, length shakespeare - size - 1)
105+ let (!trained, !bestInput) = foldl' (\(!n, !i) offset -> trainSlice (rate opts) n i shakespeare offset size) (net, io) xs
106+ results <- take 1000 <$> generateParagraph trained bestInput (temperature opts) oneHotMap oneHotDictionary ( S1D $ konst 0)
108107 putStrLn ("TRAINING STEP WITH SIZE: " ++ show size)
109108 putStrLn (unAnnotateCapitals results)
110109 return (trained, bestInput)
111− ) (net0, i0) $ replicate 10 sequenceSize
110+ ) (net0, i0) $ replicate 10 (sequenceSize opts)
112111
113− case savePath of
112+ case savePath opts of
114113 Just saveFile -> lift . B.writeFile saveFile $ runPut (put trained >> put bestInput)
115114 Nothing -> return ()
116115
@@ -122,12 +121,12 @@ generateParagraph :: forall layers shapes n a. (Last shapes ~ 'D1 n, Head shapes
122121 -> Vector a
123122 -> S ('D1 n)
124123 -> IO [a]
125−generateParagraph n s temperature hotmap hotdict =
124+generateParagraph n s temp hotmap hotdict =
126125 go s
127126 where
128127 go x y =
129128 do let (_, ns, o) = runRecurrent n x y
130− un <- sample temperature hotdict o
129+ un <- sample temp hotdict o
131130 Just re <- return $ makeHot hotmap un
132131 rest <- unsafeInterleaveIO $ go ns re
133132 return (un : rest)
src/Grenade/Layers/Convolution.hs+2−3
@@ -1,7 +1,6 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE DataKinds #-}
33{-# LANGUAGE ScopedTypeVariables #-}
4−{-# LANGUAGE RecordWildCards #-}
54{-# LANGUAGE GADTs #-}
65{-# LANGUAGE TypeOperators #-}
76{-# LANGUAGE TypeFamilies #-}
@@ -139,8 +138,8 @@ instance ( KnownNat channels
139138 , KnownNat (kernelRows * kernelColumns * channels)
140139 ) => UpdateLayer (Convolution channels filters kernelRows kernelColumns strideRows strideColumns) where
141140 type Gradient (Convolution channels filters kernelRows kernelColumns strideRows strideColumns) = (Convolution' channels filters kernelRows kernelColumns strideRows strideColumns)
142− runUpdate LearningParameters {..} (Convolution oldKernel oldMomentum) (Convolution' kernelGradient) =
143− let (newKernel, newMomentum) = descendMatrix learningRate learningMomentum learningRegulariser oldKernel kernelGradient oldMomentum
141+ runUpdate lp (Convolution oldKernel oldMomentum) (Convolution' kernelGradient) =
142+ let (newKernel, newMomentum) = descendMatrix (learningRate lp) (learningMomentum lp) (learningRegulariser lp) oldKernel kernelGradient oldMomentum
144143 in Convolution newKernel newMomentum
145144
146145 createRandom = randomConvolution
src/Grenade/Layers/Deconvolution.hs+2−3
@@ -1,7 +1,6 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE DataKinds #-}
33{-# LANGUAGE ScopedTypeVariables #-}
4−{-# LANGUAGE RecordWildCards #-}
54{-# LANGUAGE GADTs #-}
65{-# LANGUAGE TypeOperators #-}
76{-# LANGUAGE TypeFamilies #-}
@@ -138,8 +137,8 @@ instance ( KnownNat channels
138137 , KnownNat (kernelRows * kernelColumns * filters)
139138 ) => UpdateLayer (Deconvolution channels filters kernelRows kernelColumns strideRows strideColumns) where
140139 type Gradient (Deconvolution channels filters kernelRows kernelColumns strideRows strideColumns) = (Deconvolution' channels filters kernelRows kernelColumns strideRows strideColumns)
141− runUpdate LearningParameters {..} (Deconvolution oldKernel oldMomentum) (Deconvolution' kernelGradient) =
142− let (newKernel, newMomentum) = descendMatrix learningRate learningMomentum learningRegulariser oldKernel kernelGradient oldMomentum
140+ runUpdate lp (Deconvolution oldKernel oldMomentum) (Deconvolution' kernelGradient) =
141+ let (newKernel, newMomentum) = descendMatrix (learningRate lp) (learningMomentum lp) (learningRegulariser lp) oldKernel kernelGradient oldMomentum
143142 in Deconvolution newKernel newMomentum
144143
145144 createRandom = randomDeconvolution
src/Grenade/Layers/FullyConnected.hs+3−4
@@ -1,5 +1,4 @@
11{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE RecordWildCards #-}
32{-# LANGUAGE TypeOperators #-}
43{-# LANGUAGE TypeFamilies #-}
54{-# LANGUAGE MultiParamTypeClasses #-}
@@ -38,9 +37,9 @@ instance Show (FullyConnected i o) where
3837instance (KnownNat i, KnownNat o) => UpdateLayer (FullyConnected i o) where
3938 type Gradient (FullyConnected i o) = (FullyConnected' i o)
4039
41− runUpdate LearningParameters {..} (FullyConnected (FullyConnected' oldBias oldActivations) (FullyConnected' oldBiasMomentum oldMomentum)) (FullyConnected' biasGradient activationGradient) =
42− let (newBias, newBiasMomentum) = descendVector learningRate learningMomentum learningRegulariser oldBias biasGradient oldBiasMomentum
43− (newActivations, newMomentum) = descendMatrix learningRate learningMomentum learningRegulariser oldActivations activationGradient oldMomentum
40+ runUpdate lp (FullyConnected (FullyConnected' oldBias oldActivations) (FullyConnected' oldBiasMomentum oldMomentum)) (FullyConnected' biasGradient activationGradient) =
41+ let (newBias, newBiasMomentum) = descendVector (learningRate lp) (learningMomentum lp) (learningRegulariser lp) oldBias biasGradient oldBiasMomentum
42+ (newActivations, newMomentum) = descendMatrix (learningRate lp) (learningMomentum lp) (learningRegulariser lp) oldActivations activationGradient oldMomentum
4443 in FullyConnected (FullyConnected' newBias newActivations) (FullyConnected' newBiasMomentum newMomentum)
4544
4645 createRandom = randomFullyConnected
src/Grenade/Recurrent/Core/Runner.hs+4−5
@@ -6,7 +6,6 @@
66{-# LANGUAGE TypeFamilies #-}
77{-# LANGUAGE FlexibleContexts #-}
88{-# LANGUAGE RankNTypes #-}
9−{-# LANGUAGE RecordWildCards #-}
109
1110module Grenade.Recurrent.Core.Runner (
1211 runRecurrentExamples
@@ -101,11 +100,11 @@ updateRecInputs :: Fractional (RecurrentInputs sublayers)
101100 -> RecurrentInputs sublayers
102101 -> RecurrentInputs sublayers
103102
104−updateRecInputs l@LearningParameters {..} (() :~~+> xs) (() :~~+> ys)
105− = () :~~+> updateRecInputs l xs ys
103+updateRecInputs lp (() :~~+> xs) (() :~~+> ys)
104+ = () :~~+> updateRecInputs lp xs ys
106105
107−updateRecInputs l@LearningParameters {..} (x :~@+> xs) (y :~@+> ys)
108− = (realToFrac (1 - learningRate * learningRegulariser) * x - realToFrac learningRate * y) :~@+> updateRecInputs l xs ys
106+updateRecInputs lp (x :~@+> xs) (y :~@+> ys)
107+ = (realToFrac (1 - learningRate lp * learningRegulariser lp) * x - realToFrac (learningRate lp) * y) :~@+> updateRecInputs lp xs ys
109108
110109updateRecInputs _ RINil RINil
111110 = RINil
src/Grenade/Recurrent/Layers/BasicRecurrent.hs+4−5
@@ -1,7 +1,6 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE DataKinds #-}
33{-# LANGUAGE GADTs #-}
4−{-# LANGUAGE RecordWildCards #-}
54{-# LANGUAGE TypeOperators #-}
65{-# LANGUAGE TypeFamilies #-}
76{-# LANGUAGE MultiParamTypeClasses #-}
@@ -60,11 +59,11 @@ instance Show (BasicRecurrent i o) where
6059instance (KnownNat i, KnownNat o, KnownNat (i + o)) => UpdateLayer (BasicRecurrent i o) where
6160 type Gradient (BasicRecurrent i o) = (BasicRecurrent' i o)
6261
63− runUpdate LearningParameters {..} (BasicRecurrent oldBias oldBiasMomentum oldActivations oldMomentum) (BasicRecurrent' biasGradient activationGradient) =
64− let newBiasMomentum = konst learningMomentum * oldBiasMomentum - konst learningRate * biasGradient
62+ runUpdate lp (BasicRecurrent oldBias oldBiasMomentum oldActivations oldMomentum) (BasicRecurrent' biasGradient activationGradient) =
63+ let newBiasMomentum = konst (learningMomentum lp) * oldBiasMomentum - konst (learningRate lp) * biasGradient
6564 newBias = oldBias + newBiasMomentum
66− newMomentum = konst learningMomentum * oldMomentum - konst learningRate * activationGradient
67− regulariser = konst (learningRegulariser * learningRate) * oldActivations
65+ newMomentum = konst (learningMomentum lp) * oldMomentum - konst (learningRate lp) * activationGradient
66+ regulariser = konst (learningRegulariser lp * learningRate lp) * oldActivations
6867 newActivations = oldActivations + newMomentum - regulariser
6968 in BasicRecurrent newBias newBiasMomentum newActivations newMomentum
7069
src/Grenade/Recurrent/Layers/LSTM.hs+40−51
@@ -3,7 +3,6 @@
33{-# LANGUAGE DataKinds #-}
44{-# LANGUAGE GADTs #-}
55{-# LANGUAGE RankNTypes #-}
6−{-# LANGUAGE RecordWildCards #-}
76{-# LANGUAGE TypeOperators #-}
87{-# LANGUAGE TypeFamilies #-}
98{-# LANGUAGE MultiParamTypeClasses #-}
@@ -75,7 +74,7 @@ instance (KnownNat i, KnownNat o) => UpdateLayer (LSTM i o) where
7574
7675 -- Run the update function for each group matrix/vector of weights, momentums and gradients.
7776 -- Hmm, maybe the function should be used instead of passing in the learning parameters.
78− runUpdate LearningParameters {..} (LSTM w m) g =
77+ runUpdate lp (LSTM w m) g =
7978 let (wf, wf') = u lstmWf w m g
8079 (uf, uf') = u lstmUf w m g
8180 (bf, bf') = v lstmBf w m g
@@ -92,11 +91,11 @@ instance (KnownNat i, KnownNat o) => UpdateLayer (LSTM i o) where
9291 -- Utility function for updating with the momentum, gradients, and weights.
9392 u :: forall x ix out. (KnownNat ix, KnownNat out) => (x -> (L out ix)) -> x -> x -> x -> ((L out ix), (L out ix))
9493 u e (e -> weights) (e -> momentum) (e -> gradient) =
95− descendMatrix learningRate learningMomentum learningRegulariser weights gradient momentum
94+ descendMatrix (learningRate lp) (learningMomentum lp) (learningRegulariser lp) weights gradient momentum
9695
9796 v :: forall x ix. (KnownNat ix) => (x -> (R ix)) -> x -> x -> x -> ((R ix), (R ix))
9897 v e (e -> weights) (e -> momentum) (e -> gradient) =
99− descendVector learningRate learningMomentum learningRegulariser weights gradient momentum
98+ descendVector (learningRate lp) (learningMomentum lp) (learningRegulariser lp) weights gradient momentum
10099
101100 -- There's a lot of updates here, so to try and minimise the number of data copies
102101 -- we'll create a mutable bucket for each.
@@ -137,18 +136,18 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
137136 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)
138137 -- Forward propagation for the LSTM layer.
139138 -- The size of the cell state is also the size of the output.
140− runRecurrentForwards (LSTM (LSTMWeights {..}) _) (S1D cell) (S1D input) =
139+ runRecurrentForwards (LSTM lw _) (S1D cell) (S1D input) =
141140 let -- Forget state vector
142− f_s = lstmBf + lstmWf #> input + lstmUf #> cell
141+ f_s = lstmBf lw + lstmWf lw #> input + lstmUf lw #> cell
143142 f_t = sigmoid f_s
144143 -- Input state vector
145− i_s = lstmBi + lstmWi #> input + lstmUi #> cell
144+ i_s = lstmBi lw + lstmWi lw #> input + lstmUi lw #> cell
146145 i_t = sigmoid i_s
147146 -- Output state vector
148− o_s = lstmBo + lstmWo #> input + lstmUo #> cell
147+ o_s = lstmBo lw + lstmWo lw #> input + lstmUo lw #> cell
149148 o_t = sigmoid o_s
150149 -- Cell input state vector
151− c_s = lstmBc + lstmWc #> input
150+ c_s = lstmBc lw + lstmWc lw #> input
152151 c_x = tanh c_s
153152 -- Cell state
154153 c_t = f_t * cell + i_t * c_x
@@ -162,7 +161,7 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
162161 --
163162 -- There's a test version using the AD library without hmatrix in the test
164163 -- suite. These should match always.
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') =
164+ 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') =
166165 let -- Reverse Mode AD Derivitives
167166 c_t' = h_t' * o_t + cellGrad
168167
@@ -179,8 +178,8 @@ instance (KnownNat i, KnownNat o) => RecurrentLayer (LSTM i o) ('D1 i) ('D1 o) w
179178 c_s' = tanh' c_s * c_x'
180179
181180 -- The derivatives to pass sideways (recurrent) and downwards
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'
181+ cell' = tr (lstmUf lw) #> f_s' + tr (lstmUo lw) #> o_s' + tr (lstmUi lw) #> i_s' + c_t' * f_t
182+ input' = tr (lstmWf lw) #> f_s' + tr (lstmWo lw) #> o_s' + tr (lstmWi lw) #> i_s' + tr (lstmWc lw) #> c_s'
184183
185184 -- Calculate the gradient Matricies for the input
186185 lstmWf' = f_s' `outer` input
@@ -239,44 +238,34 @@ tanh' :: (Floating a) => a -> a
239238tanh' t = 1 - s ^ (2 :: Int) where s = tanh t
240239
241240instance (KnownNat i, KnownNat o) => Serialize (LSTM i o) where
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
241+ put (LSTM lw _) = do
242+ u (lstmWf lw)
243+ u (lstmUf lw)
244+ v (lstmBf lw)
245+ u (lstmWi lw)
246+ u (lstmUi lw)
247+ v (lstmBi lw)
248+ u (lstmWo lw)
249+ u (lstmUo lw)
250+ v (lstmBo lw)
251+ u (lstmWc lw)
252+ v (lstmBc lw)
253+ where
254+ u :: forall a b. (KnownNat a, KnownNat b) => Putter (L b a)
255+ u = putListOf put . LA.toList . LA.flatten . extract
256+ v :: forall a. (KnownNat a) => Putter (R a)
257+ v = putListOf put . LA.toList . extract
259258
260259 get = do
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
260+ w <- LSTMWeights <$> u <*> u <*> v <*> u <*> u <*> v <*> u <*> u <*> v <*> u <*> v
261+ return $ LSTM w (LSTMWeights w0 u0 v0 w0 u0 v0 w0 u0 v0 w0 v0)
262+ where
263+ u :: forall a b. (KnownNat a, KnownNat b) => Get (L b a)
264+ u = let f = fromIntegral $ natVal (Proxy :: Proxy a)
265+ in maybe (fail "Vector of incorrect size") return . create . LA.reshape f . LA.fromList =<< getListOf get
266+ v :: forall a. (KnownNat a) => Get (R a)
267+ v = maybe (fail "Vector of incorrect size") return . create . LA.fromList =<< getListOf get
268+
269+ w0 = konst 0
270+ u0 = konst 0
271+ v0 = konst 0
test/Test/Grenade/Recurrent/Layers/LSTM/Reference.hs+25−21
@@ -6,7 +6,6 @@
66{-# LANGUAGE DeriveFunctor #-}
77{-# LANGUAGE DeriveFoldable #-}
88{-# LANGUAGE DeriveTraversable #-}
9−{-# LANGUAGE RecordWildCards #-}
109{-# LANGUAGE FlexibleContexts #-}
1110{-# LANGUAGE RankNTypes #-}
1211
@@ -15,9 +14,13 @@ module Test.Grenade.Recurrent.Layers.LSTM.Reference where
1514
1615import Data.Reflection
1716import Numeric.AD.Mode.Reverse
18−import Numeric.AD.Internal.Reverse ( Tape )
17+import Numeric.AD.Internal.Reverse (Tape)
1918
19+import GHC.TypeLits (KnownNat)
20+
21+import Grenade.Recurrent.Layers.LSTM (LSTMWeights (..))
2022import qualified Grenade.Recurrent.Layers.LSTM as LSTM
23+
2124import qualified Numeric.LinearAlgebra.Static as S
2225import qualified Numeric.LinearAlgebra as H
2326
@@ -54,31 +57,32 @@ data RefLSTM a = RefLSTM
5457 , refLstmBc :: Vector a -- Bias Cell (b_c)
5558 } deriving (Functor, Foldable, Traversable, Eq, Show)
5659
57−lstmToReference :: LSTM.LSTMWeights a b -> RefLSTM Double
58−lstmToReference LSTM.LSTMWeights {..} =
59− let refLstmWf = Matrix . H.toLists . S.extract $ lstmWf -- Weight Forget (W_f)
60− refLstmUf = Matrix . H.toLists . S.extract $ lstmUf -- Cell State Forget (U_f)
61− refLstmBf = Vector . H.toList . S.extract $ lstmBf -- Bias Forget (b_f)
62− refLstmWi = Matrix . H.toLists . S.extract $ lstmWi -- Weight Input (W_i)
63− refLstmUi = Matrix . H.toLists . S.extract $ lstmUi -- Cell State Input (U_i)
64− refLstmBi = Vector . H.toList . S.extract $ lstmBi -- Bias Input (b_i)
65− refLstmWo = Matrix . H.toLists . S.extract $ lstmWo -- Weight Output (W_o)
66− refLstmUo = Matrix . H.toLists . S.extract $ lstmUo -- Cell State Output (U_o)
67− refLstmBo = Vector . H.toList . S.extract $ lstmBo -- Bias Output (b_o)
68− refLstmWc = Matrix . H.toLists . S.extract $ lstmWc -- Weight Cell (W_c)
69− refLstmBc = Vector . H.toList . S.extract $ lstmBc -- Bias Cell (b_c)
70− in RefLSTM {..}
60+lstmToReference :: (KnownNat a, KnownNat b) => LSTM.LSTMWeights a b -> RefLSTM Double
61+lstmToReference lw =
62+ RefLSTM
63+ { refLstmWf = Matrix . H.toLists . S.extract $ lstmWf lw -- Weight Forget (W_f)
64+ , refLstmUf = Matrix . H.toLists . S.extract $ lstmUf lw -- Cell State Forget (U_f)
65+ , refLstmBf = Vector . H.toList . S.extract $ lstmBf lw -- Bias Forget (b_f)
66+ , refLstmWi = Matrix . H.toLists . S.extract $ lstmWi lw -- Weight Input (W_i)
67+ , refLstmUi = Matrix . H.toLists . S.extract $ lstmUi lw -- Cell State Input (U_i)
68+ , refLstmBi = Vector . H.toList . S.extract $ lstmBi lw -- Bias Input (b_i)
69+ , refLstmWo = Matrix . H.toLists . S.extract $ lstmWo lw -- Weight Output (W_o)
70+ , refLstmUo = Matrix . H.toLists . S.extract $ lstmUo lw -- Cell State Output (U_o)
71+ , refLstmBo = Vector . H.toList . S.extract $ lstmBo lw -- Bias Output (b_o)
72+ , refLstmWc = Matrix . H.toLists . S.extract $ lstmWc lw -- Weight Cell (W_c)
73+ , refLstmBc = Vector . H.toList . S.extract $ lstmBc lw -- Bias Cell (b_c)
74+ }
7175
7276runLSTM :: Floating a => RefLSTM a -> Vector a -> Vector a -> (Vector a, Vector a)
73−runLSTM RefLSTM {..} cell input =
77+runLSTM rl cell input =
7478 let -- Forget state vector
75− f_t = sigmoid $ refLstmBf #+ refLstmWf #> input #+ refLstmUf #> cell
79+ f_t = sigmoid $ refLstmBf rl #+ refLstmWf rl #> input #+ refLstmUf rl #> cell
7680 -- Input state vector
77− i_t = sigmoid $ refLstmBi #+ refLstmWi #> input #+ refLstmUi #> cell
81+ i_t = sigmoid $ refLstmBi rl #+ refLstmWi rl #> input #+ refLstmUi rl #> cell
7882 -- Output state vector
79− o_t = sigmoid $ refLstmBo #+ refLstmWo #> input #+ refLstmUo #> cell
83+ o_t = sigmoid $ refLstmBo rl #+ refLstmWo rl #> input #+ refLstmUo rl #> cell
8084 -- Cell input state vector
81− c_x = fmap tanh $ refLstmBc #+ refLstmWc #> input
85+ c_x = fmap tanh $ refLstmBc rl #+ refLstmWc rl #> input
8286 -- Cell state
8387 c_t = f_t #* cell #+ i_t #* c_x
8488 -- Output (it's sometimes recommended to use tanh c_t)