More

    Installing TensorFlow and Jupyter Notebook on Apple Silicon Macs

    Learn how to install configure TensorFlow with Jupyter Notebook for use on your M1/M2 Macs

    Photo by Vlad Yera on Unsplash

    If you are into machine learning or deep learning, you would no doubt be familiar with the Anaconda package and Jupyter Notebook. And for those of you who have just gotten your brand new shiny MacBook Air or Mac Studio running Apple Silicon, you may be a little disappointed to realize that the TensorFlow package does not work when you try to install it in Anaconda.

    Last year, I wrote an article on how to install TensorFlow on your Apple Silicon Mac:

    This year I got myself a Mac Studio, and so I wanted to see what has changed. And so in this article, I will walk you through the latest steps you can take to install TensorFlow on your Apple Silicon Mac. Good news is that the procedure is now much more straight-forward. So let’s jump right into it!

    Installing TensorFlow on Apple Silicon is, unfortunately, not as straightforward as installing Anaconda on Windows and then using the pip install tensorflow command. And here’s why.

    TensorFlow does not support officially the Apple Silicon — what this means is that TensorFlow does not have packages precompiled for the Apple Silicon architecture (which is arm64).

    TensorFlow distributes official wheels only for x86 architectures (Linux, Windows, Mac (on Intel chips)), and Raspberry PI (arm64).

    To overcome this limitation, Apple maintains its own tensorflow-macospackage:

    Image by author

    Using the PluggableDevice API of Tensorflow, Apple is able to translate TensorFlow operations to code that the GPU of the Apple Silicon (M1 and M2) can understand.

    You can learn more about the PluggableDevice API of TensorFlow from https://blog.tensorflow.org/2021/06/pluggabledevice-device-plugins-for-TensorFlow.html

    The tensorflow-macospackage relies on two other packages:

    • tensorflow-deps — the dependencies to run Tensorflow on arm64, e.g. python, numpy, grpcio and h5py.
    • tensorflow-metal — a plugin to ensure that TensorFlow is able to run on Metal

    The Metal framework gives your app direct access to a device’s graphics processing unit (GPU). Essentially, this means your TensorFlow code is now able to take advantage of the powerful GPU available in the new Apple Silicon.

    Note that without the tensorflow-metal package, your TensorFlow code would be still be able to run on your Apple Silicon Mac, just that TensorFlow won’t be able to leverage the GPU of the M1 or M2 (it can only use the CPU).

    Installing TensorFlow on your Apple Silicon Mac is straight-forward. I have listed the steps in the following sections.

    Downloading Conda

    Instead of using Anaconda, you will use MiniForge, a lightweight Python interpreter with full access to the Conda ecosystem.

    Conda is an open source package and environment management system that runs on Windows, Mac OS and Linux.

    For Apple Silicon machines, you can directly download the MinForge installer from: https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh

    Once the Miniforge3-MacOSX-arm64.sh file is downloaded, open Terminal and type in the following commands:

    $ chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
    $ sh ~/Downloads/Miniforge3-MacOSX-arm64.sh

    Follow the installation instructions on-screen. Once the installation is completed, a base Python environment is created for you. You can verify this by using this command:

    $ conda info --envs
    # conda environments:
    #
    base * /Users/weimenglee/miniforge3

    Activating the Environment

    Activate the base environment using the following command:

    $ source ~/miniforge3/bin/activate

    If your machine has only one single environment, this step is optional.

    Installing TensorFlow Dependencies

    Type the following command to install the dependencies needed by TensorFlow (the tensorflow-deps package):

    $ conda install -c apple tensorflow-deps

    Installing TensorFlow

    Type the following command to install the TensorFlow for Mac OS:

    $ python -m pip install tensorflow-macos

    Type the following command to install the tensorflow-metal package:

    $ python -m pip install tensorflow-metal

    Besides installing the TensorFlow libraries, you would also very likely need to install other packages like matplotlib, sklearn, OpenCV, Pandas, etc, for your machine learning and deep learning projects. So go ahead and install them as follows:

    $ conda install -c conda-forge matplotlib -y
    $ conda install -c conda-forge scikit-learn -y
    $ conda install -c conda-forge opencv -y
    $ conda install -c conda-forge pandas -y

    The -y option saves you the trouble of answering the confirmation when installing the libraries.

    Installing Jupyter Notebook

    Finally, you would want to install Jupyter Notebook:

    $ conda install notebook -y

    That’s it! To test if Jupyter Notebook is installed correctly, launch it:

    $ jupyter notebook

    If you can see the following, you are all set!

    Image by author

    Create a new Jupyter Notebook and type in the following code:

    import tensorflow as tf
    print(tf.__version__)
    print(tf.keras.__version__)

    If you do not see any errors reported, you should see an output similar to the following:

    2.9.2
    2.9.0

    In this case, congratulations! You have successfully installed TensorFlow!

    If you are upgrading to a newer version of TensorFlow, you can follow the steps below:

    • Uninstall the tensorflow-macos and tensorflow-metal packages:
    $ python -m pip uninstall tensorflow-macos
    $ python -m pip uninstall tensorflow-metal
    • Reinstall thetensorflow-deps package:
    $ conda install -c apple tensorflow-deps --force-reinstall

    If you TensorFlow is installed in a particular enviroment, then add the -n option to the command above.

    • Reinstall tensorflow-macos package:
    $ python -m pip install tensorflow-macos

    I hope this short article makes it easy for you to install TensorFlow on your Apple Silicon Mac. For long-time Anaconda users, take note that in this article you are now using MiniForge, and I have only installed some commonly used packages like matplotlib, sklearn, OpenCV, and Pandas. As you work on your projects, it is likely that you need to manually install your required packages using the pip command (whereas if you use Anaconda a lot of packages are already pre-installed). Nevertheless, this is just a little inconvenience and never a deal breaker. Have fun with TensorFlow!

    Installing TensorFlow and Jupyter Notebook on Apple Silicon Macs Republished from Source https://towardsdatascience.com/installing-tensorflow-and-jupyter-notebook-on-apple-silicon-macs-d30b14c74a08?source=rss----7f60cf5620c9---4 via https://towardsdatascience.com/feed

    Recent Articles

    spot_img

    Related Stories

    Stay on op - Ge the daily news in your inbox