@@ -53,14 +53,14 @@ class (RecurrentUpdateLayer x, SingI (RecurrentShape x)) => RecurrentLayer x (i
5353 runRecurrentBackwards :: x -> S (RecurrentShape x) -> S i -> S (RecurrentShape x) -> S o -> (Gradient x, S (RecurrentShape x), S i)
5454
5555data RecurrentNetwork :: [*] -> [Shape] -> * where
56− OR :: (SingI i, SingI o, Layer x i o) => !x -> RecurrentNetwork '[FeedForward x] '[i, o]
56+ RNil :: SingI i => RecurrentNetwork '[] '[i]
5757 (:~~>) :: (SingI i, Layer x i h) => !x -> !(RecurrentNetwork xs (h ': hs)) -> RecurrentNetwork (FeedForward x ': xs) (i ': h ': hs)
5858 (:~@>) :: (SingI i, RecurrentLayer x i h) => !x -> !(RecurrentNetwork xs (h ': hs)) -> RecurrentNetwork (Recurrent x ': xs) (i ': h ': hs)
5959infixr 5 :~~>
6060infixr 5 :~@>
6161
6262instance Show (RecurrentNetwork l h) where
63− show (OR a) = "OR " ++ show a
63+ show RNil = "RNil"
6464 show (i :~~> o) = show i ++ "\n:~~>\n" ++ show o
6565 show (i :~@> o) = show i ++ "\n:~@>\n" ++ show o
6666
@@ -68,8 +68,7 @@ instance Show (RecurrentNetwork l h) where
6868-- | Recurrent inputs (sideways shapes on an imaginary unrolled graph)
6969-- Parameterised on the layers of a Network.
7070data RecurrentInputs :: [*] -> * where
71− ORS :: UpdateLayer x
72− => () -> RecurrentInputs '[FeedForward x]
71+ RINil :: RecurrentInputs '[]
7372 (:~~+>) :: UpdateLayer x
7473 => () -> !(RecurrentInputs xs) -> RecurrentInputs (FeedForward x ': xs)
7574 (:~@+>) :: (SingI (RecurrentShape x), RecurrentUpdateLayer x)
@@ -83,18 +82,17 @@ class CreatableRecurrent (xs :: [*]) (ss :: [Shape]) where
8382 -- | Create a network of the types requested
8483 randomRecurrent :: MonadRandom m => m (RecurrentNetwork xs ss, RecurrentInputs xs)
8584
86−instance (SingI i, SingI o, Layer x i o) => CreatableRecurrent (FeedForward x ': '[]) (i ': o ': '[]) where
87− randomRecurrent = do
88− thisLayer <- createRandom
89− return (OR thisLayer, ORS ())
85+instance SingI i => CreatableRecurrent '[] '[i] where
86+ randomRecurrent =
87+ return (RNil, RINil)
9088
91−instance (SingI i, Layer x i o, CreatableRecurrent xs (o ': r ': rs)) => CreatableRecurrent (FeedForward x ': xs) (i ': o ': r ': rs) where
89+instance (SingI i, Layer x i o, CreatableRecurrent xs (o ': rs)) => CreatableRecurrent (FeedForward x ': xs) (i ': o ': rs) where
9290 randomRecurrent = do
9391 thisLayer <- createRandom
9492 (rest, resti) <- randomRecurrent
9593 return (thisLayer :~~> rest, () :~~+> resti)
9694
97−instance (SingI i, RecurrentLayer x i o, CreatableRecurrent xs (o ': r ': rs)) => CreatableRecurrent (Recurrent x ': xs) (i ': o ': r ': rs) where
95+instance (SingI i, RecurrentLayer x i o, CreatableRecurrent xs (o ': rs)) => CreatableRecurrent (Recurrent x ': xs) (i ': o ': rs) where
9896 randomRecurrent = do
9997 thisLayer <- createRandom
10098 thisShape <- randomOfShape
@@ -102,28 +100,27 @@ instance (SingI i, RecurrentLayer x i o, CreatableRecurrent xs (o ': r ': rs)) =
102100 return (thisLayer :~@> rest, thisShape :~@+> resti)
103101
104102-- | Add very simple serialisation to the recurrent network
105−instance (SingI i, SingI o, Layer x i o, Serialize x) => Serialize (RecurrentNetwork '[FeedForward x] '[i, o]) where
106− put (OR x) = put x
107− put _ = error "impossible"
108− get = OR <$> get
103+instance SingI i => Serialize (RecurrentNetwork '[] '[i]) where
104+ put RNil = pure ()
105+ get = pure RNil
109106
110−instance (SingI i, Layer x i o, Serialize x, Serialize (RecurrentNetwork xs (o ': r ': rs))) => Serialize (RecurrentNetwork (FeedForward x ': xs) (i ': o ': r ': rs)) where
107+instance (SingI i, Layer x i o, Serialize x, Serialize (RecurrentNetwork xs (o ': rs))) => Serialize (RecurrentNetwork (FeedForward x ': xs) (i ': o ': rs)) where
111108 put (x :~~> r) = put x >> put r
112109 get = (:~~>) <$> get <*> get
113110
114−instance (SingI i, RecurrentLayer x i o, Serialize x, Serialize (RecurrentNetwork xs (o ': r ': rs))) => Serialize (RecurrentNetwork (Recurrent x ': xs) (i ': o ': r ': rs)) where
111+instance (SingI i, RecurrentLayer x i o, Serialize x, Serialize (RecurrentNetwork xs (o ': rs))) => Serialize (RecurrentNetwork (Recurrent x ': xs) (i ': o ': rs)) where
115112 put (x :~@> r) = put x >> put r
116113 get = (:~@>) <$> get <*> get
117114
118−instance (UpdateLayer x) => (Serialize (RecurrentInputs '[FeedForward x])) where
115+instance (Serialize (RecurrentInputs '[])) where
119116 put _ = return ()
120− get = return (ORS ())
117+ get = return RINil
121118
122−instance (UpdateLayer x, Serialize (RecurrentInputs (y ': ys))) => (Serialize (RecurrentInputs (FeedForward x ': y ': ys))) where
119+instance (UpdateLayer x, Serialize (RecurrentInputs ys)) => (Serialize (RecurrentInputs (FeedForward x ': ys))) where
123120 put ( () :~~+> rest) = put rest
124121 get = ( () :~~+> ) <$> get
125122
126−instance (SingI (RecurrentShape x), RecurrentUpdateLayer x, Serialize (RecurrentInputs (y ': ys))) => (Serialize (RecurrentInputs (Recurrent x ': y ': ys))) where
123+instance (SingI (RecurrentShape x), RecurrentUpdateLayer x, Serialize (RecurrentInputs ys)) => (Serialize (RecurrentInputs (Recurrent x ': ys))) where
127124 put ( i :~@+> rest ) = do
128125 _ <- (case i of
129126 (S1D x) -> putListOf put . LA.toList . LAS.extract $ x