代码如下
import numpy as npnp.random.seed(0)from sklearn import datasetsimport matplotlib.pyplot as plt%matplotlib inline%config InlineBackend.figure_format ='retina'from keras.models import Sequentialfrom keras.layers import Densefrom keras.optimizers import SGD
错误信息如下
---------------------------------------------------------------------------ModuleNotFoundError Traceback (most recent call last)~\Anaconda3\lib\site-packages\keras\__init__.py in <module> 2 try:----> 3 from tensorflow.keras.layers.experimental.preprocessing import RandomRotation 4 except ImportError:ModuleNotFoundError: No module named 'tensorflow.keras.layers.experimental.preprocessing'During handling of the above exception, another exception occurred:ImportError Traceback (most recent call last)<ipython-input-5-943507dd87a6> in <module> 6 get_ipython().run_line_magic('config', "InlineBackend.figure_format ='retina'") 7 ----> 8 from keras.models import Sequential 9 from keras.layers import Dense 10 from keras.optimizers import SGD~\Anaconda3\lib\site-packages\keras\__init__.py in <module> 4 except ImportError: 5 raise ImportError(----> 6 'Keras requires TensorFlow 2.2 or higher. ' 7 'Install TensorFlow via `pip install tensorflow`') 8 ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow
注意: 我认为主要问题在于TensorFlow的版本。我使用了一些命令,如下所示,
conda create -n tf tensorflowconda activate tf
并且我还使用了以下命令
conda create -n tf-gpu tensorflow-gpuconda activate tf-gpu
但这些都不起作用,请帮助解决这个错误。
回答:
您需要更新TensorFlow。您可以尝试使用
pip install tensorflow==2.0.0
或者,如果您使用的是GPU版本
pip install tensorflow-gpu==2.0.0
如果这些方法无法解决您的问题,您还可以尝试使用2.2.0
版本。
更多详情,请参阅此问题 查看此答案