M1 Mac 安装Tensorflow

最近尝试在m1的mac上安装tensorflow,网上的教程比较多,但是不管怎么折腾都会出现各种问题。安装github上apple分支的TensorFlow不管怎么折腾都提示下面的错误:

zhongming@ZhongMingdeMBP tensorflow_macos_venv % . "/Users/zhongming/tensorflow_macos_venv/keras/bin/activate"
(keras) zhongming@ZhongMingdeMBP tensorflow_macos_venv % python
Python 3.8.8 (default, Apr 13 2021, 12:59:45)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
File "/Users/zhongming/tensorflow_macos_venv/keras/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in 
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Users/zhongming/tensorflow_macos_venv/keras/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found. Did find:
/Users/zhongming/tensorflow_macos_venv/keras/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
/Users/zhongming/tensorflow_macos_venv/keras/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 "", line 1, in 
File "/Users/zhongming/tensorflow_macos_venv/keras/lib/python3.8/site-packages/tensorflow/__init__.py", line 41, in 
from tensorflow.python.tools import module_util as _module_util
File "/Users/zhongming/tensorflow_macos_venv/keras/lib/python3.8/site-packages/tensorflow/python/__init__.py", line 39, in 
from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
File "/Users/zhongming/tensorflow_macos_venv/keras/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 83, in 
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/Users/zhongming/tensorflow_macos_venv/keras/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in 
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Users/zhongming/tensorflow_macos_venv/keras/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found. Did find:
/Users/zhongming/tensorflow_macos_venv/keras/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
/Users/zhongming/tensorflow_macos_venv/keras/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.
>>>

 

多次尝试无果,直到找到了下面这篇文章。https://medium.com/codex/installing-tensorflow-on-m1-macs-958767a7a4b3 ,可以把关键步骤整理一下,

1.安装Miniforge3

点击这个链接https://github.com/conda-forge/miniforge#miniforge3下载osx arm64版本的sh文件。下载完成之后在终端内执行:

bash Miniforge3-MacOSX-arm64.sh

安装过程基本一路yes就可以了,安装完成之后通过下面的 命令检查是否安装成功:

file $(which python)

执行这个命令之后会看到一个指向~/miniforge3 的路径,并且显示架构为“Mach-O 64-bit executable arm64.”  如果显示的内容不是这个,那么可能安装了错误版本的miniforge3 然后执行:

which pip 

这个命令同样应该会有指向~/miniforge3 的路径

然后需要确保miniforege3的路径可以让所有的环境使用:

~/miniforge3/condabin/conda
~/miniforge3/condabin/conda init

最后一步要确保我们安装的渠道是正确的,并且保证包来资源正确的已经被测试过得conda-forge。

conda config --add channels conda-forge
conda config --set channel_priority strict

如果上面的命令没有任何的错误,那么就代表你安装成功了。

2.安装包

经过上面的文章作者的确认,下面的这些包已经适配了arm64架构。

JupyterLab
Pandas
Statsmodels
Scipy
Scikit-Learn
Matplotlib
Seaborn
Numpy
Pytorch

可以通过conda install package_name来安装指定的的包。不过在安装之前需要确定每个包的架构显示为arm64或者norch,如果包含这两个架构可以继续安装,否则就不要继续了。

3.创建虚拟环境安装TensorFlow

最后终于到了安装的流程了,作者已经准备好了一个yml环境文件,猛击此处下载,或者直接将下面的代码保存为.yml使用。

name: apple_tensorflow
channels:
  - conda-forge
  - nodefaults
dependencies:
  - grpcio
  - h5py
  - ipython
  - numpy
  - pip=20.2.4
  - python=3.8
  - scipy
  - termcolor
  - typeguard
  - wheel
  - absl-py
  - astunparse
  - python-flatbuffers
  - gast
  - google-pasta
  - keras-preprocessing
  - opt_einsum
  - protobuf
  - tensorboard
  - tensorflow-estimator
  - termcolor
  - typing_extensions
  - wrapt
  - jupyterlab
  - pandas
  - seaborn
  - scikit-learn
  - matplotlib
  - notebook

下载完成之后通过下面的命令创建vnev:

conda env create --file=COPIED_PATH_TO_THE.YML_FILE --name=YOUR_ENVIRONMENT_NAME

创建完成之后通过下面的命令激活环境:

conda activate YOUR_ENVIRONMENT_NAME

此时虚拟环境中已经安装好了各种依赖库,最后一步就是安装TensorFlow·了。通过下面的命令安装:

pip install --upgrade --force --no-dependencies https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_addons_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl

安装完成之后就可以正常使用TensorFlow了。

如果使用keras环境,可以通过下面的命令安装keras

pip install keras

测试代码:

import tensorflow as tf  

tf.compat.v1.disable_eager_execution()
x=tf.constant([[1,2],[1,2]])  
y=tf.constant([[1,1],[1,2]])
z=tf.add(x,y)
 
x1=tf.constant(1)
y1=tf.constant(2)
z1=tf.add(x1,y1)

x2=tf.constant(2)
y2=tf.constant([1,2])
z2=tf.add(x2,y2)

x3=tf.constant([[1,2],[1,2]])  
y3=tf.constant([[1,2]])
z3=tf.add(x3,y3)

with tf.compat.v1.Session() as sess:
    z_result,z1_result,z2_result,z3_result=sess.run([z,z1,z2,z3])
    print('z =\n%s'%(z_result))
    print('z1 =%s'%(z1_result))
    print('z2 =%s'%(z2_result))
    print('z3 =%s'%(z3_result))

参考文章:

 

https://medium.com/codex/installing-tensorflow-on-m1-macs-958767a7a4b3

☆版权☆

* 网站名称:obaby@mars
* 网址:https://obaby.org.cn/
* 个性:https://oba.by/
* 本文标题: 《M1 Mac 安装Tensorflow》
* 本文链接:https://obaby.org.cn/2021/05/8177
* 短链接:https://oba.by/?p=8177
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

3 comments

  1. Level 1
    Google Chrome 90 Google Chrome 90 Mac OS X 10.15 Mac OS X 10.15 us美国–加利福尼亚州–洛杉矶–洛杉矶 IT7网络

    您好,我按照上面的例子跑好像不可以,想请教一下问题在哪…感谢
    Mac mini (M1, 2020)
    Big sur: 11.4

    “`
    >>> with tf.compat.v1.Session() as sess:
    … z_result,z1_result,z2_result,z3_result=sess.run([z,z1,z2,z3])
    … print(‘z =\n%s’%(z_result))
    … print(‘z1 =%s’%(z1_result))
    … print(‘z2 =%s’%(z2_result))
    … print(‘z3 =%s’%(z3_result))

    2021-05-30 01:22:34.907371: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:196] None of the MLIR optimization passes are enabled (registered 0 passes)
    2021-05-30 01:22:34.908505: W tensorflow/core/platform/profile_utils/cpu_utils.cc:126] Failed to get CPU frequency: 0 Hz
    “`

    1. 公主 Queen 
      Google Chrome 90 Google Chrome 90 Windows 10 Windows 10 cn中国–山东–青岛 联通

      这个是个warning,执行完成之后有没有我第一张图展示的内容?如果能够正常返回,可以忽略这个警告。看我贴的第一张图,也有这个警告~~

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注