tensorflow源码安装

安装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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,214评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,307评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,543评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,221评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,224评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,007评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,313评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,956评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,441评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,925评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,018评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,685评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,234评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,240评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,464评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,467评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,762评论 2 345

推荐阅读更多精彩内容