如何解决“模块'tensorflow'没有属性'estimator'”错误

时间:2019-04-19 13:38:06

标签: python tensorflow tensorflow-estimator

我正在使用conda(通过YAML创建的环境)+ pip在Linux Mint盒子上设置Tensorflow v1.13.1环境。设置完成后,每当我尝试导入tf.estimator时,都会收到标题中描述的AttributeError

AttributeError: module 'tensorflow' has no attribute 'estimator'
  • 我没有在conda环境之外(或在pip上)安装任何版本的TF。
  • 我确实以相同的方式设置了另一个conda环境,但使用Tensorflow 2.0 alpha,可以很好地导入tf.estimator
  • 我尝试过删除环境,然后重新构建
  • 我尝试过重新启动:(
  • 由于某些原因,我无法将conda更新为4.6:
$ conda update -n base -c defaults conda

# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

    Traceback (most recent call last):
      File "/usr/share/anaconda3/lib/python3.7/site-packages/conda/exceptions.py", line 819, in __call__
        return func(*args, **kwargs)
      File "/usr/share/anaconda3/lib/python3.7/site-packages/conda/cli/main.py", line 78, in _main
        exit_code = do_call(args, p)
      File "/usr/share/anaconda3/lib/python3.7/site-packages/conda/cli/conda_argparse.py", line 77, in do_call
        exit_code = getattr(module, func_name)(args, parser)
      File "/usr/share/anaconda3/lib/python3.7/site-packages/conda/cli/main_update.py", line 14, in execute
        install(args, parser, 'update')
      File "/usr/share/anaconda3/lib/python3.7/site-packages/conda/cli/install.py", line 253, in install
        handle_txn(unlink_link_transaction, prefix, args, newenv)
      File "/usr/share/anaconda3/lib/python3.7/site-packages/conda/cli/install.py", line 282, in handle_txn
        unlink_link_transaction.execute()
      File "/usr/share/anaconda3/lib/python3.7/site-packages/conda/core/link.py", line 223, in execute
        self.verify()
      File "/usr/share/anaconda3/lib/python3.7/site-packages/conda/common/io.py", line 46, in decorated
        return f(*args, **kwds)
      File "/usr/share/anaconda3/lib/python3.7/site-packages/conda/core/link.py", line 200, in verify
        self.prepare()
      File "/usr/share/anaconda3/lib/python3.7/site-packages/conda/core/link.py", line 192, in prepare
        stp.remove_specs, stp.update_specs)
      File "/usr/share/anaconda3/lib/python3.7/site-packages/conda/core/link.py", line 282, in _prepare
        mkdir_p(transaction_context['temp_dir'])
      File "/usr/share/anaconda3/lib/python3.7/site-packages/conda/gateways/disk/__init__.py", line 60, in mkdir_p
        makedirs(path)
      File "/usr/share/anaconda3/lib/python3.7/os.py", line 221, in makedirs
        mkdir(name, mode)
    PermissionError: [Errno 13] Permission denied: '/usr/share/anaconda3/.condatmp'

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.estimator时发生,所有其他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与9.0)。

我现在将NVIDIA驱动程序更新为v418,并且能够将conda版本更新为4.16.14。我将上面显示的environment.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

4 个答案:

答案 0 :(得分:1)

只需卸载tensorflowtensorboardtensorflow-estimator,然后重新安装tensorflow。为我工作的版本是1.14.0。

pip uninstall tensorflow tensorboard tensorflow-estimator
...
pip install tensorflow==1.14.0

答案 1 :(得分:0)

最后找到了问题。我猜我还安装了一些本地(非Conda)Tensorflow软件包,我认为它们在python环境中具有更高的优先级。

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

  
      
  • 卸载tensorflow,tensorboard
  •   
  • 每晚卸载tb(如果已安装)
  •   
  • 使用“ pip Frozen | grep tensorflow”检查是否已安装tensorflow-estimator软件包。如果是这样,请将其卸载。
  •   
  • 转到站点包并删除所有与tensorflow,tensorboard,tensorflow-estimator等相关的tensorflow文件夹
  •   
  • 重新安装最新版本的tensorflow和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环境。

答案 2 :(得分:0)

  1. 卸载tensorflow-estimator
  2. 使用pip install tensorflow-estimator ==(与您的tf版本1.13.1相同)重新安装

答案 3 :(得分:-1)

完成:

我仍然安装了一些本地(非 Conda)tensorflow 包,它们在 python 环境中具有更高的优先级

在你的

cd ~/.local
rm -r <tensorflow_package_libraries>

删除每个 tensorflow 包库并重新安装 conda 环境。