[TensorFLow] tensorFlow安装

TensorFlow安装

参考文献
1. 上面的文献是官方官网教程,在测试过程中使用到的是Ubuntu 14.04 64bit机器。
2. 推荐的方法有几种,例如:pip 安装,docker安装,virtualEnv安装,Anaconda安装。以及从源码安装。
3. 下面这个使用了virtualEnv的安装方式。
3.1 安装依赖包。

# 在 Linux 上:
$ sudo apt-get install python-pip python-dev python-virtualenv

3.2 创建virtualEnv环境。

$ virtualenv --system-site-packages ~/tensorflow
$ cd ~/tensorflow
#然后, 激活 virtualenv:
$ source bin/activate  # 如果使用 bash
$ source bin/activate.csh  # 如果使用 csh
(tensorflow)$  # 终端提示符应该发生变化

3.3 whl安装。

(tensorflow)$ pip install --upgrade <$url_to_binary.whl>

3.3.1 对于那个whl使用pip安装时需要判断有无显卡,没有
参考文献

lshw -short
#或者使用下面的
sudo lshw -C display

3.3.2 然后选择那个whl文件为CPU only.

# Ubuntu/Linux 64-bit, CPU only, Python 2.7:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7. Requires CUDA toolkit 7.5 and CuDNN v4.
# For other versions, see "Install from sources" below.
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl

3.3.3 所以在虚拟环境下应该使用如下命令。

(tensorflow)$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl

但是在使用过程中发现链接超时,所以可以把这个tensorflow-0.8.0-cp27-none-linux_x86_64.whl文件单独下载下来,例如在网页中直接输入下面那个whl的网址就可以下载下来,大概21M。建议放到那个virtualEnv的路径下面去。

(tensorflow)$ pip install --upgrade (path-to-your-whl-file)/tensorflow-0.8.0-cp27-none-linux_x86_64.whl

4.安装过程中需要联网,如果可以联网跳过这个步骤,如果联网有限制,需要自己安装依赖包,有如下依赖包:
4.1 安装中需要Downloading/unpacking numpy>=1.8.2 即这个numpy包要大于1.8版本
4.2 Downloading/unpacking protobuf==3.0.0b2 (from tensorflow==0.8.0),这个protobuf安装起来比较困难。
:~/proj$ protoc –version
程序“protoc”尚未安装。 您可以使用以下命令安装:

/* sudo apt-get install protobuf-compiler
:~/proj$ sudo apt-get install protobuf-c
protobuf-c-compiler  protobuf-compiler    
:~/proj$ sudo apt-get install protobuf-compiler 
                安装那个 [protobuf](http://blog.csdn.net/wutbiao/article/details/45029779) 
                protobuf是google开源的一个序列化方案,根据google的benchmarking测试,各个方面的表现都非常不错
                运行autogen.sh时
                1. 需要自己手动下载gmock 注释掉autogen.sh中这个curl $curlopts -O https://googlemock.googlecode.com/files/gmock-1.7.0.zip
                2.缺少autoconf,需要安装下面两个包。
                 sudo apt-get install autoconf
                sudo apt-get install dh-autoreconf
                3. 在运行autogen.sh完后,执行下面的命令。
                    ./configure --prefix=/usr  
                    make  
                    make check  
                    make install  
                4. 然后
                ~/tensorflow/protobuf-master/python$ cd python/
                接着
                ~/tensorflow/protobuf-master/python$ python setup.py build
                ~/tensorflow/protobuf-master/python$python setup.py install    //这个缺少six文件。 
                Searching for six>=1.9  ----这个缺少文件是通过[参考文献](http://blog.csdn.net/walkandthink/article/details/45200597)中的链接下载的
                Reading https://pypi.python.org/simple/six/
                这样protobuf==3.0.0b3安装完成    //这里需要安装的是3.0.0b2,版本需要配置成一样。
*/

5.最后还是连接外网后下载

(tensorflow)$    pip install tensorflow-0.8.0-cp27-none-linux_x86_64.whl
Requirement already satisfied (use --upgrade to upgrade): *numpy*>=1.8.2 in ./lib/python2.7/site-packages (from tensorflow==0.8.0)
Downloading/unpacking *protobuf*==3.0.0b2 (from tensorflow==0.8.0)
  Downloading protobuf-3.0.0b2-py2.py3-none-any.whl (326kB): 326kB downloaded
Downloading/unpacking *wheel* (from tensorflow==0.8.0)
  Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB): 
Requirement already satisfied (use --upgrade to upgrade): *six*>=1.10.0 in ./lib/python2.7/site-packages (from tensorflow==0.8.0)
Requirement already satisfied (use --upgrade to upgrade): *setuptools* in ./lib/python2.7/site-packages (from protobuf==3.0.0b2->tensorflow==0.8.0)
Installing collected packages: tensorflow, protobuf, wheel
  Found existing installation: protobuf 3.0.0b3
    Uninstalling protobuf:
      Successfully uninstalled protobuf
Successfully installed tensorflow protobuf wheel
Cleaning up...

6.运行tensorflow的“hello world”程序
6.1 打开python终端

$ 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
>>>

6.2 下载源码后,可以运行下面的程序。从源码安装见附件。这个编译需要联网。

$ cd tensorflow/models/image/mnist
$ python convolutional.py
Succesfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Succesfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Succesfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Succesfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
Initialized!
Epoch 0.00
Minibatch loss: 12.054, learning rate: 0.010000
Minibatch error: 90.6%
Validation error: 84.6%
Epoch 0.12
Minibatch loss: 3.285, learning rate: 0.010000
Minibatch error: 6.2%
Validation error: 7.0%
...
...

附件

  1. 从源码安装,因为这个需要下载那个protobuf的依赖包,可能不能下载,需要单独到这个链接把tensorflow下载下来,然后参考4.2中的protobuf的编译。
$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow

1.1 需要安装bazel,也需要先安装依赖

 bazel 缺省需要使用JDK1.8,如你使用JDK1.7,请下载相应的安装包。
安装 Bazel 其他所需依赖: 
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip

1.2 在git上下载这个https://github.com/bazelbuild/bazel/releases对应机器的shell,然后运行

$ chmod +x PATH_TO_INSTALL.SH
$ ./PATH_TO_INSTALL.SH --user

1.3 安装其它依赖:

# For Python 2.7:
$ sudo apt-get install python-numpy swig python-dev python-wheel

1.4 使用bazel编译。

$ bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer
$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu

猜你喜欢

转载自blog.csdn.net/xsjyahoo/article/details/51601850