在 M1 Mac 中安装 Tensorflow

时间:2021-05-02 08:19:43

标签: python tensorflow anaconda apple-m1

我按照本教程 https://www.youtube.com/watch?v=ykCY_tJbhNw 在 mac(M1 处理器)中安装 tensorflow。但是,当我导入 tensrflow 时,出现以下错误。任何想法可能是什么?

步骤包括,

  • 正在安装 brew(已经安装)
  • 安装 conda(已安装)
  • 安装 miniforge
  • 从github页面下载tensorflow-macos
  • conda activate mlEnv
  • /Users/imantha/Software/tensorflow_macos/install_venv.sh -p
  • 当提示输入路径时:/Users/imantha/Software/anaconda3/envs/mlEnv/
  • 然后选择 yes 安装 tensorflow
  • tensorflow 已成功安装,当输入 conda list 时,将列出包。

但是,当我在 shell 中启动 python 会话并输入 import tensorflow as tf 时,出现以下错误

Traceback (most recent call last):
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found.  Did find:
/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/python/__init__.py", line 39, in <module>
from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/python/pywrap_tensorflow.py", line 83, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: 
dlopen(/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image 
found.  Did find:
/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong 
 architecture
/Users/imantha/Software/anaconda3/envs/mlEnv/lib/python3.8/site- 
packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong 
architecture


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

2 个答案:

答案 0 :(得分:0)

按照 here 中提到的说明从源代码构建 Tensorflow。 请参阅此comment

或,

尝试提到 here 的解决方法。

答案 1 :(得分:0)

创建 conda 环境

conda create --name tfenv python=3.8

激活它

conda activate mlp

安装 Tensorflow 依赖项:

conda install -c apple tensorflow-deps

并安装基础 TensorFlow:

pip install tensorflow-macos

来自苹果官网。它将本地安装 Tensorflow 2.5。

您可以在这里查看

https://caffeinedev.medium.com/how-to-install-tensorflow-on-m1-mac-8e9b91d93706

还有

https://developer.apple.com/metal/tensorflow-plugin/