Skip to content

Instantly share code, notes, and snippets.

@shoya140
Last active August 28, 2021 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoya140/ec7a87e00da8323a41638d4f07b389ae to your computer and use it in GitHub Desktop.
Save shoya140/ec7a87e00da8323a41638d4f07b389ae to your computer and use it in GitHub Desktop.
Install TensorFlow on Mac with Apple Silicon

Install asdf

$ git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.8.1
$ echo ". $HOME/.asdf/asdf.sh\nfpath=(${ASDF_DIR}/completions $fpath)\nautoload -Uz compinit && compinit" >> "~/.zshrc"
$ source ~/.bashrc

Install miniforge python

$ asdf plugin add python
$ asdf install python miniforge3-4.10
$ asdf global python miniforge3-4.10
$ asdf reshim

Install tensorflow

$ conda create --name py38 python=3.8
$ conda activate py38
$ conda install -c apple tensorflow-deps
$ pip install tensorflow-macos
$ pip install tensorflow-metal

Run sample code

import tensorflow as tf

mnist = tf.keras.datasets.mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train, X_test = X_train / 255.0, X_test / 255.0

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10)
])

model.compile(
    optimizer="adam",
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=["accuracy"]
)

model.fit(X_train, y_train, epochs=5)
model.evaluate(X_test, y_test, verbose=2)

Add this python environment as ipykernel

$ conda install jupyter
$ python -m ipykernel install --user --name=py38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment