Howto Install Tensorflow on Mac OS

This guide is for installing Tensorflow on an Apple Silicon Mac. In this example I have tested it on MacOS Sequoia 15.3.

# Ensure xcode tools are installed
xcode-select --install

# Setup env
mkdir metal_tensor
cd metal_tensor
python3 -m venv .
source bin/activate
python -m pip install -U pip

# The base TensorFlow Install > 2.13
python -m pip install tensorflow

# TensorFlow metal plugin
python -m pip install tensorflow-metal

Testing TensorFlow Mac OS install

# Source: https://developer.apple.com/metal/tensorflow-plugin/
import tensorflow as tf

cifar = tf.keras.datasets.cifar100
(x_train, y_train), (x_test, y_test) = cifar.load_data()
model = tf.keras.applications.ResNet50(
    include_top=True,
    weights=None,
    input_shape=(32, 32, 3),
    classes=100,)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)

To test the install quickly we can run the simple Hello, World TensorFlow python script. The above script is more in depth and may take a while depending on your hardware.

Leave a Reply

Your email address will not be published. Required fields are marked *