ubuntu16.04 install tensorflow from source
原文链接 https://hlthu.github.io/tensorflow/2017/01/15/ubuntu-install-tensorflow.html
注:以下为加速网络访问所做的原文缓存,经过重新格式化,可能存在格式方面的问题,或偶有遗漏信息,请以原文为准。
This page is going to tell how to install tensorflow on ubuntu 16.04 from the github sources. I sugget you to use conda or miniconda as your python, then you can skip section 6: Create the pip package and install.
1. Clone the TensorFlow
git clone https://github.com/tensorflow/tensorflow
Note that these instructions will install the latest master branch of tensorflow. If you want to install a specific branch (such as a release branch), pass -b <branchname>
to the git clone command and --recurse-submodules
for r0.8 and earlier to fetch the protobuf library that TensorFlow depends on.
2. Prepare environment
2.1 Install Pip
When installing from source you will build a pip wheel that you then install using pip. You'll need pip for that, so install it.
sudo apt-get install python-pip
2.2 Install Bazel
2.2.1 Dependencies for bazel
First please install java. For Ubuntu 16.04
sudo apt-get install openjdk-8-jdk
If you are using Ubuntu 14.04 or a lower version, then install java by
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
Then install other required packages, by
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip
Maybe some of them is install before.
2.2.2 Download Bazel
Then download the Bazel installer for your operating system. Download url: https://github.com/bazelbuild/bazel/releases.
2.2.3 Run the installer
chmod +x bazel-version-installer-os.sh
./bazel-version-installer-os.sh --user
The --user
flag installs Bazel to the $HOME/bin
directory on your system and sets the .bazelrc
path to $HOME/.bazelrc
. Use the --help
command to see additional installation options.
2.2.4 Set up your environment
If you ran the Bazel installer with the --user flag as above, the Bazel executable is installed in your $HOME/bin
directory. It's a good idea to add this directory to your default paths, as follows:
export PATH=$PATH:$HOME/bin
You can also add this command to your ~/.bashrc
file.
2.3 Install other dependencies
If you are use system's python, then
# For Python 2.7:
sudo apt-get install python-numpy python-dev python-wheel python-mock
# For Python 3.x:
sudo apt-get install python3-numpy python3-dev python3-wheel python3-mock
If you are using conda or miniconda, you can install these by
conda install numpy wheel mock
3. Install CUDA(Optional)
If you have a or some GPUs on your machine, and check that their computing capacity is >=3.0, tensorflow don't support GPU with a lower computing capacity.
For how to install cuda and cudnn, you are visit my another blog: ubuntu16.04上配置nvidia驱动、cuda和cudnn.
After you install cuda and cudnn, then install other dependencies
sudo apt-get install libcupti-dev
4. Install OpenCL(Optional)
If you want to install opencl, please follow the instructions here.
5. Configure the installation
Run the ./configure
script at the root of the tree. The configure script asks you for the path to your python interpreter and allows (optional) configuration of the CUDA libraries.
This step is used to locate the python and numpy header files as well as enabling GPU support if you have a CUDA enabled GPU and Toolkit installed. Select the option Y when asked to build TensorFlow with GPU support.
If you have several versions of Cuda or cuDNN installed, you should definitely select one explicitly instead of relying on the system default.
For example:
./configure
Please specify the location of python. [Default is /usr/bin/python]:
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] N
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with GPU support? [y/N] y
Do you wish to build TensorFlow with OpenCL support? [y/N] N
GPU support will be enabled for TensorFlow
Please specify which gcc nvcc should use 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.0
Please 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]: 5
Please 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.
Setting up Cuda include
Setting up Cuda lib
Setting up Cuda bin
Setting up Cuda nvvm
Setting up CUPTI include
Setting up CUPTI lib64
Configuration finished
This creates a canonical set of symbolic links to the Cuda libraries on your system. Every time you change the Cuda library paths you need to run this step again before you invoke the bazel build command.
6. Create the pip package and install
When building from source, you will still build a pip package and install that.
But if you are on a machine that is not for you only, and you are using conda or miniconda for python, if you don't want to install these for all users. you can skip this and go to Setting up TensorFlow for Development.
# first, without CUDA
bazel build -c opt //tensorflow/tools/pip_package:build_pip_package
# To build with support for CUDA:
bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
# Alternatively, to build with support for OpenCL:
bazel build -c opt --config=sycl //tensorflow/tools/pip_package:build_pip_package
# then
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
# last, the name of the .whl file will depend on your platform.
$ sudo pip install /tmp/tensorflow_pkg/tensorflow-0.12.1-py2-none-any.whl
7. Setting up TensorFlow for Development
If you're working on TensorFlow itself, it is useful to be able to test your changes in an interactive python shell without having to reinstall TensorFlow.
To set up TensorFlow such that all files are linked (instead of copied) from the system directories, run the following commands inside the TensorFlow root directory:
bazel build -c opt //tensorflow/tools/pip_package:build_pip_package
# To build with GPU support:
bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
mkdir _python_build
cd _python_build
ln -s ../bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow/* .
ln -s ../tensorflow/tools/pip_package/* .
python setup.py develop
8. Using python to test
To make sure if you have install pyton, you can do the following test.
$ 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
>>>
9. Train your first TensorFlow neural net model
Start by cloning the TensorFlow models repo from GitHub. Run the following commands:
$ cd models/tutorials/image/mnist
$ python convolutional.py
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Successfully 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%
...
For more tutorials, pleaese vsit here.
Reference
- https://www.tensorflow.org/versions/r0.12/tutorials/index.html
- https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md
- http://bazel.build/versions/master/docs/install.html