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 =>
11 |   ScientificDisplay l =>
12 |   {default 100 printEvery : Nat} ->
13 |   (action : p -> IO p) ->
14 |   (maxSteps : Nat) ->
15 |   (currentStep : Nat) -> (currentValue : p) ->
16 |   (loss : p -> IO l) ->
17 |   IO p
18 | runActionUntilMaxSteps action maxSteps currStep currVal lossIO
19 |   = case currStep < maxSteps of
20 |     True => do
21 |       runIf (currStep `mod` printEvery == 0 || currStep < 10) $ do
22 |         loss <- lossIO currVal
23 |         putStrLn "  \{dim "step"} \{bold (padLeft stepWidth ' ' (show currStep))} \{dim "│ loss"} \{yellow (showSci loss)}"
24 |       result <- action currVal
25 |       -- we materialise the result between every training step
26 |       runActionUntilMaxSteps {printEvery=printEvery} action maxSteps (assert_smaller currStep (currStep + 1)) (materialise result) lossIO
27 |     False => do
28 |       loss <- lossIO currVal
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 currVal)}"
33 |       putStrLn rule
34 |       pure currVal
35 |   where
36 |     stepWidth : Nat
37 |     stepWidth = length (show maxSteps)
38 |
39 |     rule : String
40 |     rule = dim (String.replicate 50 '─')
41 |