Tensorflow/keras on MacOS M1

I have the big issue with keras and tensorflow.

I have MacPro M1.
I had to delete and reinstall tensorflow. Here is what I did:

conda install -y -c apple tensorflow-deps
python -m pip install tensorflow-macos
python -m pip install tensorflow-metal

After the installation:
(base) tolynovik@ ~ % pip list | grep tensorflow
tensorflow 2.16.1
tensorflow-datasets 4.8.3+nightly
tensorflow-estimator 2.12.0
tensorflow-hub 0.8.0
tensorflow-io-gcs-filesystem 0.36.0
tensorflow-macos 2.16.1
tensorflow-metadata 0.14.0
tensorflow-metal 1.1.0
tensorflow-probability 0.14.0

(base) tolynovik@ ~ % pip list | grep keras
keras 3.1.1

But when I run the code I get these errors:

ModuleNotFoundError Traceback (most recent call last)
Cell In[18], line 4
2 import pandas as pd
3 import tensorflow as tf
----> 4 import tensorflow.keras as keras
5 from tensorflow.keras.models import Sequential, Model
6 from matplotlib import pyplot as plt

ModuleNotFoundError: No module named ‘tensorflow.keras’

ModuleNotFoundError Traceback (most recent call last)
Cell In[20], line 4
2 import pandas as pd
3 import tensorflow as tf
----> 4 import keras
6 from matplotlib import pyplot as plt

File ~/anaconda3/lib/python3.10/site-packages/keras/engine/functional.py:24
21 import itertools
22 import warnings
—> 24 import tensorflow.compat.v2 as tf
26 from keras import backend
27 from keras.dtensor import layout_map as layout_map_lib

ModuleNotFoundError: No module named ‘tensorflow.compat’

It is very frustrating!

I do not want to reinstall everything again (conda, python, etc.)
Please, help

Hi @Tony_N, I was able to import tensorflow.compat in Mac M1Pro with tensorflow 2.16.1 and keras 3.1.1 without any error. I have installed the tensorflow using pip install -U tensorflow==2.16.1.

I recommend not to use compact.v2 as it was deprecated in the latest tensorflow versions. Thank You.