Skip to content
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

pad_sequences bug in Batch #15340

Closed
ucasiggcas opened this issue Sep 10, 2021 · 9 comments
Closed

pad_sequences bug in Batch #15340

ucasiggcas opened this issue Sep 10, 2021 · 9 comments
Assignees
Labels
keras-team-review-pending Pending review by a Keras team member. type:bug/performance

Comments

@ucasiggcas
Copy link

hi,dear
want to use the pad_sequences in batch ,but got the bug down

>>> inputs=tf.keras.Input(shape=(None,),dtype=tf.int32)
>>> inputs
<tf.Tensor 'input_1:0' shape=(None, None) dtype=int32>
>>> tf.keras.preprocessing.sequence.pad_sequences(inputs)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/../python3.8/site-packages/tensorflow/python/keras/preprocessing/sequence.py", line 156, in pad_sequences
    return sequence.pad_sequences(
  File "/../python3.8/site-packages/keras_preprocessing/sequence.py", line 57, in pad_sequences
    num_samples = len(sequences)
  File "/../python3.8/site-packages/tensorflow/python/framework/ops.py", line 752, in __len__
    raise TypeError("len is not well defined for symbolic Tensors. ({}) "
TypeError: len is not well defined for symbolic Tensors. (input_1:0) Please call `x.shape` rather than `len(x)` for shape information.

could pls help me ?
thx

@sushreebarsa
Copy link
Collaborator

@ucasiggcas
In order to expedite the trouble-shooting process, please provide a code snippet to reproduce the issue reported here. Thanks!

@ucasiggcas
Copy link
Author

class Model(tf.keras.Model):
    def __init__(self, maxlen=10):
        super(Model,self).__init__()
        self.maxlen=maxlen
    def call(self,inputs):
        seqs_pad=tf.keras.preprocessing.sequence.pad_sequences(inputs)
        return seqs_pad

def gen():
    seqs=[np.random.randint(0,1000,size=10).tolist() for k in range(5,15)]
    yield seqs
#batch_size=10,dim=10,
dataset=tf.data.Dataset.from_generator(gen,output_types=(tf.int32),
                                             output_shapes=((10,10)))
model=Model()
@tf.function
def train_step(inputs):
    with tf.GradientTape() as tape:
        seqs_pad=model(inputs)
        print(seqs_pad)

for inputs in dataset:
    train_step(inputs)

bug

TypeError: in user code:

    pad_sequences_issue_.py:23 train_step  *
        seqs_pad=model(inputs)
    pad_sequences_issue_.py:10 call  *
        seqs_pad=tf.keras.preprocessing.sequence.pad_sequences(inputs)
    /../python3.8/site-packages/tensorflow/python/keras/preprocessing/sequence.py:156 pad_sequences  **
        return sequence.pad_sequences(
    /../python3.8/site-packages/keras_preprocessing/sequence.py:57 pad_sequences
        num_samples = len(sequences)
    /../python3.8/site-packages/tensorflow/python/framework/ops.py:851 __len__
        raise TypeError("len is not well defined for symbolic Tensors. ({}) "

    TypeError: len is not well defined for symbolic Tensors. (inputs:0) Please call `x.shape` rather than `len(x)` for shape information.

@sushreebarsa
Copy link
Collaborator

sushreebarsa commented Sep 13, 2021

@ucasiggcas Could you please refer to the link and let us know if it helps? Thanks!

@ucasiggcas
Copy link
Author

No, have tried
could not be useful to my code

@sushreebarsa
Copy link
Collaborator

@ucasiggcas Thanks for the update! Could you please have a look at the gist and let us know if it is the way to reproduce the issue reported here ? Please let me know if I am missing something while reproducing. Thanks!

@ucasiggcas
Copy link
Author

hi
you forget import np
should need the code
import numpy as np
then will reproduce the error

@sushreebarsa
Copy link
Collaborator

@jvishnuvardhan Was able to reproduce the issue on colab using TF v2.6 and tf-nightly ,please find the gist here for reference .Thank you!

@ucasiggcas
Copy link
Author

yes
it's the problem
thx

@jvishnuvardhan jvishnuvardhan added the keras-team-review-pending Pending review by a Keras team member. label Sep 30, 2021
@fchollet
Copy link
Member

Hi there, pad_sequences is only for NumPy data, not for TF tensors, hence the error. Prefer using tf.pad https://www.tensorflow.org/api_docs/python/tf/pad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keras-team-review-pending Pending review by a Keras team member. type:bug/performance
Projects
None yet
Development

No branches or pull requests

4 participants