Mac m1 安装TensorFlow 2 -- 踩坑

背景

我的mac版本如下

Mac m1 安装TensorFlow 2 -- 踩坑_第1张图片

 

安装步骤

1.安装arm64 macos的miniconda

下载地址

下载完以后在terminal依次输入以下三个命令,后面的命令也都是在terminal输入

注意:我的是安装arm64版本,之前安装x86_64版本有问题

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

2. 创建虚拟环境

使用3.8应该也可以,本人用的是3.9

conda create -n tf python==3.9
conda activate tf

3. 安装tensorflow2 版本

默认安装2.8版本的tensorflow,也可以指定版本。最好默认

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

4. 测试运行

pycharm默认使用苹果自带的python。记得在设置把python环境设置成刚才conda创建的

import tensorflow as tf

print(tf.__version__)
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(512, activation=tf.nn.relu),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
result = model.evaluate(x_test, y_test)

print(result)

执行结果

Mac m1 安装TensorFlow 2 -- 踩坑_第2张图片

 

踩过的坑

问题1-PackagesNotFoundError: The following packages are not available from current channels: --tensorflow-deps

$ conda install -c apple tensorflow-deps
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.

PackagesNotFoundError: The following packages are not available from current channels:

  - tensorflow-deps

Current channels:

  - https://conda.anaconda.org/apple/osx-64
  - https://conda.anaconda.org/apple/noarch
  - https://conda.anaconda.org/conda-forge/osx-64
  - https://conda.anaconda.org/conda-forge/noarch
  - https://conda.anaconda.org/main/osx-64
  - https://conda.anaconda.org/main/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.

解决方法

根据下面步骤,彻底删除Miniforge3, 重新安装我上面提供安装步骤走一遍。

rm -rf ~/miniforge3
rm -rf .conda
rm -rf .condarc
然后重新执行安装命令:
sh ~/Downloads/Miniforge3-MacOSX-arm64.sh

2. 问题2-reason: '-[MPSGraph randomUniformTensorWithShape:stateTensor:name:]: unrecognized selector sent to instance 0x6000008309c0'

2022-07-24 16:23:03.470 python[7951:104235] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MPSGraph randomUniformTensorWithShape:stateTensor:name:]: unrecognized selector sent to instance 0x6000008309c0'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000019421812c __exceptionPreprocess + 240
	1   libobjc.A.dylib                     0x0000000193f69808 objc_exception_throw + 60

解决方法

升级os系统到12.1以上

其它

怎么查看Mac上的GPU?

1。 点击系统报告

Mac m1 安装TensorFlow 2 -- 踩坑_第3张图片

 

2. 点击图形卡/显示器 

Mac m1 安装TensorFlow 2 -- 踩坑_第4张图片​​​​​​​

参考资料

如何检查您的 Mac 拥有的显卡 (GPU)|mac|gpu|英特尔|应用程序_网易订阅

查看是不是arm64

打开终端输入:
uname -a

​​​​​​​

你可能感兴趣的:(tensorflow,tensorflow,macos,python)