Skip to content

Instantly share code, notes, and snippets.

@ruixif
Last active December 28, 2022 18:23
Show Gist options
  • Save ruixif/91d0f3b62cb9dd2e6b644d339cebac22 to your computer and use it in GitHub Desktop.
Save ruixif/91d0f3b62cb9dd2e6b644d339cebac22 to your computer and use it in GitHub Desktop.
How to install tensorflow-macos-2.5.0 on M1

Python 3.9

install numpy

brew install openblas
OPENBLAS="$(brew --prefix openblas)" pip3 install numpy==1.19.3

install zlib and openssl

Maybe some deps rely on openssl@1.1 and some depend on openssl@3.0

brew install zlib openssl

export cpp and linker flags

export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib -L/opt/homebrew/opt/zlib/lib -L/opt/homebrew/opt/openblas/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include -I/opt/homebrew/opt/zlib/include -I/opt/homebrew/opt/openblas/include"

Install h5

export HDF5_DIR=/opt/homebrew/Cellar/hdf5/1.12.2
pip install --no-binary=h5py h5py==3.1.0

install grpcio

GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1 GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1 pip install grpcio==1.34.0

install tf-macos

pip install tensorflow-macos==2.5.0

Test and train, the following script should be in python shell

import tensorflow as tf
tf.__version__

tf.config.list_physical_devices()

import numpy as np


X = np.arange(1, 101, step=0.1)
y = [x**2 for x in X]

X = tf.cast(tf.constant(X), dtype=tf.float32)
y = tf.cast(tf.constant(y), dtype=tf.float32)

model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1)
])

model.compile(
    loss=tf.keras.losses.mean_absolute_error,
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.1),
    metrics=['mean_absolute_error']
)

model.fit(X, y, epochs=100)

model.predict([10, 20, 30])

End of test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment