Skip to content

Sparse tensor construction given repeated indices #371

Closed

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.

Activity

  1. ebrevdo commented on Nov 29, 2015

    @ebrevdo
    Contributor

    It's intentional. The C++ SparseTensor code has a IndicesValid method for checking validity of a SparseTensor; and this check would fail on repeated indices. However most of the ops that use STs don't run it for efficiency.

    Please note our current contributions flow is not through GitHub; see contributing.

  2. assigned and unassigned on Nov 29, 2015
  3. vrv commented on Dec 1, 2015

    @vrv

    So @ebrevdo, the answer is to document this difference in behavior?

  4. vrv commented on Dec 23, 2015

    @vrv

    @andyljones: we now accept PRs, so if you want to fix the docs, please send it!

  5. added
    type:docs-bug
    on Dec 23, 2015
  6. added a commit that references this issue on May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type:docs-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions