ruk·si

TensorFlow
Basics

Updated at 2016-11-21 18:52

TensorFlow is an open source library for machine learning. TensorFlow is used writing Python but the execution happens at C++ side.

Installation

Install dependencies:

brew install coreutils
# open ~/.bashrc
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
# https://developer.nvidia.com/cuda-downloads
# Select Mac OS X and download the latest local dmg installer.
# Use the installer to intall CUDA Driver, Toolkit and Samples.
# Toolkit should be installed to /Developer/NVIDIA/CUDA-8.0
# or something like that.
# open ~/.bashrc
export CUDA_HOME="/usr/local/cuda"
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$CUDA_HOME/lib"
export PATH="$CUDA_HOME/bin:$PATH"

Adding cuDNN:

  • Create cuDNN account and wait; or download cuDNN somewhere else.
  • Download right cuDNN e.g. cuDNN v5.1 Library for OSX.
# unzip the package, cd into the `cuda` directory
sudo mv include/cudnn.h /Developer/NVIDIA/CUDA-8.0/include/
sudo mv lib/libcudnn* /Developer/NVIDIA/CUDA-8.0/lib
sudo ln -s /Developer/NVIDIA/CUDA-8.0/lib/libcudnn* /usr/local/cuda/lib/
cd ..
rm -r cuda

Compiling CUDA:

# You would most likely encounter the following error in the next step:
# nvcc fatal   : The version ('80000') of the host compiler ('Apple clang')
# is not supported
# If you do, to fix it:
# Go to and sign in: https://developer.apple.com/download
# Find and download Command_Line_Tools_OS_X_10.11_for_Xcode_7.2.dmg
# Use the downloaded installer.
sudo xcode-select --switch /Library/Developer/CommandLineTools
clang --version # check the version
# open XCode and accept the agreements if it asks, quit XCode
cp -r /usr/local/cuda/samples ~/cuda-samples
pushd ~/cuda-samples
sudo make
popd
~/cuda-samples/bin/x86_64/darwin/release/deviceQuery
# The last row should "Result = PASS"

Test that it works:

# These are my virtualenv creation macros :(
vmk2 tensortest # Always use Python 2 with GCML.
v tensortest
pip install --upgrade https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-0.11.0-py2-none-any.whl
touch main.py && open main.py
from __future__ import print_function
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
sudo ln -s /usr/local/cuda/lib/libcuda.dylib /usr/local/cuda/lib/libcuda.1.dylib
python main.py