I have a bunch of code written using Keras that was installed as a separate pip install and the import statements are written like from keras.models import Sequential, etc..
On a new machine, I have Tensorflow installed which now includes Keras inside the contrib directory. In order to keep the versions consistent I thought it would be best to use what's in contrib instead of installing Keras separately, however this causes some import issues.
I can import Keras using import tensorflow.contrib.keras as keras but doing something like from tensorflow.contrib.keras.models import Sequential gives ImportError: No module named models, and from keras.models import Sequential gives a similar ImportError: No module named keras.models.
Is there a simple method to get the from x.y import z statements to work? If not it means changing all the instances to use the verbose naming (ie.. m1 = keras.models.Sequential()) which isn't my preferred syntax but is do-able.
Try this with recent versions of tensorflow:
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import LSTM, TimeDistributed, Dense, ...
Try with tensorflow.contrib.keras.python.keras:
from tensorflow.contrib.keras.python.keras.models import Sequential
Unfortunately from tensorflow.contrib.keras.python.keras.models import Sequential no longer works. It looks like they are changing the interface as of version 1.4 (currently at RC0). There is a note saying the tensorflow.contrib.keras interface is deprecated and you should use tensorflow.keras but this doesn't work either without python in the line.
The following did work for me under V1.4rc0
from tensorflow.python.keras.models import Sequential
import tensorflow.python.keras
import tensorflow.contrib.keras as keras
The following did not...
import tensorflow.python.keras as keras
Hopefully this gets cleaned up a bit more before final release.
Related
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) ```
I have a functioning program on a Jupyter notebook. I downloaded it as a .py file but, for some reason, I get errors while making the imports that, in jupyter, didn't cause any problems.
The imports are:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.losses import MeanSquaredError
from tensorflow.keras.metrics import RootMeanSquaredError
from tensorflow.keras.optimizers import Adam
and I get the errors:
Import "tensorflow.keras.<name>" could not be resolved
It's so weird since the only difference is the file itself (from .ipynb to .py), it's the same environment and even the same folder.
I have tensorflow version: 2.8.0
And python version: 3.10.4
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.
This is the error it gives me when I try to run it:
Traceback (most recent call last):
File "/Users/kids/Library/Mail/V5/E60CBF1C-9021-4A10-8D60-06C96C141AF1/Outbox.mbox/E7C72E99-E3DB-4CDC-B1C9-15116F3478D8/Data/Attachments/405/2/ASL-Finger-Spelling-Recognition-master/main.py", line 10, in <module>
from keras.layers.convolutional import Convolution3D, MaxPooling3D
This is the code error which it is showing:
from keras.layers.convolutional import Convolution3D, MaxPooling3D
Keras version is 2.3.1. Python version is Python 3.8. Running on MacOS.
Your import statement
from keras.layers.convolutional import Convolution3D, MaxPooling3D
attempts to import MaxPooling3D from keras.layers.convolutional. MaxPooling3D is actually part of the pooling module, i.e. keras.layers.pooling.
According to the keras source and docs, the best way to import these two classes in a single statement would be
from keras.layers import Convolution3D, MaxPooling3D
Alternatively, you could import them separately:
from keras.layers.convolutional import Convolution3D
from keras.layers.pooling import MaxPooling3D
but in general, you should pay attention to what is purposefully exposed via the package; that is, the classes accessible from keras.layers.
I was trying to run a machine learning code using Tensorflow/Keras in Anaconda 3. I initially had a problem HERE, but I downgraded Keras to 2.1.6, and that error is resolved. Now I get the following error:
raise ImportError('You need to first `import keras` '
ImportError: You need to first `import keras` in order to use `keras_applications`. For instance, you can do:
```
import keras
from keras_applications import vgg16
```
Or, preferably, this equivalent formulation:
```
from keras import applications
```
And here is my import list:
import glob, cv2, pickle, re
import numpy as np
from collections import defaultdict
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import PCA
from keras.models import load_model, Model
from keras_applications.mobilenet import relu6
from layers import SRU, Attention, ShuffleImages
import tensorflow as tf
How can I resolve the issue?
This a problem related to the backend choosed for your keras. Try verify if the backend in the keras configuration file is the same installed in your enviromment. To do so:
nano ~/.keras/keras.json
The file should be similar to :
{
"image_data_format": "channels_last",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
In this case, tensorflow must be installed correctly in your computer. If want to change the backend to another one ("theano", "tensorflow", or "cntk") just change in the keras.json file.