After installing the newest Keras and TF in a virtual env on a Win10 machine I keep having an assertion error
AssertionError Traceback (most recent call last)
<ipython-input-6-88d96843a926> in <module>()
----> 1 import keras
~\AppData\Local\Continuum\Anaconda3\envs\aind-vui\lib\site-packages\keras\__init__.py in <module>()
1 from __future__ import absolute_import
2
----> 3 from . import activations
4 from . import applications
5 from . import backend
~\AppData\Local\Continuum\Anaconda3\envs\aind-vui\lib\site-packages\keras\activations.py in <module>()
2 import six
3 import warnings
----> 4 from . import backend as K
5 from .utils.generic_utils import deserialize_keras_object
6 from .engine import Layer
~\AppData\Local\Continuum\Anaconda3\envs\aind-vui\lib\site-packages\keras\backend\__init__.py in <module>()
69 if 'KERAS_BACKEND' in os.environ:
70 _backend = os.environ['KERAS_BACKEND']
---> 71 assert _backend in {'theano', 'tensorflow', 'cntk'}
72 _BACKEND = _backend
73
AssertionError:
tensorflow itself imports fine. I also have keras working perfectly fine in my main working env and the keras.json is properly pointing to tensorflow.
pip list shows that Keras is installed in the env.
python -c "from keras import backend"
Using TensorFlow backend.
shows the proper message.
frustratingly
$ python
>>> import keras
>>> quit()
works, but doing the same thing in a jupyternotebook does not
Do
$ export KERAS_BACKEND=tensorflow
and re-run your program.
Apparently you defined it, but chose something outside of those 3 values.
Related
Python 3.9.12.
Windows 10.
jupyterlab 3.3.2.
Import tensorflow
When I try to import Tensorflow, I get the following 'tensorflow.python.eager.polymorphic_function' error.
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In [44], line 1
----> 1 import tensorflow
File ~\OD13\TFODCourse\tfod13\lib\site-packages\tensorflow\__init__.py:45
42 from tensorflow.python import tf2 as _tf2
43 _tf2.enable()
---> 45 from ._api.v2 import __internal__
46 from ._api.v2 import __operators__
47 from ._api.v2 import audio
File ~\OD13\TFODCourse\tfod13\lib\site-packages\tensorflow\_api\v2\__internal__\__init__.py:14
12 from . import eager_context
13 from . import feature_column
---> 14 from . import function
15 from . import graph_util
16 from . import mixed_precision
File ~\OD13\TFODCourse\tfod13\lib\site-packages\tensorflow\_api\v2\__internal__\function\__init__.py:8
3 """Public API for tf.__internal__.function namespace.
4 """
6 import sys as _sys
----> 8 from tensorflow.python.eager.polymorphic_function.polymorphic_function import Function
9 from tensorflow.python.eager.polymorphic_function.quarantine import defun_with_attributes
ModuleNotFoundError: No module named 'tensorflow.python.eager.polymorphic_function'
My workflow is based on this tutorial: https://www.youtube.com/watch?v=yqkISICHH-U
I found the following answer, but I'm not understanding how to implement the TFLite Authoring Tool to solve this problem:
https://stackoverflow.com/questions/74177865/tensorflow-python-eager-polymorphic-function-no-module-error-on-imports
To answer my own question:
I created a conda environment and installed an older version of Python (3.7) in it and that seems to have fixed the problem.
I found these links to be helpful:
How to downgrade the Python Version from 3.8 to 3.7 on windows?
conda install downgrade python version
Jupyter Notebook - Cannot Connect to Kernel
How to find the version of jupyter notebook from within the notebook
pip command to downgrade jupyter notebook
How to check python anaconda version installed on Windows 10 PC?
https://towardsdatascience.com/get-your-conda-environment-to-show-in-jupyter-notebooks-the-easy-way-17010b76e874
I am trying to run the code
import keras
And I am getting this stack trace.
I have tried reinstalling keras and tensorflow but nothing in working.
Here is the stack trace.
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-10-88d96843a926> in <module>
----> 1 import keras
~\Anaconda3\lib\site-packages\keras\__init__.py in <module>
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications
~\Anaconda3\lib\site-packages\keras\utils\__init__.py in <module>
4 from . import data_utils
5 from . import io_utils
----> 6 from . import conv_utils
7 from . import losses_utils
8 from . import metrics_utils
~\Anaconda3\lib\site-packages\keras\utils\conv_utils.py in <module>
7 from six.moves import range
8 import numpy as np
----> 9 from .. import backend as K
10
11
~\Anaconda3\lib\site-packages\keras\backend\__init__.py in <module>
----> 1 from .load_backend import epsilon
2 from .load_backend import set_epsilon
3 from .load_backend import floatx
4 from .load_backend import set_floatx
5 from .load_backend import cast_to_floatx
~\Anaconda3\lib\site-packages\keras\backend\load_backend.py in <module>
88 elif _BACKEND == 'tensorflow':
89 sys.stderr.write('Using TensorFlow backend.\n')
---> 90 from .tensorflow_backend import *
91 else:
92 # Try and load external backend.
ModuleNotFoundError: No module named 'keras.backend.tensorflow_backend'
Try:
pip install tensorflow==2.2.0
and then
pip install Keras==2.2.0
This worked for me with Python 3.7.
instead of use something like
from keras.backend.tensorflow_backend import set_session
Try to use it like
from keras.backend import set_session
In Tensorflow 2.0.0+ versions you should just put "compat.v1" after tf and dont use "tensorflow_backend" name. Like this:
tf.keras.backend.tensorflow_backend.set_session() -> tf.compat.v1.keras.backend.set_session()
I tried to use anaconda or pip to install tensorflow and keras, and each method met the same problem.
At last I found the problem is because the version of tensorflow or keras. When I install tensorflow==2.2 and keras==2.4.3(latest), no matter which tools I used I will meet this problem.When I install tensorflow==1.14 and keras==2.2, the code works well.
My python version is 3.5.2 under ubuntu 16.04
Just install tensorflow 2.1.0 or 2.2.0 It already has Keras inside. Dont mix using pip and conda. Carry on with what you have started.
pip install tensorflow==2.2.0
or,
conda install tensorflow==2.2.0
Uninstall Keras and reinstall the version 2.2.0 in your system, it will definately work with Tensorflow 2.2. Then you won't have to downgrade you tensorflow ie. less pain of changing codes ;)
pip uninstall keras
pip install Keras==2.2.0
For my case, I had Python 3.7(latest bug fix)
for tensorflow==2.4.1 this works:
from tensorflow.python.keras.backend import set_session
In my case, it was solved by installing a given specific version of Keras.
pip install Keras==2.2.4
I have recently installed imblearn package in jupyter using
!pip show imbalanced-learn
But I am not able to import this package.
from tensorflow.keras import backend
from imblearn.over_sampling import SMOTE
I get the following error
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-20-f19c5a0e54af> in <module>
1 # from sklearn.utils import resample
2 from tensorflow.keras import backend
----> 3 from imblearn.over_sampling import SMOTE
4
5
~/.virtualenvs/p3/lib/python3.6/site-packages/imblearn/__init__.py in <module>
32 Module which allowing to create pipeline with scikit-learn estimators.
33 """
---> 34 from . import combine
35 from . import ensemble
36 from . import exceptions
~/.virtualenvs/p3/lib/python3.6/site-packages/imblearn/combine/__init__.py in <module>
3 """
4
----> 5 from ._smote_enn import SMOTEENN
6 from ._smote_tomek import SMOTETomek
7
~/.virtualenvs/p3/lib/python3.6/site-packages/imblearn/combine/_smote_enn.py in <module>
8 from sklearn.utils import check_X_y
9
---> 10 from ..base import BaseSampler
11 from ..over_sampling import SMOTE
12 from ..over_sampling.base import BaseOverSampler
~/.virtualenvs/p3/lib/python3.6/site-packages/imblearn/base.py in <module>
14 from sklearn.utils.multiclass import check_classification_targets
15
---> 16 from .utils import check_sampling_strategy, check_target_type
17
18
~/.virtualenvs/p3/lib/python3.6/site-packages/imblearn/utils/__init__.py in <module>
5 from ._docstring import Substitution
6
----> 7 from ._validation import check_neighbors_object
8 from ._validation import check_target_type
9 from ._validation import check_sampling_strategy
~/.virtualenvs/p3/lib/python3.6/site-packages/imblearn/utils/_validation.py in <module>
11
12 from sklearn.base import clone
---> 13 from sklearn.neighbors._base import KNeighborsMixin
14 from sklearn.neighbors import NearestNeighbors
15 from sklearn.utils.multiclass import type_of_target
ModuleNotFoundError: No module named 'sklearn.neighbors._base'
Other packages in the environment
numpy==1.16.2
pandas==0.24.2
paramiko==2.1.1
matplotlib==2.2.4
scikit-learn==0.22.1
Keras==2.2.4
tensorflow==1.12.0
tensorboard==1.12.0
tensorflow-hub==0.4.0
xlrd==1.2.0
flask==1.0.2
wtforms==2.2.1
bs4==0.0.1
gensim==3.8.1
spacy==2.2.3
nltk==3.4.5
wordcloud==1.6.0
pymongo==3.10.1
imbalanced-learn==0.6.1
I checked the sklearn package, it contains base module, not _base. But modifying it may not be the right solution. Any other solution to fix this issue.
If in case you want to persist with the latest version of scikit-learn, add the following code to your script or execute the following code in your environment before installing imblearn
import sklearn.neighbors._base
sys.modules['sklearn.neighbors.base'] = sklearn.neighbors._base
This has to be after
pip install sklearn
or in a notebook environment:
!pip install sklearn
This problem stems from the fact that certain modules are named with an underscore in the newer scikit-learn releases
Previous sklearn.neighbors.base has been renamed to sklearn.neighbors._base in version 0.22.1.
You have probably a version of scikit-learn older than that.
Installing the latest release solves the problem:
pip install -U scikit-learn
or
pip install scikit-learn==0.22.1
I had a similar problem trying to import SMOTE from imblearn.over_sampling and my version of scikit-learn was up to date (0.24.1). What worked for me was:
First I downgraded my scikit learn version to 0.22.1 using
pip install scikit-learn==0.22.1
Next, I updated the imbalanced-learn package using:
pip install -U imbalanced-learn
That uninstalled scikit-learn-0.22.1, installed the updated version (scikit-learn-0.24.1), and updated the imbalanced-learn package. Everything worked fine thereafter.
If it is in a particular env, you must copy the _base file or base file to the env from package file.
I had the same problem in my tensorflow env. I just copied _base and base file to my tensorflow env and worked.
I am trying to import SMOTE in my jupyter notebook.I tried the following steps;
I first installed imblearn using the following command in my terminal
conda install -c glemaitre imbalanced-learn
Then i used the following command to import imblearn in my notebook;
from imblearn import under_sampling, over_sampling
I am getting the following error;
<ipython-input-36-d0524665b8f2> in <module>()
----> 1 from imblearn import under_sampling, over_sampling
~/anaconda3/lib/python3.6/site-packages/imblearn/under_sampling/__init__.py in <module>()
4 """
5
----> 6 from .prototype_generation import ClusterCentroids
7
8 from .prototype_selection import RandomUnderSampler
~/anaconda3/lib/python3.6/site-packages/imblearn/under_sampling/prototype_generation/__init__.py in <module>()
4 """
5
----> 6 from .cluster_centroids import ClusterCentroids
7
8 __all__ = [
~/anaconda3/lib/python3.6/site-packages/imblearn/under_sampling/prototype_generation/cluster_centroids.py in <module>()
12 from scipy import sparse
13
---> 14 from sklearn.cluster import KMeans
15 from sklearn.neighbors import NearestNeighbors
16 from sklearn.utils import safe_indexing
~/anaconda3/lib/python3.6/site-packages/sklearn/cluster/__init__.py in <module>()
4 """
5
----> 6 from .spectral import spectral_clustering, SpectralClustering
7 from .mean_shift_ import (mean_shift, MeanShift,
8 estimate_bandwidth, get_bin_seeds)
~/anaconda3/lib/python3.6/site-packages/sklearn/cluster/spectral.py in <module>()
15 from ..metrics.pairwise import pairwise_kernels
16 from ..neighbors import kneighbors_graph
---> 17 from ..manifold import spectral_embedding
18 from .k_means_ import k_means
19
~/anaconda3/lib/python3.6/site-packages/sklearn/manifold/__init__.py in <module>()
4
5 from .locally_linear import locally_linear_embedding, LocallyLinearEmbedding
----> 6 from .isomap import Isomap
7 from .mds import MDS, smacof
8 from .spectral_embedding_ import SpectralEmbedding, spectral_embedding
~/anaconda3/lib/python3.6/site-packages/sklearn/manifold/isomap.py in <module>()
9 from ..utils import check_array
10 from ..utils.graph import graph_shortest_path
---> 11 from ..decomposition import KernelPCA
12 from ..preprocessing import KernelCenterer
13
~/anaconda3/lib/python3.6/site-packages/sklearn/decomposition/__init__.py in <module>()
9 from .incremental_pca import IncrementalPCA
10 from .kernel_pca import KernelPCA
---> 11 from .sparse_pca import SparsePCA, MiniBatchSparsePCA
12 from .truncated_svd import TruncatedSVD
13 from .fastica_ import FastICA, fastica
~/anaconda3/lib/python3.6/site-packages/sklearn/decomposition/sparse_pca.py in <module>()
9 from ..utils import check_random_state, check_array
10 from ..utils.validation import check_is_fitted
---> 11 from ..linear_model import ridge_regression
12 from ..base import BaseEstimator, TransformerMixin
13 from .dict_learning import dict_learning, dict_learning_online
~/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/__init__.py in <module>()
10 # complete documentation.
11
---> 12 from .base import LinearRegression
13
14 from .bayes import BayesianRidge, ARDRegression
~/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/base.py in <module>()
25
26 from ..externals import six
---> 27 from ..utils import Parallel, delayed
28 from ..base import BaseEstimator, ClassifierMixin, RegressorMixin
29 from ..utils import check_array, check_X_y
ImportError: cannot import name 'Parallel
Can anyone please guide me?
Thanks!
So it worked after I installed SMOTE using the following steps;
pip install -U imbalanced-learn
conda install -c conda-forge imbalanced-learn
Looks like I was installing it incorrectly.
I am not able to understand the errors in the previous installation.Would appreciate if someone could point those out to me.
Thanks!
Thanks, I have the same issue, but I ran the following command in terminal or command prompt to fix the issue.
pip install -U imbalanced-learn
after its successfull execution i ran following conda command
conda install -c conda-forge imbalanced-learn
that's how i fixed my problem
Adding these comments as an answer at the suggestion of joanis:
A convenient way to install packages to the same environment backing a Jupyter notebook is to use the modern magic commands for pip and conda in cells in the notebook. The magic commands %pip install <package name> and %conda install <package name> were added to make sure the installs done inside a Jupyter notebook get placed in the actual environment that backs the notebook, see here for more information. The use of the exclamation point alone in front of pip or conda wasn't capable of insuring installation to the proper backing environment, and this often lead to issues of users not understanding why they couldn't import properly after they thought they had installed following the advice of others.
(The % symbol will work in front of pip and conda for other commands besides installs, and so it is just easiest to think of best practice for the use of pip and conda inside Jupyter notebooks now as %pip ... or %conda ..., where ... represents the rest of the command.)
Because in most modern Jupyter systems automagics are enabled by default, you can actually leave off the symbol in front of pip or conda and get the modern magic commands used behind-the-scenes as well. Although it is often best to use the explicit % symbol so that you and others are more aware of what is happening.
I am trying to import skflow on my Windows PC. I have already install and used Anaconda on Python (3.5). I have no trouble to use tensorflow but when I want to use skflow I get the error:
ImportError Traceback (most recent call last)
<ipython-input-1-04faecc7c0de> in <module>()
8 import tensorflow as tf
9 from tensorflow.contrib import learn
---> 10 from tensorflow.contrib import skflow
ImportError: cannot import name 'skflow'
Does anyone know how to fix this?
Thanks in advance!
Please try this:
>>> import tensorflow as tf
>>> from tensorflow.contrib import learn as skflow
FYI, skflow is not any special package/module/... It's an alias for tensorflow.contrib.learn which is in 'sklearn' style.