[Haskell-beginners] Avoiding stack space overflow

Hans Georg Schaathun georg+haskell at schaathun.net
Mon Jan 26 12:19:26 UTC 2015


Hi,

can someone give some hints on how to get around a stack space
overflow?

My problem is with the training function for a neural network:

trainNetwork :: Double -> Samples -> Int -> Network -> Network
trainNetwork _ _ 0 n = n
trainNetwork eta samples c n = trainNetwork eta samples (c-1) $!
                             epoch eta n samples
epoch :: Double -> Network -> Samples -> Network

So trainNetwork runs epoch c times, each time taking a Network
in and modifying the Network as output.  Clearly, space complexity
can be made constant in c, but I get stack overflow if and only
if c is too large.

As you can see, I have tried to make the epoch evaluation strict
($!).  I have also tried bang patterns on the input parameter n,
and I have tried rewriting with foldr/foldl/foldl', and I have
tried switchin the inner and outer calls (epoch vs. trainNetwork),
all to no avail.

I reckon this loop like pattern should be fairly common ... 
does it have a common solution too?

TIA
-- 
:-- Hans Georg


More information about the Beginners mailing list