TensorFlow 安装及问题解决

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jasonzzj/article/details/52583891

TensorFlow 为 Google 开源的新一代深度学习框架,与之前使用的Caffe和Torch基于层的设计理念有所不同,是基于张量流图的设计思路,这也是今后深度学习框架的一大发展趋势。

本文默认您的linux计算机已经安装有Python下面介绍一下安装与安装遇到的问题及解决方法。将包含下列内容:

  • CUDA-7.5 与 cuDNN v.5 安装

  • pip安装

  • TensorFlow 安装

  • 安装成功测试

  • 安装错误及解决


(一) CUDA-7.5 与 cuDNN v.5 安装

按照NVIDIA官网,分别安装CUDAcuDNN ,注意两个版本与计算机显卡都要相对应,下载之前可能需要注册一下,很方便。


(二) pip 安装

在命令行下输入如下安装命令:

$ sudo apt-get install python-pip python-dev

(三) TensorFlow 安装

这部分如果顺利的话一两行代码即可搞定,不顺利的话,就要看人品了。

1. 环境清理
注意:如果你之前在计算机上装过TensorFlow,那么你需要先卸载掉之前的TensorFlow和protobuf,具体如下:

$ pip uninstall tensorflow
$ pip uninstall protobuf

2. 选择合适版本的二进制安装文件链接

# Ubuntu/Linux 64-bit, CPU only, Python 2.7
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp27-none-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7
# Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp27-none-linux_x86_64.whl

# Mac OS X, CPU only, Python 2.7:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.10.0-py2-none-any.whl

# Mac OS X, GPU enabled, Python 2.7:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-0.10.0-py2-none-any.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.4
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4
# Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.5
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp35-cp35m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5
# Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.10.0-cp35-cp35m-linux_x86_64.whl

# Mac OS X, CPU only, Python 3.4 or 3.5:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.10.0-py3-none-any.whl

# Mac OS X, GPU enabled, Python 3.4 or 3.5:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-0.10.0-py3-none-any.whl

3. 下载并安装TensorFlow

根据自己计算机安装的Python版本(如何查看本机Python版本,很简单,请参考这篇文章),选择在命令行下相应的命令:

# Python 2
$ sudo pip install --upgrade $TF_BINARY_URL

# Python 3
$ sudo pip3 install --upgrade $TF_BINARY_URL

至此安装完成,如果安装过程报错,该如何解决?又如何测试成功安装?见下面部分。


(四) 安装成功测试

$ python
...
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
# 这里一般会显示你的设备显卡信息
>>> print(sess.run(hello))
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
>>>

恭喜你,安装成功!


(五) 安装问题及解决

我主要遇到以下问题:


1. pip 问题

  • 问题:
    在运行安装TensorFlow命令行时,报出了关于pip的错误。

  • 解决:
    升级pip,用如下命令行:

$ sudo pip install --upgrade pip 

2. 联网问题

# Python 2
$ sudo pip install --upgrade $TF_BINARY_URL

# Python 3
$ sudo pip3 install --upgrade $TF_BINARY_URL
  • 解决:
    为了方便没有翻墙的好宝宝,我下载了[Linux 64-bit, GPU enabled, Python 2.7]的whl类型安装包。下载地址:

    其他版本可以在留言中留下邮箱,我下载后发给您。


3. 安装后测试出现问题

  • 问题:
    当测试代码输入以下命令时:
>>> import tensorflow as tf

   出现了以下问题:“ImportError: cannot import name pywrap_tensorflow”。
test error

  • 解决:

    首先,我上网查了下,有人这么说:“python误以为tensorflow目录中的tensorflow就是要导入的模块。解决办法:不要在tensorflow中运行python或者ipython。”

    然而我并没有在tensorflow的目录中运行,怀疑和之前我打算按另一种源文件安装的方法Installing from sources来安装TensorFlow,故下载了它的源文件,于是乎我就把它删了。然后卸载重装了TensorFlow。卸载方法:

$ sudo pip uninstall tensorflow
$ pip uninstall protobuf

   重装之后,就测试OK了。

    谢谢大家的支持!

    转载请注明出处:

    原文地址:http://blog.csdn.net/jasonzzj/article/details/52583891

猜你喜欢

转载自blog.csdn.net/jasonzzj/article/details/52583891