tensorflow源码安装

96
风果
2017.05.24 07:23* 字数 2287

安装tensorflow

下载tensorflow源文件

Gitclone--recurse-submoduleshttps://github.com/tensorflow/tensorflow

安装Bazel,Bazel是Google开源构建工具,类似于Make的工具,用来编译构建tensorflow

http://bazel.io/docs/install.html

Bazel下载地址:

https://github.com/bazelbuild/bazel/releases(bazel-0.2.2b-installer-linux-x86_64.sh)

sudo apt-get install pkg-config zip g++zlib1g-dev unzip

sudo apt-get install build-essential openjdk-8-jdk python zip unzip

./compile.sh install

export PATH="$PATH:$HOME/bin"

或者安装bazel with apt

Using Bazel custom APT repository (recommended)

1. Add Bazel distribution URI as a package source (one time setup)

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list

curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -

If you want to use the JDK 7, please replacejdk1.8withjdk1.7and if youwant to install the testing version of Bazel, replacestablewithtesting.

2. Install and update Bazel

sudo apt-get update&&sudo apt-get install bazel

Once installed, you can upgrade to a newer version of Bazel with:

sudo apt-get upgrade bazel

如果使用mpi, 修改tensorflow/contrib/mpi_collectives/BUILD,加一行

“//tensorflow/stream_executor,”

```

/tensorflow/contrib/mpi_collectives/BUILD

@@ -53,6 +53,7 @@ tf_custom_op_library(

":mpi_defines",

":mpi_message_proto_cc",

"//third_party/mpi",

(+) "//tensorflow/stream_executor",

```

安装tensorflow其他的依赖

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

配置tensorflow,需要你指定相应文件的安装目录。cd进tensorflow源文件。

 ./configure

使用Bazel编译构建

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

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

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

# Lots of output. This tutorial iteratively calculates the major eigenvalue of# a 2x2 matrix, on GPU. The last few lines look like this.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]

OK!

Test:

```

cd /home/lei/tensorflow/tensorflow/models/image/mnist

python convolutional.py

```

https://www.tensorflow.org/install/install_sources#install_the_pip_package

Installing TensorFlow from Sources

This guide explains how to build TensorFlow sources into a TensorFlowbinary and how to install that TensorFlow binary.  Note that we providewell-tested, pre-built TensorFlow binaries for Linux, Mac, and Windowssystems. In addition, there are pre-built TensorFlowdocker images.So, don't build a TensorFlow binary yourself unless you are verycomfortable building complex packages from source and dealing withthe inevitable aftermath should things not go exactly as documented.

If the last paragraph didn't scare you off, welcome.  This guide explains

how to build TensorFlow on the following operating systems:

Ubuntu

Mac OS X

We don't officially support building TensorFlow on Windows; however, you may tryto build TensorFlow on Windows if you don't mind using the highly experimentalBazel on WindowsorTensorFlow CMake build.

Determine which TensorFlow to install

You must choose one of the following types of TensorFlow to build and

install:

TensorFlow with CPU support only. If your system does not have a  NVIDIA® GPU, build and install this version. Note that this version of  TensorFlow is typically easier to build and install, so even if you  have an NVIDIA GPU, we recommend building and installing this version  first.

TensorFlow with GPU support. TensorFlow programs typically run  significantly faster on a GPU than on a CPU. Therefore, if your system  has a NVIDIA GPU and you need to run performance-critical applications,  you should ultimately build and install this version.  Beyond the NVIDIA GPU itself, your system must also fulfill the NVIDIA  software requirements described in one of the following documents:

Installing TensorFlow on Ubuntu

Clone the TensorFlow repository

Start the process of building TensorFlow by cloning a TensorFlow

repository.

To clonethe latestTensorFlow repository, issue the following command:

$git clone git://github.com/tensorflow/tensorflow

The precedinggit clonecommand creates a subdirectorynamedtensorflow.  After cloning, you may optionally build aspecific branch(such as a release branch) by invoking thefollowing commands:

$cd tensorflow$git checkoutBranch# whereBranchis the desired branch

For example, to work with ther1.0release instead of the master release,issue the following command:

$git checkout r1.0

Next, you must prepare your environment forLinuxorMac OS

Prepare environment for Linux

Before building TensorFlow on Linux, install the following build

tools on your system:

bazel

TensorFlow Python dependencies

optionally, NVIDIA packages to support TensorFlow for GPU.

Install Bazel

If bazel is not installed on your system, install it now by followingthese directions.

Install TensorFlow Python dependencies

To install TensorFlow, you must install the following packages:

numpy, which is a numerical processing package that TensorFlow requires.

dev, which enables adding extensions to Python.

pip, which enables you to install and manage certain Python packages.

wheel, which enables you to manage Python compressed packages in    the wheel (.whl) format.

To install these packages for Python 2.7, issue the following command:

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

To install these packages for Python 3.n, issue the following command:

$sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel

Optional: install TensorFlow for GPU prerequisites

If you are building TensorFlow without GPU support, skip this section.

The following NVIDIAhardwaremust be installed on your system:

GPU card with CUDA Compute Capability 3.0 or higher.  SeeNVIDIA documentationfor a list of supported GPU cards.

The following NVIDIAsoftwaremust be installed on your system:

NVIDIA's Cuda Toolkit (>= 7.0). We recommend version 8.0.    For details, seeNVIDIA's documentation.    Ensure that you append the relevant Cuda pathnames to theLD_LIBRARY_PATHenvironment variable as described in the    NVIDIA documentation.

The NVIDIA drivers associated with NVIDIA's Cuda Toolkit.

cuDNN (>= v3). We recommend version 5.1. For details, seeNVIDIA's documentation,    particularly the description of appending the appropriate pathname    to yourLD_LIBRARY_PATHenvironment variable.

Finally, you must also installlibcupti-devby invoking the followingcommand:

$sudo apt-get install libcupti-dev

Next

After preparing the environment, you must nowconfigure the installation.

Prepare environment for Mac OS

Before building TensorFlow, you must install the following on your system:

bazel

TensorFlow Python dependencies.

optionally, NVIDIA packages to support TensorFlow for GPU.

Install bazel

If bazel is not installed on your system, install it now by followingthese directions.

Install python dependencies

To install TensorFlow, you must install the following packages:

six

numpy, which is a numerical processing package that TensorFlow requires.

wheel, which enables you to manage Python compressed packages

in the wheel (.whl) format.

You may install the python dependencies using pip. If you don't have pipon your machine, we recommend using homebrew to install Python and pip asdocumented here.If you follow these instructions, you will not need to disable SIP.

After installing pip, invoke the following commands:

$sudo pip install six numpy wheel

Optional: install TensorFlow for GPU prerequisites

If you do not have brew installed, install it by followingthese instructions.

After installing brew, install GNU coreutils by issuing the following command:

$brew install coreutils

If you want to compile tensorflow and have XCode 7.3 and CUDA 7.5 installed,

note that Xcode 7.3 is not yet compatible with CUDA 7.5.  To remedy this

problem, do either of the following:

Upgrade to CUDA 8.0.

Download Xcode 7.2 and select it as your default by issuing the following

command:

$sudo xcode-select -s /Application/Xcode-7.2/Xcode.app

NOTE:Your system must fulfill the NVIDIA software requirements describedin one of the following documents:

Installing TensorFlow on Linux

Installing TensorFlow on Mac OS

Configure the installation

The root of the source tree contains a bash script namedconfigure. This script asks you to identify the pathname of allrelevant TensorFlow dependencies and specify other build configuration optionssuch as compiler flags. You must run this scriptpriortocreating the pip package and installing TensorFlow.

If you wish to build TensorFlow with GPU,configurewill askyou to specify the version numbers of Cuda and cuDNN. If severalversions of Cuda or cuDNN are installed on your system, explicitly selectthe desired version instead of relying on the system default.

Here is an example execution of theconfigurescript.  Note that yourown input will likely differ from our sample input:

$cd tensorflow# cd to the top-level directory created$

./configure

Please specify the location of python. [Default is /usr/bin/python]:/usr/bin/python2.7

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:Do you wish to use jemalloc as the malloc implementation? [Y/n]jemalloc enabledDo you wish to build TensorFlow with Google Cloud Platform support? [y/N]No Google Cloud Platform support will be enabled for TensorFlowDo you wish to build TensorFlow with Hadoop File System support? [y/N]No Hadoop File System support will be enabled for TensorFlowDo you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]No XLA JIT support will be enabled for TensorFlowFound possible Python library paths:  /usr/local/lib/python2.7/dist-packages  /usr/lib/python2.7/dist-packagesPlease input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/dist-packages]Using python library path: /usr/local/lib/python2.7/dist-packagesDo you wish to build TensorFlow with OpenCL support? [y/N] NNo OpenCL support will be enabled for TensorFlowDo you wish to build TensorFlow with CUDA support? [y/N] YCUDA support will be enabled for TensorFlowPlease specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]:8.0Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:Please specify the cuDNN version you want to use. [Leave empty to use system default]:5Please specify the location where cuDNN 5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:Please specify a list of comma-separated Cuda compute capabilities you want to build with.You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.Please note that each additional compute capability significantly increases your build time and binary size.[Default is: "3.5,5.2"]:3.0Setting up Cuda includeSetting up Cuda libSetting up Cuda binSetting up Cuda nvvmSetting up CUPTI includeSetting up CUPTI lib64Configuration finished

If you toldconfigureto build for GPU support, thenconfigurewill create a canonical set of symbolic links to the Cuda librarieson your system.  Therefore, every time you change the Cuda library paths,you must rerun theconfigurescript before re-invokingthebazel buildcommand.

Note the following:

Although it is possible to build both Cuda and non-Cuda configs    under the same source tree, we recommend runningbazel cleanwhen    switching between these two configurations in the same source tree.

If you don't run theconfigurescriptbeforerunning thebazel buildcommand, thebazel buildcommand will fail.

Build the pip package

To build a pip package for TensorFlow with CPU-only support,

invoke the following command:

$bazel build --config=opt --config=mkl-dnn //tensorflow/tools/pip_package:build_pip_package

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

To build a pip package for TensorFlow with GPU support,

invoke the following command:

$bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_packag

Install the pip package

Invokepip installto install that pip package.The filename of the.whlfile depends on your platform.For example, the following command will install the pip packagefor TensorFlow 1.1.0 on Linux:

$sudo pip install /tmp/tensorflow_pkg/tensorflow-1.1.0-py2-none-any.whl

Validate your installation

Validate your TensorFlow installation by doing the following:

Start a terminal.

Change directory (cd) to any directory on your system other than thetensorflowsubdirectory from which you invoked theconfigurecommand.

Invoke python:

$python

Enter the following short program inside the python interactive shell:

>>>importtensorflowastf

>>>hello=tf.constant('Hello, TensorFlow!')

>>>sess=tf.Session()

>>>print(sess.run(hello))

If the system outputs the following, then you are ready to begin writing

TensorFlow programs:

Hello, TensorFlow!

If you are new to TensorFlow, seeGetting Started with

TensorFlow.

If the system outputs an error message instead of a greeting, seeCommon

installation problems.

Common installation problems

The installation problems you encounter typically depend on the

operating system.  See the "Common installation problems" section

of one of the following guides:

Installing TensorFlow on Linux

Beyond the errors documented in those two guides, the following tablenotes additional errors specific to building TensorFlow.  Note that weare relying on Stack Overflow as the repository for build and installationproblems.  If you encounter an error message not listed in the precedingtwo guides or in the following table, search for it on Stack Overflow.  IfStack Overflow doesn't show the error message, ask a new question onStack Overflow and specify thetensorflowtag.

日记本