Mac M1芯片安装tensorflow2.5

十分艰难

0. conda新建一个环境

conda create -n tf25 python=3.8
conda activate tf25

1. TensorFlow 2.4安装

tensorflow2.4的安装流程可以参考:
https://zhuanlan.zhihu.com/p/380943713

实测有效,但是tf2.4不能调用GPU好像。所以又装了tf2.5

2. Tesorflow 2.5安装

首先需要升级macOS到12.0及以上,否则不能安装2.5
可以从这里下,免费:
https://www.macw.com/mac/3504.html

系统升级完了以后可以这个官方教程安装tf2.5:
https://developer.apple.com/metal/tensorflow-plugin/

以下是安装的基本内容

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

至此标准安装结束了


3. 报错

安装结束以后,我在import tensorflow as tf 的时候报错了:
如果你也遇到同样问题,可以参考如下方案

Traceback (most recent call last):
  File "/Users/chenyangkang/Downloads/m1-tensorflow-config-main/confirm.py", line 2, in 
    import tensorflow as tf
  File "/Users/chenyangkang/miniforge3/envs/tf25/lib/python3.9/site-packages/tensorflow/__init__.py", line 41, in 
    from tensorflow.python.tools import module_util as _module_util
  File "/Users/chenyangkang/miniforge3/envs/tf25/lib/python3.9/site-packages/tensorflow/python/__init__.py", line 40, in 
    from tensorflow.python.eager import context
  File "/Users/chenyangkang/miniforge3/envs/tf25/lib/python3.9/site-packages/tensorflow/python/eager/context.py", line 29, in 
    import numpy as np
  File "/Users/chenyangkang/miniforge3/envs/tf25/lib/python3.9/site-packages/numpy/__init__.py", line 140, in 
    from . import core
  File "/Users/chenyangkang/miniforge3/envs/tf25/lib/python3.9/site-packages/numpy/core/__init__.py", line 48, in 
    raise ImportError(msg)
ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.9 from "/Users/chenyangkang/miniforge3/envs/tf25/bin/python"
  * The NumPy version is: "1.19.5"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: dlopen(/Users/chenyangkang/miniforge3/envs/tf25/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so, 0x0002): Library not loaded: @rpath/libcblas.3.dylib
  Referenced from: /Users/chenyangkang/miniforge3/envs/tf25/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-darwin.so
  Reason: tried: '/Users/chenyangkang/miniforge3/envs/tf25/lib/libcblas.3.dylib' (no such file), '/Users/chenyangkang/miniforge3/envs/tf25/lib/libcblas.3.dylib' (no such file), '/Users/chenyangkang/miniforge3/envs/tf25/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/chenyangkang/miniforge3/envs/tf25/lib/libcblas.3.dylib' (no such file), '/Users/chenyangkang/miniforge3/envs/tf25/lib/libcblas.3.dylib' (no such file), '/Users/chenyangkang/miniforge3/envs/tf25/lib/python3.9/site-packages/numpy/core/../../../../libcblas.3.dylib' (no such file), '/Users/chenyangkang/miniforge3/envs/tf25/lib/libcblas.3.dylib' (no such file), '/Users/chenyangkang/miniforge3/envs/tf25/bin/../lib/libcblas.3.dylib' (no such file), '/Users/chenyangkang/miniforge3/envs/tf25/lib/libcblas.3.dylib' (no such file), '/Users/chenyangkang/miniforge3/envs/tf25/bin/../lib/libcblas.3.dylib' (no such file), '/usr/local/lib/libcblas.3.dylib' (no such file), '/usr/lib/libcblas.3.dylib' (no such file)

原因是numpy版本不对。解决方法:

pip uninstall -y numpy
pip uninstall -y setuptools
pip install setuptools
pip install numpy=1.19.2

跑以下命令检测一下

#!/usr/bin/env python
import tensorflow as tf

print(f"Running TensorFlow {tf.__version__} with {len(tf.config.list_physical_devices('GPU'))} GPUs recognized")

输出:

Init Plugin
Init Graph Optimizer
Init Kernel
Running TensorFlow 2.5.0 with 1 GPUs recognized

说明在正常使用GPU了

不过听说目前tf2.5的速度没有tf2.4的快,斟酌。

你可能感兴趣的:(Mac M1芯片安装tensorflow2.5)