lightbulb on whiteboard

This article will describe the process of getting Tensorflow working on OSX.  By the end of the article, you will have all the prerequisites installed to allow you to run Tensorflow-based python applications on your MacOS/OSX computer.

Install Homebrew

Homebrew is a package manager for OSX.  It is similar to apt-get on Ubuntu or yum on Redhat linux.  Homebrew requires xcode to be installed. If you do not already have xcode to be installed, the install command below will install it for you. If it needs to install xcode, it will prompt you to press enter.

/usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install”

Install Python 3

OSX comes with python 2 installed by default.  However, the world is moving towards Python 3.  For that reason, I have geared this tutorial towards Python 3.  To install Python 3, run the following:

brew install python3

Install pip3

pip is a package manager for python.  It is not required.  However, it makes it a lot easier to install dependencies as you are doing your development with python.  There are two versions of pip:

  • pip
  • pip3

The first command will install packages for python 2.  While the second command will install packages for python 3.  Below is the command to install pip:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Install Tensorflow

Finally we have installed all of the dependencies and we can install tensorflow.  If you are using python3, you would run the following:

Pip3 install tensorflow

If you are running python 2, you can run the following:

pip install tensorflow

Consolidated commands

In case you want to copy and paste to run it all at once, see below:

/usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install”
brew install python3
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Pip3 install tensorflow

Testing

If you are looking a way to try out your. tensorflow intallation, you can check out this stock predictor written in python:

https://github.com/sebastianheinz/stockprediction

The above github repo contains both the Neural Network code and the training data. There are a few more prerequisites for that script, so to get ready to run it, you will want to run:

  • pip3 install matplotlib
  • pip3 install pandas
  • Pip3 install numpy
  • pip3 install sklearn
  • pip3 install scipy

A good explanation of the stockmarket predictor can be found here:
https://medium.com/mlreview/a-simple-deep-learning-model-for-stock-price-prediction-using-tensorflow-30505541d877

 

Troubleshooting

If you get an error similar to RuntimeWarning: compiletime version 3.5 of module ‘tensorflow.python.framework.fast_tensor_util’ does not match runtime version 3.6 while on OSX high Sierra, the solution is to run the following:
pip3 install –ignore-installed –upgrade “https://github.com/lakshayg/tensorflow-build/raw/master/tensorflow-1.4.0-cp36-cp36m-macosx_10_12_x86_64.whl”