本文共 3491 字,大约阅读时间需要 11 分钟。
折腾了一整晚,终于搞定了在Apple Silicon M1硬件上安装优化版TensorFlow!这确实是一段充满挑战的旅程。刚开始我并不打算折腾,直到发现使用bash安装Python虚拟环境的不便之处。虽然许多软件包可以通过源码编译安装到虚拟环境,但仍然有一些不合作的包装程序安装不了,比如sklearn。
对于Apple Silicon M1硬件,官方明确指出只有conda-forge方法可以使用。因此,我只能硬着头皮寻找解决方法。在Apple的GitHub主页中发现了一篇关于使用miniforge安装TensorFlow的方法,这个方法安装起来并不复杂,除了那可怕的网络速度...
安装完成TensorFlow后,接下来是安装sklearn。除了需要特别指定brew安装的libomp包的位置,其他步骤都按照官网指南一路走下来即可安装。以下是具体操作步骤:
conda create -n tf24 python=3.8conda activate tf24
libs="/Users/xxx/tensorflow_macos/arm64/"env="/opt/homebrew/Caskroom/miniforge/base/envs/tf24/"
conda install -c conda-forge pip setuptools cached-property sixconda upgrade -c conda-forge pip setuptools cached-property sixpip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl"pip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/h5py-2.10.0-cp38-cp38-macosx_11_0_arm64.whl"pip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/tensorflow_addons_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl"
conda install -c conda-forge -y absl-pyconda install -c conda-forge -y astunparseconda install -c conda-forge -y gastconda install -c conda-forge -y opt_einsumconda install -c conda-forge -y termcolorconda install -c conda-forge -y typing_extensionsconda install -c conda-forge -y wheelconda install -c conda-forge -y typeguard
pip install tensorboard
pip install wrapt flatbuffers tensorflow_estimator google_pasta keras_preprocessing protobuf
pip install --upgrade -t "$env/lib/python3.8/site-packages/" --no-dependencies --force "$libs/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl"
相比于在tf_venv环境中安装软件包(需要进行各种编译安装),直接使用conda install走天下简直是简单到爆了,只需指定channel为conda-forge即可。
conda install pandas -c conda-forgeconda install pytables -c conda-forge
conda install matplotlib -c conda-forge
conda install ipython -c conda-forge
由于sklearn不支持直接用homebrew编译安装,那只能硬着头皮编译了。
conda activate tf24conda install -c conda-forge scipy cython joblib threadpoolctl pytest compilers llvm-openmpbrew install libomp
记录libomp的位置以便后续导入:
/opt/homebrew/Cellar/libomp/11.1.0/lib/opt/homebrew/Cellar/libomp/11.1.0/include
make cleanpip install --verbose --no-build-isolation --editable .
conda list
查看包信息:
scikit-learn 0.24.1 dev_0scipy 1.6.0 py38hdf044fb_0 conda-forge...
完成!不过还不能庆祝,查看sklearn安装位置:
In [6]: import sklearnOut[6]:
安装目标是虚拟环境的/opt/homebrew/Caskroom/miniforge/base/envs/tf24/lib/python3.8/site-packages/,因此需要进行以下操作:
cp -r sklearn /opt/homebrew/Caskroom/miniforge/base/envs/tf24/lib/python3.8/site-packages/cp -r scikit_learn.egg-info /opt/homebrew/Caskroom/miniforge/base/envs/tf24/lib/python3.8/site-packages/
cd /opt/homebrew/Caskroom/miniforge/base/envs/tf24/lib/python3.8/site-packages/rm scikit-learn.egg-link
这样就能完美安装完成!(并且可以删除sklearn的源码文件了~)
conda activate tf24ipython -c "import sklearn; print(sklearn._config)"
后来在一篇Medium博客中找到更简便的方法:
conda install -c conda-forge scikit-learn
直接使用conda安装即可解决问题,不用手动编译!希望大家不要学我的错误,尽量找到最方便快捷的方法。
转载地址:http://iazg.baihongyu.com/