How to Fix ImportError: cannot import name ‘pad_sequences’ from ‘keras.preprocessing.sequence’

cannot import name 'pad_sequences' from 'keras.preprocessing.sequence'

ImportError: cannot import name ‘pad_sequences’ from ‘keras.preprocessing.sequence’ error occurs because the “pad_sequences function has been moved from the keras.preprocessing.sequence module to the keras.utils module.

How to fix it?

To fix the ImportError: cannot import name ‘pad_sequences’ from ‘keras.preprocessing.sequence’ error, import “pad_sequences” from “keras.utils” module.

from keras.utils import pad_sequences

You can also import pad_sequences from the keras_preprocessing.sequence module, but this is deprecated and will be removed in future versions of Keras.

The keras package is bundled in tensorflow starting version “2.0.0.”

Here is an example of how to use the pad_sequences function:

from keras.utils import pad_sequences

data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

padded_sequences = pad_sequences(data, maxlen=4)

print(padded_sequences)

Output

cannot import name 'pad_sequences' from 'keras.preprocessing.sequence'

As you can see, the pad_sequences() function has padded the sequences to the length of 4, using 0 as the padding value.

I hope this will fix the error you are having right now!

Related posts

AttributeError: module ‘keras.preprocessing.image’ has no attribute ‘load_img’

AttributeError: module ‘keras.utils’ has no attribute ‘get_file’

AttributeError: module ‘keras.utils’ has no attribute ‘get_value’

AttributeError: module ‘keras.utils’ has no attribute ‘to_categorical’

AttributeError: module ‘keras.utils’ has no attribute ‘plot_model’

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.