Deep Learning (TensorFlow, Jupyterlab, VSCode) on Apple Silicon M1 Mac

Xcode

Install Xcode from App Store.

Command Line Tools

Install Xcode Command Line Tools by downloading it from Apple Developer or by typing:

$ xcode-select --install

Homebrew

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Miniforge

Anaconda cannot run on M1, Miniforge is used to replace it.

Download the Miniforge3-MacOSX-arm64 from https://github.com/conda-forge/miniforge.

Install Miniforge using the terminal.

$ bash Miniforge3-MacOSX-arm64.sh

Restart the terminal and check it.

$ which python
/Users/catchzeng/miniforge3/bin/python
$ which pip
/Users/catchzeng/miniforge3/bin/pip

Download Apple TensorFlow

Download TensorFlow from https://github.com/apple/tensorflow_macos/releases, untar it, and go under the arm64 directory.

Create virtual environment

Create and activate a conda virtual environment with python 3.8 (as required for ATF 2.4).

$ conda create -n tensorflow python=3.8
$ conda activate tensorflow

Install needed packages

$ brew install libjpeg$ conda install -y pandas matplotlib scikit-learn jupyterlab

Note: libjpeg is a required dependency for matplotlib.

Install specific pip version and some other base packages

$ pip install --force pip==20.2.4 wheel setuptools cached-property six packaging

Note: Apple TensorFlow needs a specific pip version.

Install packages(numpy, grpcio, h5py) provided by Apple

$ pip install --upgrade --no-dependencies --force numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl h5py-2.10.0-cp38-cp38-macosx_11_0_arm64.whl

Install additional packages

$ pip install absl-py astunparse flatbuffers gast google_pasta keras_preprocessing opt_einsum protobuf tensorflow_estimator termcolor typing_extensions wrapt wheel tensorboard typeguard

Install TensorFlow

$ pip install --upgrade --no-dependencies --force tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl$ pip install --upgrade --no-dependencies --force 
tensorflow_addons_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl

Finally, upgrade the pip to give the developers the correct version.

$ pip install --upgrade pip

Test

TensorFlow

$ python
Python 3.8.8 | packaged by conda-forge | (default, Feb 20 2021, 15:50:57)
[Clang 11.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> print(tf.__version__)
2.4.0-rc0
>>>

JupyterLab

$ jupyter lab
from tensorflow.keras import layers
from tensorflow.keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)
test_loss, test_acc = model.evaluate(test_images, test_labels)
test_acc

VSCode

Install Python support

Select virtualenv and trust the notebook

Run the notebook

Reference

Deep Learning and DevOps enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store