TensorFlow study notes (b) - TensorFlow installation

Download and install

You can use binary packages we offer, or use the source code, installation TensorFlow.

Binary installation

TensorFlow Python API rely on Python 2.7 version.

In the simplest way to install Linux and Mac, it is to use  pip  install.

If you encounter an error during the installation process, please refer to the  FAQ . To simplify the installation procedure is recommended virtualenv, see the tutorial  here .

Ubuntu/Linux

# 仅使用 CPU 的版本
$ pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl

# 开启 GPU 支持的版本 (安装该版本的前提是已经安装了 CUDA sdk)
$ pip install https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl

Mac OS X

On OS X systems, we recommend to install  homebrew , then perform  brew install pythonto be able to use Python installation TensorFlow homebrew in. Another way is recommended in  virtualenv  installation TensorFlow in.

# 当前版本只支持 CPU
$ pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl

Docker-based installation

We also support by  Docker  run TensorFlow. The advantage of this approach is to worry about software dependencies.

First,  install Docker once Docker has been up and running, you can start a container through the command:

$ docker run -it b.gcr.io/tensorflow/tensorflow

This command will start a container has been installed TensorFlow and related dependencies.

Other Mirror

Docker default image only contains TensorFlow required to start and run a minimum set of dependent libraries we provide the following additional container can likewise the above.  docker run Install command:

  • b.gcr.io/tensorflow/tensorflow-full: The image from source TensorFlow complete installation comprising all the tools needed to compile and run TensorFlow on the mirror, the source code can be used directly experiment, without the need to install any of the above-described dependency.

Based VirtualEnv installation

We recommend using  virtualenv  create an isolated container, to install TensorFlow. This is optional, but doing so can make it easier to troubleshoot installation problems.

First, install all the necessary tools:

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

# 在 Mac 上:
$ sudo easy_install pip  # 如果还没有安装 pip
$ sudo pip install --upgrade virtualenv

Next, create a new environment in order to virtualenv built environment.  ~/tensorflow Directory, execute:

$ virtualenv --system-site-packages ~/tensorflow
$ cd ~/tensorflow

Then, activate virtualenv:

$ source bin/activate  # 如果使用 bash
$ source bin/activate.csh  # 如果使用 csh
(tensorflow)$  # 终端提示符应该发生变化

In virtualenv, installation TensorFlow:

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

Next, use the command to run a similar program TensorFlow:

(tensorflow)$ cd tensorflow/models/image/mnist
(tensorflow)$ python convolutional.py

# 当使用完 TensorFlow
(tensorflow)$ deactivate  # 停用 virtualenv

$  # 你的命令提示符会恢复原样

Try your first program TensorFlow

(Optional) Enable GPU support

If you install a binary package using pip open GPU support TensorFlow, you must ensure that the system correctly CUDA sdk and CUDNN version installed. See between  CUDA installation tutorial

You also need to set  LD_LIBRARY_PATH and  CUDA_HOME environment variables can consider adding the following command to.  ~/.bash_profile File so that each note automatically take effect after landing, the following command assumes CUDA installation directory.  /usr/local/cuda:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"
export CUDA_HOME=/usr/local/cuda

Run TensorFlow

Open a python terminal:

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

Installation from source

Cloning TensorFlow warehouse

$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow

--recurse-submodules Parameters have to be used to obtain TesorFlow dependent protobuf library.

Linux installation

Installation Bazel

First, in accordance with the  tutorial  installation depends Bazel then use the following command to download the source code and compile Bazel:

$ git clone https://github.com/bazelbuild/bazel.git
$ cd bazel
$ git checkout tags/0.1.0
$ ./compile.sh

The code pulls the tab above command is  0.1.0compatible with the current version Tensorflow. Bazel's HEAD version (ie latest version) here may be unstable.

The execution path  output/bazel added to the  $PATH environment variable.

Install other dependencies

$ sudo apt-get install python-numpy swig python-dev

Optional: Install CUDA (GPU support on the open Linux)

In order to be able to compile and run using the GPU TensorFlow, you need to install the NVIDIA provided Cuda Toolkit 7.0 and CUDNN 6.5 V2.

GPU characteristics TensorFlow only supports NVidia Compute Capability> = 3.5 is supported graphics cards include but are not limited to:

  • NVidia Titan
  • NVidia Titan X
  • NVidia K20
  • NVidia K40

Download and install the Cuda Toolkit 7.0

download link

Mounted to the tool, such as a  /usr/local/cuda path or the like.

Download and install CUDNN Toolkit 6.5

download link

CUDNN decompression and copy files to the installation path assumed Cuda Toolkit 7.0 Cuda Toolkit 7.0 installed.  /usr/local/cuda, Run the following command:

tar xvzf cudnn-6.5-linux-x64-v2.tgz
sudo cp cudnn-6.5-linux-x64-v2/cudnn.h /usr/local/cuda/include
sudo cp cudnn-6.5-linux-x64-v2/libcudnn* /usr/local/cuda/lib64

Configuration options TensorFlow of Cuba

Execution path from the root of the source tree:

$ ./configure
Do you wish to bulid TensorFlow with GPU support? [y/n] y
GPU support will be enabled for TensorFlow

Please specify the location where CUDA 7.0 toolkit is installed. Refer to
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda

Please specify the location where CUDNN 6.5 V2 library is installed. Refer to
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda

Setting up Cuda include
Setting up Cuda lib64
Setting up Cuda bin
Setting up Cuda nvvm
Configuration finished

These configurations will create a symbolic link to the library system Cuda. Whenever Cuda library path is changed, you must repeat the steps again, otherwise can not be called bazel compile command.

Compiled object program, open GPU support

Execution path from the root of the source tree:

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

$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu
# 大量的输出信息. 这个例子用 GPU 迭代计算一个 2x2 矩阵的主特征值 (major eigenvalue).
# 最后几行输出和下面的信息类似.
000009/000005 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]
000006/000001 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]
000009/000009 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]

Note, GPU supports the need to "--config = cuda" compile options open.

Known Issues

  • Although you can open the same source tree to compile Cuda Cuda support and disabling supported version, we recommend when switching between two different build configurations, use the "bazel clean" clean up the environment.

  • Must be run before performing bazel compiled configure, or the compiler will fail with an error message. The future, we may consider steps to configure included in the compilation process to streamline the entire process, provided that bazel able to offer new features to support this.

Mac OS X installation

Mac and Linux software needed to rely on exactly the same, but the big difference between the installation process following links to help you install these dependencies on Mac OS X.:

Basel

See this page Mac OS X Installation Guide.

SWIG

Mac OS X installation tutorial .

Note: You need to install PCRE , rather than  PCRE2.

Numpy

See installation tutorial .

Create a package and install pip

$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

# .whl 文件的实际名字与你所使用的平台有关
$ pip install /tmp/tensorflow_pkg/tensorflow-0.5.0-cp27-none-linux_x86_64.whl

Train your first TensorFlow neural network model

Execution path from the root of the source tree:

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

common problem

GPU-related issues

If the following error occurs when you try to run a program TensorFlow:

ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory

Make sure you properly installed GPU support, refer to  the relevant sections .

On Linux

If there is an error:

...
 "__add__", "__radd__",
             ^
SyntaxError: invalid syntax

Solution: Make sure you are using Python version of Python 2.7.

On Mac OS X

If there is an error:

import six.moves.copyreg as copyreg

ImportError: No module named copyreg

Solution: protobuf rely TensorFlow used  six-1.10.0however, Apple's default python environment has been installed.  six-1.4.1The version here can be difficult to upgrade provides several ways to solve this problem:

  1. System-wide upgrade  six:

     sudo easy_install -U six
    
  2. By installing a homebrew isolated python copies:

     brew install python
    
  3. In the virtualenv inner compile or use TensorFlow.

If there is an error:

>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/tensorflow/__init__.py", line 4, in <module>
    from tensorflow.python import *
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 13, in <module>
    from tensorflow.core.framework.graph_pb2 import *
...
  File "/usr/local/lib/python2.7/site-packages/tensorflow/core/framework/tensor_shape_pb2.py", line 22, in <module>
    serialized_pb=_b('\n,tensorflow/core/framework/tensor_shape.proto\x12\ntensorflow\"d\n\x10TensorShapeProto\x12-\n\x03\x64im\x18\x02 \x03(\x0b\x32 .tensorflow.TensorShapeProto.Dim\x1a!\n\x03\x44im\x12\x0c\n\x04size\x18\x01 \x01(\x03\x12\x0c\n\x04name\x18\x02 \x01(\tb\x06proto3')
TypeError: __init__() got an unexpected keyword argument 'syntax'

This is due to the installation protobuf version conflict caused, TensorFlow need is protobuf 3.0.0 is currently the best solution is to ensure protobuf not install an older version, you can use the following command to reinstall the protobuf to resolve the conflict:

brew reinstall --devel protobuf
Published 48 original articles · won praise 121 · views 680 000 +

Guess you like

Origin blog.csdn.net/guoyunfei123/article/details/82429167