How to import LSTM in Keras, Tensorflow - python

When try to import the LSTM layer I encounter the following error:
from keras.layers.recurrent import LSTM
No module named 'LSTM'
So, I tried to download this module from website and another problem is the file type is .tar I don't know how to install it.

Use this one:
from tensorflow.keras.layers import LSTM

try
from tensorflow.python.keras._impl.keras.layers.recurrent import LSTM

this seems to work in my project (with tensorflow 2):
from tensorflow.python.keras.layers.recurrent import LSTM

Related

Alternative to 'tensorflow.contrib' with tensorflow=2.0.0

I am using TensorFlow version=2.0.0, python version=3.7.9 and I was trying to import the following:
import tensorflow.contrib.layers as layers
I get this error:
Exception has occurred: ModuleNotFoundError
No module named 'tensorflow.contrib'
As I read in this thread, tensorflow.contrib doesn't exist in 2.0. Does anyone know an alternative to it? I am trying to upgrade my code from 1.0 to 2.0 as I don't want to use tf 1.0. I am using this package to build up a RNN cell.
tf.contrib is deprecated in Tensorflow 2.x.
Replace import tensorflow.contrib.layers as layers
with
from tensorflow.keras.layers import RNN

"Cannot find reference" when import from Keras

Following this example to start https://machinelearningmastery.com/tutorial-first-neural-network-python-keras/
# first neural network with keras tutorial
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
Installed numpy and keras, numpy is ok, but there are red lines under "Sequential" and "Dense".
Here's the Error messages:
Cannot find reference 'Sequential' in 'models.py'
Cannot find reference 'Dense' in 'init.py'
Wonder how I can fix this? I had a look here but think it might be a different problem?
Also, on a completely different note, I can not install tensorflow for some reason? ...
As of October 2020 Tensorflow only supports the 64-bit version of Python and Tensorflow only supports Python 3.5 to 3.8.
Update your project in PyCharm to run Python 64-bit and this should solve your problem.

YOLOv3 ModuleNotFoundError: No module named 'tensorflow.python.keras.engine.base_layer_v1'

I need to train a model using YOLO. I am using the Kaggle kernel and I am training custom images.
When I execute the train module, I get this error.
ModuleNotFoundError: No module named 'tensorflow.python.keras.engine.base_layer_v1'
preinstalled is Tensorflow 2.+. I read somewhere that Yolov3 does not work with TensorFlow 2.+ so I installed Tensor 1.14.
I also tried using ImageAi and got exactly the same error.
Does anyone understand this basekayer_v1? Is it the tensorflow version that give the error?
Thanks
I found the solution
Well, it looks that I am doing things wrong or more difficult since no one encounter this error. I found the solution.
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
to:
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras import backend as K
If you have the same problem.
YOLO files are outdated. They are said that they are not compatible with Tensorflow 1.14 but for me it worked. I needed to fork the git and edit the model.py. As shown above the important thing is add tensorflow.keras
I also needed to edit Change K.controlflowops.whileloop to tf.whileloop in the code or will give you an error.
I used Kaggle kernel. Using Tensorflow 2.0.2
It took me 4 days to solve this, now it is finally training. I hope it helps.

Can't import tensorflow.keras in VS Code

I'm running into problems using tensorflow 2 in VS Code. The code executes without a problem, the errors are just related to pylint in VS Code.
For example this import from tensorflow.keras.layers import Dense gives a warning "Unable to import 'tensorflow.keras.layers'pylint(import-error)". Importing tensorflow and using tf.keras.layers.Dense does not produce an error. I'm just using a global python environment (3.7.2) on Windows 10, tensorflow is installed via Pip.
The imports that were causing the issue for me:
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense
The way I resolved it:
from tensorflow import keras
from keras.models import Model
from keras.layers import Dense
My way to work with that: The problem is related to the custom import system in tf2 (see this issue). A work around for this is possible (Windows, Linux) which basically tricks VS Code to directly import tensorflow_core and don't use the custom lazy loader. If you just want to remove the red lines (as this is only an editor-problem), use
"python.linting.pylintArgs":
["--ignored-modules=tensorflow.keras"]
I solved it by pressing ctrl+shift+P in Visual Studio Code, searching Python: Select Interpreter, and choosing the main environment.
More detailed information can be found here.
I too faced the same issue. I solved it by installing keras as a new package and then I changed all packages name removing the prefix tensorflow.. So in your case after installing keras you should replace tensorflow.keras.layers with keras.layers
Instead of importing from tensorflow.keras import everything from "keras" solely. From one of the tensorflow versions until now there is a common error.
For instance:
Instead of
From tensorflow.keras import layers, models
Write this:
From keras import layers,models
Its just better to use pycharm instead of vscode. This issue does not exist in pycharm. However, if you are insistent on using vscode, then the import statements have to be changed as follows.
from tensorflow.python.keras import Sequential
from tensorflow.python.keras.layers import Dense

Module import works differently for two ways of importing tensorflow

I'm currently trying to teach myself tensorflow. The new version has keras built in.
I can access the Dense function in the following way
import tensorflow as tf
tf.keras.layers.Dense
but this does not work:
from tensorflow.keras.layers import Dense
Why is that? I notice that:
from tensorflow.python.keras.layers import Dense
Does work. When I import tensorflow, does it know to intelligently add the .python to the module name?
In the GitHub Repo for Tensorflow, if you look at the two __init__.py files inside tensorflow-master/tensorflow/python/keras/ and tensorflow-master/tensorflow/python/keras/layers/, you can see which modules are imported as part of the package structure. This determines what and how you as a user import things when using the package and its modules.
David Beazley has a really good talk on the innerworkings of this:
https://www.youtube.com/watch?v=0oTh1CXRaQ0

Categories