4 | import Data.ScientificNotation
8 | runActionUntilMaxSteps : ScientificDisplay p =>
9 | ScientificDisplay l =>
10 | {default 100 printEvery : Nat} ->
11 | (action : p -> IO p) ->
13 | (currentStep : Nat) -> (currentValue : p) ->
14 | (loss : p -> IO l) ->
16 | runActionUntilMaxSteps action maxSteps currStep currVal lossIO
17 | = case currStep < maxSteps of
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
25 | loss <- lossIO currVal
27 | putStrLn " \Max steps (\{bold (show maxSteps)}) reached."
28 | putStrLn " \{dim "Final loss: "} \{yellow (showSci loss)}"
29 | putStrLn " \{dim "Final params: "} \{cyan (showSci currVal)}"
34 | stepWidth = length (show maxSteps)
37 | rule = dim (String.replicate 50 '─')