Why can't I use preprocessing module in Keras?

Question:

I’m trying to use the function pad_sequences() but the same error keeps rising: ‘AttributeError: ‘module’ object has no attribute ‘sequence”

I have followed Keras documentation and I can’t figure out why It does not work. Here is the line of code:

from keras import preprocessing
import keras

X_test = sequence.pad_sequences(X_test, maxlen=500)
X_test = preprocessing.sequence.pad_sequences(X_test, maxlen=500)   
X_test = keras.preprocessing.sequence.pad_sequences(X_test, maxlen=500)

None of the above lines seem to work.

Asked By: h3h325

||

Answers:

In the first line please use

X_test = preprocessing.sequence.pad_sequences(X_test, maxlen=500)

You can simply import pad_sequences like so instead

from keras.preprocessing.sequence import pad_sequences

and replace preprocessing.sequence.pad_sequences with just pad_sequences

Answered By: Kashyap

in tf version 2.9 it is under keras.utils

from keras.utils import pad_sequences

always search the function in TF git repo and locate the right path.

Answered By: Areza

Try replacing X_test = preprocessing.sequence.pad_sequences(X_test, maxlen=500) with
X_test = keras.utils.pad_sequences(X_test, maxlen=500)
and using
(from keras.utils import pad_sequences)
keras–v=2.10.2

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.