Skip to content

Instantly share code, notes, and snippets.

@mrdbourke
Created November 27, 2021 03:11
Show Gist options
  • Save mrdbourke/79aef5b47607c2306e6dc10830b54505 to your computer and use it in GitHub Desktop.
Save mrdbourke/79aef5b47607c2306e6dc10830b54505 to your computer and use it in GitHub Desktop.
Setting up a data science and machine learning environment with TensorFlow on M1, M1 Pro, M1 Max.

How to setup a TensorFlow environment on M1, M1 Pro, M1 Max using Miniforge

You: have a new M1, M1 Pro, M1 Max machine and would like to get started doing machine learning and data science on it.

This page: goes through a list of steps to install package managers and common machine learning and data science packages (software) on your machine and make sure they run using sample code.

Steps

  1. Download and install Homebrew from https://brew.sh. Follow the steps it prompts you to go through after installed.
  2. Download Miniforge3 (Conda installer) for macOS arm64 chips (M1, M1 Pro, M1 Max).
  3. Install Miniforge3 into home directory.
chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
sh ~/Downloads/Miniforge3-MacOSX-arm64.sh
source ~/miniforge3/bin/activate
  1. Restart terminal.
  2. Create directory to setup TensorFlow environment.
mkdir tensorflow-test
cd tensorflow-test
  1. Make and activate Conda environment.
conda create --prefix ./env
conda activate ./env
  1. Install TensorFlow dependencies from Apple Conda channel.
conda install -c apple tensorflow-deps
  1. Install base TensorFlow (Apple's fork of TensorFlow is called tensorflow-macos).
python -m pip install tensorflow-macos
  1. Install Apple's tensorflow-metal to leverage Apple Metal (Apple's GPU framework) for M1, M1 Pro, M1 Max GPU acceleration.
python -m pip install tensorflow-metal
  1. (Optional) Install TensorFlow Datasets to run benchmarks included in this repo.
python -m pip install tensorflow-datasets
  1. Install common data science packages.
conda install jupyter pandas numpy matplotlib scikit-learn
  1. Start Jupyter Notebook.
jupyter notebook
  1. Import dependencies and check TensorFlow version/GPU access.
import numpy as np
import pandas as pd
import sklearn
import tensorflow as tf
import matplotlib.pyplot as plt

# Check for TensorFlow GPU access
print(f"TensorFlow has access to the following devices:\n{tf.config.list_physical_devices()}")

# See TensorFlow version
print(f"TensorFlow version: {tf.__version__}")

If it all worked, you should see something like:

TensorFlow has access to the following devices:
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),
PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
TensorFlow version: 2.5.0

Resources

If you have any issues, leave a comment below so others can see.

@Sylvia34343
Copy link

I did all those things but when I run the last part of code in Jupyter Notebook, I get this:

_ImportError Traceback (most recent call last)
~/tensorflow-test/env/lib/python3.9/site-packages/numpy/core/init.py in
21 try:
---> 22 from . import multiarray
23 except ImportError as exc:

~/tensorflow-test/env/lib/python3.9/site-packages/numpy/core/multiarray.py in
11
---> 12 from . import overrides
13 from . import _multiarray_umath

~/tensorflow-test/env/lib/python3.9/site-packages/numpy/core/overrides.py in
6
----> 7 from numpy.core._multiarray_umath import (
8 add_docstring, implement_array_function, _get_implementing_args)

ImportError: dlopen(/Users/sylvia/tensorflow-test/env/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so, 0x0002): Library not loaded: @rpath/libcblas.3.dylib
Referenced from: /Users/sylvia/tensorflow-test/env/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so
Reason: tried: '/Users/sylvia/tensorflow-test/env/lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/bin/../lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/bin/../lib/libcblas.3.dylib' (no such file), '/usr/local/lib/libcblas.3.dylib' (no such file), '/usr/lib/libcblas.3.dylib' (no such file)

During handling of the above exception, another exception occurred:

ImportError Traceback (most recent call last)
/var/folders/sv/4j_5x1q56yd8vjc_g8jnrvn00000gn/T/ipykernel_29759/116520098.py in
----> 1 import numpy as np
2 import pandas as pd
3 import sklearn
4 import tensorflow as tf
5 import matplotlib.pyplot as plt

~/tensorflow-test/env/lib/python3.9/site-packages/numpy/init.py in
138 from . import _distributor_init
139
--> 140 from . import core
141 from .core import *
142 from . import compat

~/tensorflow-test/env/lib/python3.9/site-packages/numpy/core/init.py in
46 """ % (sys.version_info[0], sys.version_info[1], sys.executable,
47 version, exc)
---> 48 raise ImportError(msg)
49 finally:
50 for envkey in env_added:

ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  • The Python version is: Python3.9 from "/Users/sylvia/tensorflow-test/env/bin/python"
  • The NumPy version is: "1.19.5"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: dlopen(/Users/sylvia/tensorflow-test/env/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so, 0x0002): Library not loaded: @rpath/libcblas.3.dylib
Referenced from: /Users/sylvia/tensorflow-test/env/lib/python3.9/site-packages/numpy/core/multiarray_umath.cpython-39-darwin.so
Reason: tried: '/Users/sylvia/tensorflow-test/env/lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/bin/../lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/lib/libcblas.3.dylib' (no such file), '/Users/sylvia/tensorflow-test/env/bin/../lib/libcblas.3.dylib' (no such file), '/usr/local/lib/libcblas.3.dylib' (no such file), '/usr/lib/libcblas.3.dylib' (no such file)

Really don't know what happened.

Could anyone help me with this please? Big THANKS!!!!!!

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