module Numeric.Neural.Pipes
( TS(..)
, descentP
, simpleBatchP
, reportTSP
, consumeTSP
, module Pipes
) where
import Data.MyPrelude
import Numeric.Neural.Model
import Data.Utils.Random (takeR)
import Pipes
import qualified Pipes.Prelude as P
data TS f g a b c = TS
{ tsModel :: Model f g a b c
, tsGeneration :: Int
, tsEta :: Double
, tsBatchError :: Double
}
descentP :: (Foldable h, Monad m) =>
Model f g a b c
-> Int
-> (Int -> Double)
-> Pipe (h a) (TS f g a b c) m r
descentP m i f = loop m i where
loop m' i' = do
xs <- await
let !eta = f i'
let (e, m'') = descent m' eta xs
m'' `deepseq` yield TS
{ tsModel = m''
, tsGeneration = i'
, tsEta = eta
, tsBatchError = e
}
loop m'' (succ i')
simpleBatchP :: MonadRandom m
=> [a]
-> Int
-> Producer [a] m r
simpleBatchP xs n = forever $ lift (takeR n xs) >>= yield
reportTSP :: Monad m
=> Int
-> (TS f g a b c -> m ())
-> Pipe (TS f g a b c) (TS f g a b c) m r
reportTSP n act = P.mapM $ \ts -> do
when (tsGeneration ts `mod` n == 0) (act ts)
return ts
consumeTSP :: Monad m
=> (TS f g a b c -> m (Maybe x))
-> Consumer (TS f g a b c) m x
consumeTSP check = loop where
loop = do
ts <- await
mx <- lift (check ts)
case mx of
Just x -> return x
Nothing -> loop