4 | import Data.ScientificNotation
5 | import Data.Materialise
9 | runActionUntilMaxSteps : Materialise p =>
10 | ScientificDisplay p =>
11 | ScientificDisplay l =>
12 | {default 100 printEvery : Nat} ->
13 | (action : p -> IO p) ->
15 | (currentStep : Nat) -> (currentValue : p) ->
16 | (loss : p -> IO l) ->
18 | runActionUntilMaxSteps action maxSteps currStep currVal lossIO
19 | = case currStep < maxSteps of
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
26 | runActionUntilMaxSteps {printEvery=printEvery} action maxSteps (assert_smaller currStep (currStep + 1)) (materialise result) lossIO
28 | loss <- lossIO currVal
30 | putStrLn " Max steps (\{bold (show maxSteps)}) reached."
31 | putStrLn " \{dim "Final loss: "} \{yellow (showSci loss)}"
32 | putStrLn " \{dim "Final params: "} \{cyan (showSci currVal)}"
37 | stepWidth = length (show maxSteps)
40 | rule = dim (String.replicate 50 '─')