keras icon indicating copy to clipboard operation
keras copied to clipboard

Reshape layer drops mask from previous layers

Open brian36 opened this issue 4 years ago • 8 comments
trafficstars

System information.

  • Have I written custom code (as opposed to using a stock example script provided in Keras): Yes, very basic code
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ran it on google colab notebook (https://colab.research.google.com/) as well as CentOS 7
  • TensorFlow installed from (source or binary): Binary / however it is installed in colab
  • TensorFlow version (use command below): v2.6.0-0-g919f693420e 2.6.0
  • Python version: 3.7
  • Bazel version (if compiling from source): N/A
  • GPU model and memory: N/A
  • Exact command to reproduce:
import tensorflow as tf
import numpy as np

num = 10

embedding_size = 5
window_size = 2

emb = tf.keras.layers.Embedding(
    num, embedding_size, input_length=1, mask_zero=True
)

td = tf.keras.layers.TimeDistributed(emb)

inp = tf.constant(
    np.array([
              [0,1],[3,0],[4,0]
    ])
)

inp = tf.keras.layers.Reshape((window_size, 1))(inp)
print(inp)

out = td(inp)
print(out.shape,out._keras_mask)

out2 = tf.keras.layers.Reshape((window_size,embedding_size ))(out)
print(out2.shape)

#The following throws an error, because Reshape dropped the mask from the previous tensor:
print(out2._keras_mask)

Error from last line:

AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute '_keras_mask'

Describe the problem.

Applying the Reshape layer to a tensor that has a mask (e.g., from the Embedding Layer) gets rid of the mask, instead of reshaping it.
This is a problem because in order to re-use an Embedding Layer for different outputs, I need to apply reshape after certain transformations, before passing to other layers (e.g., LSTM), but instead of also reshaping the mask of the tensor (as a user would expect), Reshape apparently discards the mask entirely, so I cannot use it.

To resolve this I would have to write a custom version of the Reshape layer to replace it.

Describe the current behavior. Reshape layer applied to a tensor with a mask discards the mask instead of reshaping it.

Describe the expected behavior. Reshape layer should not discard the mask of a tensor when reshaping it, but instead should correspondingly reshape the mask as well (so the mask now correctly applies to the reshaped tensor and can be passed to subsequent layers like LSTM).

I.e., in the code snippet above, for 'out', I have a tensor with shape (3, 2, 1, 5) and mask =

tf.Tensor(
[[[False]
  [ True]]

 [[ True]
  [False]]

 [[ True]
  [False]]], shape=(3, 2, 1), dtype=bool)

After the Reshape application above, the output is shape (3,2,5) - I would expect the mask to be correspondingly reshaped like:

new_mask = tf.keras.layers.Reshape((window_size,))(out._keras_mask)

So that the new mask is now shape (3,2) =

<tf.Tensor: shape=(3, 2), dtype=bool, numpy=
array([[False,  True],
       [ True, False],
       [ True, False]])>

Contributing.

  • Do you want to contribute a PR? (yes/no): no

Standalone code to reproduce the issue. See the code block at the beginning.

Source code / logs.

N/A

brian36 avatar Aug 18 '21 22:08 brian36

issue reproducible at colab

kashyapraval avatar Aug 19 '21 17:08 kashyapraval

@brian36 , Can you please take a look at this issue with similar error.It helps.Thanks

tilakrayal avatar Aug 24 '21 11:08 tilakrayal

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.

google-ml-butler[bot] avatar Aug 31 '21 11:08 google-ml-butler[bot]

Closing as stale. Please reopen if you'd like to work on this further.

google-ml-butler[bot] avatar Sep 07 '21 11:09 google-ml-butler[bot]

@brian36 , Can you please take a look at this issue with similar error.It helps.Thanks

This other issue is unrelated.

The issue here is, there is no keras mask when there should be one, after passing a tensor with a mask through reshape.

This is not a support issue or question about implementation, but in my opinion a bug with the Reshape layer implementation. At the very least it should give the option of keeping the underlying mask when passing through the Reshape layer, and it definitely should warn the user it's dropping the underlying mask / mention this in the documentation if nothing else. Instead it does neither.

brian36 avatar Sep 07 '21 16:09 brian36

@jvishnuvardhan I Was able to reproduce the issue in TF v2.6 and TF-nightly please find the gist .Thanks!

kumariko avatar Oct 20 '21 14:10 kumariko

We don't believe the reshape layer supports masking at this time, though it could be added. We are happy to accept contributions that add it as a feature

(Separately, we should consider raising an error when passing a tensor with a mask to a layer that does not support masking, rather than silently dropping the mask)

tomerk avatar Oct 21 '21 17:10 tomerk

@tomerk Can I work on this if no one else is?

Shreyz-max avatar Aug 27 '22 03:08 Shreyz-max