当前位置: 首页>>技术教程>>正文


python – 运行基本的TensorFlow示例时出错

, ,

问题描述

我刚刚在ubuntu上重新安装了最新的tensorflow:

$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.7.1-cp27-none-linux_x86_64.whl
[sudo] password for ubuntu: 
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting tensorflow==0.7.1 from https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.7.1-cp27-none-linux_x86_64.whl
  Downloading https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.7.1-cp27-none-linux_x86_64.whl (13.8MB)
    100% |████████████████████████████████| 13.8MB 32kB/s 
Requirement already up-to-date: six>=1.10.0 in /usr/local/lib/python2.7/dist-packages (from tensorflow==0.7.1)
Requirement already up-to-date: protobuf==3.0.0b2 in /usr/local/lib/python2.7/dist-packages (from tensorflow==0.7.1)
Requirement already up-to-date: wheel in /usr/local/lib/python2.7/dist-packages (from tensorflow==0.7.1)
Requirement already up-to-date: numpy>=1.8.2 in /usr/local/lib/python2.7/dist-packages (from tensorflow==0.7.1)
Requirement already up-to-date: setuptools in /usr/local/lib/python2.7/dist-packages (from protobuf==3.0.0b2->tensorflow==0.7.1)
Installing collected packages: tensorflow
  Found existing installation: tensorflow 0.7.1
    Uninstalling tensorflow-0.7.1:
      Successfully uninstalled tensorflow-0.7.1
Successfully installed tensorflow-0.7.1

遵循测试说明进行操作时失败,无法导入名称pywrap_tensorflow:

$ ipython

/git/tensorflow/tensorflow/__init__.py in <module>()
     21 from __future__ import print_function
     22 
---> 23 from tensorflow.python import *

/git/tensorflow/tensorflow/python/__init__.py in <module>()
     43 _default_dlopen_flags = sys.getdlopenflags()
     44 sys.setdlopenflags(_default_dlopen_flags | ctypes.RTLD_GLOBAL)
---> 45 from tensorflow.python import pywrap_tensorflow
     46 sys.setdlopenflags(_default_dlopen_flags)
     47 

ImportError: cannot import name pywrap_tensorflow

我的python或ubuntu /bash环境是否需要进行其他更改?

最佳回答

从堆栈跟踪中的路径(/git/tensorflow/tensorflow/…),看来您的Python路径可能正在从源目录而不是已安装的版本加载tensorflow库。结果,无法找到(编译的)pywrap_tensorflow库,该库安装在其他目录中。

常见的解决方案是在启动pythonipython之前,从/git/tensorflow目录中删除cd

次佳回答

下面的命令帮助了我。

 pip install tensorflow --upgrade --force-reinstall

第三种回答

我在Python 2.7虚拟环境(venv)中从源代码(GitHub:https://github.com/tensorflow/tensorflow)编译安装了TensorFlow。它工作正常,但是我需要(如其他人所述,例如Error running basic tensorflow example的用户”mrry”)将cd从我编译TensorFlow的分区中移出到另一个分区,以便能够将tensorflow导入Python。否则,我会遇到各种错误,具体取决于我所在的(源分区)目录。

         source: /mnt/Vancouver/apps/tensorflow
can't import tf: Python launched in any of /mnt/...
  can import tf: Python launched in /home/victoria/...

我后来只是按照这里的指示进行操作,

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md#anaconda-installation

一切都正常,很好。

供参考,我正在

  • Arch Linux [4.6.3-1-ARCH] x86_64

  • 英特尔i7-4790

  • xfce 4.12桌面环境

安装步骤:

根据您的喜好修改路径,虚拟名称。

  1. 创建tf-env:

    cd /home/victoria/anaconda3/envs
    
    conda create -n tf-env python=2.7 anaconda
    

注意:附加’anaconda’元软件包会安装所有Anaconda软件包(NumPy; …)。

  1. 源激活该静脉(tf-env)

    source activate tf-env
    

注意:作为别名添加到〜/.bashrc中:

alias tf='echo "  [TensorFlow in Anaconda Python 2.7 venv (source activate tf]" && source activate tf-env'
  1. 在tf-env venv中安装TensorFlow:

    (tf-env)$ conda install -c conda-forge tensorflow
    

这避免了pip的使用(包括* .whl安装脚本),这是安装TensorFlow的另一种方法,如下所述:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md).

作品!

(tf-env)[victoria@victoria ~]$ P

  [P: python]
Python 2.7.12 |Anaconda 4.1.1 (64-bit)| (default, Jul  2 2016, 17:42:40)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org

>>> import tensorflow
>>> print tensorflow.__version__
0.9.0
>>> [Ctrl-D]

(tf-env)[victoria@victoria ~]$

然后,您可以在tf-env中使用TensorFlow。例如,在Jupyter中,此(tf-env)静脉发射的笔记本电脑。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/9858.html,未经允许,请勿转载。