Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tf.matmul() fails when matrices are sparse, even if a_is_sparse is set. Cryptic error. #3246

Closed
davidbernat opened this issue Jul 9, 2016 · 3 comments

Comments

@davidbernat
Copy link

tf.matmul(A, B, a_is_sparse=True) fails on a = ops.convert_to_tensor(a, name="a") with cryptic error.

Example code:

import tensorflow as tf
import numpy as np

x = tf.sparse_placeholder(tf.float32)
y = tf.Variable(tf.random_uniform([9, 9], minval=0.0, maxval=1.0, dtype=tf.float32))

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    indices = np.array([[3, 2], [4, 5]], dtype=np.int64)
    values = np.array([1.0, 2.0], dtype=np.float32)
    shape = np.array([9, 9], dtype=np.int64)
    _ = sess.run(tf.matmul(x, y), feed_dict={
        x: tf.SparseTensorValue(indices, values, shape)})

Error:
line 12, in
_ = sess.run(tf.matmul(x, y, a_is_sparse=True), feed_dict={
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 1189, in matmul
a = ops.convert_to_tensor(a, name="a")
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 620, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/constant_op.py", line 179, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/constant_op.py", line 162, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 421, in make_tensor_proto
tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values])
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/util/compat.py", line 44, in as_bytes
raise TypeError('Expected binary or unicode string, got %r' % bytes_or_text)
TypeError: Expected binary or unicode string, got <tensorflow.python.framework.ops.SparseTensor object at 0x11d952d90>

Is fixed by explicitly converting from sparse to dense tensor before multiplication:


import tensorflow as tf
import numpy as np

x = tf.sparse_placeholder(tf.float32)
z = tf.sparse_tensor_to_dense(x)
y = tf.Variable(tf.random_uniform([9, 9], minval=0.0, maxval=1.0, dtype=tf.float32))

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    indices = np.array([[3, 2], [4, 5]], dtype=np.int64)
    values = np.array([1.0, 2.0], dtype=np.float32)
    shape = np.array([9, 9], dtype=np.int64)
    _ = sess.run(tf.matmul(z, y), feed_dict={
        x: tf.SparseTensorValue(indices, values, shape)})
@aselle
Copy link
Contributor

aselle commented Jul 11, 2016

@concretevitamin, Is this expected to work, or is this a known limitation of sparse tensors.

@aselle aselle added the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Jul 11, 2016
@concretevitamin
Copy link
Contributor

Flags a_is_sparse and b_is_sparse do not indicate the operands being SparseTensors. Instead, they are algorithmic hints to invoke a more efficient algorithm on the two dense Tensor operands.

To matmul a SparseTensor and a dense Tensor, you could use tf.sparse_tensor_dense_matmul() instead.

@aselle aselle removed the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Jul 11, 2016
@MarvinTeichmann
Copy link

Flags a_is_sparse and b_is_sparse do not indicate the operands being SparseTensors. Instead, they are algorithmic hints to invoke a more efficient algorithm on the two dense Tensor operands.

This should be part of the documentation of matmut. It is very unclear what the flag a_is_sparse does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants