Cannot import name 'TextVectorization' - google-colaboratory

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?

I faced the same issue with conda.
The solution can be found here
you should import TextVectorization from tensorflow.keras.layers.experimental.preprocessing instead

Related

How to import Model and other related classes in Tensorflow version 2.11 or keras version 2.11?

I am trying to import the following using latest version of tensorflow and keras i.e., 2.11:
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate, Conv2DTranspose, BatchNormalization, \
Dropout, Lambda
It was working really fine in the previous version I had. It just automatically upgraded and now, I am facing problems. Can somebody help me?
It is not enabled in the latest version or particularly, I am getting the following error:
Traceback (most recent call last):
File "C:\Users\Marco\PycharmProjects\pythonProject\venv\U_NET.py", line 3, in
from keras.models import Model
File "C:\Users\Marco\PycharmProjects\pythonProject\venv\lib\site-packages\keras_init_.py", line 21, in
from keras import models
File "C:\Users\Marco\PycharmProjects\pythonProject\venv\lib\site-packages\keras\models_init_.py", line 18, in
from keras.engine.functional import Functional
File "C:\Users\Marco\PycharmProjects\pythonProject\venv\lib\site-packages\keras\engine\functional.py", line 24, in
import tensorflow.compat.v2 as tf
ModuleNotFoundError: No module named 'tensorflow.compat'

Module not found using Input function from tensorflow.keras.layers

Im quite new learning machine learning and my first project is the creation of a Neural Network in order to detect key facial points on google colab. Everything has been working ok but today when I wanted to train my neural network I came accross with an error that has never appeared before when I trained my neural network.
The error is:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-189-47fd3efd0229> in <module>()
5
6
----> 7 X_input = Input(input_shape)
8
9 # Zero-padding
4 frames
/usr/lib/python3.7/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)
ModuleNotFoundError: No module named 'keras.engine.base_layer_v1'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
I don't understand the line ModuleNotFoundError: No module named 'keras.engine.base_layer_v1' because the line that is not working is when I'm using Input from tensorflow.keras.layers.
I really don't know what is going on because I never got this error before. I've seen that it could be the version of TensorFlow or maybe my libraries.
I am using 2.3.0 versions in TensorFlow and Keras and these are the libraries I am importing:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications import DenseNet121
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.initializers import glorot_uniform
from tensorflow.keras.utils import plot_model
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, LearningRateScheduler
from IPython.display import display
from tensorflow.python.keras import *
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers, optimizers
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.layers import *
from tensorflow.keras import backend as K
from keras import optimizers
I would really appreciate any help :)
Re-installing tensorflow and keras works for me

ModuleNotFoundError: No module named 'tf' But the TensorFlow v2.3 is already installed

I'm trying to import some regularizers, with the following code:
import tensorflow as tf
from tensorflow import keras
import numpy as np
# importing regularizers
from tf.keras.regularizers import l2, activity_l2
The problem is that I get the following error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8996/2415362791.py in <module>
4 from tensorflow import keras
5 # importing regularisers
----> 6 from tf.keras.regularizers import l2, activity_l2
ModuleNotFoundError: No module named 'tf'
However, I've already installed TensorFlow, and I can build neural networks, with several layers, without any error showing up...
I'm using Anaconda v2.1, Jupyter Notebooks v6.4.3.
Why is this happening?
You can use tensorflow.keras.regularizers instead of tf.keras.regularizers as below:
from tensorflow.keras.regularizers import l2 (to import specific regularizers)
or
from tensorflow.keras import regularizers (to use and import all other regularizers.)
activity_l2 is deleted and is equal to l2. Find the related github reference here.

ImportError: cannot import name 'to_categorical' from 'keras.utils' (/usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py)

I have just got below error from an import that use to work fine few hours ago going downward.
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-33-4c52b67c20bf> in <module>()
1 import keras
----> 2 from keras.utils import to_categorical
ImportError: cannot import name 'to_categorical' from 'keras.utils' (/usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py)
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
Is Keras busy with Update to have this method deprecated?
use this
from tensorflow.keras.utils import to_categorical
instead of
from keras.utils import to_categorical

How do I resolve 'GraphKeys' Attribute Error while importing Tensorflow?

I am trying to run the following codes:
import os
import pprint
import numpy as np
import tensorflow as tf
import tensorlayer as tl
from tensorlayer.layers import *
from tensorlayer.prepro import *
from random import shuffle
However, I am getting this error:
~\Documents\Jupyter Notebooks far Run\cGAN at Lockdown\Re-implement CycleGAN in
Tensorlayer=GD\CycleGAN_Tensorlayer-master\tensorlayer\layers.py in <module>
31 TF_GRAPHKEYS_VARIABLES = tf.GraphKeys.GLOBAL_VARIABLES
32 except: # For TF11 and before
---> 33 TF_GRAPHKEYS_VARIABLES = tf.GraphKeys.VARIABLES
34
35 ## Variable Operation
AttributeError: module 'tensorflow' has no attribute 'GraphKeys'
The version of my tf is 2.0.0-beta1 while I have installed TensorLayer 2.2.1
Kindly let me know where I am getting it wrong.