question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TimeDistributed Wrapper not working with LSTM/GRU

See original GitHub issue

Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on StackOverflow or join the Keras Slack channel and ask there instead of filing a GitHub issue.

Thank you!

  • Check that you are up-to-date with the master branch of Keras. You can update with: pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps

  • If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.

  • If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with: pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps

  • Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

I am trying to apply word level attention to words in a document after passing the sentences through a GRU. However, the TimeDistributed Wrapper isn’t working with GRU/LSTM.

I get the following error

File "/home/##/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 569, in __call__
    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
File "/home/##/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 632, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
File "/home/##/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 164, in create_node
    output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
File "/home/##/.local/lib/python2.7/site-packages/keras/layers/wrappers.py", line 129, in call
    y = self.layer.call(X)  # (nb_samples * timesteps, ...)
File "/home/##/.local/lib/python2.7/site-packages/keras/layers/recurrent.py", line 201, in call
    input_shape = K.int_shape(x)
File "/home/##/.local/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 128, in int_shape
    raise Exception('Not a Keras tensor:', x)
Exception: ('Not a Keras tensor:', Reshape{3}.0)

The code snipped is written below

input_layer = Input(shape=(document_size, img_h,), dtype='int32', name='input_layer')
embedding_layer = TimeDistributed(Embedding(len(W2V), img_w, input_length=img_h, weights=[W2V], trainable=True, mask_zero=True))(input_layer)
gru_word = TimeDistributed(GRU(GRU_layers[0], return_sequences=True, activation=conv_non_linear))(embedding_layer)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mahnerakcommented, Jul 25, 2017

@peschn I understand that, but K.rnn() calls step with non-keras tensors by design. In my opinion the problem should be fixed similary https://github.com/fchollet/keras/blob/master/keras/layers/wrappers.py#L186

Maybe it’s better to ask @fchollet before making pull-request.

1reaction
peschncommented, Jul 25, 2017

@mahnerak the helper step is called within the method K.rnn(), but before it gets called, the dimshuffle (which is right at the beginning of the method: https://github.com/fchollet/keras/blob/0bc856f90a746ce3c8078f5ec4fb5156c88d8fdd/keras/backend/theano_backend.py#L1280) removes the _keras_shape, so when self.layer.call(x, **kwargs) within step is called, x doesn’t contain the shape information anymore, so call() will fail.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is TimeDistributed not needed in my Keras LSTM?
This is caused by the fact that you use the return_sequences=True parameter which means that the Dense layer is applied to every timestep...
Read more >
CoreML model compiler error Time Dist BI-RNN->FC
It appears to be the BI-RNN into FC that has the time distributed wrapper that has the problem. Any advice appreciated.
Read more >
How to Use the TimeDistributed Layer in Keras
This tutorial aims to clear up confusion around using the TimeDistributed wrapper with LSTMs with worked examples that you can inspect, run, and ......
Read more >
tf.keras.layers.LSTM | TensorFlow v2.11.0
When the input numpy array is not compatible with the RNN layer state, either size wise or dtype wise. Was this helpful?
Read more >
Recurrent neural networks
... LSTM, GRU from keras.layers.wrappers import TimeDistributed from ... Not to mention Elizabeth Taylor at her most gloriously beautiful and sympathetic, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found