This is my personal list of to-do things for a new Macbook.
👉 My dockerfiles on Github.
Updated on Mar 22, 2022 : There is no way to install Tensorflow with Docker on Mac M1.

Installation

1# Install XCode (from Appstore)
2
3# Install XCode Command Line Tolls
4xcode-select --install
5
6# Download & install miniforge3
7# <https://github.com/conda-forge/miniforge>
8# (Choose "MacOSX-arm64" version)
9
10chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
11sh ~/Downloads/Miniforge3-MacOSX-arm64.sh
12source ~/miniforge3/bin/activate
13
14# Restart terminal and check
15which python
16# /Users/thi/miniforge3/bin/python
17which pip
18# /Users/thi/miniforge3/bin/pip
1# Init conda with zsh?
2conda init zsh
3source ~/.zshrc

With PyTorch

Make sure the package on anaconda supports osx-arm64 and try:
1pip3 install torch torchvision
1# Verification
2import torch
3x = torch.rand(5, 3)
4print(x)
1# Return
2tensor([[0.3380, 0.3845, 0.3217],
3        [0.8337, 0.9050, 0.2650],
4        [0.2979, 0.7141, 0.9069],
5        [0.1449, 0.1132, 0.1375],
6        [0.4675, 0.3947, 0.1426]])

With Tensorflow

👉 Getting Started with tensorflow-metal PluggableDevice | Apple Official doc (required macOS 12.0+)
1# Create virtual env
2conda create -n ds-tf2.5 python=3.10
3conda activate ds-tf2.5
4
5# Install Tensorflow
6python -m pip install tensorflow
7# (tf <= 2.12)
8python -m pip install tensorflow-macos
9
10# Install metal plugin
11python -m pip install tensorflow-metal
1python
2
3# In python environement
4import tensorflow as tf
5print(tf.__version__)
Or checking the working of GPU, CPU,...
1# Jupyter Notebook
2jupyter lab
Then in the notebook,
1import tensorflow as tf
2
3cifar = tf.keras.datasets.cifar100
4(x_train, y_train), (x_test, y_test) = cifar.load_data()
5model = tf.keras.applications.ResNet50(
6    include_top=True,
7    weights=None,
8    input_shape=(32, 32, 3),
9    classes=100,)
10
11loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
12model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
13model.fit(x_train, y_train, epochs=5, batch_size=64)

Scikit-learn

👉 Install scikit-learn on M1 (official note).
1conda install -y scikit-learn

Troubleshooting

✳️ RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xe
1pip install numpy --upgrade

References

Loading comments...