-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathConcatRecurrent.hs
169 lines (144 loc) · 7.62 KB
/
ConcatRecurrent.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-|
Module : Grenade.Layers.Concat
Description : Concatenation layer
Copyright : (c) Huw Campbell, 2016-2017
License : BSD2
Stability : experimental
This module provides the concatenation layer, which runs two chilld layers in parallel and combines their outputs.
-}
module Grenade.Recurrent.Layers.ConcatRecurrent (
ConcatRecurrent (..)
) where
import Data.Serialize
import Data.Singletons
import GHC.TypeLits
import Grenade.Core
import Grenade.Recurrent.Core
import Numeric.LinearAlgebra.Static ( (#), split, R )
-- | A Recurrent Concatentating Layer.
--
-- This layer shares it's input state between two sublayers, and concatenates their output.
--
-- With Networks able to be Layers, this allows for very expressive composition of complex Networks.
--
-- The ConcatRecurrent layer currently supports D1 shape concatenation only, but allows a mix of
-- recurrent and feedforward layers.
data ConcatRecurrent :: Shape -> * -> Shape -> * -> * where
ConcatRecLeft :: x -> y -> ConcatRecurrent m (Recurrent x) n (FeedForward y)
ConcatRecRight :: x -> y -> ConcatRecurrent m (FeedForward x) n (Recurrent y)
ConcatRecBoth :: x -> y -> ConcatRecurrent m (Recurrent x) n (Recurrent y)
instance (Show x, Show y) => Show (ConcatRecurrent m (p x) n (q y)) where
show (ConcatRecLeft x y) = "ConcatRecLeft\n" ++ show x ++ "\n" ++ show y
show (ConcatRecRight x y) = "ConcatRecRight\n" ++ show x ++ "\n" ++ show y
show (ConcatRecBoth x y) = "ConcatRecBoth\n" ++ show x ++ "\n" ++ show y
instance (RecurrentUpdateLayer x, UpdateLayer y) => UpdateLayer (ConcatRecurrent m (Recurrent x) n (FeedForward y)) where
type Gradient (ConcatRecurrent m (Recurrent x) n (FeedForward y)) = (Gradient x, Gradient y)
runUpdate lr (ConcatRecLeft x y) (x', y')
= ConcatRecLeft (runUpdate lr x x') (runUpdate lr y y')
createRandom
= ConcatRecLeft <$> createRandom <*> createRandom
instance (UpdateLayer x, RecurrentUpdateLayer y) => UpdateLayer (ConcatRecurrent m (FeedForward x) n (Recurrent y)) where
type Gradient (ConcatRecurrent m (FeedForward x) n (Recurrent y)) = (Gradient x, Gradient y)
runUpdate lr (ConcatRecRight x y) (x', y')
= ConcatRecRight (runUpdate lr x x') (runUpdate lr y y')
createRandom
= ConcatRecRight <$> createRandom <*> createRandom
instance (RecurrentUpdateLayer x, RecurrentUpdateLayer y) => UpdateLayer (ConcatRecurrent m (Recurrent x) n (Recurrent y)) where
type Gradient (ConcatRecurrent m (Recurrent x) n (Recurrent y)) = (Gradient x, Gradient y)
runUpdate lr (ConcatRecBoth x y) (x', y')
= ConcatRecBoth (runUpdate lr x x') (runUpdate lr y y')
createRandom
= ConcatRecBoth <$> createRandom <*> createRandom
instance (RecurrentUpdateLayer x, UpdateLayer y) => RecurrentUpdateLayer (ConcatRecurrent m (Recurrent x) n (FeedForward y)) where
type RecurrentShape (ConcatRecurrent m (Recurrent x) n (FeedForward y)) = RecurrentShape x
instance (UpdateLayer x, RecurrentUpdateLayer y) => RecurrentUpdateLayer (ConcatRecurrent m (FeedForward x) n (Recurrent y)) where
type RecurrentShape (ConcatRecurrent m (FeedForward x) n (Recurrent y)) = RecurrentShape y
instance (RecurrentUpdateLayer x, RecurrentUpdateLayer y) => RecurrentUpdateLayer (ConcatRecurrent m (Recurrent x) n (Recurrent y)) where
type RecurrentShape (ConcatRecurrent m (Recurrent x) n (Recurrent y)) = RecurrentInputs '[ Recurrent x, Recurrent y ]
instance ( SingI i
, Layer x i ('D1 m)
, RecurrentLayer y i ('D1 n)
, KnownNat o
, KnownNat m
, KnownNat n
, o ~ (m + n)
, n ~ (o - m)
, (m <=? o) ~ 'True
) => RecurrentLayer (ConcatRecurrent ('D1 m) (FeedForward x) ('D1 n) (Recurrent y)) i ('D1 o) where
type RecTape (ConcatRecurrent ('D1 m) (FeedForward x) ('D1 n) (Recurrent y)) i ('D1 o) = (Tape x i ('D1 m), RecTape y i ('D1 n))
runRecurrentForwards (ConcatRecRight x y) s input =
let (xT, xOut :: S ('D1 m)) = runForwards x input
(yT, side, yOut :: S ('D1 n)) = runRecurrentForwards y s input
in case (xOut, yOut) of
(S1D xOut', S1D yOut') ->
((xT, yT), side, S1D (xOut' # yOut'))
runRecurrentBackwards (ConcatRecRight x y) (xTape, yTape) s (S1D o) =
let (ox :: R m , oy :: R n) = split o
(x', xB :: S i) = runBackwards x xTape (S1D ox)
(y', side, yB :: S i) = runRecurrentBackwards y yTape s (S1D oy)
in ((x', y'), side, xB + yB)
instance ( SingI i
, RecurrentLayer x i ('D1 m)
, Layer y i ('D1 n)
, KnownNat o
, KnownNat m
, KnownNat n
, o ~ (m + n)
, n ~ (o - m)
, (m <=? o) ~ 'True
) => RecurrentLayer (ConcatRecurrent ('D1 m) (Recurrent x) ('D1 n) (FeedForward y)) i ('D1 o) where
type RecTape (ConcatRecurrent ('D1 m) (Recurrent x) ('D1 n) (FeedForward y)) i ('D1 o) = (RecTape x i ('D1 m), Tape y i ('D1 n))
runRecurrentForwards (ConcatRecLeft x y) s input =
let (xT, side, xOut :: S ('D1 m)) = runRecurrentForwards x s input
(yT, yOut :: S ('D1 n)) = runForwards y input
in case (xOut, yOut) of
(S1D xOut', S1D yOut') ->
((xT, yT), side, S1D (xOut' # yOut'))
runRecurrentBackwards (ConcatRecLeft x y) (xTape, yTape) s (S1D o) =
let (ox :: R m , oy :: R n) = split o
(x', side, xB :: S i) = runRecurrentBackwards x xTape s (S1D ox)
(y', yB :: S i) = runBackwards y yTape (S1D oy)
in ((x', y'), side, xB + yB)
instance ( SingI i
, RecurrentLayer x i ('D1 m)
, RecurrentLayer y i ('D1 n)
, Fractional (RecurrentShape x)
, Fractional (RecurrentShape y)
, KnownNat o
, KnownNat m
, KnownNat n
, o ~ (m + n)
, n ~ (o - m)
, (m <=? o) ~ 'True
) => RecurrentLayer (ConcatRecurrent ('D1 m) (Recurrent x) ('D1 n) (Recurrent y)) i ('D1 o) where
type RecTape (ConcatRecurrent ('D1 m) (Recurrent x) ('D1 n) (Recurrent y)) i ('D1 o) = (RecTape x i ('D1 m), RecTape y i ('D1 n))
runRecurrentForwards (ConcatRecBoth x y) (sx :~@+> (sy :~@+> RINil)) input =
let (xT, s'x, xOut :: S ('D1 m)) = runRecurrentForwards x sx input
(yT, s'y, yOut :: S ('D1 n)) = runRecurrentForwards y sy input
in case (xOut, yOut) of
(S1D xOut', S1D yOut') ->
((xT, yT), (s'x :~@+> (s'y :~@+> RINil)), S1D (xOut' # yOut'))
runRecurrentBackwards (ConcatRecBoth x y) (xTape, yTape) (sx :~@+> (sy :~@+> RINil)) (S1D o) =
let (ox :: R m , oy :: R n) = split o
(x', s'x, xB :: S i) = runRecurrentBackwards x xTape sx (S1D ox)
(y', s'y, yB :: S i) = runRecurrentBackwards y yTape sy (S1D oy)
in ((x', y'), (s'x :~@+> (s'y :~@+> RINil)), xB + yB)
instance (Serialize a, Serialize b) => Serialize (ConcatRecurrent sa (FeedForward a) sb (Recurrent b)) where
put (ConcatRecRight a b) = put a *> put b
get = ConcatRecRight <$> get <*> get
instance (Serialize a, Serialize b) => Serialize (ConcatRecurrent sa (Recurrent a) sb (Recurrent b)) where
put (ConcatRecBoth a b) = put a *> put b
get = ConcatRecBoth <$> get <*> get
instance (Serialize a, Serialize b) => Serialize (ConcatRecurrent sa (Recurrent a) sb (FeedForward b)) where
put (ConcatRecLeft a b) = put a *> put b
get = ConcatRecLeft <$> get <*> get