Avoiding stack space overflow

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

I don't think the problem is with trainNetwork, but rather epoch. You might try adjusting your network datatype and sub datatypes in the manner of data Network = Network !Int !Int until you narrow down which piece is causing the problem. On Mon, Jan 26, 2015 at 7:19 AM, Hans Georg Schaathun < georg+haskell@schaathun.net> wrote:
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 _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Mon, Jan 26, 2015 at 09:55:03AM -0500, David McBride wrote:
I don't think the problem is with trainNetwork, but rather epoch. You might try adjusting your network datatype and sub datatypes in the manner of data Network = Network !Int !Int until you narrow down which piece is causing the problem.
It turns out that you are absolutely right. A number of randomly inserted bang patterns got rid of the overflow. Thanks a lot for the idea.. I feel even more clueless than I used too, but I have at least a way to proceed. -- :-- Hans Georg

Here are my go-to resources for Haskell's evaluation: http://chimera.labs.oreilly.com/books/1230000000929/ch02.html#sec_par-eval-w... https://hackhands.com/lazy-evaluation-works-haskell/ On Mon, Jan 26, 2015 at 8:45 AM, Hans Georg Schaathun < georg+haskell@schaathun.net> wrote:
On Mon, Jan 26, 2015 at 09:55:03AM -0500, David McBride wrote:
I don't think the problem is with trainNetwork, but rather epoch. You might try adjusting your network datatype and sub datatypes in the manner of data Network = Network !Int !Int until you narrow down which piece is causing the problem.
It turns out that you are absolutely right. A number of randomly inserted bang patterns got rid of the overflow. Thanks a lot for the idea..
I feel even more clueless than I used too, but I have at least a way to proceed.
-- :-- Hans Georg _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Bob Ippolito wrote:
Here are my go-to resources for Haskell's evaluation:
http://chimera.labs.oreilly.com/books/1230000000929/ch02.html#sec_par-eval-w... https://hackhands.com/lazy-evaluation-works-haskell/
I just wanted to mention that I have now expanded the latter resource. It is best reached from the following URL: https://hackhands.com/guide-lazy-evaluation-haskell/ Best regards, Heinrich Apfelmus -- http://apfelmus.nfshost.com

On Mon, Mar 9, 2015 at 4:15 PM, Heinrich Apfelmus wrote: Bob Ippolito wrote: Here are my go-to resources for Haskell's evaluation: http://chimera.labs.oreilly.com/books/1230000000929/ch02.
html#sec_par-eval-whnf
https://hackhands.com/lazy-evaluation-works-haskell/ I just wanted to mention that I have now expanded the latter resource. It
is best reached from the following URL: Thank you, this is great! Perhaps you could also link to this from the top
and/or bottom of each individual article to make them all more discoverable
from existing links?
-bob

Bob Ippolito wrote:
On Mon, Mar 9, 2015 at 4:15 PM, Heinrich Apfelmus
wrote:
Bob Ippolito wrote:
Here are my go-to resources for Haskell's evaluation:
http://chimera.labs.oreilly.com/books/1230000000929/ch02. html#sec_par-eval-whnf https://hackhands.com/lazy-evaluation-works-haskell/
I just wanted to mention that I have now expanded the latter resource. It is best reached from the following URL:
Thank you, this is great! Perhaps you could also link to this from the top and/or bottom of each individual article to make them all more discoverable from existing links?
Good idea! I have added a link at the top of each post. Does this work as intended? Best regards, Heinrich Apfelmus -- http://apfelmus.nfshost.com

This is really nice stuff. Thanks Animesh Sent from my iPhone
On 10-Mar-2015, at 5:49 pm, Heinrich Apfelmus
wrote: Bob Ippolito wrote:
On Mon, Mar 9, 2015 at 4:15 PM, Heinrich Apfelmus
wrote: Bob Ippolito wrote:
Here are my go-to resources for Haskell's evaluation:
http://chimera.labs.oreilly.com/books/1230000000929/ch02. html#sec_par-eval-whnf https://hackhands.com/lazy-evaluation-works-haskell/ I just wanted to mention that I have now expanded the latter resource. It is best reached from the following URL:
https://hackhands.com/guide-lazy-evaluation-haskell/ Thank you, this is great! Perhaps you could also link to this from the top and/or bottom of each individual article to make them all more discoverable from existing links?
Good idea! I have added a link at the top of each post. Does this work as intended?
Best regards, Heinrich Apfelmus
-- http://apfelmus.nfshost.com
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On Tue, Mar 10, 2015 at 2:49 AM, Heinrich Apfelmus < apfelmus@quantentunnel.de> wrote:
Bob Ippolito wrote:
On Mon, Mar 9, 2015 at 4:15 PM, Heinrich Apfelmus < apfelmus@quantentunnel.de
wrote:
Bob Ippolito wrote:
Here are my go-to resources for Haskell's evaluation:
http://chimera.labs.oreilly.com/books/1230000000929/ch02. html#sec_par-eval-whnf https://hackhands.com/lazy-evaluation-works-haskell/
I just wanted to mention that I have now expanded the latter resource.
It is best reached from the following URL:
Thank you, this is great! Perhaps you could also link to this from the top and/or bottom of each individual article to make them all more discoverable from existing links?
Good idea! I have added a link at the top of each post. Does this work as intended?
Absolutely, works perfectly! -bob

We're all guilty of throwing around the bang patterns to fix performance problems, and that is a reasonable solution. But if you wish to understand the problem, see if you can narrow down much of your leak to a specific field within one of your types, then you can look at epoch and sub functions and identify why that particular field is not being fully evaluated by the time you have returned from epoch. Hopefully it will be obvious in hindsight and you will not run into this next time you have similar code. GHCI's debugger may also give you a nice way to determine where your problem lies. On Mon, Jan 26, 2015 at 11:45 AM, Hans Georg Schaathun < georg+haskell@schaathun.net> wrote:
On Mon, Jan 26, 2015 at 09:55:03AM -0500, David McBride wrote:
I don't think the problem is with trainNetwork, but rather epoch. You might try adjusting your network datatype and sub datatypes in the manner of data Network = Network !Int !Int until you narrow down which piece is causing the problem.
It turns out that you are absolutely right. A number of randomly inserted bang patterns got rid of the overflow. Thanks a lot for the idea..
I feel even more clueless than I used too, but I have at least a way to proceed.
-- :-- Hans Georg _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (5)
-
Animesh Saxena
-
Bob Ippolito
-
David McBride
-
Hans Georg Schaathun
-
Heinrich Apfelmus