Building Tensorflow from source on Mac (CPU only) for maximum performance:
TensorFlow is now distributed under an Apache v2 open source license on GitHub.
Step 1. Create new conda environment:
It's a good idea to isolate your dev environment for Tensorflow because:
- It keeps the workspace clean
- Environments can be exported and imported to different machines (In this instance though the point is moot as you'ld be building Tensorflow optimized for your particular system.
$ conda create -n <environment name, say tf> python=<Python version of your choice, say 3.6>
Step 2. Activating your environment :
This where you instruct the shell to use your virtual environment.
$ conda activate <environment name, say tf>
If this step fails, try running this first:
$ source activate <environment name>
Step 3. Install Dependencies:
Conda comes preinstalled with pip and some other basic utils, but the pip version maybe old, so:
$ pip install --upgrade pip
Install numpy:
$ pip install numpy
Step 4. Install Bazel:
To build TensorFlow from source, the Bazel build system must first be installed as follows.
$ brew install bazel
If brew is inot installed:
$ cd /usr/local
$ mkdir homebrew && curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew
Step 5. Install TensorFlow
To obtain the best performance with TensorFlow we recommend building it from source.
First, clone the TensorFlow source code repository:
$ git clone https://github.com/tensorflow/tensorflow
$ cd tensorflow
$ git checkout <version name, say r1.8>
Then run the configure script as follows:
$ ./configure
Output:
Please specify the location of python. [Default is /usr/bin/python]: [enter]
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] n
Configuration finished
Then call bazel to build the TensorFlow pip package:
bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-mfpmath=both --copt=-msse4.2 //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
This will build the package with optimizations for FMA, AVX and SSE.
And finally install the TensorFlow pip package
For Python 2.7.*:
$ sudo pip install --upgrade /tmp/tensorflow_pkg/tensorflow-*.whl
Python 3.*:
$ sudo pip install --upgrade /tmp/tensorflow_pkg/tensorflow-*.whl
Step 6. Test your installation:
To test the installation, open an interactive Python shell and import the TensorFlow module. Make sure you are not in the tensorflow git repo directory, as that will throw error:
$ cd
$ python
import tensorflow as tf
With the TensorFlow module imported, the next step to test the installation is to create a TensorFlow Session, which will initialize the available computing devices and provide a means of executing computation graphs:
>>> sess = tf.Session()
You should now be able to run a Hello World application:
>>> hello_world = tf.constant("Hello, TensorFlow!")
>>> print sess.run(hello_world)
Hello, TensorFlow!
>>> print sess.run(tf.constant(123)*tf.constant(456))
56088