tensorflow: tf.sparse_tensor_to_dense does not have a gradient
Differentiating tf.sparse_tensor_to_dense
and also tf.sparse_to_dense
returns None
. At the same time, a hacky way of converting a sparse tensor to a dense tensor by using tf.sparse_add
works. Please see the code below:
indices = tf.placeholder(tf.int64, (None, 2))
values = tf.placeholder(tf.float32, (None,))
sparse_tensor = tf.SparseTensor(indices, values, (5, 7))
dense_tensor1 = tf.sparse_tensor_to_dense(sparse_tensor)
dense_tensor2 = tf.sparse_add(tf.zeros((5, 7)), sparse_tensor)
sum1 = tf.reduce_sum(dense_tensor1)
sum2 = tf.reduce_sum(dense_tensor2)
print tf.gradients(sum1, values)
>>> [None]
print tf.gradients(sum2, values)
>>> [tf.Tensor ...]
What related GitHub issues or StackOverflow threads have you found by searching the web for your problem?
Didn’t find anything.
Environment info
- A link to the pip package you installed:
pip install --user https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.11.0rc1-cp27-none-linux_x86_64.whl
- The output from
python -c "import tensorflow; print(tensorflow.__version__)"
0.11.0rc1
If possible, provide a minimal reproducible example (We usually don’t have time to read hundreds of lines of your code)
Please see the code above.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 1
- Comments: 18 (10 by maintainers)
Meanwhile, you guys could at least print a warning, raise an exception, deprecate this API, or add a line to the documentation. I do like Tensorflow, and I think you are doing a great job, but for people outside Google it is often very unclear which ops are “mostly deprecated” in favor of which.
This uses sparse to dense op, which is mostly deprecated in favor of scatter_nd op. That op has a gradient. We should change the sparse to dense Python wrapper to call scatter_nd.
The example provided above still does return None – added
sparse_tensor_dense_matmul
andsparse_to_dense
to the mix because in my use case I can (with some overhead) use each of those functions.Executed:
Ready to copy & run:
BTW this op now has a gradient.
On Jun 16, 2017 2:45 PM, “Olivia” notifications@github.com wrote:
Automatically closing due to lack of recent activity. Since this issue is old at this point, please reopen the issue if it still occurs when tried with the latest version of Tensorflow. Thank you.
Here is the solution.
@ops.RegisterGradient(“SparseToDense”) def _SparseToDenseGrad(op, grad): sparse_indices, output_shape, _, _ = op.inputs
sparse_values_grad = array_ops.gather_nd(grad, sparse_indices) default_value_grad = math_ops.reduce_sum(grad) - math_ops.reduce_sum( sparse_values_grad) return [ array_ops.zeros_like(sparse_indices), array_ops.zeros_like(output_shape), sparse_values_grad, default_value_grad ]
This implementation is from TF2.0 version.
I second that printing a warning should be in order for this. I only found a bug in my code relating to this issue by looking at the graph in tensorboard and saw that one of my variables was a different colour…