Set-up your Mac for Deep Learning with Python, Tensorflow and Keras. March 9, 2017. Download and install Anaconda Python. Mar 10, 2017 Set-up your Mac for Deep Learning with Python, Tensorflow and Keras. March 9, 2017. Download and install Anaconda Python. Anaconda is the leading open data science platform powered by Python. The open source version of Anaconda is a high performance distribution of Python and R and includes over 100 of the most popular Python.
Google provides two methods for installing TensorFlow, and the simpler option involves installing precompiled packages. This discussion presents a three-step process for installing these packages:
- Install Python on your development system.
- Install the pip package manager.
- Use pip to install TensorFlow.
The second installation method involves compiling TensorFlow from its source code. This option takes time and effort, but you can obtain better performance because your TensorFlow package will take the fullest advantage of your processor’s capabilities.
Install Python and pip/pip3
TensorFlow supports development with Java and C++, but this discussion focuses on Python. Python 3 is used in the example code, but you’re welcome to use Python 2.
Python’s official package manager is pip, which is a recursive acronym that stands for “pip installs Python.” To install packages like TensorFlow, you can use pip on Python 2 systems or pip3 on Python 3 systems. Package management commands have the following format:
pip and pip3 accept similar commands and perform similar operations. For example, executing
pip list
or pip3 list
prints all the Python packages installed on your system. The table lists this and five other commands.Package Management Commands
Command Name | Description |
install | Installs a specified package |
uninstall | Uninstalls a specified package |
download | Downloads a package, but doesn’t install it |
list | Lists installed packages |
show | Prints information about a specified package |
search | Searches for a package whose name or summary contains the given text |
For this discussion, the most important command to know is
pip install
and pip3 install
. But keep in mind that pip/pip3 can perform many other operations.If you execute a TensorFlow application using a precompiled package, you may receive messages like “The TensorFlow library wasn’t compiled to use XYZ instructions, but these are available on your machine and could speed up CPU computations.” To turn off these messages, create an environment variable named
TF_CPP_MIN_LOG_LEVEL
and set its value to 3.Installing on Mac OS
Many versions of Mac OS have Python already installed, but you should obtain and install a new Python package. If you visit Python Software Foundation, you see one button for Python 2 and another for Python 3. If you click one of these buttons, your browser downloads a PKG file that serves as the Python installer.
When you launch the installer, the Python installation dialog box appears. To install the package, follow these five steps:
- In the Introduction page, click the button labeled Continue.
- In the Read Me page, click the button labeled Continue.
- In the License page, click the button labeled Continue and then click Agree to accept the software license agreement.
- In the Installation Type page, click Install to begin the installation process, entering your password, if necessary.
- When the installation is complete, click Close to close the dialog box.
If the installation completes successfully, you can run
pip
or pip3
on a command line. You can install TensorFlow with the following command:This command tells the package manager to download TensorFlow, TensorBoard, and a series of dependencies. One dependency is six, which supports compatibility between Python 2 and 3. If the installation fails due to a preinstalled six package, you can fix the issue by executing the following command:
This command tells pip to install
six
on top of the existing installation. After this installation completes, you should be able to run pip install tensorflow
without error. On the system used here, the installer stores the TensorFlow files in the /Library/Frameworks/Python.framework/Versions/<ver>/lib/python<ver>/site-packages/tensorflow
directory.Installing on Linux
Many popular distributions of Linux are based on Debian, including Ubuntu and Linux Mint. These distributions rely on the Advanced Package Tool (APT) to manage packages, which you can access on the command line by entering
apt-get
. This discussion explains how to install TensorFlow on these and similar operating systems.Most Linux distributions already have Python installed, but it’s a good idea to install the full development version and pip/pip3. The following command installs both for Python 2:
Alternatively, the following command performs the installation for Python 3:
After installation completes, you should be able to execute
pip
or pip3
on the command line. The following command installs the TensorFlow package and its dependencies (use pip3
for Python 3):This command installs TensorFlow, TensorBoard, and their dependencies. On an Ubuntu system, the installer stores the files in the
/usr/local/lib/python<ver>/dist-packages/tensorflow
directory.Installing on Windows
For Windows users, TensorFlow’s documentation specifically recommends installing a 64-bit version of Python 3.5. To download the installer, visit Python Software Foundation, find a version of Python 3, and click the link entitled Windows x86-64 executable installer. This downloads an
*.exe
file that serves as the installer.When you launch the installer, the Python setup dialog box appears. The following steps install Python on your system:
- Check the checkbox for adding the Python installation directory to the PATH variable.
- Click the link labeled Install Now.
- When installation finishes, click the Close button to close the installer.
After you install Python, you should be able to run pip3 on a command line. You can install TensorFlow with the following command:
The package manager downloads TensorFlow, TensorBoard, and the packages’ dependencies. On my Windows system, the installer stores the files to the
C:Users<name>AppDataLocalProgramsPythonPython<ver>Libsite-packagestensorflow
directory.Intel Math Kernel Library (MKL) for Intel based systems accelerate math processing routines, increase application performance, and reduce development time. This ready-to-use math library includes:
Linear Algebra | Fast Fourier Transforms (FFT) | Deep Neural Networks | Vector Statistics & Data Fitting | Vector Math & Miscellaneous Solvers
This is my first successful build of Tensorflow which has integrated with MKL-DNN 2018 Initial Release. You can download the compiled pip wheel file (.whl) through Release section. This build has enabled support of AVX, SSE4 features on Intel CPU for better performance.
Please remind that this build is NOT for CUDA GPU. The intention to utilize Intel MKL is to accelerate Intel Core i5 (Haswell) or above CPU (not GPU) on Mac computer which has well-known limit of OpenCL support on its integrated Graphic processor (not even has SYCL/ComputeCPP support for Mac at the moment).
For OpenCL 1.2 support release of Tensorflow, please see https://github.com/hughperkins/tf-coriander.
Please remind that this is just a guideline and you might want to extend it a bit further with the latest version of Tensorflow framework. Let us know if you have any success on it.
Here's the build instruction:
System: Mac OS X 10.12.6
Intel MKL-DNN library: mklml_mac_2018.0.20170908.tgz (from https://github.com/01org/mkl-dnn/releases)
Tensorflow release: 1.3.1 (https://github.com/tensorflow/tensorflow/releases)
Bazel release: 0.5.4 (https://github.com/bazelbuild/bazel/releases)
Installed MKL to /opt/intel/mklml
$ tar -xvzf mklml_mac_2018.0.20170908.tgz
$ mv mklml_mac_2018.0.20170908 /opt/intel/mklml
Symlink the two .dylib files (lib/libmklml.dylib, lib/libiomp5.dylib) to /usr/local/lib
$ ln -sf /opt/intel/mklml/lib/libmklml.dylib /usr/local/lib/libmklml.dylib
$ ln -sf /opt/intel/mklml/lib/libiomp5.dylib /usr/local/lib/libiomp5.dylib
Remember to assign proper file ownership of the .dylib links in order to avoid errors at the later stage:
$ chown $(whoami):staff /usr/local/lib/libmklml.dylib
$ chown $(whoami):staff /usr/local/lib/libiomp5.dylib
Assign correct 'install name' to the two .dylib symlinks in order to avoid any compiling error:
$ install_name_tool -id '/opt/intel/mklml/lib/libmklml.dylib' /usr/local/lib/libmklml.dylib
Tensorflow Download Mac
$ install_name_tool -id '/opt/intel/mklml/lib/libiomp5.dylib' /usr/local/lib/libiomp5 .dylib
Download and extract Tensorflow:
$ wget https://github.com/tensorflow/tensorflow/archive/v1.3.1.tar.gz
$ tar -xzvf v1.3.1.tar.gz
Download Tensorflow Mac Download
Supposing new folder tensorflow-1.3.1 has been extracted:
$ cd tensorflow-1.3.1
Key changes to edit the file tensorflow-1.3.1/configure:
- Change MKL_ML_LIB_PATH to lib/libmklml.dylib
- Change MKL_ML_OMP_LIB_PATH to lib/libiomp5.dylib
- Comment out the parts that say 'if linux:'
- Comment out the parts that say 'Darwin is not supported' and 'exit' clause
- Comment out the parts that deal with libdl.so.2
Changes to tensorflow-1.3.1/third_party/mkl/BUILD:Change the list of three .so files to the two .dylib files
Start configuring the source:
$ ./configure
Type Yes to use MKL, No to download MKL.MKL installed separately. Path to MKL: /opt/intel/mklml
Apple builtin LLVM (clang) doesn't support compiler option like '-fopenmp' so let's skip checking it by modifying tensorflow/tensorflow.bzl:
- Find the line:
]) + if_cuda(['-DGOOGLE_CUDA=1']) + if_mkl(['-DINTEL_MKL=1', '-fopenmp',]) + if_android_arm(
- Then remove the value '-fopenmp' from the statement:
]) + if_cuda(['-DGOOGLE_CUDA=1']) + if_mkl(['-DINTEL_MKL=1']) + if_android_arm(
Build command:
$ cd tensorflow-1.3.1
$ bazel build -c opt --config=opt --config=mkl --copt='-DEIGEN_USE_VML' --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-msse4.1 --copt=-msse4.2 --linkopt='-Wl,-rpath,/opt/intel/mklml/lib' --linkopt='-L/opt/intel/mklml/lib' --linkopt='-lmklml' --linkopt='-iomp5' //tensorflow/tools/pip_package:build_pip_package
This build process lasted for more than half hour on my Mac.
Once the build process is completed, let's check the wheel out:
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
Double check if the file is there:
$ ls -l /tmp/tensorflow_pkg/tensorflow-1.3.1-cp27-cp27m-macosx_10_12_intel.whl
Finally, use pip to install the wheel:
$ pip install --ignore-installed --upgrade /tmp/tensorflow_pkg/tensorflow-1.3.1-cp27-cp27m-macosx_10_12_intel.whl
After the installation, let's have a fast (10x-50x) implementation of protobuf to boost the performance:
(*This is also the solution to KeyError: 'Couldn't find enum google.protobuf.MethodOptions.IdempotencyLevel' while running command like 'import tensorflow' within python)
For Python 2.7:
pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/protobuf-3.1.0-cp27-none-macosx_10_11_x86_64.whl
For Python 3.n:
$ pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/protobuf-3.1.0-cp35-none-macosx_10_11_x86_64.whl
To check if the Tensorflow is running properly, try the followings:
$ python
If no error message appears, then Tensorflow is imported successfully in Python.
Time to start your spiritual work in Deep Learning!