-
Notifications
You must be signed in to change notification settings - Fork 115
Support concatenating the tensor adding offset. #1846
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
Merged
brightcoder01
merged 9 commits into
sql-machine-learning:develop
from
workingloong:add_concat_with_offset
Mar 25, 2020
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0d2a2b8
Support concatenating the tensor adding offset
workingloong 27882ec
Merge branch 'develop' into add_concat_with_offset
workingloong 9223335
Reformat code by pre-commit hooks
workingloong aa23d97
Merge branch 'develop' into add_concat_with_offset
workingloong d34eabc
Check equality of sparse tensors and ragged tensors
workingloong ec3445e
Reformat code by hooks
workingloong e580f27
Check the inputs is ragged tensor
workingloong b433126
Check inputs are SparseTensor
workingloong bc42427
Fix docstring by comments
workingloong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import tensorflow as tf | ||
|
||
|
||
class ConcatenateWithOffset(tf.keras.layers.Concatenate): | ||
"""Layer that add offset for tensor in the list of inputs and | ||
concatenate the tensors. | ||
|
||
It takes as input a list of tensors and returns a single tensor. | ||
Firstly, it will add an offset in offsets for each tensor in inputs. | ||
Then concatenate them to a single tensor. The tensor in inputs | ||
must have the same type, `Tensor` or `RaggedTensor` or `SparseTensor` and | ||
the same shape. | ||
|
||
Example : | ||
```python | ||
a1 = tf.constant([[1], [1], [1]]) | ||
a2 = tf.constant([[2], [2], [2]]) | ||
offsets = [0, 10] | ||
layer = ConcatenateWithOffset(offsets=offsets, axis=1) | ||
layer([a1, a2]) | ||
[[ 1 12] | ||
[ 1 12] | ||
[ 1 12]] | ||
``` | ||
|
||
Arguments: | ||
offsets: numeric list to add | ||
axis: Axis along which to concatenate. | ||
**kwargs: standard layer keyword arguments. | ||
""" | ||
def __init__(self, offsets, axis=-1): | ||
super(ConcatenateWithOffset, self).__init__() | ||
self.offsets = offsets | ||
self.axis = axis | ||
|
||
def call(self, inputs): | ||
ids_with_offset = [] | ||
if len(self.offsets) != len(inputs): | ||
raise ValueError( | ||
"The offsets length is not equal to inputs length" | ||
"the inputs are {}, offsets are {}".format( | ||
inputs, self.offsets | ||
) | ||
) | ||
for i, tensor in enumerate(inputs): | ||
if isinstance(tensor, tf.SparseTensor): | ||
ids_with_offset.append( | ||
tf.SparseTensor( | ||
indices=tensor.indices, | ||
values=tensor.values + self.offsets[i], | ||
dense_shape=tensor.dense_shape, | ||
) | ||
) | ||
else: | ||
ids_with_offset.append(tensor + self.offsets[i]) | ||
|
||
if isinstance(ids_with_offset[0], tf.SparseTensor): | ||
result = tf.sparse.concat( | ||
axis=self.axis, sp_inputs=ids_with_offset | ||
) | ||
else: | ||
result = tf.keras.layers.concatenate( | ||
ids_with_offset, axis=self.axis | ||
) | ||
return result |
31 changes: 31 additions & 0 deletions
31
elasticdl_preprocessing/tests/concatenate_with_offset_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import unittest | ||
|
||
import numpy as np | ||
import tensorflow as tf | ||
|
||
from elasticdl_preprocessing.layers.concatenate_with_offset import ( | ||
ConcatenateWithOffset | ||
) | ||
|
||
|
||
class ConcatenateWithOffsetTest(unittest.TestCase): | ||
def test_concatenate_with_offset(self): | ||
tensor_1 = tf.constant([[1], [1], [1]]) | ||
tensor_2 = tf.constant([[2], [2], [2]]) | ||
offsets = [0, 10] | ||
concat_layer = ConcatenateWithOffset(offsets=offsets, axis=1) | ||
|
||
output = concat_layer([tensor_1, tensor_2]) | ||
expected_out = np.array([[1, 12], [1, 12], [1, 12]]) | ||
self.assertTrue(np.array_equal(output.numpy(), expected_out)) | ||
|
||
ragged_tensor_1 = tf.ragged.constant([[1], [], [1]]) | ||
ragged_tensor_2 = tf.ragged.constant([[2], [2], []]) | ||
output = concat_layer([ragged_tensor_1, ragged_tensor_2]) | ||
expected_out = np.array([1, 12, 12, 1]) | ||
self.assertTrue(np.array_equal(output.values.numpy(), expected_out)) | ||
workingloong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
sparse_tensor_1 = ragged_tensor_1.to_sparse() | ||
sparse_tensor_2 = ragged_tensor_2.to_sparse() | ||
output = concat_layer([sparse_tensor_1, sparse_tensor_2]) | ||
self.assertTrue(np.array_equal(output.values.numpy(), expected_out)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.