How to resolve importing tensorflow library - python

I am working on deep learing project so for that I wanted to import tensorflow library but when I run the code cell in the jupyter notebook it pops up the following error.
Importing tensforflow library error screenshot
Importing tensforflow library error screenshot continue
import tensorflow as tf
error
AttributeError Traceback (most recent call last)
Cell In[3], line 1
----> 1 import tensorflow as tf
File ~\anaconda3\lib\site-packages\tensorflow\__init__.py:469
467 if hasattr(_current_module, "keras"):
468 try:
--> 469 _keras._load()
470 except ImportError:
471 pass
File ~\anaconda3\lib\site-packages\tensorflow\python\util\lazy_loader.py:41, in LazyLoader._load(self)
39 """Load the module and insert it into the parent's globals."""
40 # Import the target module and insert it into the parent's namespace
---> 41 module = importlib.import_module(self.__name__)
42 self._parent_module_globals[self._local_name] = module
44 # Emit a warning if one was specified
File ~\anaconda3\lib\importlib\__init__.py:127, in import_module(name, package)
125 break
126 level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)
File ~\anaconda3\lib\site-packages\keras\__init__.py:21
15 """Implementation of the Keras API, the high-level API of TensorFlow.
16
17 Detailed documentation and user guides are available at
18 [keras.io](https://keras.io).
19 """
20 from keras import distribute
---> 21 from keras import models
22 from keras.engine.input_layer import Input
23 from keras.engine.sequential import Sequential
File ~\anaconda3\lib\site-packages\keras\models\__init__.py:18
1 # Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
(...)
13 # limitations under the License.
14 # ==============================================================================
15 """Keras models API."""
---> 18 from keras.engine.functional import Functional
19 from keras.engine.sequential import Sequential
20 from keras.engine.training import Model
File ~\anaconda3\lib\site-packages\keras\engine\functional.py:34
32 from keras.engine import input_spec
33 from keras.engine import node as node_module
---> 34 from keras.engine import training as training_lib
35 from keras.engine import training_utils
36 from keras.saving.legacy import serialization
File ~\anaconda3\lib\site-packages\keras\engine\training.py:45
43 from keras.saving.experimental import saving_lib
44 from keras.saving.legacy import hdf5_format
---> 45 from keras.saving.legacy import save
46 from keras.saving.legacy import saving_utils
47 from keras.saving.legacy import serialization
File ~\anaconda3\lib\site-packages\keras\saving\legacy\save.py:24
22 from keras.saving.legacy import serialization
23 from keras.saving.legacy.saved_model import load as saved_model_load
---> 24 from keras.saving.legacy.saved_model import load_context
25 from keras.saving.legacy.saved_model import save as saved_model_save
26 from keras.utils import traceback_utils
File ~\anaconda3\lib\site-packages\keras\saving\legacy\saved_model\load_context.py:68
64 """Returns whether under a load context."""
65 return _load_context.in_load_context()
---> 68 tf.__internal__.register_load_context_function(in_load_context)
AttributeError: module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'
I am expecting such explanation which can easily be understood and even a beginner can interpret the solution.

You have an incompatible version of keras installed.
Uninstall it with pip uninstall keras and try again.
The Keras API is available within Tensorflow itself if you need it; from tensorflow import keras.

Related

hello everyone, Iam new to programming, I am using jupyter notebook using anaconda, here i found some problem with the importing of libraries

# lstm model
import tensorflow as tf
from numpy import mean
from numpy import std
from numpy import dstack
from pandas import read_csv
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import LSTM
from tensorflow.keras.utils import to_categorical
from matplotlib import pyplot
When i run this cell iam getting the below error, I am using jupyter notebook using anaconda, using anaconda every packages will be installed related to python but in this it is showing regarding tensorflow problem while the importing libraries, I am using latest version of python 3.9,would anyone help to resolve the issue
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10616/748904884.py in <module>
1 # lstm model
----> 2 import tensorflow as tf
3 from numpy import mean
4 from numpy import std
5 from numpy import dstack
~\AppData\Roaming\Python\Python39\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
~\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\__init__.py in <module>
44
45 # Bring in subpackages.
---> 46 from tensorflow.python import data
47 from tensorflow.python import distribute
48 # from tensorflow.python import keras
~\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\data\__init__.py in <module>
23
24 # pylint: disable=unused-import
---> 25 from tensorflow.python.data import experimental
26 from tensorflow.python.data.ops.dataset_ops import AUTOTUNE
27 from tensorflow.python.data.ops.dataset_ops import Dataset
~\AppData\Roaming\Python\Python39\sitepackages\tensorflow\python\data\experimental\__init__.py
in <module>
96
97 # pylint: disable=unused-import
---> 98 from tensorflow.python.data.experimental import service
99 from tensorflow.python.data.experimental.ops.batching import dense_to_ragged_batch
100 from tensorflow.python.data.experimental.ops.batching import dense_to_sparse_batch
~\AppData\Roaming\Python\Python39\site-
packages\tensorflow\python\data\experimental\service\__init__.py in <module>
372 from __future__ import print_function
373
--> 374 from tensorflow.python.data.experimental.ops.data_service_ops import distribute
375 from tensorflow.python.data.experimental.ops.data_service_ops import from_dataset_id
376 from tensorflow.python.data.experimental.ops.data_service_ops import register_dataset
~\anaconda3\envs\mygpu\lib\site-
packages\tensorflow\python\data\experimental\ops\data_service_ops.py in <module>
23
24 from tensorflow.python import tf2
---> 25 from tensorflow.python.data.experimental.ops import compression_ops
26 from tensorflow.python.data.experimental.ops.distribute_options import AutoShardPolicy
27 from tensorflow.python.data.experimental.ops.distribute_options import
ExternalStatePolicy
~\anaconda3\envs\mygpu\lib\site-
packages\tensorflow\python\data\experimental\ops\compression_ops.py in <module>
18 from __future__ import print_function
19
---> 20 from tensorflow.python.data.util import structure
21 from tensorflow.python.ops import gen_experimental_dataset_ops as ged_ops
22
~\anaconda3\envs\mygpu\lib\site-packages\tensorflow\python\data\util\structure.py in <module>
24 import wrapt
25
---> 26 from tensorflow.python.data.util import nest
27 from tensorflow.python.framework import composite_tensor
28 from tensorflow.python.framework import ops
~\anaconda3\envs\mygpu\lib\site-packages\tensorflow\python\data\util\nest.py in <module>
38 import six as _six
39
---> 40 from tensorflow.python.framework import sparse_tensor as _sparse_tensor
41 from tensorflow.python.util import _pywrap_utils
42 from tensorflow.python.util import nest
~\anaconda3\envs\mygpu\lib\site-packages\tensorflow\python\framework\sparse_tensor.py in
<module>
26 from tensorflow.python import tf2
27 from tensorflow.python.framework import composite_tensor
---> 28 from tensorflow.python.framework import constant_op
29 from tensorflow.python.framework import dtypes
30 from tensorflow.python.framework import ops
~\anaconda3\envs\mygpu\lib\site-packages\tensorflow\python\framework\constant_op.py in
<module>
27 from tensorflow.core.framework import types_pb2
28 from tensorflow.python.eager import context
---> 29 from tensorflow.python.eager import execute
30 from tensorflow.python.framework import dtypes
31 from tensorflow.python.framework import op_callbacks
~\anaconda3\envs\mygpu\lib\site-packages\tensorflow\python\eager\execute.py in <module>
25 from tensorflow.python import pywrap_tfe
26 from tensorflow.python.eager import core
---> 27 from tensorflow.python.framework import dtypes
28 from tensorflow.python.framework import ops
29 from tensorflow.python.framework import tensor_shape
~\anaconda3\envs\mygpu\lib\site-packages\tensorflow\python\framework\dtypes.py in <module>
30 from tensorflow.python.util.tf_export import tf_export
31
---> 32 _np_bfloat16 = _pywrap_bfloat16.TF_bfloat16_type()
33
34
TypeError: Unable to convert function return value to a Python type! The signature was
() -> handle
First try to install each package one by one and you will be able to use it at once.
I was having this issue on m1 macbook, turns out my tensorflow dependency was not in sync with my tensorflow-macos package. This would be a good place to check, In this page the version they give is 2.6.0 but the newest version for tf-macos when running this command python -m pip install tensorflow-macos gives 2.8.0 thus the error. If you just updated your tensorflow version this could very well be there case, go through you tensorflow dependencies and see if there's a mix up.
If that doesn't solve it, then yeah install tokenizer first then transformer, see if that solves the issue

ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' - ANN Visualizer

I'm getting this error when trying to run Ann Visualizer from Anaconda/Jupyter. I have read several threads on this site and others and can't seem to make the error go away:
ImportError Traceback (most recent call last)
<ipython-input-54-bdba1202aeda> in <module>
1 from ann_visualizer.visualize import ann_viz
2
----> 3 ann_viz(model, title="VOC Neural Network")
~\Anaconda3\lib\site-packages\ann_visualizer\visualize.py in ann_viz(model, view, filename, title)
29 """
30 from graphviz import Digraph;
---> 31 import keras;
32 from keras.models import Sequential;
33 from keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten;
~\AppData\Roaming\Python\Python37\site-packages\keras\__init__.py in <module>
23
24 # See b/110718070#comment18 for more details about this import.
---> 25 from keras import models
26
27 from keras.engine.input_layer import Input
~\AppData\Roaming\Python\Python37\site-packages\keras\models.py in <module>
17
18 import tensorflow.compat.v2 as tf
---> 19 from keras import backend
20 from keras import metrics as metrics_module
21 from keras import optimizer_v1
~\AppData\Roaming\Python\Python37\site-packages\keras\backend.py in <module>
35 from tensorflow.python.distribute import distribute_coordinator as dc
36 from tensorflow.python.distribute import distribute_coordinator_context as dc_context
---> 37 from tensorflow.python.eager.context import get_config
38 from tensorflow.python.framework import config
39 from keras import backend_config
ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context'
My Tensorflow/Keras versions are 2.4.0 when I run
print("Tensorflow : " + tf.__version__)
print("Keras : " + keras.__version__)
But in the Anaconsa environment it says 2.3.0 - Not sure if that's a factor?
I'm aware the ANN Visualizer only runs with Tensorflow > 2.1
I updated all my library calls to from tensorflow import keras (for example)
I'm not really sure what to do next - does anyone have any ideas?
Many thanks in advance for any help you can give..
from tensorflow.python.eager.context import get_config Works fine with Tensorflow 2.5.
Install Tensorflow 2.5
!pip install tensorflow==2.5

GluonCV ImportError: cannot import name 'is_np_array'

I'm trying to set up a GluonCV in a jupyter notebook in a virtual environment. For some reason whenever I try to import GluonCV I get this error:
ImportError Traceback (most recent call last)
<ipython-input-2-9a2bc396118f> in <module>
----> 1 import gluoncv
~\anaconda3\envs\mxnet\lib\site-packages\gluoncv\__init__.py in <module>
10 _require_mxnet_version('1.4.0', '2.0.0')
11
---> 12 from . import data
13 from . import model_zoo
14 from . import nn
~\anaconda3\envs\mxnet\lib\site-packages\gluoncv\data\__init__.py in <module>
29 from .sampler import SplitSampler, ShuffleSplitSampler
30 from .otb.tracking import OTBTracking
---> 31 from .kitti.kitti_dataset import KITTIRAWDataset, KITTIOdomDataset
32
33 datasets = {
~\anaconda3\envs\mxnet\lib\site-packages\gluoncv\data\kitti\__init__.py in <module>
1 # pylint: disable=missing-module-docstring
----> 2 from .kitti_dataset import *
3 from .kitti_utils import *
~\anaconda3\envs\mxnet\lib\site-packages\gluoncv\data\kitti\kitti_dataset.py in <module>
19
20 from ...utils.filesystem import try_import_skimage
---> 21 from .kitti_utils import generate_depth_map
22 from .mono_dataset import MonoDataset
23
~\anaconda3\envs\mxnet\lib\site-packages\gluoncv\data\kitti\kitti_utils.py in <module>
10
11 import mxnet as mx
---> 12 from mxnet.util import is_np_array
13
14
ImportError: cannot import name 'is_np_array'
I've tried using the same files that work on Google Collaboratory but I still get that error. I've tried reinstalling gluon and all that stuff in all manners. No idea what's going on. For convenience I really need this to work.
I resolved this error by installing the compatible versions of mxnet and gluoncv.In my case I had installed mxnet with with gluoncv native, that resolved the error.

Module tesnorflow has no attribute "UninitializedVariable" tensorflow hub error

I have tensorflow version "1.14.0" and pip3 is also updated. But when I do:-
import tensorflow as tf
I get this error:
AttributeError: module 'tensorflow.python.ops.resource_variable_ops' has no attribute 'UninitializedVariable'
Tensorflow hub has version 0.5.0. Some answers on stack overflow have suggested that tensorflow version should be greater than 1.7.0 and mine is greater and still I don't know why is hub not being imported. The complete traceback of error is:-
AttributeError Traceback (most recent call last)
<ipython-input-24-d70b4e927300> in <module>
1 from sklearn.model_selection import train_test_split
2 import tensorflow as tf
----> 3 import tensorflow_hub as hub
~/.local/lib/python3.6/site-packages/tensorflow_hub/__init__.py in <module>
27 # error message is thrown instead of an obscure error of missing
28 # symbols at executing the imports.
---> 29 from tensorflow_hub.estimator import LatestModuleExporter
30 from tensorflow_hub.estimator import register_module_for_export
31 from tensorflow_hub.feature_column import image_embedding_column
~/.local/lib/python3.6/site-packages/tensorflow_hub/estimator.py in <module>
23 from absl import logging
24 import tensorflow as tf
---> 25 from tensorflow_hub import tf_utils
26 from tensorflow_hub import tf_v1
27
~/.local/lib/python3.6/site-packages/tensorflow_hub/tf_utils.py in <module>
31 # depending on TensorFlow internal implementation details.
32 # pylint: disable=g-direct-tensorflow-import
---> 33 from tensorflow.python.feature_column import feature_column_v2
34 # pylint: enable=g-direct-tensorflow-import
35
~/.local/lib/python3.6/site-packages/tensorflow/python/feature_column/feature_column_v2.py in <module>
164 from tensorflow.python.platform import tf_logging as logging
165 from tensorflow.python.training import checkpoint_utils
--> 166 from tensorflow.python.training.tracking import tracking
167 from tensorflow.python.util import deprecation
168 from tensorflow.python.util import nest
~/.local/lib/python3.6/site-packages/tensorflow/python/training/tracking/tracking.py in <module>
21 import weakref
22
---> 23 from tensorflow.python.eager import def_function
24 from tensorflow.python.eager import function as defun
25 from tensorflow.python.framework import dtypes
~/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py in <module>
38
39
---> 40 class UnliftedInitializerVariable(resource_variable_ops.UninitializedVariable):
41 """Variable which does not lift its initializer out of function context.
42
AttributeError: module 'tensorflow.python.ops.resource_variable_ops' has no attribute 'UninitializedVariable'
I have tried uninstalling tf completely and then installing again, reinstalling tf hub and updating pip so far. Any help will be very much appreciated!

Cannot import Tensorflow in ipython

I have tensorflow version 1.0.0 non-GPU version on my macbook pro Yosemite. It is installed in a virtualenv, and I have python 2.7. I have six version 1.4.1. When I do python on the command line and import six, it works fine. But when I enter 'ipython' and import tensorflow, I see this error:
AttributeError Traceback (most recent call last)
<ipython-input-1-cd232424f60d> in <module>()
----> 1 execfile('hw4/net.py')
/Users/lingxiao/Documents/research/dialogue-systems/hw4/net.py in <module>()
8 import time
9 import numpy as np
---> 10 import tensorflow as tf
11
12 import app
/Users/lingxiao/tensorflow/lib/python2.7/site-packages/tensorflow/__init__.py in <module>()
22
23 # pylint: disable=wildcard-import
---> 24 from tensorflow.python import *
25 # pylint: enable=wildcard-import
26
/Users/lingxiao/tensorflow/lib/python2.7/site-packages/tensorflow/python/__init__.py in <module>()
122 from tensorflow.python.platform import resource_loader
123 from tensorflow.python.platform import sysconfig
--> 124 from tensorflow.python.platform import test
125
126 from tensorflow.python.util.all_util import remove_undocumented
/Users/lingxiao/tensorflow/lib/python2.7/site-packages/tensorflow/python/platform/test.py in <module>()
67 # pylint: disable=g-bad-import-order
68 from tensorflow.python.client import device_lib as _device_lib
---> 69 from tensorflow.python.framework import test_util as _test_util
70 from tensorflow.python.platform import googletest as _googletest
71 from tensorflow.python.util.all_util import remove_undocumented
/Users/lingxiao/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/test_util.py in <module>()
41 from tensorflow.python.framework import random_seed
42 from tensorflow.python.framework import versions
---> 43 from tensorflow.python.platform import googletest
44 from tensorflow.python.platform import tf_logging as logging
45 from tensorflow.python.util import compat
/Users/lingxiao/tensorflow/lib/python2.7/site-packages/tensorflow/python/platform/googletest.py in <module>()
31
32 from tensorflow.python.platform import app
---> 33 from tensorflow.python.platform import benchmark # pylint: disable=unused-import
34
35 Benchmark = benchmark.TensorFlowBenchmark # pylint: disable=invalid-name
/Users/lingxiao/tensorflow/lib/python2.7/site-packages/tensorflow/python/platform/benchmark.py in <module>()
115
116
--> 117 class Benchmark(six.with_metaclass(_BenchmarkRegistrar, object)):
118 """Abstract class that provides helper functions for running benchmarks.
119
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.pyc in with_metaclass(meta, *bases)
564 def with_metaclass(meta, *bases):
565 """Create a base class with a metaclass."""
--> 566 return meta("NewBase", bases, {})
567
568 def add_metaclass(metaclass):
/Users/lingxiao/tensorflow/lib/python2.7/site-packages/tensorflow/python/platform/benchmark.py in __new__(mcs, clsname, base, attrs)
110 newclass = super(mcs, _BenchmarkRegistrar).__new__(
111 mcs, clsname, base, attrs)
--> 112 if not newclass.is_abstract():
113 GLOBAL_BENCHMARK_REGISTRY.add(newclass)
114 return newclass
AttributeError: type object 'NewBase' has no attribute 'is_abstract'
I tried upgrading six and I am still seeing version 1.4.1. Is six the problem here? if not what is?
It seems that there's a number of things tangled. I'm using Anaconda2 with six==1.10.0, tensorflow==1.0.0 and Python 2.7.12. A quick experiment would be to install Anaconda2 (i.e. with python2.7) then pip install TF and other dependencies and try it out.
The Anaconda installation is self-contained, so that you won't be affected by other system-wide issues. I just did it by running /Users/myusername/anaconda2/bin/ipython
My guess is that you need to install iPython from within the virtual environment. if you haven't done that, ipython will use the vanilla interpreter even though you are in a virtual environment.

Categories