Note that there are some explanatory texts on larger screens.

plurals
  1. POMathNet Numerics out of memory exception
    primarykey
    data
    text
    <p>I'm using MathNet.Numerics in my F# project, and it runs out of memory when dealing with matrices that should be well within its remit.</p> <p><strong>EDIT: The problem is definitely not with MathNet.Numerics. It's something I'm doing, though I have yet to work out what that may be.</strong></p> <p>Here is how I'm building the MNIST matrix. Perhaps I'm leaving a file stream open or something, but I can't see how that would be the case.</p> <pre><code>let readInt (b : BinaryReader) = [1..4] |&gt; List.fold (fun res item -&gt; (res &lt;&lt;&lt; 8) ||| (int)(b.ReadByte())) 0 let readImage (b : BinaryReader, rowArray, colArray) = rowArray |&gt; List.collect (fun r -&gt; List.map (fun c -&gt; (b.ReadByte() |&gt; int |&gt; float)/255.0 ) colArray) let loadMnistImage file = use stream = File.Open(file, FileMode.Open) use reader = new BinaryReader(stream) let magicNumber = readInt(reader) let nImages = readInt(reader) let nRows = readInt(reader) let nCols = readInt(reader) let row = [1..nRows] let col = [1..nCols] let images = [1..nImages] |&gt; List.map (fun _ -&gt; readImage(reader, row, col)) matrix images; </code></pre> <p>Using this,</p> <pre><code>let mnistTrainingImages = loadMnistImage MnistTrainingImageData let tr = mnistTrainingImages.Transpose() </code></pre> <p>blows up.</p> <p>Is it them or is it me? I pulled the file from <a href="http://yann.lecun.com/exdb/mnist/" rel="nofollow">http://yann.lecun.com/exdb/mnist/</a>, in case anyone fancies trying this themselves.</p> <p><strong>EDIT 1: The culprit is the call to <code>loadMnistImage</code>.</strong> According to Gene Belitski's suggestion, I ran the code</p> <pre><code>let rand = System.Random() let m: double [,] = Array2D.init 60000 784 (fun _ _ -&gt; rand.NextDouble()) </code></pre> <p>before and after the line </p> <pre><code>let mnistTrainingImages = loadMnistImage MnistTrainingImageData </code></pre> <p>Before the line, it was fine; after the line, it blew up with the same out of memory exception. I would have thought that the <code>use</code> statements would release the file and I'd be OK (and this is not a massive file). However, I'm missing something, so I'll keep digging, and if anyone sees anything please do let me know.</p> <p><strong>EDIT 2: When I simplified the file reader as follows, the problem went away. It looks like the problem was the lazy evaluation of the list.</strong></p> <pre><code>let readImage (b : BinaryReader) = (b.ReadByte() |&gt; int |&gt; float)/255.0 let loadMnistImage file = use stream = File.Open(file, FileMode.Open) use reader = new BinaryReader(stream) let magicNumber = readInt(reader) let nImages = readInt(reader) let nRows = readInt(reader) let nCols = readInt(reader) Array2D.init nImages (nRows * nCols) (fun _ _ -&gt; readImage reader) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload