Assertion Errors while importing Keras modules - python

I installed Keras using Pip and when I am trying to import modules from Keras, it gives me an assertion error on utils and init modules.
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-48-eda57b582878> in <module>()
----> 1 from keras.callbacks import LambdaCallback
C:\ProgramData\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
C:\ProgramData\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
8 # Globally-importable utils.
C:\ProgramData\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
C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\__init__.py in <module>()
34 assert isinstance(_epsilon, float)
35 _backend = _config.get('backend', _BACKEND)
---> 36 assert _backend in {'theano', 'tensorflow', 'cntk'}
37 _image_data_format = _config.get('image_data_format',
38 image_data_format())
AssertionError:
I found a similar question in SOF and checked
python -c "from keras import backend"
in cmd prompt and it gives me the following error.
C:\ProgramData\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\__init__.py", line 6, in <module>
from . import conv_utils
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\conv_utils.py", line 9, in <module>
from .. import backend as K
File "C:\ProgramData\Anaconda3\lib\site-
packages\keras\backend\__init__.py", line 36, in <module>
assert _backend in {'theano', 'tensorflow', 'cntk'}
AssertionError
Keras.json:
{
"image_dim_ordering": "tf",
"backend": "mxnet",
"epsilon": 1e-07,
"floatx": "float32"
}
What should I be changing in these modules to avoid this error?
Thanks in Advance.

Based on your update with the contents of your keras.json file, it appears you have set the backend to "mxnet", but this is not a permitted backend of Keras.
Change this value to one of "tensorflow", "theano", or "cntk" and be sure you have that corresponding package also installed. Save the file and try again.

Related

ModuleNotFoundError: No module named 'numpy.testing'

I keep trying to import sklearn but I when I do I get the error [ModuleNotFoundError: No module named 'numpy.testing']. I have upgraded numpy, pandas, scipy as well as scikit-learn to the latest versions but I still keep getting the same error. Any idea why. Here is the full error below:
'''
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-152-b7c74cbf5af0> in <module>
----> 1 import sklearn
~\anaconda3\lib\site-packages\sklearn\__init__.py in <module>
80 from . import _distributor_init # noqa: F401
81 from . import __check_build # noqa: F401
---> 82 from .base import clone
83 from .utils._show_versions import show_versions
84
~\anaconda3\lib\site-packages\sklearn\base.py in <module>
15 from . import __version__
16 from ._config import get_config
---> 17 from .utils import _IS_32BIT
18 from .utils._tags import (
19 _DEFAULT_TAGS,
~\anaconda3\lib\site-packages\sklearn\utils\__init__.py in <module>
27 from ..exceptions import DataConversionWarning
28 from .deprecation import deprecated
---> 29 from .fixes import parse_version, threadpool_info
30 from ._estimator_html_repr import estimator_html_repr
31 from .validation import (
~\anaconda3\lib\site-packages\sklearn\utils\fixes.py in <module>
17 import numpy as np
18 import scipy
---> 19 import scipy.stats
20 import threadpoolctl
21 from .._config import config_context, get_config
~\anaconda3\lib\site-packages\scipy\stats\__init__.py in <module>
465 from ._warnings_errors import (ConstantInputWarning, NearConstantInputWarning,
466 DegenerateDataWarning, FitError)
--> 467 from ._stats_py import *
468 from ._variation import variation
469 from .distributions import *
~\anaconda3\lib\site-packages\scipy\stats\_stats_py.py in <module>
35 from numpy import array, asarray, ma
36 from numpy.lib import NumpyVersion
---> 37 from numpy.testing import suppress_warnings
38
39 from scipy.spatial.distance import cdist
ModuleNotFoundError: No module named 'numpy.testing'
'''
This cause due to the numpy version you are using. Therefore you have to downgrade your numpy version to 1.16.4. Use following command.
sudo pip uninstall numpy
pip install numpy==1.16.4
Reference :
https://github.com/DeepLabCut/Docker4DeepLabCut2.0/issues/26

What would prevent scikit-learn, and more specifically sklearn.base, from importing into Python?

I cannot import any part of scikit-learn library package in Python. I need sklearn.linear_model.LinearRegression but have tried importing various scikit-learn packages each without success. Has anyone had a similar issues with BaseEstimator, or see a solution to loading scikit-learn? Thanks for the help!
System Parameters
Windows 10
Anaconda 1.9.7
scikit-learn 0.21.3
Tried:
Uninstalling and resintalling scikit-learn with conda
Uninstalling and reinstalling Anaconda distribution
loading the specific item that seems to be issue with from sklearn.base import BaseEstimator
running library in Jupyter Notebook, Microsoft VS Code, and Spyder
Code Entered: from sklearn import linear_model
Error Message:
ImportError Traceback (most recent call last)
<ipython-input-1-b94db01ee140> in <module>
----> 1 from sklearn import linear_model
C:\Util\Anaconda3\lib\site-packages\sklearn\__init__.py in <module>
74 else:
75 from . import __check_build
---> 76 from .base import clone
77 from .utils._show_versions import show_versions
78
C:\Util\Anaconda3\lib\site-packages\sklearn\base.py in <module>
14
15 from . import __version__
---> 16 from .utils import _IS_32BIT
17
18 _DEFAULT_TAGS = {
C:\Util\Anaconda3\lib\site-packages\sklearn\utils\__init__.py in <module>
18 from ..exceptions import DataConversionWarning
19 from .deprecation import deprecated
---> 20 from .validation import (as_float_array,
21 assert_all_finite,
22 check_random_state, column_or_1d, check_array,
C:\Util\Anaconda3\lib\site-packages\sklearn\utils\validation.py in <module>
19 from numpy.core.numeric import ComplexWarning
20
---> 21 from .fixes import _object_dtype_isnan
22 from .. import get_config as _get_config
23 from ..exceptions import NonBLASDotWarning
C:\Util\Anaconda3\lib\site-packages\sklearn\utils\fixes.py in <module>
16 import scipy.sparse as sp
17 import scipy
---> 18 from scipy.sparse.linalg import lsqr as sparse_lsqr # noqa
19
20
C:\Util\Anaconda3\lib\site-packages\scipy\sparse\linalg\__init__.py in <module>
114 from .dsolve import *
115 from .interface import *
--> 116 from .eigen import *
117 from .matfuncs import *
118 from ._onenormest import *
C:\Util\Anaconda3\lib\site-packages\scipy\sparse\linalg\eigen\__init__.py in <module>
9 from __future__ import division, print_function, absolute_import
10
---> 11 from .arpack import *
12 from .lobpcg import *
13
C:\Util\Anaconda3\lib\site-packages\scipy\sparse\linalg\eigen\arpack\__init__.py in <module>
----> 1 from sklearn.base import BaseEstimator
ImportError: cannot import name 'BaseEstimator' from 'sklearn.base' (C:\Util\Anaconda3\lib\site-packages\sklearn\base.py)

unable to import pandas in jupyter

When I import pandas in jupyter library the following is generated.
AttributeError Traceback (most recent call last)
<ipython-input-24-f869c0f42cc0> in <module>
----> 1 import pandas as pd
2 import numpy as np
3 get_ipython().run_line_magic('matplotlib', 'inline')
C:\ProgramData\Anaconda3\lib\site-packages\pandas\__init__.py in <module>
24
25 try:
---> 26 from pandas._libs import (hashtable as _hashtable,
27 lib as _lib,
28 tslib as _tslib)
pandas\_libs\hashtable_class_helper.pxi in init pandas._libs.hashtable()
AttributeError: type object 'pandas._libs.hashtable.HashTable' has no attribute '__reduce_cython__'
Can you help me in figuring out the problem? Thanks
Use pd at the place of pandas in your code. If you still get error, then write your code also in the question.

Why the attribute error occurs when importing tensorflow_hub?

from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds
I'm following tensorflow but when I try to import tensorflow_hub, error occurs like
Traceback (most recent call last):
File "C:/PythonProjects/191217_TensorFlow_Project/model3.py", line 7, in <module>
import tensorflow_hub as hub
File "C:\Users\UserK\Anaconda3\envs\191217_TensorFlow_Project_1\lib\site-packages\tensorflow_hub\__init__.py", line 53, in <module>
from tensorflow_hub.keras_layer import KerasLayer
File "C:\Users\UserK\Anaconda3\envs\191217_TensorFlow_Project_1\lib\site-packages\tensorflow_hub\keras_layer.py", line 39, in <module>
class KerasLayer(tf.keras.layers.Layer):
AttributeError: 'function' object has no attribute 'layers'
I don't have any file named 'tensorflow_hub.py', so i don't know why it happens.

Trying to understand the PYTHONPATH

I am trying to understand PYTHONPATH related to my project.
My project is in the dir $HOME/Programs/medusa-2.0, and my source files are in $HOME/Programs/medusa-2.0/medusa.
I have set my PYTHONPATH in the .bashrc like this:
export MEDUSA_HOME=$HOME/Programs/medusa-2.0
export PYTHONPATH=${MEDUSA_HOME}/medusa:${PYTHONPATH}
When I try to import a class, from system import error_handler, hello, I get errors saying that it cannot find the function execute_command. I don' t understand why I get this error? Is it because I am doing a loop cycle in the imports because execute_command is in medusasettings?
ImportError Traceback (most recent call last)
<ipython-input-2-7f959e81c735> in <module>()
----> 1 from medusasystem import error_handler, hello
/home/ubuntu/Programs/medusa-2.0/medusa/medusasystem.py in <module>()
9 from local import lcat
10 import psutil
---> 11 import ranking
12 import settings
13 import simplejson as json
/home/ubuntu/Programs/medusa-2.0/medusa/ranking.py in <module>()
7 import cache
8 from decors import make_verbose
----> 9 from scheduler.predictionranking import get_prediction_metrics
10 from scheduler.randomranking import get_random_metrics
11 from settings import medusa_settings
/home/ubuntu/Programs/medusa-2.0/medusa/scheduler/predictionranking.py in <module>()
6
7 from celery import task
----> 8 import hdfs
9 from networkdaemon import read_network_data
10 from numpylinearregression import estimate_job_execution, calculate_linear_regression_numpy
/home/ubuntu/Programs/medusa-2.0/medusa/hdfs.py in <module>()
4 from hadoopy._hdfs import _checked_hadoop_fs_command
5 from celery import task
----> 6 from medusasystem import execute_command
7 import settings
8
ImportError: cannot import name execute_command
I have tried to launch a python file with python -v, and I've got this error:
# /home/ubuntu/Programs/medusa-2.0/medusa/hdfs.pyc matches /home/ubuntu/Programs/medusa-2.0/medusa/hdfs.py
import hdfs # precompiled from /home/ubuntu/Programs/medusa-2.0/medusa/hdfs.pyc
Traceback (most recent call last):
File "tests/testHello.py", line 3, in <module>
from medusasystem import error_handler, hello
File "/home/ubuntu/Programs/medusa-2.0/medusa/medusasystem.py", line 11, in <module>
import ranking
File "/home/ubuntu/Programs/medusa-2.0/medusa/ranking.py", line 9, in <module>
from scheduler.predictionranking import get_prediction_metrics
File "/home/ubuntu/Programs/medusa-2.0/medusa/scheduler/predictionranking.py", line 8, in <module>
import hdfs
File "/home/ubuntu/Programs/medusa-2.0/medusa/hdfs.py", line 6, in <module>
from medusasystem import execute_command
ImportError: cannot import name execute_command
If I launch my virtualenv for my project, shouldn't the PYTHONPATH be defined inside the virtualenv?
With the information presented, I think you are just importing it from the wrong place: "I don't understand why I get this error? Is it because I am doing a loop cycle in the imports because execute_command is in medusasettings?" but in the trace(s) there are from settings import medusa_settings and from medusasystem import execute_command. Verify that execute_command is in medusasystem.
virtualenv is not going to change PYTHONPATH, so it will be the same after activation (unless of course, you do something like what is in the next sentence). If your question is about setting it when using the virtualenv, see this answer: How do you set your pythonpath in an already-created virtualenv?. By doing it in .bashrc, you are defining it when you open the shell rather than attaching it to the virtualenv script(s).
The problom was with circular import issue. I replaced:
from medusasystem import execute_command
with
import medusasystem
execute_command = medusasystem.execute_command
and it worked.

Categories