Contributed by Mark (Zixuan) Song
Apple has updated Tensorflow (for MacOS) so that installation on M-series (until M2) and Intel-series Mac can follow the exact same procedure.
For completely new Macos or Macos without Xcode installed.
If you have Xcode installed, skip to Install TC backends.
Need graphical access to the machine.
Run xcode-select --install
to install if on optimal internet.
Or Download it from Apple Command Line Tools installation image then install it if the internet connection is weak.
There are four backends to choose from, Numpy, Tensorflow, Jax, and Torch.
pip install [Package Name]
If you wish to install Tensorflow optimized for MacOS (tensorflow-macos
) or Tensorflow GPU optimized (tensorflow-metal
) please install miniconda.
If you wish to install Vanilla Tensorflow developed by Google (tensorflow
) please skip this step.
curl -o ~/miniconda.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
bash ~/miniconda.sh -b -p $HOME/miniconda
source ~/miniconda/bin/activate
conda install -c apple tensorflow-deps
pip install tensorflow
If you wish to use tensorflow-metal PluggableDevice, then continue install (not recommended):
pip install tensorflow-metal
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=True)
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)
pip install tensorcircuit
This data is collected by running benchmarks/scripts/vqe_tc.py
10 times and average results.
Vanilla Tensorflow | Apple Tensorflow | Apple Tensorflow with Metal Plugin | |
---|---|---|---|
Construction Time | 11.49241641s | 11.31878941s | 11.6103961s |
Iteration time | 0.002313011s | 0.002333004s | 0.046412581s |
Total time | 11.72371747s | 11.55208979s | 16.25165417s |
Until July 2023, this has been tested on Intel Macs running Ventura, M1 Macs running Ventura, M2 Macs running Ventura, and M2 Macs running Sonoma beta.