why import sklearn is not working in Jupyter? - scikit-learn

I installed the recent version of Anaconda but "Import sklearn" gives an error
ImportError Traceback (most recent call last)
<ipython-input-5-b7c74cbf5af0> in <module>
----> 1 import sklearn
~\anaconda3\lib\site-packages\sklearn\__init__.py in <module>
62 else:
63 from . import __check_build
---> 64 from .base import clone
65 from .utils._show_versions import show_versions
66
~\anaconda3\lib\site-packages\sklearn\base.py in <module>
11 from scipy import sparse
12 from .externals import six
---> 13 from .utils.fixes import signature
14 from . import __version__
15
~\anaconda3\lib\site-packages\sklearn\utils\__init__.py in <module>
14 from . import _joblib
15 from ..exceptions import DataConversionWarning
---> 16 from .fixes import _Sequence as Sequence
17 from .deprecation import deprecated
18 from .validation import (as_float_array,
~\anaconda3\lib\site-packages\sklearn\utils\fixes.py in <module>
90 from ._scipy_sparse_lsqr_backport import lsqr as sparse_lsqr
91 else:
---> 92 from scipy.sparse.linalg import lsqr as sparse_lsqr # noqa
93
94
~\anaconda3\lib\site-packages\scipy\sparse\linalg\__init__.py in <module>
115 from .dsolve import *
116 from .interface import *
--> 117 from .eigen import *
118 from .matfuncs import *
119 from ._onenormest import *
~\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
~\anaconda3\lib\site-packages\scipy\sparse\linalg\eigen\arpack\__init__.py in <module>
20 from __future__ import division, print_function, absolute_import
21
---> 22 from .arpack import *
~\anaconda3\lib\site-packages\scipy\sparse\linalg\eigen\arpack\arpack.py in <module>
43 __all__ = ['eigs', 'eigsh', 'svds', 'ArpackError', 'ArpackNoConvergence']
44
---> 45 from . import _arpack
46 import numpy as np
47 import warnings
ImportError: DLL load failed: The specified procedure could not be found.

I found out what the problem was. I had created an env file for Jupyter and edited it to open jupyter in Chrome. After updating python, import sklearn did not work. So I had to go back and delete that env file and everything worked fine after that. Hope this helps future readers of this post.

Related

No module named keras.api Mask R CNN

I am in the demo file of the Mask R CNN repo from matterport. Trying to run the first cell but encountering the following error. I have keras 2.3.0 installed. Running Python 3.8. The below is a trace of the error from the model.py file inside the Mask R CNN repo, which came with the clone. Thanks!
The repo referred to is here: https://github.com/matterport/Mask_RCNN.git
Thank you all for your kind support.
ModuleNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_3220/983756133.py in <module>
7 import matplotlib
8 import matplotlib.pyplot as plt
----> 9 import keras
10
11 # Root directory of the project
~\OneDrive\New Project\myenv1\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
~\OneDrive\New Project\myenv1\lib\site-packages\keras\utils\__init__.py in <module>
24 from .layer_utils import get_source_inputs
25 from .layer_utils import print_summary
---> 26 from .vis_utils import model_to_dot
27 from .vis_utils import plot_model
28 from .np_utils import to_categorical
~\OneDrive\New Project\myenv1\lib\site-packages\keras\utils\vis_utils.py in <module>
5
6 import os
----> 7 from ..models import Model
8 from ..layers.wrappers import Wrapper
9
~\OneDrive\New Project\myenv1\lib\site-packages\keras\models.py in <module>
10 from .engine.input_layer import Input
11 from .engine.input_layer import InputLayer
---> 12 from .engine.training import Model
13 from .engine.sequential import Sequential
14 from .engine.saving import save_model
~\OneDrive\New Project\myenv1\lib\site-packages\keras\engine\__init__.py in <module>
6 from .base_layer import Layer
7 from .network import get_source_inputs
----> 8 from .training import Model
~\OneDrive\New Project\myenv1\lib\site-packages\keras\engine\training.py in <module>
12 from .network import Network
13 from .base_layer import Layer
---> 14 from . import training_utils
15 from . import training_arrays
16 from . import training_generator
~\OneDrive\New Project\myenv1\lib\site-packages\keras\engine\training_utils.py in <module>
15 from .. import backend as K
16 from .. import losses
---> 17 from .. import metrics as metrics_module
18 from ..utils import Sequence
19 from ..utils import generic_utils
~\OneDrive\New Project\myenv1\lib\site-packages\keras\metrics.py in <module>
1848 import tensorflow as tf
1849 if tf.__version__ >= '2.0.0':
-> 1850 BaseMeanIoU = tf.keras.metrics.MeanIoU
1851
1852
~\OneDrive\New Project\myenv1\lib\site-packages\tensorflow\python\util\lazy_loader.py in __getattr__(self, item)
60
61 def __getattr__(self, item):
---> 62 module = self._load()
63 return getattr(module, item)
64
~\OneDrive\New Project\myenv1\lib\site-packages\tensorflow\python\util\lazy_loader.py in _load(self)
43 """Load the module and insert it into the parent's globals."""
44 # Import the target module and insert it into the parent's namespace
---> 45 module = importlib.import_module(self.__name__)
46 self._parent_module_globals[self._local_name] = module
47
~\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py in import_module(name, package)
125 break
126 level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)
128
129
ModuleNotFoundError: No module named 'keras.api'
I read the repo that you have referred. You maybe try run your code with environment below:
Python 3.4, TensorFlow 1.3, Keras 2.0.8

"Module 'tensorflow.python.distribute.values' has no attribute 'AutoPolicy'" from importing Tensorflow Hub

I've been trying to run this TensorFlow tutorial on my computer, but while running the following code I've been getting the error from the title:
import os
import shutil
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text as text
from official.nlp import optimization # to create AdamW optmizer
import matplotlib.pyplot as plt
tf.get_logger().setLevel('ERROR')
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
This is the entire error traceback. While running it in the tutorial's Collab notebook there doesn't seem any problem whatsoever.
AttributeErrorTraceback (most recent call last)
<ipython-input-7-3afaa91eeb1f> in <module>
3
4 import tensorflow as tf
----> 5 import tensorflow_hub as hub
6 import tensorflow_text as text
7 from official.nlp import optimization # to create AdamW optmizer
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/__init__.py in <module>
86
87
---> 88 from tensorflow_hub.estimator import LatestModuleExporter
89 from tensorflow_hub.estimator import register_module_for_export
90 from tensorflow_hub.feature_column import image_embedding_column
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/estimator.py in <module>
60
61
---> 62 class LatestModuleExporter(tf.compat.v1.estimator.Exporter):
63 """Regularly exports registered modules into timestamped directories.
64
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/lazy_loader.py in __getattr__(self, item)
60
61 def __getattr__(self, item):
---> 62 module = self._load()
63 return getattr(module, item)
64
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/lazy_loader.py in _load(self)
43 """Load the module and insert it into the parent's globals."""
44 # Import the target module and insert it into the parent's namespace
---> 45 module = importlib.import_module(self.__name__)
46 self._parent_module_globals[self._local_name] = module
47
/usr/lib/python3.6/importlib/__init__.py in import_module(name, package)
124 break
125 level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)
127
128
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/api/_v1/estimator/__init__.py in <module>
8 import sys as _sys
9
---> 10 from tensorflow_estimator.python.estimator.api._v1.estimator import experimental
11 from tensorflow_estimator.python.estimator.api._v1.estimator import export
12 from tensorflow_estimator.python.estimator.api._v1.estimator import inputs
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/api/_v1/estimator/experimental/__init__.py in <module>
8 import sys as _sys
9
---> 10 from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder
11 from tensorflow_estimator.python.estimator.canned.kmeans import KMeansClustering as KMeans
12 from tensorflow_estimator.python.estimator.canned.linear import LinearSDCA
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/canned/dnn.py in <module>
29 from tensorflow.python.keras.utils import losses_utils
30 from tensorflow.python.util.tf_export import estimator_export
---> 31 from tensorflow_estimator.python.estimator import estimator
32 from tensorflow_estimator.python.estimator.canned import head as head_lib
33 from tensorflow_estimator.python.estimator.canned import optimizers
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in <module>
50 from tensorflow.python.util.tf_export import estimator_export
51 from tensorflow_estimator.python.estimator import model_fn as model_fn_lib
---> 52 from tensorflow_estimator.python.estimator import run_config
53 from tensorflow_estimator.python.estimator import util as estimator_util
54 from tensorflow_estimator.python.estimator.export import export_lib
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/run_config.py in <module>
28 from tensorflow.core.protobuf import rewriter_config_pb2
29 from tensorflow.python.distribute import estimator_training as distribute_coordinator_training
---> 30 from tensorflow.python.distribute import parameter_server_strategy_v2
31 from tensorflow.python.util import compat_internal
32 from tensorflow.python.util import function_utils
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/parameter_server_strategy_v2.py in <module>
26
27 from tensorflow.python.distribute import distribute_lib
---> 28 from tensorflow.python.distribute import distribute_utils
29 from tensorflow.python.distribute import parameter_server_strategy
30 from tensorflow.python.distribute import sharded_variable
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_utils.py in <module>
372 # (synchronization=ON_READ, aggregation=NONE,SUM,MEAN,ONLY_FIRST_REPLICA)
373 VARIABLE_POLICY_MAPPING = {
--> 374 vs.VariableSynchronization.AUTO: values_lib.AutoPolicy,
375 vs.VariableSynchronization.ON_WRITE: values_lib.OnWritePolicy,
376 vs.VariableSynchronization.ON_READ: values_lib.OnReadPolicy,
AttributeError: module 'tensorflow.python.distribute.values' has no attribute 'AutoPolicy'
Is there any particular reason as to why is this happening?
Thank you in advance.
My guess is this could be related to a version mismatch between the versions you're using for tensorflow and tensorflow_hub.
It looks like AutoPolicy was removed from TF in a recent commit, so if you built tensorflow from source and included this commit, then that could be the issue. If you want to build TF from source, checkout the latest official release before building (2.4.1).

Why do I have problems with TensorFlow in Jupyter?

I have been fine with my Jupyter notebook for running my python codes for some time. However, I recently picked interests in Style Transfer with deep learning. The codes I got for practice required that I downgrade TensorFlow to a lower version. Fortunately, this worked but unfortunately, all other codes that I have developed for months (requiring TensorFlow backend) stopped working. I tried using JupyterLab and sometimes, they worked while most times, I get the same error just as in Jupyter notebook. Now, the only get around is to move my codes to Spyder (which is quite frustrating).
Every time I run my codes, I get the following error:
AttributeError Traceback (most recent call last)
<ipython-input-2-6d5587dfcc0c> in <module>
1 # import all libraries
----> 2 import keras
3 from keras.models import Sequential
4 from keras.layers import Dense, Activation, Dropout
5 from keras.layers.convolutional import Conv1D
~\Miniconda3\envs\tensorflow\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
~\Miniconda3\envs\tensorflow\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.
~\Miniconda3\envs\tensorflow\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
~\Miniconda3\envs\tensorflow\lib\site-packages\keras\backend\__init__.py in <module>
87 elif _BACKEND == 'tensorflow':
88 sys.stderr.write('Using TensorFlow backend.\n')
---> 89 from .tensorflow_backend import *
90 else:
91 # Try and load external backend.
~\Miniconda3\envs\tensorflow\lib\site-packages\keras\backend\tensorflow_backend.py in <module>
3 from __future__ import print_function
4
----> 5 import tensorflow as tf
6 from tensorflow.python.framework import ops as tf_ops
7 from tensorflow.python.training import moving_averages
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\__init__.py in <module>
22
23 # pylint: disable=g-bad-import-order
---> 24 from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
25
26 try:
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\__init__.py in <module>
86 # Bring in subpackages.
87 from tensorflow.python import data
---> 88 from tensorflow.python import keras
89 from tensorflow.python.feature_column import feature_column_lib as feature_column
90 from tensorflow.python.layers import layers
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\__init__.py in <module>
23
24 from tensorflow.python.keras import activations
---> 25 from tensorflow.python.keras import applications
26 from tensorflow.python.keras import backend
27 from tensorflow.python.keras import callbacks
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\applications\__init__.py in
<module>
23
24 from tensorflow.python.keras import backend
---> 25 from tensorflow.python.keras import engine
26 from tensorflow.python.keras import layers
27 from tensorflow.python.keras import models
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\__init__.py in
<module>
21 # TODO(fchollet): Remove hourglass imports once external code is done importing
22 # non-public APIs.
---> 23 from tensorflow.python.keras.engine.base_layer import InputSpec
24 from tensorflow.python.keras.engine.base_layer import Layer
25 from tensorflow.python.keras.engine.input_layer import Input
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in
<module>
37 from tensorflow.python.keras import initializers
38 from tensorflow.python.keras import regularizers
---> 39 from tensorflow.python.keras.utils import generic_utils
40 from tensorflow.python.keras.utils import tf_utils
41 # A module that only depends on `keras.layers` import these from here.
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\utils\__init__.py in <module>
30 from tensorflow.python.keras.utils.generic_utils import Progbar
31 from tensorflow.python.keras.utils.generic_utils import serialize_keras_object
---> 32 from tensorflow.python.keras.utils.io_utils import HDF5Matrix
33 from tensorflow.python.keras.utils.layer_utils import convert_all_kernels_in_model
34 from tensorflow.python.keras.utils.layer_utils import get_source_inputs
~\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\utils\io_utils.py in <module>
27
28 try:
---> 29 import h5py
30 except ImportError:
31 h5py = None
~\Miniconda3\envs\tensorflow\lib\site-packages\h5py\__init__.py in <module>
32 raise
33
---> 34 from . import version
35
36 if version.hdf5_version_tuple != version.hdf5_built_version_tuple:
~\Miniconda3\envs\tensorflow\lib\site-packages\h5py\version.py in <module>
15
16 from collections import namedtuple
---> 17 from . import h5 as _h5
18 import sys
19 import numpy
h5py\h5.pyx in init h5py.h5()
AttributeError: type object 'h5py.h5.H5PYConfig' has no attribute '__reduce_cython__'
How do I rectify this situation?
Many thanks in anticipation of your suggestions.

Problem in importing Autograph in Tensorflow

I have installed Tensorflow (1.13.0) using conda as follows:
conda install -c anaconda tensorflow
on my Win64 machine with python 3.6 in my conda environment. I am able to import tensorflow from my Jupyter notebook. In my project, I need to use AutoGraph. However, when I try to import autograph, then I get this error - ImportError: cannot import name 'deprecated_endpoints'
I am able to verify that autograph folder exists in my tensorflow package "..\py36\Lib\site-packages\tensorflow\python\autograph". However, the below code statement gives that Import Error.
import tensorflow.python.autograph as ag
Complete error trace:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-13-9224a57db065> in <module>
1 import tensorflow as tf
----> 2 import tensorflow.python.autograph as ag
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site-packages\tensorflow\python\autograph\__init__.py in <module>
33
34 # TODO(mdan): Bring only the relevant symbols to the top level.
---> 35 from tensorflow.python.autograph import operators
36 from tensorflow.python.autograph import utils
37 from tensorflow.python.autograph.core.converter import ConversionOptions
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site-packages\tensorflow\python\autograph\operators\__init__.py in <module>
38 from __future__ import print_function
39
---> 40 from tensorflow.python.autograph.operators.control_flow import for_stmt
41 from tensorflow.python.autograph.operators.control_flow import if_stmt
42 from tensorflow.python.autograph.operators.control_flow import while_stmt
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site-packages\tensorflow\python\autograph\operators\control_flow.py in <module>
19 from __future__ import print_function
20
---> 21 from tensorflow.python.autograph.operators import py_builtins
22 from tensorflow.python.data.ops import dataset_ops
23 from tensorflow.python.framework import tensor_util
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site-packages\tensorflow\python\autograph\operators\py_builtins.py in <module>
24 import six
25
---> 26 from tensorflow.python.autograph.utils import py_func
27 from tensorflow.python.autograph.utils import tensors
28 from tensorflow.python.framework import constant_op
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site-packages\tensorflow\python\autograph\utils\__init__.py in <module>
22 from tensorflow.python.autograph.utils.misc import alias_tensors
23 from tensorflow.python.autograph.utils.py_func import wrap_py_func
---> 24 from tensorflow.python.autograph.utils.tensor_list import dynamic_list_append
25 from tensorflow.python.autograph.utils.testing import fake_tf
26 from tensorflow.python.autograph.utils.type_check import is_tensor
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site-packages\tensorflow\python\autograph\utils\tensor_list.py in <module>
20
21 from tensorflow.python.framework import ops
---> 22 from tensorflow.python.ops import list_ops
23 from tensorflow.python.ops import tensor_array_ops
24
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\list_ops.py in <module>
24 from tensorflow.python.framework import tensor_shape
25 from tensorflow.python.ops import array_ops
---> 26 from tensorflow.python.ops import gen_list_ops
27 # go/tf-wildcard-import
28 # pylint: disable=wildcard-import
~\AppData\Local\Continuum\anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\gen_list_ops.py in <module>
21 from tensorflow.python.framework import ops as _ops
22 from tensorflow.python.framework import op_def_library as _op_def_library
---> 23 from tensorflow.python.util.deprecation import deprecated_endpoints
24 from tensorflow.python.util import dispatch as _dispatch
25 from tensorflow.python.util.tf_export import tf_export
ImportError: cannot import name 'deprecated_endpoints'
---------------------------------------------------------------------------
How should I fix it?

ImportError: DLL load failed: The specified procedure could not be found. (import sklearn)

When I import sklearn, it throws this ImportError(DLL load failed: The specified procedure could not be found.)
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-b7c74cbf5af0> in <module>()
----> 1 import sklearn
~\Anaconda3\lib\site-packages\sklearn\__init__.py in <module>()
62 else:
63 from . import __check_build
---> 64 from .base import clone
65 from .utils._show_versions import show_versions
66
~\Anaconda3\lib\site-packages\sklearn\base.py in <module>()
11 from scipy import sparse
12 from .externals import six
---> 13 from .utils.fixes import signature
14 from . import __version__
15
~\Anaconda3\lib\site-packages\sklearn\utils\__init__.py in <module>()
11
12 from .murmurhash import murmurhash3_32
---> 13 from .validation import (as_float_array,
14 assert_all_finite,
15 check_random_state, column_or_1d, check_array,
~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in <module>()
20
21 from ..externals import six
---> 22 from ..utils.fixes import signature
23 from .. import get_config as _get_config
24 from ..exceptions import NonBLASDotWarning
~\Anaconda3\lib\site-packages\sklearn\utils\fixes.py in <module>()
81 return stats.boxcox(x, lmbda)
82 else:
---> 83 from scipy.special import boxcox # noqa
84
85
~\Anaconda3\lib\site-packages\scipy\special\__init__.py in <module>()
638 from .sf_error import SpecialFunctionWarning, SpecialFunctionError
639
--> 640 from ._ufuncs import *
641
642 from .basic import *
ImportError: DLL load failed: The specified procedure could not be found.
If you already have a working installation of numpy and scipy, then do this:
pip install -U scikit-learn
otherwise:
conda install scikit-learn
finally check for updates:
conda update pip

Resources