@@ -53,14 +53,14 @@ class (RecurrentUpdateLayer x, SingI (RecurrentShape x)) => RecurrentLayer x (i
53 runRecurrentBackwards :: x -> S (RecurrentShape x) -> S i -> S (RecurrentShape x) -> S o -> (Gradient x, S (RecurrentShape x), S i)
53 runRecurrentBackwards :: x -> S (RecurrentShape x) -> S i -> S (RecurrentShape x) -> S o -> (Gradient x, S (RecurrentShape x), S i)
55data RecurrentNetwork :: [*] -> [Shape] -> * where
55data 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]
57 (:~~>) :: (SingI i, Layer x i h) => !x -> !(RecurrentNetwork xs (h ': hs)) -> RecurrentNetwork (FeedForward x ': xs) (i ': h ': hs)
57 (:~~>) :: (SingI i, Layer x i h) => !x -> !(RecurrentNetwork xs (h ': hs)) -> RecurrentNetwork (FeedForward x ': xs) (i ': h ': hs)
58 (:~@>) :: (SingI i, RecurrentLayer x i h) => !x -> !(RecurrentNetwork xs (h ': hs)) -> RecurrentNetwork (Recurrent x ': xs) (i ': h ': hs)
58 (:~@>) :: (SingI i, RecurrentLayer x i h) => !x -> !(RecurrentNetwork xs (h ': hs)) -> RecurrentNetwork (Recurrent x ': xs) (i ': h ': hs)
59infixr 5 :~~>
59infixr 5 :~~>
60infixr 5 :~@>
60infixr 5 :~@>
62instance Show (RecurrentNetwork l h) where
62instance Show (RecurrentNetwork l h) where
63− show (OR a) = "OR " ++ show a
63+ show RNil = "RNil"
64 show (i :~~> o) = show i ++ "\n:~~>\n" ++ show o
64 show (i :~~> o) = show i ++ "\n:~~>\n" ++ show o
65 show (i :~@> o) = show i ++ "\n:~@>\n" ++ show o
65 show (i :~@> o) = show i ++ "\n:~@>\n" ++ show o
@@ -68,8 +68,7 @@ instance Show (RecurrentNetwork l h) where
68-- | Recurrent inputs (sideways shapes on an imaginary unrolled graph)
68-- | Recurrent inputs (sideways shapes on an imaginary unrolled graph)
69-- Parameterised on the layers of a Network.
69-- Parameterised on the layers of a Network.
70data RecurrentInputs :: [*] -> * where
70data RecurrentInputs :: [*] -> * where
72− => () -> RecurrentInputs '[FeedForward x]
71+ RINil :: RecurrentInputs '[]
73 (:~~+>) :: UpdateLayer x
72 (:~~+>) :: UpdateLayer x
74 => () -> !(RecurrentInputs xs) -> RecurrentInputs (FeedForward x ': xs)
73 => () -> !(RecurrentInputs xs) -> RecurrentInputs (FeedForward x ': xs)
75 (:~@+>) :: (SingI (RecurrentShape x), RecurrentUpdateLayer x)
74 (:~@+>) :: (SingI (RecurrentShape x), RecurrentUpdateLayer x)
@@ -83,18 +82,17 @@ class CreatableRecurrent (xs :: [*]) (ss :: [Shape]) where
83 -- | Create a network of the types requested
82 -- | Create a network of the types requested
84 randomRecurrent :: MonadRandom m => m (RecurrentNetwork xs ss, RecurrentInputs xs)
83 randomRecurrent :: MonadRandom m => m (RecurrentNetwork xs ss, RecurrentInputs xs)
86−instance (SingI i, SingI o, Layer x i o) => CreatableRecurrent (FeedForward x ': '[]) (i ': o ': '[]) where
85+instance SingI i => CreatableRecurrent '[] '[i] where
87− randomRecurrent = do
86+ randomRecurrent =
88− thisLayer <- createRandom
87+ return (RNil, RINil)
89− return (OR thisLayer, ORS ())
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
92 randomRecurrent = do
90 randomRecurrent = do
93 thisLayer <- createRandom
91 thisLayer <- createRandom
94 (rest, resti) <- randomRecurrent
92 (rest, resti) <- randomRecurrent
95 return (thisLayer :~~> rest, () :~~+> resti)
93 return (thisLayer :~~> rest, () :~~+> resti)
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
98 randomRecurrent = do
96 randomRecurrent = do
99 thisLayer <- createRandom
97 thisLayer <- createRandom
100 thisShape <- randomOfShape
98 thisShape <- randomOfShape
@@ -102,28 +100,27 @@ instance (SingI i, RecurrentLayer x i o, CreatableRecurrent xs (o ': r ': rs)) =
102 return (thisLayer :~@> rest, thisShape :~@+> resti)
100 return (thisLayer :~@> rest, thisShape :~@+> resti)
104-- | Add very simple serialisation to the recurrent network
102-- | 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
103+instance SingI i => Serialize (RecurrentNetwork '[] '[i]) where
106− put (OR x) = put x
104+ put RNil = pure ()
107− put _ = error "impossible"
105+ get = pure RNil
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
111 put (x :~~> r) = put x >> put r
108 put (x :~~> r) = put x >> put r
112 get = (:~~>) <$> get <*> get
109 get = (:~~>) <$> get <*> get
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
115 put (x :~@> r) = put x >> put r
112 put (x :~@> r) = put x >> put r
116 get = (:~@>) <$> get <*> get
113 get = (:~@>) <$> get <*> get
118−instance (UpdateLayer x) => (Serialize (RecurrentInputs '[FeedForward x])) where
115+instance (Serialize (RecurrentInputs '[])) where
119 put _ = return ()
116 put _ = return ()
120− get = return (ORS ())
117+ get = return RINil
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
123 put ( () :~~+> rest) = put rest
120 put ( () :~~+> rest) = put rest
124 get = ( () :~~+> ) <$> get
121 get = ( () :~~+> ) <$> get
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
127 put ( i :~@+> rest ) = do
124 put ( i :~@+> rest ) = do
128 _ <- (case i of
125 _ <- (case i of
129 (S1D x) -> putListOf put . LA.toList . LAS.extract $ x
126 (S1D x) -> putListOf put . LA.toList . LAS.extract $ x