-
Notifications
You must be signed in to change notification settings - Fork 296
Simple modification #2385
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
Simple modification #2385
Changes from all commits
299102c
59627a4
5d1b2c0
97cada7
85bb256
6ab5ea9
f1c55ac
8c40279
7ceef48
edc01a2
11f77c5
6fa4685
04a61e6
0aa74c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,17 +21,24 @@ | |
NO_CONVERT_COUNTER = threading.local() | ||
|
||
|
||
def pad(x, shape, padding_side, pad_value): | ||
if padding_side == "left": | ||
def pad(x, shape, padding_side, pad_value, axis=-1): | ||
if padding_side == "left" and pad_value is not None: | ||
x = x[..., ::-1] | ||
|
||
outputs = x.to_tensor( | ||
default_value=pad_value, | ||
shape=shape, | ||
) | ||
|
||
if padding_side == "left": | ||
outputs = x.to_tensor( | ||
default_value=pad_value, | ||
) | ||
outputs = outputs[..., ::-1] | ||
padding_shape = [tf.shape(outputs)[0]] + [1] * (len(outputs.shape) - 1) | ||
padding_shape[axis] = shape[axis] - tf.shape(outputs)[axis] | ||
padding_shape = tf.cast(padding_shape, "int64") | ||
padding = tf.fill(padding_shape, pad_value) | ||
padding = tf.cast(padding, outputs.dtype) | ||
outputs = tf.concat([outputs, padding], axis=axis) | ||
else: | ||
outputs = x.to_tensor( | ||
default_value=pad_value, | ||
shape=tf.cast(shape, "int64"), | ||
) | ||
return outputs | ||
Comment on lines
+24
to
42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The updated implementation of This bug causes tests in To fix this, the previous implementation strategy of reversing the tensor, padding, and then reversing back should be restored. This is a standard and correct way to achieve left-padding. Here is a suggested implementation that restores the correct behavior: def pad(x, shape, padding_side, pad_value, axis=-1):
shape = tf.cast(shape, "int64")
if padding_side == "left":
# Reverse, pad, and reverse back is a standard way to do left padding.
x = x[..., ::-1]
outputs = x.to_tensor(
default_value=pad_value,
shape=shape,
)
outputs = outputs[..., ::-1]
else: # padding_side == "right"
outputs = x.to_tensor(
default_value=pad_value,
shape=shape,
)
return outputs |
||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change introduces a bug for string-based tensors. When
pad_value
isNone
, it will be set to0
. If the layer is then used with string inputs, this will cause aTypeError
during padding, as you cannot pad a string tensor with an integer0
.The previous implementation was correct. The default value for padding should be handled where the tensor's
dtype
is known, or be left asNone
for the underlying padding function to handle (whichtf.ragged.to_tensor
does correctly, using0
for numeric types and""
for strings).