The following code fails to execute:

i = SparseArray[{{1, 1} -> 1, {2, 2} -> 2, {3, 3} -> 3, {1, 3} -> 4}];
o = {2, 3, 4};
NetTrain[LinearLayer[], i -> o]
(* NetTrain::invgenout: Output of generator function SparseArray[Automatic,{<<2>>},0,{<<3>>}]->{2,3,4} was incorrect: generator did not return an association or list of rules. *)

Applying Normal to i makes it work.

There is no mention of sparse matrix support in the docs. Is there a way to make NetTrain support sparse input, and if not, is it on the product roadmap? MXNet (the backend that Mathematica's deep learning functions internally use) appears to support sparse arrays (see reference), so hopefully this will eventually be supported.

My use case is a "tall" sparse array with roughly $10^9$ elements, and I'd rather not have it in RAM all at once.

Possible workarounds

One way might be to use the NetTrain[net, f] method, where f grabs a random sample of rows from the sparse matrix, converts the sample into dense form, and feeds them through the net using dense operations. This seems wasteful, though.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.