Theano/Theano — pull request #627 by ynd (2012-04-27 19:39:34+00:00)
Title: Fix crash and corruption bug in CSM's grad

Fixes 2 related things:
1. A crash bug in CSM's grad when the gradient is sparser than the input. In those cases the data vector of the gradient is smaller than the data vector of the input, and this causes a crash with the current implementation. To be a valid gradient, the data vector should contain zero entries for those who have zero gradient.
2. A corruption bug in the same op. The order of the data vector is not guaranteed in the CSM format (the elements on the sparse axis are allowed to be unsorted), therefore the elements of the data vector in the gradient may be in a slightly different order than in the input. This was causing problems for me by leading to NaNs for example. This happens whenever there is an unsorted sparse matrix somewhere in the graph. This happens for instance when you use advanced indexing on a sparse matrix. In scipy.sparse they solve this by having two versions for the affected ops: one that works when the matrix is sorted, one when it isn't. See https://github.com/scipy/scipy/blob/master/scipy/sparse/sparsetools/csr.h (csr_binop_csr_canonical and csr_binop_csr_general for instance)

I provide an efficient C implementation for the corrected gradient op, and I provide tests for CSM and it's grad.