Mac M1 安装配置TensorFlow-GPU

人工智能72

在Mac上安装了TensorFlow,但是import Tensorflow时却一直显示TensorFlow不存在。看了各种安装方法,最后终于成功了。

进入正题:

直接进官网,根据苹果官方给的安装方法进行安装:https://developer.apple.com/metal/tensorflow-plugin/

这个方法真的简单很多很多,实在不明白为什么网上一搜全是那种贼复杂的安装方法,步骤贼多,一看就晕。

  • 安装步骤:

注意: 要求python版本 3.8 or 3.9

以下几步均在终端运行

Step 1: Environment setup

选苹果自己的芯片,红框那一栏;
Mac M1 安装配置TensorFlow-GPU

下载 Miniforge3

下载地址:https://github.com/conda-forge/miniforge
Mac M1 安装配置TensorFlow-GPU


chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh

sh ~/Downloads/Miniforge3-MacOSX-arm64.sh
source ~/miniforge3/bin/activate

创建conda环境

conda create -n tf_m1 python=3.9


conda activate tf_m1
  • 接下来,安装Tensorflow依赖
    Mac M1 安装配置TensorFlow-GPU
    Mac M1 安装配置TensorFlow-GPU

conda install -c apple tensorflow-deps==2.6.0

Step 2: Install base TensorFlow


python -m pip install tensorflow-macos

python -m pip install tensorflow-macos -i https://pypi.tuna.tsinghua.edu.cn/simple/

Step 3: Install tensorflow-metal plugin


python -m pip install tensorflow-metal -i https://pypi.tuna.tsinghua.edu.cn/simple/

测试

终端输入python,输入以下代码测试是否成功安装tensorflow-GPU版

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

输出显示有GPU就是成功了

Metal device set to: Apple M1

systemMemory: 8.00 GB
maxCacheSize: 2.67 GB

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 17101579642361186921
xla_global_id: -1
, name: "/device:GPU:0"
device_type: "GPU"
locality {
  bus_id: 1
}
incarnation: 4245710310575921387
physical_device_desc: "device: 0, name: METAL, pci bus id: "
xla_global_id: -1
]

在PyCharm里面配置Tensorflow

已经成功安装好Tensorflow了,但是Pycharm里import Tensorflow显示没有Tensorflow包,此时是因为Pycharm里面没有配置环境。

打开 PyCharm -> Preferences -> Project -> Python Interpreter -> Show All -> 点左下角的"+"号
Mac M1 安装配置TensorFlow-GPU

选择 Conda Environment -> Existing environment -> Interpreter后面的"..." -> Interpreter后面的"..."

在Users里面找miniforge3,再找bin文件夹里面的python,再手动把miniforge3前面的改成~,点ok就好了。

Mac M1 安装配置TensorFlow-GPU

Mac M1 安装配置TensorFlow-GPU

接着测试一下是否成功,和上面的一样,显示GPU就是成功了。

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

至此,已经可以自由使用Tensorflow了

Original: https://blog.csdn.net/weixin_51221144/article/details/124922468
Author: 葫芦娃啊啊啊啊
Title: Mac M1 安装配置TensorFlow-GPU