簡體   English   中英

運行基本張量流示例時出錯

[英]Error running basic tensorflow example

我剛剛在 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。 它工作正常,但我需要(正如其他人提到的,例如用戶“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/...

后來我只是按照這里的說明,

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 桌面環境

安裝步驟:

根據您的喜好修改路徑、venv 名稱。

  1. 創建 tf-env:

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

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

  1. 源激活那個 venv (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; 例如,在這個 (tf-env) venv 中啟動的 Jupyter 筆記本中。

Tensorflow 2.0 兼容答案:mrry 的解決方案和 neustart47 針對 Tensorflow 版本 >= 2.0 的工作。

答案1:在啟動python或ipython之前cd/git/tensorflow目錄。

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

在我的python -m venv environment它似乎是通過以下方式修復的:

pip uninstall tensorflow

requirements.txt更改為tensorflow==1.5.0而不是tensorflow==1.8.0

pip install -r requirements.txt

如果您使用 2011 年之前制造的 CPU,則將 tensorflow 版本從 1.8.0 降級到 1.5.0 或 1.2.0,並嘗試導入對我有用的模塊。

通過以下命令安裝可以解決問題:

pip install --upgrade

這是下載鏈接

我發現 TensorFlow 1.12.0 僅適用於 Python 3.5.2 版。 我有 Python 3.7 但這沒有用。 所以,我不得不降級 Python,然后我才能安裝 TensorFlow,它可以工作。

將 Python 版本從 3.7 降級到 3.6

conda install python=3.6.8

我正在使用 tensorflow 1.13.1。 只是重新安裝 tensorflow 對我有用。

我也有這個問題,研究了很久。 在python 3+上似乎沒有這樣的問題。 在 py2.7- 問題實際上是在 google.protobuf

解決方案1:

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

解決方案2:

在“google”文件夾中創建一個__init__.py

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

希望它會起作用。

我解決了這個問題。 嘗試以下命令:

pip install --upgrade pip

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM