Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import name 'TextVectorization'

I can successfully run the Keras mnist example. I now would like to run the "English-to-Spanish translation with a sequence-to-sequence Transformer" example found here. I'm running TensorFlow 2.5.0 https://colab.research.google.com/github/keras-team/keras-io/blob/master/examples/nlp/ipynb/neural_machine_translation_with_transformer.ipynb#scrollTo=HPyHRZvOO1G1

When I try to import TextVectorization I get the following error:

>>> import pathlib
>>> import random
>>> import string
>>> import re
>>> import numpy as np
>>> import tensorflow as tf
2021-09-01 11:45:11.253159: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] 
Successfully opened dynamic library libcudart.so.11.0
>>> from tensorflow import keras
>>> from tensorflow.keras import layers
>>> from tensorflow.keras.layers import TextVectorization
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'TextVectorization' from 'tensorflow.keras.layers' 
(/home/users/stren/anaconda3/lib/python3.8/site-packages/tensorflow/keras/layers/__init__.py)
>>> tf.__version__
'2.5.0'

Any ideas what I'm doing wrong?

like image 385
Strenski Avatar asked Apr 12 '26 21:04

Strenski


1 Answers

I faced the same issue with conda.

The solution can be found here

you should import TextVectorization from tensorflow.keras.layers.experimental.preprocessing instead

like image 116
GnohChengYi Avatar answered Apr 18 '26 09:04

GnohChengYi