I was trying to run this code, first time using Tensorflow API in Python. Donwloaded latest version of tensorflow and pip through terminal. However, when I attempt to import from keras subpackage, an error is returned
from keras.models import Model
from keras.layers import Dense
from keras.layers import Input
ImportError: cannot import name 'pywrap_tensorflow' from partially initialized module 'tensorflow.python' (most likely due to a circular import) (/Users/macbook/Library/Python/3.9/lib/python/site-packages/tensorflow/python/__init__.py) ```
Related
I am trying to do several imports from the keras library. I am doing this on a Jupyter notebook using an Anaconda installed Python. I have used keras before fine, and I just checked for updates but found none. Here are the import statements.
from keras.preprocessing.text import Tokenizer
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import SimpleRNN
from keras.layers import Embedding
I get a warning when I run this that says 'Using TensorFlow backend. Then I get a long error output that ends with 'NoneType' object has no attribute 'message_types_by_name'. Any ideas on what is wrong?
Since Tensorflow supports Keras as High level API, suggested way to import Keras libraries/modules is as below.
import tensorflow as tf
from tf.keras.preprocessing.text import Tokenizer
from tf.keras.utils import to_categorical
from tf.keras.models import Sequential
from tf.keras.layers import Dense
from tf.keras.layers import SimpleRNN
from tf.keras.layers import Embedding
Check the document from Keras here for reference.
I'm trying my hand at replicating what this dude did on his github and trying to run some of his scripts (stock price prediction allegedly). I keep bumping into this error no matter what I do. I have a feeling I didn't set up tensorflow or keras properly.
File "/home/mihai/.local/lib/python3.8/site-packages/keras/api/_v2/keras/utils/__init__.py", line 38, in <module>
from keras.utils.vis_utils import plot_model
ImportError: cannot import name 'plot_model' from partially initialized module 'keras.utils.vis_utils' (most likely due to a circular import) (/home/mihai/.local/lib/python3.8/site-packages/keras/utils/vis_utils.py)
Would appreciate any input!
you can look here -> This worked taking from TensorFlow documentation
try is also
from keras.utils.vis_utils import plot_model
or
from tensorflow.keras.utils import plot_model
I have tried several things, and I am experiencing many difficulties in trying to install keras to Python.
First, I tried by simply trying to import it into my Jupyter Notebook using the following:
from keras.models import Sequential
from keras.layers import Dense
which resulted in the following error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-17-b9e2c8277ae4> in <module>
----> 1 from keras.models import Sequential
2 from keras.layers import Dense
ModuleNotFoundError: No module named 'keras'
Then, I tried importing tensorflow.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout,Activation, Flatten, Conv2D, MaxPooling2D
This didn't work either, for I received a similar error as before.
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-18-9d4e366b5092> in <module>
----> 1 from tensorflow.keras.models import Sequential #used for model building
2 from tensorflow.keras.layers import Dense,Dropout,Activation, Flatten, Conv2D, MaxPooling2D #used for model building
ModuleNotFoundError: No module named 'tensorflow'
I finally tried to use the terminal through Anaconda. This failed as well.
UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:
I have Python 3.8, but apparently keras requires python[version='>=2.7,<2.8.0a0|>=3.5,<3.6.0a0|>=3.6,<3.7.0a0']. What can I do to import keras?
For whatever reason, keras and tensorflow don't work well with Python 3.8. I would try reverting to Python 3.7.
I'm having problem with tensorflow. I want to use ImageDataGenerator, but I'm receiving error ModuleNotFoundError: No module named 'tf'. Not sure what is the problem. I added this tf.version to test will it work, and it shows the version of tensorflow.
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
from tf.keras.preprocessing.image import ImageDataGenerator
When I run this code, I get this:
2.1.0
Traceback (most recent call last):
File "q:/TF/Kamen papir maaze/rks.py", line 14, in <module>
from tf.keras.preprocessing.image import ImageDataGenerator
ModuleNotFoundError: No module named 'tf'
The line
import tensorflow as tf
means you are importing tensorflow with an alias as tf to call it modules/functions.
You cannot use the alias to import other modules.
For your case, if you call directly
tf.keras.preprocessing.image.ImageDataGenerator(...)
then it will work.
or
you need to import the module with the right module name. i.e.
from tensorflow.keras.preprocessing.image import ImageDataGenerator
This works, tested on kaggle with tf_v2.6 and tf_v2.7
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
...
in Tensorflow 2.0+, to use keras instead of tf use tensorflow always-
import tensorflow
from tensorflow.keras.preprocessing.image import ImageDataGenerator
I am trying to set up Keras with the following code:
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
import lstm, time #helper libraries
Unfortunately it gives me two error message and I can't get my head around it:
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
return f(*args, **kwds)
and
ModuleNotFoundError Traceback (most recent call last)
ModuleNotFoundError: No module named 'lstm'
Thanks in advance,
Victor
Use one of the following two:
pip install seq2seq-lstm
or
pip install phased-lstm-keras
For me, the following solved the issue:
from keras.layers import LSTM