如何修复“模块‘tensorflow’没有属性‘estimator’”

2024-09-30 20:38:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用conda(通过YAML创建的env)+pip在我的linuxmint box上设置一个tensorflowv1.13.1环境。安装后,每当我尝试导入tf.estimator时,我会收到标题中描述的AttributeError

AttributeError: module 'tensorflow' has no attribute 'estimator'
  • 我没有在conda环境(或pip)之外安装任何TF版本。在
  • 我确实有另一个conda环境以同样的方式设置,但是使用了tensorflow2.0alpha,它可以导入tf.estimator。在
  • 我试着把环境移走重新建造
  • 我试过重新启动:(
  • 由于某些原因,我无法将conda更新到4.6:
^{pr2}$

yml文件如下所示:

dependencies:
- python
- numpy
- tensorflow
- cudatoolkit==9.0
...

从相关环境内部:

  • $ conda list tensorflow
    # packages in environment at /home/cjs/.conda/envs/my-env:
    # 
    # Name                    Version                   Build  Channel
    tensorflow                1.13.1          mkl_py37h54b294f_0
    tensorflow-base           1.13.1          mkl_py37h7ce6ba3_0
    tensorflow-estimator      1.13.0                     py_0
  • $ pip list | grep tensorflow
    tensorflow                  1.13.1
    tensorflow-estimator        1.13.0
  • $ which pip
    /home/cjs/.conda/envs/my-env/bin/pip
  • $ conda --version
    conda 4.5.11
  • $ pip --version
    pip 19.0.3 from /home/cjs/.local/lib/python3.7/site-packages/pip (python 3.7)

这里是这个问题的一个最小的例子。如你所见,这只发生在tf.估计器调用时,所有其他Tensorflow属性按预期操作:

Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'1.13.1'
>>> tf.estimator
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'estimator'
>>> tf.estimator.Estimator()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'estimator'
>>> from tensorflow import estimator
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'estimator' from 'tensorflow' (/home/cjs/.conda/envs/my-env/lib/python3.7/site-packages/tensorflow/__init__.py)
>>> tf.Variable
<class 'tensorflow.python.ops.variables.VariableV1'>
>>> tf.keras
<module 'tensorflow._api.v1.keras' from '/home/cjs/.conda/envs/my-env/lib/python3.7/site-packages/tensorflow/_api/v1/keras/__init__.py'>
>>> tf.constant
<function constant_v1 at 0x7fb25ea24950>

更新

根据https://docs.nvidia.com/deploy/cuda-compatibility/index.html#binary-compatibility__table-toolkit-driver我可以发现我的nvidia驱动程序和cudatoolkit版本不同步(390.46 vs 9.0)。在

我现在已经将我的NVIDIA驱动程序更新到v418,并且能够将我的conda版本更新到4.16.14。我更新了环境.yml上面显示给cudatoolkit==10.1,但我似乎不知道如何真正安装它。在

我的numba -s输出包括这一部分,这让我认为从一开始的整个问题就是cuda找不到我的GPU(或者无法连接到它?)。在

__CUDA Information__
Error: CUDA device intialisation problem. Message:Error at driver init:
[100] Call to cuInit results in CUDA_ERROR_NO_DEVICE:
Error class: <class 'numba.cuda.cudadrv.error.CudaSupportError'>

(次要)更新

能够确定numba问题的原因是,自从更新GPU驱动程序(duh)后,我没有重新启动。在

不过,这还没完全脱离困境。新版本如下:

__CUDA Information__
Found 1 CUDA devices
id 0          b'Quadro K620'                              [SUPPORTED]
                      compute capability: 5.0
                           pci device id: 0
                              pci bus id: 1
Summary:
        1/1 devices are supported
CUDA driver version                 : 10010
CUDA libraries:
Finding cublas
        ERROR: can't locate lib
Finding cusparse
        ERROR: can't locate lib
Finding cufft
        ERROR: can't locate lib
Finding curand
        ERROR: can't locate lib
Finding nvvm
        ERROR: can't locate lib
        finding libdevice for compute_20...     ERROR: can't open libdevice for compute_20
        finding libdevice for compute_30...     ERROR: can't open libdevice for compute_30
        finding libdevice for compute_35...     ERROR: can't open libdevice for compute_35
        finding libdevice for compute_50...     ERROR: can't open libdevice for compute_50

Tags: pipenvfor环境libtftensorflowerror
1条回答
网友
1楼 · 发布于 2024-09-30 20:38:50

终于找到了问题。我还安装了一些本地(非Conda)Tensorflow包,这些包在python环境中具有更高的优先级。在

这个链接解决了我的问题: https://github.com/tensorflow/tensorboard/issues/2067

  • Uninstall tensorflow, tensorboard
  • Uninstall tb-nightly(if it is installed)
  • Use "pip freeze | grep tensorflow" to check if tensorflow-estimator package has been installed. If so, uninstall it.
  • Go to site-packages and remove all tensorflow folders related to tensorflow, tensorboard, tensorflow-estimator etc
  • Reinstall the latest versions of tensorflow and tensorboard

我的问题的关键是站点包,这两个站点都可以找到

  • ~/.conda/envs/<my-env>/lib/python3.<xx>/site-packages
  • ~/.local/lib/python3.<xx>/site-packages

其中,<my-env>是conda环境,<xx>是python版本。在

只需rm -r <path to package>~/.local/库中的每个tensorflow包,然后重新安装conda环境。在

相关问题 更多 >