1
import os

os.environ["KERAS_BACKEND"] = "tensorflow"

import pandas as pd
import pathlib
import random
import string
import re
import numpy as np
import tensorflow as tf
import keras
from keras import layers

MAX_SEQUENCE_LENGTH = 40
INP_VOCAB_SIZE = 15000

input_vectorization = keras.layers.TextVectorization(
    max_tokens=INP_VOCAB_SIZE,
    output_mode="int",
    output_sequence_length=MAX_SEQUENCE_LENGTH
)

AttributeError: module 'keras.layers' has no attribute 'TextVectorization'

I import the keras module and get layers from keras, but when I try to use keras.layers.TextVectorization, my code throws that error.

I've tried writing the call differently but then I get a new AttributeError.

input_vectorization = tf.keras.TextVectorization(
    #same inside as before
)

AttributeError: module 'keras' has no attribute '__version__'

I've also tried reinstalling both tensorflow and keras using pip, but that didn't change anything. What should I do?

Keras version: 3.1.1

Tensorflow version: 2.16.1

4
  • replace tf.keras to keras. Commented Mar 20, 2024 at 8:19
  • @Innat thank you for your reply! When I change it to input_vectorization = keras.layers.TextVectorization(), I get the error module 'keras.layers' has no attribute 'TextVectorization' Commented Mar 20, 2024 at 8:35
  • You are using keras 3. So, first check if this layer is removed or updated it namespace. Commented Mar 20, 2024 at 10:02
  • @Innat I looked in the Keras 3 documentation, and it's still there. It says I access it by typing keras.layers.TextVectorization(), but that hasn't worked. Is there anywhere else I can check? Commented Mar 20, 2024 at 21:25

2 Answers 2

1

After tensorflow 2.0 you should access keras using tf.keras whenever you work with tensorflow and Textvectorization as per the docs exists there - https://www.tensorflow.org/api_docs/python/tf/keras/layers/TextVectorization

so all you should need to do is change your call to tf.keras.layers.TextVectorization

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response! I changed it to what you said, but I'm getting the error AttributeError: module 'keras' has no attribute '__version__'
@LeilaSaxby can you edit your question with the versions of keras and tf that you are running ?
Sorry, I added them!
0

I figured out the issue. There was something wrong with the virtual environment I was using in vs code. Creating a new enviroment with conda and reinstalling tensorflow and keras locally had everything working smoothly again.

Thanks for everyone's help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.