我有一个使用tensorflow进行机器学习的Python代码
重要的是要注意,在安装tensorflow-gpu之前,代码可以正常使用tensorflow。
安装tensorflow-gpu后,运行文件时出现了以下错误
File "fileName.py", line 1, in <module> import tensorflow as tf File "/home/<xyz>/.local/lib/python3.7/site-packages/tensorflow/__init__.py", line 41, in <module> from tensorflow.python.tools import module_util as _module_util File "/home/<xyz>/.local/lib/python3.7/site-packages/tensorflow/python/__init__.py", line 84, in <module> from tensorflow.python import keras File "/home/<xyz>/.local/lib/python3.7/site-packages/tensorflow/python/keras/__init__.py", line 27, in <module> from tensorflow.python.keras import models File "/home/<xyz>/.local/lib/python3.7/site-packages/tensorflow/python/keras/models.py", line 24, in <module> from tensorflow.python.keras import metrics as metrics_module File "/home/<xyz>/.local/lib/python3.7/site-packages/tensorflow/python/keras/metrics.py", line 37, in <module> from tensorflow.python.keras.engine import base_layer File "/home/<xyz>/.local/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 51, in <module> from tensorflow.python.keras import initializers File "/home/<xyz>/.local/lib/python3.7/site-packages/tensorflow/python/keras/initializers/__init__.py", line 127, in <module> populate_deserializable_objects() File "/home/<xyz>/.local/lib/python3.7/site-packages/tensorflow/python/keras/initializers/__init__.py", line 85, in populate_deserializable_objects generic_utils.populate_dict_with_module_objects(AttributeError: module 'tensorflow.python.keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'
tensorflow和tensorflow-gpu都通过pip成功安装
使用pip show tensorflow
检查tensorflow的安装情况
Name: tensorflowVersion: 2.2.0Summary: TensorFlow is an open source machine learning framework for everyone.Home-page: https://www.tensorflow.org/Author: Google Inc.Author-email: [email protected]License: Apache 2.0Location: /home/<xyz>/.local/lib/python3.7/site-packagesRequires: scipy, astunparse, termcolor, tensorboard, grpcio, opt-einsum, tensorflow-estimator, google-pasta, h5py, absl-py, protobuf, six, wheel, gast, wrapt, keras-preprocessing, numpyRequired-by:
使用pip show tensorflow-gpu
检查tensorflow-gpu的安装情况
Name: tensorflow-gpuVersion: 2.2.0Summary: TensorFlow is an open source machine learning framework for everyone.Home-page: https://www.tensorflow.org/Author: Google Inc.Author-email: [email protected]License: Apache 2.0Location: /home/<xyz>/.local/lib/python3.7/site-packagesRequires: google-pasta, tensorflow-estimator, wrapt, absl-py, termcolor, opt-einsum, keras-preprocessing, six, scipy, h5py, protobuf, wheel, numpy, tensorboard, gast, grpcio, astunparseRequired-by:
我尚未安装CUDA驱动。这可能是问题所在吗?
回答:
这个GitHub问题讨论了这个问题。似乎是与tf-nightly
存在冲突。
尝试以下操作:
pip uninstall tf-nightlypip install tensorflow --upgrade --force-reinstall # 或者,tensorflow-gpu