Python 运行基本张量流示例时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35953210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:11:59  来源:igfitidea点击:

Error running basic tensorflow example

pythonubuntutensorflow

提问by javadba

I have just reinstalled latest tensorflow on ubuntu:

我刚刚在 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

When following the directions to test it fails with cannot import name pywrap_tensorflow:

当按照说明进行测试时,它失败了,无法导入名称 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

Is there an additional change needed to my python or ubuntu/bash environment?

我的 python 或 ubuntu/bash 环境是否需要额外更改?

回答by mrry

From the path in your stack trace (/git/tensorflow/tensorflow/…), it looks like your Python path may be loading the tensorflow libraries from the source directory, rather than the version that you have installed. As a result, it is unable to find the (compiled) pywrap_tensorflowlibrary, which is installed in a different directory.

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

A common solution is to cdout of the /git/tensorflowdirectory before starting pythonor ipython.

一个常见的解决方案是cd出的/git/tensorflow目录开始之前pythonipython

回答by neustart47

Command below helped me.

下面的命令帮助了我。

 pip install tensorflow --upgrade --force-reinstall

回答by Victoria Stuart

I compiled, installed TensorFlow from source (GitHub: https://github.com/tensorflow/tensorflow) in a Python 2.7 virtual environment (venv). It worked fine, but I needed (as others have mentioned, e.g. user "mrry" at Error running basic tensorflow example) to cd out of the partition in which I had compiled TensorFlow, to another partition, in order to be able to import tensorflow into Python. Otherwise, I got various errors, depending on which (source partition) directory I was in. To clarify:

我在 Python 2.7 虚拟环境 (venv) 中从源代码 (GitHub: https://github.com/tensorflow/tensorflow)编译、安装了 TensorFlow 。它工作正常,但我需要(正如其他人提到的,例如用户“mrry”在运行基本 tensorflow 示例出错)从我编译 TensorFlow 的分区中 cd 到另一个分区,以便能够导入 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/...

I later just followed the instructions here,

后来我只是按照这里的说明,

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

and everything just works, fine.

一切正常,很好。

For reference, I am operating on

作为参考,我正在操作

  • Arch Linux [4.6.3-1-ARCH] x86_64
  • Intel i7-4790
  • xfce 4.12 desktop environment
  • Arch Linux [4.6.3-1-ARCH] x86_64
  • 英特尔 i7-4790
  • xfce 4.12 桌面环境

INSTALL STEPS:

安装步骤:

Modify paths, venv names as per your preferences.

根据您的喜好修改路径、venv 名称。

  1. Create tf-env:

    cd /home/victoria/anaconda3/envs
    
    conda create -n tf-env python=2.7 anaconda
    
  1. 创建 tf-env:

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

Note: appending the 'anaconda' metapackage installs ALL Anaconda packages (NumPy; ...).

注意:附加“anaconda”元包会安装所有 Anaconda 包(NumPy; ...)。

  1. Source activate that venv (tf-env)

    source activate tf-env
    
  1. 源激活那个 venv (tf-env)

    source activate tf-env
    

Note: added to ~/.bashrc as an alias:

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

alias tf='echo "  [TensorFlow in Anaconda Python 2.7 venv (source activate tf]" && source activate tf-env'
  1. Install TensorFlow in the tf-env venv:

    (tf-env)$ conda install -c conda-forge tensorflow
    
  1. 在 tf-env venv 中安装 TensorFlow:

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

THIS obviates the use of pip (including the *.whl install script), an alternative approach to installing TensorFlow that is described here:

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

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

WORKS!

作品!

(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 ~]$

You may then work with TensorFlow in the tf-env; e.g., in Jupyter notebooks launched in this (tf-env) venv.

然后你可以在 tf-env 中使用 TensorFlow;例如,在这个 (tf-env) venv 中启动的 Jupyter 笔记本中。

回答by Tensorflow Support

Tensorflow 2.0 Compatible Answer: Both mrry's solution and neustart47's work for Tensorflow Version >= 2.0.

Tensorflow 2.0 兼容答案:mrry 的解决方案和 neustart47 针对 Tensorflow 版本 >= 2.0 的工作。

Answer 1: cdout of the /git/tensorflowdirectory before starting python or ipython.

答案1:启动python或ipython前cd退出/git/tensorflow目录。

Answer 2: pip install tensorflow --upgrade --force-reinstallor
!pip install tensorflow==2.0 --force-reinstall

答案2: pip install tensorflow --upgrade --force-reinstall
!pip install tensorflow==2.0 --force-reinstall

回答by Cees Timmerman

In my python -m venv environmentit appeared to be fixed by:

在我python -m venv environment看来,它似乎是通过以下方式修复的:

pip uninstall tensorflow

Change requirements.txtto say tensorflow==1.5.0instead of tensorflow==1.8.0.

更改requirements.txttensorflow==1.5.0不是tensorflow==1.8.0

pip install -r requirements.txt

回答by iwilldo

if you using CPU manufactured before 2011 then downgraded the tensorflow version from 1.8.0 to 1.5.0 or 1.2.0 and try to import the module it worked for me.

如果您使用 2011 年之前制造的 CPU,则将 tensorflow 版本从 1.8.0 降级到 1.5.0 或 1.2.0,并尝试导入对我有用的模块。

回答by Rajesh Tc

Installing via the following command solves the problem:

通过以下命令安装可以解决问题:

pip install --upgrade

pip install --upgrade

Here's the linkto download

这是下载链接

回答by Thirumal Alagu

I figured out that TensorFlow 1.12.0 only works with Python version 3.5.2. I had Python 3.7 but that didn't work. So, I had to downgrade Python and then I could install TensorFlow it works.

我发现 TensorFlow 1.12.0 仅适用于 Python 3.5.2 版。我有 Python 3.7 但这没有用。所以,我不得不降级 Python,然后我才能安装 TensorFlow,它可以工作。

To downgrade your python version from 3.7 to 3.6

将 Python 版本从 3.7 降级到 3.6

conda install python=3.6.8

回答by Roberto Deresu

I'm using tensorflow 1.13.1. Just reinstalling tensorflow worked for me.

我正在使用 tensorflow 1.13.1。只是重新安装 tensorflow 对我有用。

回答by Jerryhtz

I also have this issue and have been looking into it for a long time. It seems that there is no such problem on python 3+. On py2.7- The problem is actually on google.protobuf

我也有这个问题,研究了很久。在python 3+上似乎没有这样的问题。在 py2.7 上-问题实际上出在 google.protobuf 上

Solution 1:

解决方案1:

pip uninstall protobuf
pip uninstall google
pip install google
pip install protobuf
pip install google-cloud

Solution 2:

解决方案2:

create an __init__.pyin "google" folder.

__init__.py在“谷歌”文件夹中创建一个。

cd /path/to/your/env/lib/python2.7/site-packages/google
touch __init__.py

Hopefully it will work.

希望它会起作用。