An older piece from Steven Levy on how Apple incorporates machine learning and AI into its products. Very interesting read.
Category: Neural Networks
This guide is inspired by the tremendous walkthrough on TechPolyMath. It walks through building and installing the TensorFlow binary from source on a macOS system. Mostly I am publishing this also as a reference for myself but others might find it useful
The following assumptions were made, using a 2016 MacBook Pro:
- macOS High Sierra
- Intel Core i5 Processor
- TensorFlow 1.4.1
- Python 3.6.2
- JDK 8u151
- Bazel 0.5.4
Note that the resulting binary we build will be optimized for CPU support only.
Installing Prerequisites on macOS
Install Java Development Kit
At the time of publication, Bazel required the Java Development Kit 8.0. If it is not already installed follow these steps.
- Download the DMG image that containers the JDK installer from Oracle’s Java SE Development Kit 8 Downloads page.
- The file should be named something like
jdk-8u151-macosx-x64.dmg
- Mount the DMG image, double-click the installer, and follow the guided prompts.
Install Dependencies with Anaconda
It is strongly recommended that you use a virtualenv or Conda environment to isolate the packages and installation of TensorFlow from source. This enables you to maintain multiple combinations of package versions for various projects or experiments.
Since I use Anaconda, I created a new environment by executing the command conda create -n tensorflow python=3.6
Install Bazel
Instead of using the popular Homebrew System, I used anaconda. Basically I wanted to get by with as few tools as possible and since I am a Macports user I tried to avoid homebrew. You can refer to the Bazel documentation for alternatives.
- From a Terminal, change into your newly created environment
source activate tensorflow
- Use
anaconda search bazel
to get the right version from conda-forge - Install using
conda install --channel https://conda.anaconda.org/c3i_test2 bazel
- Confirm Bazel is installing by verifying the output of
bazel version
I ran into a weird bug, where tensorflow wouldn’t compile with the conda-forge version of bazel 0.8.1, so had to use the next best version available.
Install Python Dependencies
TensorFlow requires the following Python packages:
- six
- NumPy
- wheel
If these are not present on your machine or current environment execute the following to install:
git clone https://github.com/tensorflow/tensorflow
This will take a short while depending on your Internet connection bandwidth due to the repository’s size (~250 MB).
Most of the time you want install a release version rather than trunk. You can display the available releases and check out one using the following commands (example is TensorFlow release 1.4, since it is the newest release):
git checkout r1.4
Configure and Build the TensorFlow pip Package
Frank Hinek from TechPolyMath posted a script that is based on the work of Sasha Nikiforov and a Stackoverflow post with a few minor modifications to support Anaconda environments.
Clone the build_tf.sh script gist to the same directory containing the TensorFlow source:
chmod u+x build_tf.sh
Execute the script to configure and build the TensorFlow pip package using all of the CPU optimizations available for your CPU:
pip install /tmp/tensorflow_pkg/tensorflow-1.4.1-cp36-cp36m-macosx_10_7_x86_64.whl
Verify Installation
The last step is to confirm that TensorFlow is now installed and working as expected. Before proceeding be sure that you change to a directory other than the one that contains the TensorFlow source tree. We want to import the pip installed package and not the one in the source directory.
Launch the python
interactive shell and execute the following commands:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello)
If the system outputs the following, then TensorFlow is installed and working:
Hello, TensorFlow!
Uninstall everything you don’t need anymore
JDK8
Take a look at this nice page documenting how you get rid of all traces of Java by Oracle: Uninstall Java, Uninstall Java JDK
Conda Dependencies
conda uninstall openjdk
conda uninstall bazel
conda uninstall libcxx
conda uninstall libcxxabi
Tensorflow git Repository
sudo rm -rf tensorflow
And finally you’re done, have fun with your newly compiled and optimized tensorflow on your Macbook. As you will probably notice it’s somewhat faster now. Personally I wonder if MPI support would make any difference on the Macbook, so maybe I try that next.