Skip to content

Sparse tensor construction given repeated indices #371

@andyljones

Description

@andyljones

TensorFlow behaves differently (and surprisingly) to SciPy when converting sparse matrices to dense matrices.

import tensorflow as tf
import scipy as sp
import scipy.sparse

i = [0, 0]
j = [0, 0]
values = [1, 2]

sp_answer = sp.sparse.coo_matrix((values, (i, j))).todense()
print("SciPy's answer is " + str(sp_answer)) # prints [[3]]

tf_answer = tf.sparse_to_dense([i, j], [1, 1], values, 0)
print("TF's answer is " + str(tf.Session().run(tf_answer))) # prints [[2]]

Specifically, if an index pair is repeated, then SciPy will sum the corresponding values, while TF just keeps the last value.

Is this behaviour intentional or accidental? If it's intentional, I'll make a pull request to add a mention of it to the docs. If it's accidental, I'll make a pull request to fix it.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions