Created
January 23, 2022 22:25
-
-
Save dkapitan/8e7ae2d3e2facf756a4a2206b2636aa3 to your computer and use it in GitHub Desktop.
Convert SciPy sparse matrix to tf.sparse.SparseTensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sparse_matrix_to_tensor(X): | |
"""Transforms SciPy sparse matrix to tensorflow.sparse.SparseTensor.""" | |
row_nnz = np.diff(X.indptr) | |
indices = np.asarray([[row_i, col_i] | |
for row_i, nnz in enumerate(row_nnz) | |
for col_i in range(nnz)], dtype=np.int64) | |
values = X.data | |
return SparseTensor(indices=indices, values=values, dense_shape=X.shape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment