Sparse matrix representations in SciPy (2017)
heydenberk.com
heydenberk.com
It would be really nice if all of the sparse linear algebra in SciPy could be heavily optimized (to a similar extent as e.g. Intel optimizes their sparse operations in MKL), so that the entire ecosystem could benefit from that. This is probably something that would require some corporate support, but given how many data science and finance companies use Python for their workflow these days, it might be a wise investment.
Tensorflow is better than most, in that it at least implements most ops for sparse tensors, even if it doesn't have efficient implementations for things. I was recently frustrated to find that PyTorch doesn't implement a lot of basic functionality for sparse tensors at all -- it just throws an exception.
I did have great success using Netflix Vectorflow. I roughly gained 2x performance for my use case. It does leave some other performance on the table, no vectorized minibatches etc. If you are training on huge sparse datasets with a shallow ffw network, Vectorflow is a good alternative.
I do want to say a word about how hard it is to offer a set of optimized sparse representations. It is by many measures many times harder than offering an optimized dense linear algebra library. The crux is that the library or routine can not know what the actual data movement and dependencies are until the data arrives. Dense routines do not care about what the data actually is beyond its dimensions: the access pattern, compute and control flow stay exactly the same. The result is that the set of optimized sparse linear algebra routines has, on top of the usual optimization difficulties, this entire extra cartesian product of different representations and low-level operation implementation strategies. Intel MKL has a good SparseBLAS, but compare the performance of feeding it a clustered or block-sparse dataset (e.g. something out of a solver) to a large graph500 RMAT matrix.
In short, do not underestimate the great effort to offer a generally-applicable yet optimized set of sparse linear algebra routines. It makes dense algebra look really easy.
In the same way that people almost universally use BLAS and LAPACK or the MKL for linear algebra.
I realise my comment may have come off a bit flippant - it wouldn't be a trivial piece of work to write Python bindings for SuiteSparse, but would conceivably be simpler than trying to write a better sparse matrix algebra implementation.
I agree with much of your comment, but there is a great deal of serious numerics which doesn't use sparse linear algebra at all. In fact there are entire subfields of numerical analysis which involve fast algorithms for dense linear algebra.
But it also seems like for a given sparse matrix in a given situation, there's no guaranteed that there's algorithm for handling it. The whole thing requires deep experience or black magic.
If you don't have that information, you can look at how your matrix will be accessed. Do you need fast access to random rows, or columns? Do you need to write new values? Are those values already in the sparsity structure?
All of these questions can lead you to choosing the right sparse format, but it does take some experience to know where to look.
I think this is one of those shibboleths that separate folks who are experts in ML/data-science at scale vs those who are coming more from a enterprise/DBA background. I often have to convince folks why its not a great idea to write sparse matrix multiplication in SQL.
For many domains, it's a big worry. I routinely encounter large, sparse datasets where I/O and storage dominate other concerns. Big sparse tensors are worse than the "slowness" associated with a suboptimal sparse vector math implementation.
"I think this is one of those shibboleths that separate folks who are experts in ML/data-science at scale vs those who are coming more from a enterprise/DBA background."
I've been doing ML work for a long time. This is actually a shibboleth that separates folks who mostly do image classification from other kinds of ML people.
Interesting comment regarding image classification. I would have assumed they are the ones who don't have to worry about sparsity much. Physics, and image and video are some of the places you encounter large and dense matrices. Lapack's wet dream :)