pythontensorflowpipcondaapple-silicon

How to install TensorFlow 2.16 on Macbook Pro M2?


I currently use TensorFlow 2.13.1 (tensorflow-macos) with TF-metal (1.0.0). I want to migrate to TensorFlow 2.16.1 to keep up with the updates.

In the update website, they say the following:

Apple Silicon

If you previously installed TensorFlow using pip install tensorflow-macos, please update your installation method. Use pip install tensorflow from now on. tensorflow-macos package will no longer receive updates. Future updates will be released to tensorflow.

That sounds great, but I have a few questions:

  1. Do I need to install tensorflow-metal for GPU acceleration? It doesn't seem to be possible.
  2. Is it stable?

My tests so far have been unsuccessful.

  1. It keeps asking for Keras, even though it is installed.
  2. When I solved the Keras issue, a simple NN worked well. However, a simple GAN makes my Jupyter Notebook kernel die consistently.

My computer is a 2023 Macbook Pro with an M2 Max chip. The OS is up-to-date.

Do you have any suggestions as to what may be going on? Is there a better way to perform this update?

Here are the specs for the environment that seems to work best for my machine:

conda create -n myenv python=3.9.18
conda activate myenv
conda install -c apple tensorflow-deps
pip install matplotlib==3.7.4
pip install numpy==1.24.3
pip install pandas==2.1.4
pip install scipy==1.11.4
pip install typing-extensions==4.5.0
pip install seaborn==0.13.0
pip install tensorflow-macos==2.13.1
pip install tensorflow-metal==1.0.0
pip install plotly==5.17.0
pip install scikit-learn pyarrow
conda install -c conda-forge notebook
conda install ipykernel
python -m ipykernel install --user --name=myenv --display-name "Python (myenv)"

Here is the most general spec for the update:

conda create -n myenv python==3.11.9
conda activate myenv
conda install matplotlib
conda install numpy
conda install pandas
conda install scipy
conda install typing-extensions
conda install seaborn
conda install tensorflow
conda install plotly
conda install scikit-learn pyarrow
conda install -c conda-forge notebook
conda install ipykernel
python -m ipykernel install --user --name=myenv --display-name "Python (myenv)"

I tried mix-and-match pip with conda (when appropriate) and also tried to change versions of packages to see if that would be the issue, but no success.


Solution

  • I have had a similar question regarding the environments. to address it you should create a new environment (before deleting the old one) You can use the following lines as a guide:

    conda create -name <Environment Name> python 3.11.11 or 3.12.9 
    conda activate <Environment name> 
    pip install tensorflow==2.17 or 2.18 tensorflow-metal 
    conda install -c conda-forge <other package name>
    

    to verify:

    python --version
    python -c "import tensorflow as tf; import keras ; print(tf.__version__); print(keras.__version__)"
    python -c "import tensorflow as tf; print(tf.config.list_physical_devices(‘GPU'))"
    

    In the documentation of tensorflow from version 2.16 onwards it is recommended to use pip install tensorflow and not other methods. (If you use conda or poetry there is a bug when using TF-2.17-2.18 with TF-Metal1.2). I confirm that you are still required to install tensorflow-metal version1.1 or the new version1.2 for TF and GPU usage in Apple Silicon (M1,M2,M3,M4) You should see a similar result to:

        ❯ python --version
        Python 3.11.11
        ❯ python -c "import tensorflow as tf; import keras ; print(tf.__version__); print(keras.__version__)"
        2.17.0
        3.8.0
        ❯ python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
        [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
    

    I have been testing the update recently and it seems quite stable TF2.17, Keras3.8 and Python 3.11 with some layers in keras3 and TF.