M1 Mac 安装Tensorflow

2023-02-22 10:58:18 浏览数 (1)

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

代码语言:javascript复制
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文件。下载完成之后在终端内执行:

代码语言:javascript复制
bash Miniforge3-MacOSX-arm64.sh

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

代码语言:javascript复制
file $(which python)

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

代码语言:javascript复制
which pip 

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

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

代码语言:javascript复制
~/miniforge3/condabin/conda
代码语言:javascript复制
~/miniforge3/condabin/conda init

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

代码语言:javascript复制
conda config --add channels conda-forge
conda config --set channel_priority strict

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

2.安装包

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

代码语言:javascript复制
JupyterLab
Pandas
Statsmodels
Scipy
Scikit-Learn
Matplotlib
Seaborn
Numpy
Pytorch

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

3.创建虚拟环境安装TensorFlow

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

代码语言:javascript复制
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:

代码语言:javascript复制
conda env create --file=COPIED_PATH_TO_THE.YML_FILE --name=YOUR_ENVIRONMENT_NAME

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

代码语言:javascript复制
conda activate YOUR_ENVIRONMENT_NAME

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

代码语言:javascript复制
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

代码语言:javascript复制
pip install keras

测试代码:

代码语言:javascript复制
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://h4ck.org.cn/

* 本文标题: 《M1 Mac 安装Tensorflow》

* 本文链接:https://h4ck.org.cn/2021/05/m1-mac-安装tensorflow/

* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


分享文章:

相关文章:

  1. m3u8 下载工具 V20.6.01 [OSX]
  2. 由apscheduler引发的django.db.utils.InternalError: (1054, u”Unknown column ‘rms.go_datetime’ in ‘field list'”)
  3. Install PyQt4 on Mac OSX 10.8
  4. Django 限制访问频率
  5. CUDNN_STATUS_NOT_INITIALIZED
  6. Win10 Tensorflow-gpu 不完全安装手册
  7. ModuleNotFoundError: No module named ‘cryptography.hazmat.bindings._padding’
  8. Calling IDA APIs from IDAPython with ctypes
  9. IDA批量模式 Python Script[fix]
  10. certbot-auto 阿里云配置安装

0 人点赞