简体   繁体   中英

Why tensorflow.keras and tensorflow.python.keras does not work?

When I import the module it says that there is an invalid sintax.


from tensorflow import keras

from tensorflow.keras.models import Sequential

from tensorflow.python.keras.layers import Dense, Activation, Dropout
from tensorflow.python.keras.optimizers import SGD

For all of this imports I got the same error: invalid sintax. I upgraded my pycharm till the latest version, and the interpreter is Python 3.8.

The traceback:

  File "E:/pythonProject/main.py", line 8, in <module>
    from tensorflow.keras.models import Sequential
  File "E:\pythonProject\venv\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "E:\pythonProject\venv\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "E:\pythonProject\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "E:\pythonProject\venv\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 114
    def TFE_ContextOptionsSetAsync(arg1, async):
                                         ^
SyntaxError: invalid syntax

Your pycharm is not at fault for an invalid syntax.

The reason why you are getting an error is because of the tensorflow.python.keras.layers the right way to write it is tensorflow.keras.layers see documentation here https://www.tensorflow.org/api_docs/python/tf/keras/layers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM