No module named 'tensorflow.python._impl - python

I want to run code for try neural network with python but
No module named 'tensorflow.python._impl
part of my code:
import tensorflow as tf
from tensorflow import keras
from tensorflow.python._impl import activations
I did all solutions that some sites said such as
- Uninstall and install package like tensorflow,..
also:
- Upgrade tensorflow
- Upgrade keras
but it doesn't solve yet. I have this error on co lab too.
please help me if you have any solution
thanks

from keras.layers.recurrent import ...

Official TensorFlow Support discourages use of tf.python.* as it is private and intended for development purposes only. While it may work in some cases, it will "break unannounced" in many others.
Instead, try importing tensorflow with the .python portion removed, e.g.:
from tensorflow._impl import activations

Related

"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.

ModuleNotFoundError: No module named 'tensorflow.contrib' with tensorflow=2.0.0

I am using TensorFlow version=2.0.0
python version=3.7.3
I am trying to import the below statement
from tensorflow.contrib import rnn
And it gives error as
Module 'tensorflow' has no attribute 'contrib'
How can I resolve this?
from tensor flow
https://www.tensorflow.org/guide/upgrade#compatibility_modules
Because of TensorFlow 2.x module deprecations (for example, tf.flags and tf.contrib), some changes can not be worked around by switching to compat.v1. Upgrading this code may require using an additional library (for example, absl.flags) or switching to a package in tensorflow/addons.
and as describe in this thread
tensorflow.contrib doesn't exist in 2.0.
https://github.com/tensorflow/tensorflow/issues/31350#issuecomment-518749548
I haven't used older versions of tensorflow. Is this what you're looking for?
from tensorflow.keras.layers import RNN
Info on contrib:
https://www.tensorflow.org/guide/migrate#a_note_on_slim_contriblayers

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

Importing Tensorflow's Dataset class

I am running Ubuntu 16.04, and have installed tensorflow through pip, using pip3 install tensorflow. I now want to import and use the Dataset class, which is part of the contrib.data package. In various tutorials, this has simply been done using:
import tensorflow as tf
from tf.contrib.data import Dataset
But this prints out:
ImportError: No module named 'tf'
I have also tried using this without the renaming of tensorflow:
import tensorflow
from tensorflow.contrib.data import Dataset
But this prints out:
ImportError: cannot import name 'Dataset'
If I navigate to /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/data, then there are two directories (__pycache__ and python), and two files (_dataset_ops.so, __init__.py). But I'm not sure where the Dataset class should be defined here, if at all...
So what should I do differently to enable me to import Dataset?
data was moved out of contrib several Tensorflow versions ago. As far as I know a recent update completely removed most "standard" ops from tf.contrib.data. It now only contains "experimental"/volatile code. Simply use tf.data.Dataset instead.

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