0 | module NN.Utils
 1 |
 2 | import Data.Nat
 3 | import Data.String
 4 | import Data.ScientificNotation
 5 | import Data.Materialise
 6 | import Misc
 7 |
 8 | public export
 9 | runActionUntilMaxSteps : Materialise p =>
10 |   ScientificDisplay p => ScientificDisplay l =>
11 |   {default 100 printEvery : Nat} ->
12 |   (action : p -> IO p) ->
13 |   (maxSteps : Nat) ->
14 |   (currentStep : Nat) -> (currentValue : p) ->
15 |   (loss : p -> IO l) ->
16 |   IO p
17 | runActionUntilMaxSteps action maxSteps currStep currVal lossIO
18 |   = go (minus maxSteps currStep) currStep currVal
19 |   where
20 |     stepWidth : Nat
21 |     stepWidth = length (show maxSteps)
22 |
23 |     rule : String
24 |     rule = dim (String.replicate 50 '─')
25 |
26 |     go : (remaining : Nat) -> (step : Nat) -> p -> IO p
27 |     go 0 step val = do
28 |       loss <- lossIO val
29 |       putStrLn rule
30 |       putStrLn "  Max steps (\{bold (show maxSteps)}) reached."
31 |       putStrLn "  \{dim "Final loss:     "} \{yellow (showSci loss)}"
32 |       putStrLn "  \{dim "Final params:   "} \{cyan (showSci val)}"
33 |       putStrLn rule
34 |       pure val
35 |     go (S k) step val = do
36 |       runIf (step `mod` printEvery == 0 || step < 10) $ do
37 |         loss <- lossIO val
38 |         putStrLn "  \{dim "step"} \{bold (padLeft stepWidth ' ' (show step))} \{dim "│ loss"} \{yellow (showSci loss)}"
39 |       result <- action val
40 |       -- we materialise the result between every training step
41 |       go k (S step) (materialise result)
42 |