python - 在 M1 Mac 中安装 Tensorflow

标签 python tensorflow anaconda apple-m1

我遵循了本教程,https://www.youtube.com/watch?v=ykCY_tJbhNw在Mac(M1处理器)中安装tensorflow。但是,当我导入tensrflow时,出现以下错误。有什么想法吗?

步骤包括,

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

但是,当我在 shell 中启动 python session 并输入 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.

最佳答案

要在 Macbook M1 芯片上安装 TensorFlow,需要做很多事情:

  • 首先,安装Python 3.8.9 (这个版本我试过,其他版本不知道)

  • 转到您的项目目录

  • 通过运行 brew install hdf5 安装 hdf5如果您没有brew,可以在这里下载:https://brew.sh/

  • 以上hdf5安装将吐出它的位置:使用它并运行: export HDF5_DIR=<hdf5_location>对于版本 1.13.0(我的情况)如下:

export HDF5_DIR=/opt/homebrew/Cellar/hdf5/1.13.0
  • 使用 virtualenv venv 创建虚拟环境(选择任何创建虚拟环境的方法或者不创建虚拟环境都可以。要安装virtualenv,运行pip3 install virtualenv)
  • (如果不使用虚拟环境则跳过此步)通过运行: source venv/bin/activate 激活虚拟环境
  • 现在,我们已经具备了在配备 M1 芯片的 Mac 上安装 Tensorflow 所需的一切,包括 GPU 版本
  • 要安装 Tensorflow 和相关库,请运行以下命令: (如果使用虚拟环境,请运行以下命令)
venv/bin/pip install --no-binary=h5py h5py
venv/bin/pip install tensorflow-macos
venv/bin/pip install tensorflow-metal

不使用虚拟环境,运行以下命令:

pip3 install --no-binary=h5py h5py
pip3 install tensorflow-macos
pip3 install tensorflow-metal
  • 现在,您的 TensorFlow 安装已完成!
  • 尝试一下:
import tensorflow as tf

print("TF version:", tf.__version__)
print("Number GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
print("Number CPUs Available: ", len(tf.config.experimental.list_physical_devices('CPU')))

enter image description here

关于python - 在 M1 Mac 中安装 Tensorflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67354277/

相关文章:

python - 如何打印每秒迭代次数?

python - 为 Tensorflow 对象检测 API 创建 PASCAL Voc

python - 无法创建目录 : logs/fit

java - 用conda打包一组可执行jar

python - 如何在 cythonize 脚本中包含 numpy?

python - 将 Linux Terminal 放入 wxPython 窗口

python - 无法全局访问 Logger

Python:如何动态写入多个文件

android - Android 上的基准量化

ubuntu - Jupyter Lab 未在 Ubuntu 上打开