Skip to content

Commit fad90d0

Browse files
SliceNdLayer: added comment explaining the possible gradient issue of the padding
1 parent 8f07ded commit fad90d0

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

returnn/tf/layers/basic.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,14 @@ def __init__(self, start, size, min_size=None, **kwargs):
928928
placeholder = gather_layer.output.placeholder
929929
self.output.size_placeholder = gather_layer.output.size_placeholder
930930
# zero padding
931+
# In principle, the padded frames are being ignored
932+
# (unless get_padding_info_dict_ref et al are used).
933+
# However, you can still end up with gradients for them
934+
# in unexpected ways.
935+
# Due to our gather implementation,
936+
# the gradient flow would go into wrong frames
937+
# and might lead to unexpected behavior.
938+
# So to be on the safe side, we do the masking here.
931939
pad_mask = expand_multiple_dims(pad_mask, [-1] * (len(placeholder.shape) - len(pad_mask.shape)))
932940
self.output.placeholder = where_bc(pad_mask, tf.zeros_like(placeholder), placeholder)
933941

0 commit comments

Comments
 (0)