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

Viewed 13762

i'm trying to import these :

from numpy import array
from keras.preprocessing.text import one_hot

from keras.preprocessing.sequence import pad_sequences


from keras.models import Sequential
from keras.layers.core import Activation, Dropout, Dense
from keras.layers import Flatten, LSTM
from keras.layers import GlobalMaxPooling1D
from keras.models import Model

But i'm getting error as cannot import name 'pad_sequences' from 'keras.preprocessing.sequence'

Can anyone help me here please?

5 Answers

Replace:

from keras.preprocessing.sequence import pad_sequences

With:

from keras_preprocessing.sequence import pad_sequences

most likely you are using tf version 2.9 - go back to 2.8 and the same path works

alternatively import it from keras.utils.data_utils import pad_sequences

TF is not so stable with paths - the best way is check their git source corresponding to the version you succeeded to install !! in the case of TF2.9 you can see how it is importedhere

you can use this. It is worked for me.

from tensorflow.keras.preprocessing.sequence import pad_sequences

The correct path to import is keras.io.preprocessing.sequence.pad_sequences. Your path lacks the io.

from keras.io.preprocessing.sequence import pad_sequences

I came across the same problem just now but still don't know what is going on(still waiting for an answer). I gave up importing pad_sequences and write it in full and it works

keras.preprocessing.sequence.pad_sequences()
Related