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

As happens quite frequently in our field, the helpful Keras function pad_sequences has been moved.

We make use of this function in several of my courses, including:

Tensorflow 2.0: Deep Learning and Artificial Intelligence

Natural Language Processing with Deep Learning in Python

Deep Learning: Advanced NLP and RNNs


Old way (no longer works):



New way (works):



Interestingly, with Tensorflow (not Keras), the old method still works. So as of now, there’s no need to update your Tensorflow code as long as you are importing directly from Tensorflow:



However, the “new” way also works in Tensorflow…



Yet another alternative is to import from the keras_processing module (no need for separate installation, just install Keras as normal):