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

Improve shape function of tf.sparse_concat when inputs are fully defined #24018

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions tensorflow/python/kernel_tests/sparse_concat_op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,15 @@ def testShapeInferenceUnknownShapes(self):
self.assertEqual(sp_concat.values.get_shape().as_list(), [None])
self.assertEqual(sp_concat.dense_shape.get_shape(), [3])

def testConcatShape(self):
# Test case for GitHub 21964.
x = sparse_tensor.SparseTensor(
indices=[[0, 0], [1, 1]], values=[1, 2], dense_shape=[2, 2])
y = sparse_tensor.SparseTensor(
indices=[[0, 0], [1, 1]], values=[1, 2], dense_shape=[2, 2])
z = sparse_ops.sparse_concat(-1, [x, y])
self.assertEqual(z.get_shape().as_list(), [2, 4])


if __name__ == "__main__":
test.main()
6 changes: 6 additions & 0 deletions tensorflow/python/ops/sparse_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ def sparse_concat(axis,
output_ind, output_val, output_shape = (
gen_sparse_ops.sparse_concat(inds, vals, shapes, axis, name=name))

shapes_value = [tensor_util.constant_value(shape) for shape in shapes]
if shapes_value and all(shape is not None for shape in shapes_value):
dim = sum(shape[axis] for shape in shapes_value)
output_shape = shapes_value[0]
output_shape[axis] = dim
output_shape = ops.convert_to_tensor(output_shape)
return sparse_tensor.SparseTensor(output_ind, output_val, output_shape)


Expand Down