@@ -66,11 +66,13 @@ trainRecurrent rate network recinputs examples =
6666 -- This is a recurrent layer, so we need to do a scan, first input to last, providing
6767 -- the recurrent shape output to the next layer.
6868 go !xs (layer :~@> n) (g :~@+> nIn)
69− = let ys = scanlFrom layer g xs
69+ = let tys = scanlFrom layer g xs
70+ tapes = fst <$> tys
71+ ys = snd <$> tys
7072
71− (newFN, ig, grads) = go (snd <$> ys) n nIn
73+ (newFN, ig, grads) = go ys n nIn
7274
73− backExamples = zip3 (fst <$> reverse ys) (reverse xs) grads
75+ backExamples = zip (reverse tapes) grads
7476
7577 (rg, back) = myscanbackward layer backExamples
7678 -- the new trained layer.
@@ -105,22 +107,22 @@ scanlFrom :: forall x i o. RecurrentLayer x i o
105107 => x -- ^ the layer
106108 -> S (RecurrentShape x) -- ^ place to start
107109 -> [S i] -- ^ list of inputs to scan through
108− -> [(S (RecurrentShape x), S o)] -- ^ list of scan inputs and outputs
110+ -> [(RecTape x i o, S o)] -- ^ list of scan inputs and outputs
109111scanlFrom !layer !recShape (x:xs) =
110− let (lerec, lepush) = runRecurrentForwards layer recShape x
111− in (recShape, lepush) : scanlFrom layer lerec xs
112+ let (tape, lerec, lepush) = runRecurrentForwards layer recShape x
113+ in (tape, lepush) : scanlFrom layer lerec xs
112114scanlFrom _ _ [] = []
113115
114116myscanbackward :: forall x i o. RecurrentLayer x i o
115117 => x -- ^ the layer
116− -> [(S (RecurrentShape x), S i, S o)] -- ^ the list of inputs and output to scan over
118+ -> [(RecTape x i o, S o)] -- ^ the list of inputs and output to scan over
117119 -> (S (RecurrentShape x), [(Gradient x, S i)]) -- ^ list of gradients to fold and inputs to backprop
118120myscanbackward layer =
119121 goX 0
120122 where
121− goX :: S (RecurrentShape x) -> [(S (RecurrentShape x), S i, S o)] -> (S (RecurrentShape x), [(Gradient x, S i)])
122− goX !lastback ((recShape, lastin, backgrad):xs) =
123− let (layergrad, recgrad, ingrad) = runRecurrentBackwards layer recShape lastin lastback backgrad
123+ goX :: S (RecurrentShape x) -> [(RecTape x i o, S o)] -> (S (RecurrentShape x), [(Gradient x, S i)])
124+ goX !lastback ((recTape, backgrad):xs) =
125+ let (layergrad, recgrad, ingrad) = runRecurrentBackwards layer recTape lastback backgrad
124126 (pushedback, ll) = goX recgrad xs
125127 in (pushedback, (layergrad, ingrad) : ll)
126128 goX !lastback [] = (lastback, [])
@@ -134,8 +136,8 @@ runRecurrent (layer :~~> n) (() :~~+> nr) !x
134136 (nr', o) = runRecurrent n nr ys
135137 in (() :~~+> nr', o)
136138runRecurrent (layer :~@> n) (recin :~@+> nr) !x
137− = let (recin', y) = runRecurrentForwards layer recin x
138− (nr', o) = runRecurrent n nr y
139+ = let (_, recin', y) = runRecurrentForwards layer recin x
140+ (nr', o) = runRecurrent n nr y
139141 in (recin' :~@+> nr', o)
140142runRecurrent RNil RINil !x
141143 = (RINil, x)