<div dir="ltr"><div dir="ltr" class="gmail_msg">I do not think that `show` or `read` are good candidates for serialising data into a file. I'd suggest to use a dedicated serialisation library, to write out proper JSON/YAML/XML/binary.</div><br class="gmail_msg"><div class="gmail_quote gmail_msg"><div dir="ltr" class="gmail_msg">Mahdi Dibaiee <<a href="mailto:mdibaiee@aol.com" class="gmail_msg" target="_blank">mdibaiee@aol.com</a>> schrieb am Do., 27. Okt. 2016 um 10:53 Uhr:<br class="gmail_msg"></div><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><font color="black" size="2" face="arial" class="gmail_msg"><font size="2" class="gmail_msg">Hi,<br class="gmail_msg">
<br class="gmail_msg">
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 class="gmail_msg">
to avoid the long computation every time (I'm training a recurrent neural network).<br class="gmail_msg">
<br class="gmail_msg">
First attempt, the simplest method:<br class="gmail_msg">
<br class="gmail_msg">
<font face="Courier New, Courier, mono" class="gmail_msg"> writeFile "rnn" (show dataInstance)</font><br class="gmail_msg">
<br class="gmail_msg">
It starts to take all of the memory and then bails out with `out-of-memory` error.<br class="gmail_msg">
<br class="gmail_msg">
So I wrote a function to write the string chunk by chunk, without buffering, here is the code:<br class="gmail_msg">
<a class="gmail_msg">https://github.com/mdibaiee/sibe/blob/728df02fbdd6f134af107c098f5477094c61ea76/examples/recurrent.hs#L52-L64</a><br class="gmail_msg">
<br class="gmail_msg">
Copy/pasted from the link:</font><font face="Courier New, Courier, mono" class="gmail_msg"><br class="gmail_msg">
</font><pre class="gmail_msg"><font face="Courier New, Courier, mono" class="gmail_msg"> 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 class="gmail_msg">
<br class="gmail_msg">
<font size="2" face="Arial, Helvetica, sans-serif" class="gmail_msg">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 class="gmail_msg">
I asked on IRC and someone said "Show is not lazy <i class="gmail_msg">enough</i>", if that's the case, I would appreciate an explanation of that.<br class="gmail_msg">
<br class="gmail_msg">
Thanks,<br class="gmail_msg">
Mahdi<br class="gmail_msg">
</font></font></pre><br class="gmail_msg">
</font>_______________________________________________<br class="gmail_msg">
Beginners mailing list<br class="gmail_msg">
<a class="gmail_msg">Beginners@haskell.org</a><br class="gmail_msg">
<a rel="noreferrer" class="gmail_msg">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br class="gmail_msg">
</blockquote></div></div>