Get rows of a csr sparse matrix


#1

I’m trying to solve a matrix completion kind of problem. I have a sparse input matrix A and a factor matrix U that I’m trying to compute. So UU^T ~= A, or more precisely I’m minimizing

sum_ij Loss(A_ij, U[i] dot(U[j]))

The gradient computations involve dot products that are like dot(A, U) and also they also involve computing the values of the dot products on the sparsity pattern of A: so dot(U[i] , U[j] ) where A[i,j] is nonzero.

I have been trying to optimize these two computations that are the most expensive ones in my code. It’s best is to store U as a csr_matrix even though it is a sparse matrix. Rationale: A is sparse and we want to compute products dot(A, U) . In scipy you can multiply a sparse matrix A by a dense numpy array U but mxnet did not allow this.

Now for computing the pairwise products dot(U[i] , U[j] ) on the sparsity pattern of A I get errors like this

File “/Users/myamazingusername/anaconda3/lib/python3.6/site-packages/mxnet/ndarray/sparse.py”, line 380, in getitem
raise ValueError(‘Undefined behaviour for {}’.format(key))
ValueError: Undefined behaviour for 108

the workaround I found was to pass the matrix U to numpy using U.asnumpy() at each iteration and things work fine. But I think this is very inefficient: inner products would be faster to compute using mxnet (GPU). Right? Can someone advice? @eric-haibin-lin :slight_smile:

Thanks,

Emile


#2

For CSR matrix, slice with int key is supposed to work. Is your key integer type?


#3

My key was np.int64. Casting to int fixed the bug. Thanks