'Tensorflow on M1
Importing Tensorflow gives this error
This is my "code":
import tensorflow as tf
My versions: protoc --version
libprotoc 3.15.8
pip show protobuf
Name: protobuf Version: 3.19.1
Tensorflow 2.6.0
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-64156d691fe5> in <module>
----> 1 import tensorflow as tf
~/miniforge3/envs/userenv/lib/python3.8/site-packages/tensorflow/__init__.py in <module>
     39 import sys as _sys
     40 
---> 41 from tensorflow.python.tools import module_util as _module_util
     42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
     43 
~/miniforge3/envs/userenv/lib/python3.8/site-packages/tensorflow/python/__init__.py in <module>
     38 # pylint: disable=wildcard-import,g-bad-import-order,g-import-not-at-top
     39 
---> 40 from tensorflow.python.eager import context
     41 from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
     42 
~/miniforge3/envs/userenv/lib/python3.8/site-packages/tensorflow/python/eager/context.py in <module>
     30 import six
     31 
---> 32 from tensorflow.core.framework import function_pb2
     33 from tensorflow.core.protobuf import config_pb2
     34 from tensorflow.core.protobuf import rewriter_config_pb2
~/miniforge3/envs/userenv/lib/python3.8/site-packages/tensorflow/core/framework/function_pb2.py in <module>
      3 # source: tensorflow/core/framework/function.proto
      4 """Generated protocol buffer code."""
----> 5 from google.protobuf import descriptor as _descriptor
      6 from google.protobuf import message as _message
      7 from google.protobuf import reflection as _reflection
~/miniforge3/envs/userenv/lib/python3.8/site-packages/google/protobuf/descriptor.py in <module>
     45   import binascii
     46   import os
---> 47   from google.protobuf.pyext import _message
     48   _USE_C_DESCRIPTORS = True
     49 
AttributeError: module 'google.protobuf.internal.containers' has no attribute 'MutableMapping'
I'm using Jupyter and am on a Apple M1. I use Miniforge, and I really have no clue where to look next. I switched from Anaconda to Miniforge because of the native M1 chip.
Solution 1:[1]
I tried to install tf on my Macbook pro (M1) successfully by using the method as below.
conda install -c apple tensorflow-deps==2.6.0
pip install tensorflow-macos
I successfully import tensorflow,and run the test script.
References?https://blog.csdn.net/qq_43731778/article/details/122525442
Solution 2:[2]
You can now use conda to get tensorflow to work natively. Follow the sequence below
Create Conda environment for osx-arm64
CONDA_SUBDIR=osx-arm64 conda create -n <env name> python=3.8 -c conda-forge
You can also use 3.9 which are the two currently supported versions
Set default subdir after creating env to look for osx-arm64 packages
conda activate <env name>
conda config --env --set subdir osx-arm64
Add conda-forge channel to env for osx-arm64 packages
conda config --env --add channels conda-forge
Set channel priority to strict so it only looks in conda-forge channel by default when installing other packages
conda config --env --set channel_priority strict
Install all the dependencies
conda install -c apple tensorflow-deps
Install base tensorflow
SYSTEM_VERSION_COMPAT=0 python -m pip install tensorflow-macos
install tensorflow-metal plugin
python -m pip install tensorflow-metal
    					Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | Dharman | 
| Solution 2 | languageoftheuniverse | 
