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