<font color='black' size='2' face='arial'><font size="2">Hi,<br>
<br>
So I have a data instance which contains a few big matrices, and I want to save my instance to a file so I can `read` it back later<br>
to avoid the long computation every time (I'm training a recurrent neural network).<br>
<br>
First attempt, the simplest method:<br>
<br>
<font face="Courier New, Courier, mono">  writeFile "rnn" (show dataInstance)</font><br>
<br>
It starts to take all of the memory and then bails out with `out-of-memory` error.<br>
<br>
So I wrote a function to write the string chunk by chunk, without buffering, here is the code:<br>
https://github.com/mdibaiee/sibe/blob/728df02fbdd6f134af107c098f5477094c61ea76/examples/recurrent.hs#L52-L64<br>
<br>
Copy/pasted from the link:</font><font face="Courier New, Courier, mono"><br>
</font><pre><font face="Courier New, Courier, mono">  saveRecurrent :: FilePath -> String -> Int -> IO ()
  saveRecurrent path str chunkSize = do
    handle <- openFile path AppendMode
    hSetBuffering handle NoBuffering
    loop handle str
    hClose handle
    where
      loop _ [] = return ()
      loop handle s = do
        hPutStr handle $ take chunkSize s
        hFlush handle
        loop handle $ drop chunkSize s<br>
<br>
<font size="2" face="Arial, Helvetica, sans-serif">But it doesn't work either, I still get `out-of-memory` errors. From what I understand, this should work, but it isn't.<br>
I asked on IRC and someone said "Show is not lazy <i>enough</i>", if that's the case, I would appreciate an explanation of that.<br>
<br>
Thanks,<br>
Mahdi<br>
</font></font></pre><br>
</font>