cannot import name 'pad_sequences' from 'keras.preprocessing.sequence'
Asked Answered
U

8

54

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?

Umbrian answered 21/5, 2022 at 1:28 Comment(0)
P
82

Replace:

from keras.preprocessing.sequence import pad_sequences

With:

from keras_preprocessing.sequence import pad_sequences
Philipps answered 22/5, 2022 at 15:32 Comment(2)
I'm curious why this worked. Could you provide more info about it?Mercado
they restructured the modulePhilipps
D
53

According to the TensorFlow v2.10.0 doc, the correct path to pad_sequences is tf.keras.utils.pad_sequences. So in your script one should write:

from keras.utils import pad_sequences

It has resolved the problem for me.

Discriminator answered 18/10, 2022 at 9:21 Comment(1)
Thanks! This helped me as well. Any reason why this worked but not other patterns? Again module restructuring?Warwick
P
21

you can use this. It is worked for me.

from tensorflow.keras.preprocessing.sequence import pad_sequences
Philbert answered 3/6, 2022 at 12:33 Comment(0)
L
7

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

alternatively import it as:

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 imported here

Loraine answered 21/5, 2022 at 21:31 Comment(0)
N
2

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()
Nedry answered 21/5, 2022 at 17:0 Comment(0)
C
1

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
Cecilia answered 21/5, 2022 at 1:45 Comment(0)
S
0
from tensorflow.keras.preprocessing import sequence

worked for me

Saprophagous answered 23/12, 2022 at 8:35 Comment(0)
I
0

Try this code:

from keras.utils import pad_sequences
Interwork answered 15/1 at 4:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.