0 | module NN.Training.Training
3 | import Data.Container.Additive as Additive
4 | import public Data.ScientificNotation
6 | import NN.Architectures.LossFunctions
9 | import NN.Training.DataLoader
41 | optimiseStep : {p, l : AddCont} -> {e : Cont} -> InterfaceOnPositions l Num =>
42 | (f : p =%+> e >-+@ l) ->
43 | (handleEffect : Costate (IO <!> e)) ->
44 | (optimiser : Optimiser p stateTy) ->
45 | Costate (IO <!> (Const (p.Shp, stateTy)))
46 | optimiseStep f handleEffect (MkOptimiser opt _ _) =
47 | let closeFunction : p =%+> !* e
48 | closeFunction = f %+>> (id >-+@ constantOne) %+>> actionToFree
50 | closeFunctionT : UC p =%> e
51 | closeFunctionT = addContTransposeInv closeFunction
53 | in (IO <!> (opt %>> closeFunctionT)) %>> handleEffect
57 | evalFw : {0 e : Cont} ->
58 | (f : a -> Ext e b) ->
59 | (handleEffect : Costate (IO <!> e)) ->
60 | Costate (IO <!> (Const2 a b))
61 | evalFw f handleEffect = toCostate $
\ps => do
62 | let (eInp <| outGivenEffect) = f ps
63 | e <- fromCostate handleEffect eInp
64 | pure $
outGivenEffect e
69 | optimise : {p, l : AddCont} -> {e : Cont} -> InterfaceOnPositions l Num =>
70 | {default 100 printEvery : Nat} ->
71 | {default Nothing customInitParam : Maybe p.Shp} ->
72 | ScientificDisplay p.Shp => ScientificDisplay l.Shp => ScientificDisplay stateTy =>
73 | (f : p =%+> e >-+@ l) ->
74 | (handleEffect : Costate (IO <!> e)) ->
75 | (opt : Optimiser p stateTy) ->
78 | optimise f handleEffect opt numSteps = do
79 | currentValue : p.Shp <- case customInitParam of
81 | Nothing => opt.initParam
82 | currentState <- opt.initState
83 | runActionUntilMaxSteps
85 | {printEvery=printEvery}
86 | (fromCostate $
optimiseStep f handleEffect opt)
89 | (currentValue, currentState)
90 | (fromCostate $
evalFw (f.fwd . opt.fwd) handleEffect)
97 | buildSupervisedLearningSystem : (f : ParaAddDLens x y) ->
98 | (loss : Loss y {l=l}) ->
99 | (GetParam f) =%+> SupervisedData x.Shp y.Shp >-+@ l
100 | buildSupervisedLearningSystem (MkPara p f) loss =
101 | let rebracket : ((x >*< y) >*< p) =%+> ((x >*< p) >*< y)
102 | rebracket = assocL %+>> (id >*< swap) %+>> assocR
103 | in pushIntoContinuation {d=x>*<y} (rebracket %+>> (f >*< id) %+>> loss)
106 | namespace WithEffect
114 | totalLoss : Show l.Shp => Num l.Shp =>
115 | (f : ParaAddDLens x (e >-+@ y)) ->
116 | (loss : Loss y {l=l}) ->
117 | (p : (GetParam f).Shp) ->
118 | (handleEffect : Costate (IO <!> e)) ->
119 | Costate (IO <!> (Const2 (Vect n (x.Shp, y.Shp)) l.Shp))
120 | totalLoss (MkPara pCont f) loss p handleEffect = toCostate $
\testData => do
121 | let evalFWithLoss : (x.Shp, y.Shp) -> IO l.Shp
122 | evalFWithLoss (x, yTrue) = do
123 | yPred <- fromCostate (evalFw f.fwd handleEffect) (x, p)
124 | pure $
loss.fwd (yPred, yTrue)
126 | losses <- traverse evalFWithLoss testData
127 | pure $
Prelude.sum losses
130 | averageLoss : {n : Nat} ->
131 | Show l.Shp => Num l.Shp => Fractional l.Shp => Cast Nat l.Shp =>
132 | (f : ParaAddDLens x (e >-+@ y)) ->
133 | (loss : Loss y {l=l}) ->
134 | (p : (GetParam f).Shp) ->
135 | (handleEffect : Costate (IO <!> e)) ->
136 | Costate (IO <!> (Const2 (Vect n (x.Shp, y.Shp)) l.Shp))
137 | averageLoss f loss p handleEffect = toCostate $
\testData => do
138 | lossSum <- fromCostate (totalLoss f loss p handleEffect) testData
139 | pure (lossSum / cast n)
143 | evalWithLoss : ScientificDisplay x.Shp => ScientificDisplay y.Shp => ScientificDisplay l.Shp =>
144 | (f : ParaAddDLens x (e >-+@ y)) ->
145 | (loss : Loss y {l=l}) ->
146 | (p : (GetParam f).Shp) ->
147 | (handleEffect : Costate (IO <!> e)) ->
148 | Costate (IO <!> (Const2 (Vect n (x.Shp, y.Shp)) Unit))
149 | evalWithLoss (MkPara pCont f) loss p handleEffect = toCostate $
\testData => do
150 | let evalFWithLoss : (x.Shp, y.Shp) -> IO ()
151 | evalFWithLoss (x, yTrue) = do
152 | yPred <- fromCostate (evalFw f.fwd handleEffect) (x, p)
153 | let lossVal = loss.fwd (yPred, yTrue)
154 | putStrLn "Input: \{showSci x}, Predicted: \{showSci yPred}, Loss: \{showSci lossVal}"
155 | _ <- traverse evalFWithLoss testData
160 | eval : ScientificDisplay x.Shp => ScientificDisplay y.Shp =>
161 | (f : ParaAddDLens x (e >-+@ y)) ->
162 | (p : (GetParam f).Shp) ->
163 | (handleEffect : Costate (IO <!> e)) ->
164 | Costate (IO <!> (Const2 (Vect n x.Shp) Unit))
165 | eval (MkPara _ f) p handleEffect = toCostate $
\testData => do
166 | let evalF : x.Shp -> IO ()
168 | yPred <- fromCostate (evalFw f.fwd handleEffect) (x, p)
169 | putStrLn "Input: \{showSci x}, Predicted: \{showSci yPred}"
170 | _ <- traverse evalF testData
173 | namespace WithoutEffect
175 | trivialEffect : {y : AddCont} ->
176 | ParaAddDLens x y -> ParaAddDLens x (Scalar >+@ y)
177 | trivialEffect (MkPara p f) = MkPara p
178 | (f %+>> leftUnitInv)
181 | handleTrivial : Costate (IO <!> Scalar)
182 | handleTrivial = toCostate $
\() => pure ()
187 | eval : {y : AddCont} -> ScientificDisplay x.Shp => ScientificDisplay y.Shp =>
188 | (f : ParaAddDLens x y) ->
189 | (p : (GetParam f).Shp) ->
190 | Costate (IO <!> (Const2 (Vect n x.Shp) Unit))
191 | eval (MkPara pCont f) p
192 | = eval {e=Scalar} (MkPara pCont (f %+>> unitor)) p handleTrivial
195 | averageLoss : {y : AddCont} -> {n : Nat} ->
196 | Show l.Shp => Num l.Shp => Fractional l.Shp => Cast Nat l.Shp =>
197 | (f : ParaAddDLens x y) ->
198 | (loss : Loss y {l=l}) ->
199 | (p : (GetParam f).Shp) ->
200 | Costate (IO <!> (Const2 (Vect n (x.Shp, y.Shp)) l.Shp))
201 | averageLoss (MkPara pCont f) loss p = averageLoss {e=Scalar}
202 | (MkPara pCont (f %+>> unitor))