Mathew
Mathew

Reputation: 1430

can't import keras from tensorflow

I am trying to use keras but am unable to because when I run from tensorflow import keras I get this error:

➜  kerasTutorial python3
Python 3.7.7 (default, Mar 10 2020, 15:43:33)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from tensorflow import keras
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/tensorflow/__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/__init__.py", line 64, in <module>
    from tensorflow.python.framework.framework_lib import *  # pylint: disable=redefined-builtin
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/framework_lib.py", line 25, in <module>
    from tensorflow.python.framework.ops import Graph
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 64, in <module>
    from tensorflow.python.platform import app
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/platform/app.py", line 23, in <module>
    from absl.app import run as _run
  File "/usr/local/lib/python3.7/site-packages/absl/app.py", line 35, in <module>
    import pdb
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pdb.py", line 76, in <module>
    import code
  File "/Users/mathewlewis/Documents/Y4S3/Crypto/kerasTutorial/code.py", line 4, in <module>
    from tensorflow.keras.models import Sequential
  File "/usr/local/lib/python3.7/site-packages/tensorflow/keras/__init__.py", line 14, in <module>
    from . import activations
  File "/usr/local/lib/python3.7/site-packages/tensorflow/keras/activations/__init__.py", line 10, in <module>
    from tensorflow.python.keras.activations import deserialize
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/__init__.py", line 27, in <module>
    from tensorflow.python.keras import models
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/models.py", line 23, in <module>
    from tensorflow.python.keras import backend as K
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/backend.py", line 36, in <module>
    from tensorflow.python.client import session as session_module
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 38, in <module>
    from tensorflow.python.framework import sparse_tensor
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/sparse_tensor.py", line 29, in <module>
    from tensorflow.python.framework import constant_op
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py", line 324, in <module>
    ops.register_tensor_conversion_function(
AttributeError: module 'tensorflow.python.framework.ops' has no attribute 'register_tensor_conversion_function'
>>>

I am using python3 on mac. I have installed both keras and tensorflow using pip3 install keras and pip3 install tensorflow

I am under the impression that importing keras from tensorflow should be possible given that here on keras.io they provide the following sample code:

import numpy as np
import tensorflow as tf
from tensorflow import keras

Because of this, I suspect I have improperly installed either tensorflow or keras. For this reason, I have included below the results I get when I run pip3 install tensorflow and pip3 install keras:

➜  kerasTutorial pip3 install tensorflow
Requirement already satisfied: tensorflow in /usr/local/lib/python3.7/site-packages (2.2.0)
Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.14.0)
Requirement already satisfied: grpcio>=1.8.6 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.29.0)
Requirement already satisfied: astunparse==1.6.3 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.6.3)
Requirement already satisfied: wheel>=0.26; python_version >= "3" in /usr/local/lib/python3.7/site-packages (from tensorflow) (0.34.2)
Requirement already satisfied: google-pasta>=0.1.8 in /usr/local/lib/python3.7/site-packages (from tensorflow) (0.2.0)
Requirement already satisfied: h5py<2.11.0,>=2.10.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (2.10.0)
Requirement already satisfied: gast==0.3.3 in /usr/local/lib/python3.7/site-packages (from tensorflow) (0.3.3)
Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.7/site-packages (from tensorflow) (3.2.1)
Requirement already satisfied: protobuf>=3.8.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (3.12.0)
Requirement already satisfied: wrapt>=1.11.1 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.12.1)
Requirement already satisfied: scipy==1.4.1; python_version >= "3" in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.4.1)
Requirement already satisfied: absl-py>=0.7.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (0.9.0)
Requirement already satisfied: tensorflow-estimator<2.3.0,>=2.2.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (2.2.0)
Requirement already satisfied: tensorboard<2.3.0,>=2.2.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (2.2.1)
Requirement already satisfied: keras-preprocessing>=1.1.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.1.2)
Requirement already satisfied: numpy<2.0,>=1.16.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.18.4)
Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.7/site-packages (from tensorflow) (1.1.0)
Requirement already satisfied: setuptools in /Users/mathewlewis/Library/Python/3.7/lib/python/site-packages (from protobuf>=3.8.0->tensorflow) (46.4.0)
Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.7/site-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow) (2.23.0)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.7/site-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow) (3.2.2)
Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.7/site-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow) (1.15.0)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.7/site-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow) (0.4.1)
Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.7/site-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow) (1.0.1)
Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.7/site-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow) (1.6.0.post3)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/site-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/site-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow) (2020.4.5.1)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/site-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow) (2.9)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/site-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow) (1.25.9)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /usr/local/lib/python3.7/site-packages (from markdown>=2.6.8->tensorboard<2.3.0,>=2.2.0->tensorflow) (1.6.0)
Requirement already satisfied: rsa<4.1,>=3.1.4 in /usr/local/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow) (4.0)
Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow) (4.1.0)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow) (0.2.8)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.7/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.3.0,>=2.2.0->tensorflow) (1.3.0)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->markdown>=2.6.8->tensorboard<2.3.0,>=2.2.0->tensorflow) (3.1.0)
Requirement already satisfied: pyasn1>=0.1.3 in /usr/local/lib/python3.7/site-packages (from rsa<4.1,>=3.1.4->google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow) (0.4.8)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.7/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.3.0,>=2.2.0->tensorflow) (3.1.0)


➜  kerasTutorial pip3 install keras
Requirement already satisfied: keras in /usr/local/lib/python3.7/site-packages (2.3.1)
Requirement already satisfied: keras-applications>=1.0.6 in /usr/local/lib/python3.7/site-packages (from keras) (1.0.8)
Requirement already satisfied: h5py in /usr/local/lib/python3.7/site-packages (from keras) (2.10.0)
Requirement already satisfied: keras-preprocessing>=1.0.5 in /usr/local/lib/python3.7/site-packages (from keras) (1.1.2)
Requirement already satisfied: numpy>=1.9.1 in /usr/local/lib/python3.7/site-packages (from keras) (1.18.4)
Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.7/site-packages (from keras) (1.14.0)
Requirement already satisfied: scipy>=0.14 in /usr/local/lib/python3.7/site-packages (from keras) (1.4.1)
Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/site-packages (from keras) (5.3.1)

In case this is relavent, I am on a mid 2015 macbook pro, running macos catalina version 10.15.4

This was my best attempt at including all relavent information. If you'd like to know something else please let me know and I'll try and find that out for you. Thanks very much for taking the time to read this long question.

I have also tried using keras directly rather than importing from tensorflow. To do so I ran from keras.models import Sequential and got this error:

➜  kerasTutorial python3
Python 3.7.7 (default, Mar 10 2020, 15:43:33)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from keras.models import Sequential
Using TensorFlow backend.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/keras/__init__.py", line 3, in <module>
    from . import utils
  File "/usr/local/lib/python3.7/site-packages/keras/utils/__init__.py", line 6, in <module>
    from . import conv_utils
  File "/usr/local/lib/python3.7/site-packages/keras/utils/conv_utils.py", line 9, in <module>
    from .. import backend as K
  File "/usr/local/lib/python3.7/site-packages/keras/backend/__init__.py", line 1, in <module>
    from .load_backend import epsilon
  File "/usr/local/lib/python3.7/site-packages/keras/backend/load_backend.py", line 90, in <module>
    from .tensorflow_backend import *
  File "/usr/local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 5, in <module>
    import tensorflow as tf
  File "/usr/local/lib/python3.7/site-packages/tensorflow/__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/__init__.py", line 64, in <module>
    from tensorflow.python.framework.framework_lib import *  # pylint: disable=redefined-builtin
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/framework_lib.py", line 25, in <module>
    from tensorflow.python.framework.ops import Graph
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 64, in <module>
    from tensorflow.python.platform import app
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/platform/app.py", line 23, in <module>
    from absl.app import run as _run
  File "/usr/local/lib/python3.7/site-packages/absl/app.py", line 35, in <module>
    import pdb
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pdb.py", line 76, in <module>
    import code
  File "/Users/mathewlewis/Documents/Y4S3/Crypto/kerasTutorial/code.py", line 4, in <module>
    from tensorflow.keras.models import Sequential
  File "/usr/local/lib/python3.7/site-packages/tensorflow/keras/__init__.py", line 14, in <module>
    from . import activations
  File "/usr/local/lib/python3.7/site-packages/tensorflow/keras/activations/__init__.py", line 10, in <module>
    from tensorflow.python.keras.activations import deserialize
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/__init__.py", line 27, in <module>
    from tensorflow.python.keras import models
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/models.py", line 23, in <module>
    from tensorflow.python.keras import backend as K
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/backend.py", line 36, in <module>
    from tensorflow.python.client import session as session_module
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 38, in <module>
    from tensorflow.python.framework import sparse_tensor
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/sparse_tensor.py", line 29, in <module>
    from tensorflow.python.framework import constant_op
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py", line 324, in <module>
    ops.register_tensor_conversion_function(
AttributeError: module 'tensorflow.python.framework.ops' has no attribute 'register_tensor_conversion_function'
>>>

Also, I am not using conda. I'd like to have a solution which uses python3 and packages which can be installed using pip3.

Upvotes: 2

Views: 17471

Answers (4)

palazzo train
palazzo train

Reputation: 3409

From your question you are installing tensorflow 2.2

Requirement already satisfied: tensorflow in /usr/local/lib/python3.7/site-packages (2.2.0)

In fact, if you are using tf 2.2 as backend and want to use keras API, you should not install keras but should use from tensorflow import keras

Because keras package does not support tensorflow 2.2. If you check the keras official release https://github.com/keras-team/keras/releases, it supports up to tensorflow 2.0 only.

Therefore, you should uninstall both tensorflow and keras , and only reinstall tensorflow v2.2. Then test by

from tensorflow.keras.models import Sequential

You should be able to run your tutorial

Upvotes: 6

palazzo train
palazzo train

Reputation: 3409

You should install Tensorflow 2.2 only

From your question you are installing tensorflow 2.2

Requirement already satisfied: tensorflow in /usr/local/lib/python3.7/site-packages (2.2.0)

In fact, if you are using tf 2.2 as backend and want to use keras API, you should not install keras but should use from tensorflow import keras

Because keras package does not support tensorflow 2.2. If you check the keras official release https://github.com/keras-team/keras/releases, it supports up to tensorflow 2.0 only.

Therefore, you should uninstall both tensorflow and keras , and only reinstall tensorflow v2.2. Then test by

from tensorflow.keras.models import Sequential

You should be able to run your tutorial

Upvotes: 0

bhristov
bhristov

Reputation: 3187

I looked into the error. Some people had it because of an older version of Keras. You have a version which shouldn't be causing any issues.

I would suggest to uninstall keras and tensorflow:

pip3 uninstall tensorflow
pip3 uninstall keras

upgrade pip3:

pip3 install --upgrade pip

and then install tensorflow again:

pip3 install tensorflow

and try to run the project. If this doesn't work try reinstalling python as well.

Upvotes: 0

yudhiesh
yudhiesh

Reputation: 6799

If you have conda installed just download tensorflow using it.

Create a conda environment called tf and install the latest version of tensorflow with the commaned below.

conda create -n tf tensorflow
conda activate tf

Upvotes: 0

Related Questions