AttributeError: module 'keras.preprocessing' has no attribute 'text' - python

When I am trying to import the Tokenizer() function in Keras, I am getting the following error.
I am following official Keras documentation at https://keras.io/preprocessing/text/#tokenizer
import keras
tokenizer = keras.preprocessing.text.Tokenizer()
Getting error like this :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'keras.preprocessing' has no attribute 'text'
Using Keras version '2.1.3'
Where I have done wrong?

You want
from keras.preprocessing import text
tokenizer = text.Tokenizer(...)
The reason is that keras/preprocessing/__init__.py doesn't import nor expose the text submodule

Related

'tensorflow' has no attribute 'config' error

I am trying to run the following lines:
import tensorflow as tf
physical_devices = tf.config.list_physical_devices("GPU")
for i in range(len(physical_devices)):
tf.config.experimental.set_memory_growth(physical_devices[i], True)
I am trying to run this lines too:
import tensorflow as tf
physical_devices = tf.test.gpu_device_name("GPU")
for i in range(len(physical_devices)):
tf.config.experimental.set_memory_growth(physical_devices[i], True)
But the first method errors out like this:
Traceback (most recent call last):
File "main_train.py", line 4, in <module>
physical_devices = tf.config.list_physical_devices("GPU")
AttributeError: module 'tensorflow' has no attribute 'config'
And the second method errors out like this:
Traceback (most recent call last):
File "main_train.py", line 5, in <module>
for i in range(len(tf.test.gpu_device_name("GPU"))):
AttributeError: module 'tensorflow' has no attribute 'test'
I am using Tensorflow 2.4.0 .Any ideas what I'm doing wrong?
If anyone could help that would be great:)
Thank's for every help in advance:)
I read that you already fixed it but since you didn't included it in your post here is the solution everyone else having this error: you need to change some modules from e.g. tf.config.list_physical_devices("GPU") to tf.compat.v1.config.list_physical_devices("GPU"). By adding ".compat.v1" you are ensuring backwards capability. You can use this code snippet for all other modules, that don't work.

AttributeError: module 'tensorflow.keras.mixed_precision' has no attribute 'set_global_policy'

I need to add mixed precision to my code in order to save some memory. Specifically, I have tried adding mixed precision policy near line 27 in https://github.com/nimRobotics/google-research/blob/master/ravens/train.py, below is the code excerpt
import argparse
import datetime
import os
import numpy as np
from ravens import agents
from ravens import Dataset
import tensorflow as tf
# tf.keras.mixed_precision.set_global_policy('mixed_float16')
# OR
# policy = tf.keras.mixed_precision.Policy('mixed_float16')
# mixed_precision.set_global_policy(policy)
Both the methods result in attribute error as shown below, I'm using Google Colab with TF 2.3.0
Using tf.keras.mixed_precision.set_global_policy('mixed_float16') results in
Traceback (most recent call last):
File "train.py", line 28, in <module>
tf.keras.mixed_precision.set_global_policy('mixed_float16')
AttributeError: module 'tensorflow.keras.mixed_precision' has no attribute 'set_global_policy'
Using
policy = tf.keras.mixed_precision.Policy('mixed_float16')
mixed_precision.set_global_policy(policy)
results in
Traceback (most recent call last):
File "train.py", line 29, in <module>
policy = tf.keras.mixed_precision.Policy('mixed_float16')
AttributeError: module 'tensorflow.keras.mixed_precision' has no attribute 'Policy'
Any help or hints will be highly appreciated!
For tf < 2.4 you should use mixed precision's experimental package.
tf.keras.mixed_precision.experimental.Policy(
name, loss_scale='auto'
)
For ex, in tf 2.3
policy = tf.keras.mixed_precision.experimental.Policy('mixed_float16')
tf.keras.mixed_precision.experimental.set_policy(policy)
and in tf 2.4
tf.keras.mixed_precision.set_global_policy('mixed_float16')
From tf 2.4, this feature is no longer experimental.

using tfds for using my custom dataset with tensorflow fails

according to the tutorial at this link I want to create my custom dataset and use it with tensorflow.
I have installed the tfds command and when I entering tfds new my_dataset command, I will encounter to this error :
tfds new my_dataset
Traceback (most recent call last):
File "/root/anaconda3/envs/simclr/bin/tfds", line 5, in <module>
from tensorflow_datasets.scripts.cli.main import launch_cli
File "/root/anaconda3/envs/simclr/lib/python3.7/site-packages/tensorflow_datasets/scripts/cli/main.py", line 39, in <module>
from tensorflow_datasets.scripts.cli import build
File "/root/anaconda3/envs/simclr/lib/python3.7/site-packages/tensorflow_datasets/scripts/cli/build.py", line 275, in <module>
def _search_script_path(ds_to_build: str) -> Optional[tfds.core.ReadOnlyPath]:
AttributeError: module 'tensorflow_datasets.core' has no attribute 'ReadOnlyPath'
I changed the access level to rwx, and again the error raise.
I don't know how to solve it.
this problem solved in tensorflow V2. so use tensorflow V2 or higher versions.

module 'pyomo' has no attribute 'AbstractModel'

I have installed pyomo and trying to create a instance model from AbstractModel as follows:
import pyomo as pmo
absmd = pmo.AbstractModel()
Traceback (most recent call last):
File "", line 1, in
absmd = pmo.AbstractModel()
But i am getting the following error:
AttributeError: module 'pyomo' has no attribute 'AbstractModel'
Any advise?
You're importing the wrong stuff. Either use pmo.environ.AbstractModel() or
from pyomo.environ import *
model = AbstractModel()

Why scipy.io.wavfile.read does not return a tuple?

I am trying to read a *.wav file using scipy. I do the following:
import scipy
x = scipy.io.wavfile.read('/usr/share/sounds/purple/receive.wav')
As a result of this code I get:
Traceback (most recent call last):
File "test3.py", line 2, in <module>
x = scipy.io.wavfile.read('/usr/share/sounds/purple/receive.wav')
AttributeError: 'module' object has no attribute 'io'
Does anybody know what is wrong here? Thank you in advance.
As the error says, scipy module does not have 'io'.
io.wavfile is a submodule, you need to from scipy.io import wavfile and then do wavfile.read("/usr/share/sounds/purple/receive.wav")
This gives me an error with the file you are using as an example, however...

Categories