How do you run tensorflow on colaboratory? - python-3.x

I'm trying to use Google's Colaboratory. The first line of output was hopeful.
Using TensorFlow backend.
But then I got this error message.
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory
That error apparently triggered another error. (Sorry for the long stack trace, but the error message itself suggested including it when asking for help.)
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last)
<ipython-input-6-0b846698ae4a> in <module>()
2 import numpy as np
3 import matplotlib.pylab as plt
----> 4 from keras.models import Sequential
5 from keras.layers import Dense, InputLayer
6
/usr/local/lib/python3.6/dist-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
/usr/local/lib/python3.6/dist-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.
/usr/local/lib/python3.6/dist-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
/usr/local/lib/python3.6/dist-packages/keras/backend/__init__.py in <module>()
82 elif _BACKEND == 'tensorflow':
83 sys.stderr.write('Using TensorFlow backend.\n')
---> 84 from .tensorflow_backend import *
85 else:
86 raise ValueError('Unknown backend: ' + str(_BACKEND))
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in <module>()
3 from __future__ import print_function
4
----> 5 import tensorflow as tf
6 from tensorflow.python.training import moving_averages
7 from tensorflow.python.ops import tensor_array_ops
/usr/local/lib/python3.6/dist-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 # pylint: disable=wildcard-import
26 from tensorflow.tools.api.generator.api import * # pylint: disable=redefined-builtin
/usr/local/lib/python3.6/dist-packages/tensorflow/python/__init__.py in <module>()
47 import numpy as np
48
---> 49 from tensorflow.python import pywrap_tensorflow
50
51 # Protocol buffers
/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow.py in <module>()
72 for some common reasons and solutions. Include the entire stack trace
73 above this error message when asking for help.""" % traceback.format_exc()
---> 74 raise ImportError(msg)
75
76 # pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line-too-long
ImportError: Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "/usr/lib/python3.6/imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
File "/usr/lib/python3.6/imp.py", line 343, in load_dynamic
return _load(spec)
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/install_sources#common_installation_problems
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
Below that was a suggestion to consult https://www.tensorflow.org/install/install_sources#common_installation_problems.
But that page is about installing TensorFlow from source. I'm not trying to install from source. I just want a runnable version that I can call.
Any suggestions would be appreciated.
Thanks.

Related

RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback): initialization failed

Goal: Run a GPT-2 model instance.
I am using the latest Tensorflow and Hugging Face 🤗 Transformers.
Tensorflow - 2.9.1
Transformers - 4.21.1
Notebook:
pip install tensorflow
pip install transformers
from transformers import pipeline, set_seed
generator = pipeline('text-generation', model='gpt2')
set_seed(42)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
ImportError: numpy.core.multiarray failed to import
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call last)
SystemError: <built-in method __contains__ of dict object at 0x7f5b58a64d00> returned a result with an error set
The above exception was the direct cause of the following exception:
ImportError Traceback (most recent call last)
~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/utils/import_utils.py in _get_module(self, module_name)
1001 try:
-> 1002 return importlib.import_module("." + module_name, self.__name__)
1003 except Exception as e:
~/anaconda3/envs/python3/lib/python3.8/importlib/__init__.py in import_module(name, package)
126 level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)
128
~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap.py in _gcd_import(name, package, level)
~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap.py in _find_and_load(name, import_)
~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)
~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap.py in _load_unlocked(spec)
~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap_external.py in exec_module(self, module)
~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)
~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/pipelines/__init__.py in <module>
36 from ..utils import HUGGINGFACE_CO_RESOLVE_ENDPOINT, http_get, is_tf_available, is_torch_available, logging
---> 37 from .audio_classification import AudioClassificationPipeline
38 from .automatic_speech_recognition import AutomaticSpeechRecognitionPipeline
~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/pipelines/audio_classification.py in <module>
19 from ..utils import add_end_docstrings, is_torch_available, logging
---> 20 from .base import PIPELINE_INIT_ARGS, Pipeline
21
~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/pipelines/base.py in <module>
33 from ..feature_extraction_utils import PreTrainedFeatureExtractor
---> 34 from ..modelcard import ModelCard
35 from ..models.auto.configuration_auto import AutoConfig
~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/modelcard.py in <module>
43 )
---> 44 from .training_args import ParallelMode
45 from .utils import (
~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/training_args.py in <module>
25 from .debug_utils import DebugOption
---> 26 from .trainer_utils import (
27 EvaluationStrategy,
~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/trainer_utils.py in <module>
46 if is_tf_available():
---> 47 import tensorflow as tf
48
~/anaconda3/envs/python3/lib/python3.8/site-packages/tensorflow/__init__.py in <module>
36
---> 37 from tensorflow.python.tools import module_util as _module_util
38 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
~/anaconda3/envs/python3/lib/python3.8/site-packages/tensorflow/python/__init__.py in <module>
36 from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
---> 37 from tensorflow.python.eager import context
38
~/anaconda3/envs/python3/lib/python3.8/site-packages/tensorflow/python/eager/context.py in <module>
34 from tensorflow.python import tf2
---> 35 from tensorflow.python.client import pywrap_tf_session
36 from tensorflow.python.eager import executor
~/anaconda3/envs/python3/lib/python3.8/site-packages/tensorflow/python/client/pywrap_tf_session.py in <module>
18 from tensorflow.python import pywrap_tensorflow
---> 19 from tensorflow.python.client._pywrap_tf_session import *
20 from tensorflow.python.client._pywrap_tf_session import _TF_SetTarget
ImportError: initialization failed
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
/tmp/ipykernel_4924/2487422996.py in <cell line: 1>()
----> 1 from transformers import pipeline, set_seed
2
3 generator = pipeline('text-generation', model='gpt2')
4 set_seed(42)
~/anaconda3/envs/python3/lib/python3.8/importlib/_bootstrap.py in _handle_fromlist(module, fromlist, import_, recursive)
~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/utils/import_utils.py in __getattr__(self, name)
990 value = self._get_module(name)
991 elif name in self._class_to_module.keys():
--> 992 module = self._get_module(self._class_to_module[name])
993 value = getattr(module, name)
994 else:
~/anaconda3/envs/python3/lib/python3.8/site-packages/transformers/utils/import_utils.py in _get_module(self, module_name)
1002 return importlib.import_module("." + module_name, self.__name__)
1003 except Exception as e:
-> 1004 raise RuntimeError(
1005 f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its"
1006 f" traceback):\n{e}"
RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback):
initialization failed
def query(payload, multiple, min_tokens, max_tokens):
nlp_setup()
list_dict = generator(payload, min_length=min_tokens, max_new_tokens=max_tokens, num_return_sequences=multiple)
return [d['generated_text'].split(payload)[1].strip() for d in list_dict
output = query("Banking customer's needs:", 3000, 50, 50)
RunTime, SystemError and ImportError all occur during import of transformers:
RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback): initialization failed
Changed Kernel: conda_tensorflow2_p38

TensorFlow 2.2.0 not importing Sequential from tensorflow.keras.models PyVer==3.8

Here below the error showing up after trying to run, the error does not persist if rolling back top Tensorflow 2.0 version.
When autocompleting in Jupyter Notebook all sections are showing up normally, just fails to load.
Any possible workaround to avoid rolling back to older version?
In:
from tensorflow.keras.models import Sequential
Out:
ImportError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
57
---> 58 from tensorflow.python.pywrap_tensorflow_internal import *
59
~\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in <module>
27 return _mod
---> 28 _pywrap_tensorflow_internal = swig_import_helper()
29 del swig_import_helper
~\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in swig_import_helper()
23 try:
---> 24 _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
25 finally:
~\Anaconda3\lib\imp.py in load_module(name, file, filename, details)
241 else:
--> 242 return load_dynamic(name, filename, file)
243 elif type_ == PKG_DIRECTORY:
~\Anaconda3\lib\imp.py in load_dynamic(name, path, file)
341 name=name, loader=loader, origin=path)
--> 342 return _load(spec)
343
ImportError: DLL load failed: The specified module could not be found.
During handling of the above exception, another exception occurred:
ImportError Traceback (most recent call last)
<ipython-input-4-f05745da3b73> in <module>
----> 1 from tensorflow.keras.models import Sequential
~\Anaconda3\lib\site-packages\tensorflow\__init__.py in <module>
39 import sys as _sys
40
---> 41 from tensorflow.python.tools import module_util as _module_util
42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
43
~\Anaconda3\lib\site-packages\tensorflow\python\__init__.py in <module>
48 import numpy as np
49
---> 50 from tensorflow.python import pywrap_tensorflow
51
52 # Protocol buffers
~\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
67 for some common reasons and solutions. Include the entire stack trace
68 above this error message when asking for help.""" % traceback.format_exc()
---> 69 raise ImportError(msg)
70
71 # pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line-too-long
ImportError: Traceback (most recent call last):
File "C:\Users\Michele\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Users\Michele\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "C:\Users\Michele\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "C:\Users\Michele\Anaconda3\lib\imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "C:\Users\Michele\Anaconda3\lib\imp.py", line 342, in load_dynamic
return _load(spec)
ImportError: DLL load failed: The specified module could not be found.
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/errors
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
you can do these examples of the sequential model in TensorFlow Keras.you can also add layers according to your case.
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import Sequential
model = Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(64, activation='relu'))
# Add another:
model.add(layers.Dense(64, activation='relu'))
# Add an output layer with 10 output units:
model.add(layers.Dense(10))
or
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
model = tf.keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(64, activation='relu'))
# Add another:
model.add(layers.Dense(64, activation='relu'))
# Add an output layer with 10 output units:
model.add(layers.Dense(10))
As of the latest TensorFlow version >= 2.2.0, the developers have modified the way (made it easier) to load the models/layers etc.
Therefore, the Sequential class is not longer in the package tensorflow.keras.models but in tensorflow.keras

pandas and numpy not functioning properly after installing SHAP

I'm new to python programming and want to install SHAP. After installing SHAP I'm getting error for basic codes, which were executing fine earlier(pandas and numpy).
My versions are
python 3.7.1
conda 4.7.11
pd__version__ :'0.23.4'
I have gone through similar problems and tried to troubleshoot but now neither SHAP, nor numpy,pandas are working properly.
I'm working in anaconda distribution for windows. And pip install --upgrade does not solve my problem.
#Explaining with SHAP
import shap
ImportError Traceback (most recent call last)
<ipython-input-15-f08ae71e45dd> in <module>
1 #Explaining with SHAP
----> 2 import shap
~\AppData\Local\Continuum\anaconda3\lib\site-packages\shap\__init__.py in <module>
9 from .explainers.gradient import GradientExplainer
10 from .explainers.linear import LinearExplainer
---> 11 from .plots.summary import summary_plot
12 from .plots.dependence import dependence_plot
13 from .plots.force import force_plot, initjs, save_html
~\AppData\Local\Continuum\anaconda3\lib\site-packages\shap\plots\summary.py in <module>
13 pass
14 from . import labels
---> 15 from . import colors
16
17 # TODO: remove unused title argument / use title argument
~\AppData\Local\Continuum\anaconda3\lib\site-packages\shap\plots\colors.py in <module>
5
6 import numpy as np
----> 7 import skimage.color
8
9 try:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\skimage\__init__.py in <module>
174 dtype_limits)
175
--> 176 from .util.lookfor import lookfor
177 from .data import data_dir
178
~\AppData\Local\Continuum\anaconda3\lib\site-packages\skimage\util\__init__.py in <module>
6 from .apply_parallel import apply_parallel
7
----> 8 from .arraycrop import crop
9 from ._regular_grid import regular_grid, regular_seeds
10 from .unique import unique_rows
~\AppData\Local\Continuum\anaconda3\lib\site-packages\skimage\util\arraycrop.py in <module>
6
7 import numpy as np
----> 8 from numpy.lib.arraypad import _validate_lengths
9
10
ImportError: cannot import name '_validate_lengths' from 'numpy.lib.arraypad' (C:\Users\z026355\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\arraypad.py)
Currently even pd.read_csv is not working
os.chdir(r"C:\Users\z026355\Desktop\Titanic")
with open("train.csv", encoding="utf8", errors='ignore') as g:
data = pd.read_csv(g)
Traceback (most recent call last):
File "<ipython-input-136-f58724e2278b>", line 2, in <module>
data = pd.read_csv(g)
File "C:\Users\z026355\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py", line 678, in parser_f
error_bad_lines=error_bad_lines,
File "C:\Users\z026355\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py", line 440, in _read
filepath_or_buffer, encoding, compression
File "C:\Users\z026355\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py", line 774, in __init__
File "C:\Users\z026355\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py", line 797, in _get_options_with_defaults
Passed dialect overrides any of the related parser options
AttributeError: module 'pandas.compat' has no attribute 'iteritems'
Upgrading or re installing pandas gives requirement already fulfilled.

Trouble importing modules into ipython

On a freshly installed Windows 10 machine, I installed the newest 64bit version of anaconda. Now when I try importing modules into ipython, I get errors. Some sample errors are reproduced below.
I am new to Python and Programming, so would appreciate a bit of help. This is a brand new Lenovo laptop, so there's no scope for any Python version conflict etc. (since nothing was installed on it earlier).
In [1]: import sklearn
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-b7c74cbf5af0> in <module>
----> 1 import sklearn
C:\Software\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:\Software\anaconda3\lib\site-packages\sklearn\base.py in <module>
11 import re
12
---> 13 import numpy as np
14
15 from . import __version__
C:\Software\anaconda3\lib\site-packages\numpy\__init__.py in <module>
138
139 # Allow distributors to run custom init code
--> 140 from . import _distributor_init
141
142 from . import core
C:\Software\anaconda3\lib\site-packages\numpy\_distributor_init.py in <module>
32
33 with RTLD_for_MKL():
---> 34 from . import _mklinit
35
36 del RTLD_for_MKL
ImportError: DLL load failed: The specified module could not be found.
In [2]: import matplotlib.pyplot as plt
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-a0d2faabd9e9> in <module>
----> 1 import matplotlib.pyplot as plt
C:\Software\anaconda3\lib\site-packages\matplotlib\__init__.py in <module>
136 # cbook must import matplotlib only within function
137 # definitions, so it is safe to import from it here.
--> 138 from . import cbook, rcsetup
139 from matplotlib.cbook import (
140 MatplotlibDeprecationWarning, dedent, get_label, sanitize_sequence)
C:\Software\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py in <module>
29 from weakref import WeakMethod
30
---> 31 import numpy as np
32
33 import matplotlib
C:\Software\anaconda3\lib\site-packages\numpy\__init__.py in <module>
138
139 # Allow distributors to run custom init code
--> 140 from . import _distributor_init
141
142 from . import core
C:\Software\anaconda3\lib\site-packages\numpy\_distributor_init.py in <module>
32
33 with RTLD_for_MKL():
---> 34 from . import _mklinit
35
36 del RTLD_for_MKL
ImportError: DLL load failed: The specified module could not be found.
In [3]: import pystan
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-3-ca8618bb8e4a> in <module>
----> 1 import pystan
ModuleNotFoundError: No module named 'pystan'
In [4]:
After a lot of google searching, I found a few sites that touch upon some closely related issues (ex. https://github.com/stan-dev/pystan/issues/520). It so happens that one needs to add some anaconda directroties to the Windows path. I added anaconda/Library/bin, anaconda/Scripts to the path. Now everything is working as expected.

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