TensorFlow 2.8 was recently released and I installed it as soon as it was out. I really need it for support of higher NumPy versions and a few new features. However, after installing it in my conda environment with
python3 -m pip install --upgrade tensorflow
neither PyCharm nor VSCode can no longer resolve the import from tensorflow.keras import ....
The imports themselves seem to work at runtime, but because the import cannot be resolved I can't make use of code completion, visualizing signatures of functions and etc. Has anybody encountered a similar issue?
everything was working with TF 2.7 - the version I had before.
Note: I'm using Python 3.8
Vs Code
PyCharm
I tried to check the versions through the PyCharm interpreter tab and this is what I saw. For some reason PyCharm isn't aware that there are versions after 2.0 (I have the latest version of pip installed in that environment). I'm guessing this is related, but not sure what to do with that.
I had the same problem and resolved it by importing it as
from tensorflow.python.keras.layers import Dense
This is a bug in the current version of tensorflow, as discussed in this issue.
You can work around it by either
modifying the file site-packages/tensorflow/__init__.py as described in this answer from the referenced issue or
using import keras.api._v2.keras as keras since this seems to be the exact package tensorflow loads itself. (Though you need to reference the protected member _v2 here, which is against python conventions.)
The reason here is that tensorflow tries to load the keras module in a lazy fashion, which means that it holds only a reference to the module until the module is used. Only then the keras module will be actually loaded. Therefore IDEs only know about the reference tensorflow holds to the keras module and not its content.
I see the problem in Google Colab as well. Although running the code works just fine. It's just an IDE complaint that supposedly it cannot find the imports. Very strange. I hope someone from the TensorFlow team gives feedback soon.
Resolving
import tensorflow
foo = tenstorflow.keras.foo
# if foo is a submodule but not an attribute, this will fail
and
from tensorflow.keras import foo
# if foo is an attribute, this is (roughly) equivalent to
import tensorflow.keras
foo = tenstorflow.keras.foo
# if foo is a submodule but not an attribute, this is (roughly) equivalent to
import tensorflow.keras.foo as foo
are different.
The first one need tensorflow has keras attribute with correct type statically during type checking.
But the second one need tensorflow.__path__ contains keras module statically during type checking.
BTW, for from tensorflow import keras:
If tensorflow has keras attribute, then it uses the attribute, otherwise it import keras as a submodule.
Theoretically, the second one should only work for 2.2.0 <= TF < 2.6.0, which has tensorflow/keras folder. Because tensorflow/keras should be removed in TF 2.6 according to TF 2.6 Release Log, otherwise from tensorflow import keras(tensorflow.keras backed by the keras PIP package) will be different from import tensorflow.keras as keras(tensorflow/keras backed by tensorflow/python/keras).
In fact, however, the second one works for 2.2.0 <= TF < 2.8.0, since tensorflow/keras is not removed until TF 2.8. Interestingly, tensorflow/python/keras is not removed yet (Release 2.9.1), violating the claim in TF 2.6 Release Log that "... will be removed in future release (2.7)".
The first one is broken for TF >= 2.5.0 because of keras lazy loading introduced in TF 2.5, and haven't been fixed yet (Release 2.9.1) though related commits have been merged into master branch.
See
https://github.com/tensorflow/tensorflow/pull/54104
and
https://github.com/tensorflow/tensorflow/commit/e65b68a0914408118995d2f8b55c4286859362f8
See also https://github.com/tensorflow/tensorflow/pull/54104#issuecomment-1067102133
This is happening to me in tensorflow-macos 2.10.0.
This has been a pattern as this post in GitHub shows. I'm getting the same. Ignoring it since the code still runs, but would rather not have the yellow. I hope someone from tensorflow can chime in. :)
This one worked for me:
from keras.utils.np_utils import to_categorical
You can create a symlink in tensorflow directory pointing to keras sources like below:
cd ./virtualenvs/myenv/lib/python3.x/site-packages/tensorflow
ln -s ../keras/api/_v2/keras/ keras
When I run the following code from a tutorial, I keep getting the following error at the end in almost every video I attempt.
Source: https://pythonprogramming.net/k-means-from-scratch-2-machine-learning-tutorial/?completed=/k-means-from-scratch-machine-learning-tutorial/
I get the following error:
from sklearn import preprocessing, cross_validation
ImportError: cannot import name 'cross_validation' from 'sklearn
I did pip installs, changing the way cross_validation is stated based on other suggestions but I still can't solve it.
I could not find cross_validation as a library in sklearn.
You need to use from sklearn.model_selection import cross_validate as per the documentation. This has already been answered here.
I also suggest going through the documentation of the functions you use to gain a better understanding of what you are doing.
I am getting error while importing mean absolute percentage error from sklearn.metrics:
Cannot import name 'mean_absolute_percentage_error' from sklearn.metrics
I am using sklearn version 0.23.1
mean_absolute_percentage_error is new in version 0.24. So you need to update your sklearn version either you can implement it on your own.
Here is the source if you want to implement it yourself.
I was wondering, what's the difference between importing keras from tensorflow using import tensorflow.keras or just pip installing keras alone and importing it using import keras as both seemed to work nicely so far, the only difference I noticed is that i get Using TensorFlow backend. in the command line every time I execute the one using keras.
Tensorflow.keras is an version of Keras API implemented specifically for use with Tensorflow. It is a part of Tensorflow repo and from TF version 2.0 will become main high level API replacing tf.layers and slim.
The only reason to use standalone keras is to maintain framework-agnostic code, i.e. use it with another backend.
I am new to the "scikit-Learn" API and wish to implement a multilabel classification problem. After importing the following packages:
import numpy as np
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.svm import LinearSVC
from sklearn.metrics import classification_report
I get an error which says 'Unresolved import: MultiLabelBinarizer'. But other related packages imported seem to work fine. I wonder why the 'MultiLabelBinarizer' cannot be imported, given the fact that the 'sklearn ' package was properly installed. Any help will be appreciated.
I found out the reason, in case someone comes across the same problem. The error was due to fact that I was running the above code on 'sklearn' version 0.14 (which was installed by default on Ubuntu 14.04 LTS) instead of 0.16. I also think the MultiLabelBinarizer Class is only available on 'sklearn' version 0.16 (I have not tried the 0.15 - in case there is any).