我正在尝试在我的 M1 Mac 上开始使用 TensorFlow。但是,当我尝试导入tensorflow时,我只收到以下消息
Python 3.8.5 (v3.8.5:580fbb018f, Jul 20 2020, 12:11:27)
[Clang 6.0 (clang-600.0.57)] on darwin
In [2]: import tensorflow as tf
Process finished with exit code 132 (interrupted by signal 4: SIGILL)
我尝试过使用tensorflow版本2.7.0、2.6.0和2.5.0,但都有同样的问题。
我不相信 M1 支持已合并到主线 TF 中。您需要 pip 安装 Apple 的特殊分支和软件包,包括(但不仅限于)以下内容。
tensorflow-macos
tensorflow-metal
tensorflow-deps
tensorflow-macos
完整的说明在这里,这就是我开始的地方。
首先您必须安装与Apple Silicon兼容的tensorflow软件包。我已经使用conda完成了,版本如下:
python==3.10.15
tensorflow==2.16.2
tensorflow-macos==2.16.2
tensorflow-metal==1.1.0
然后您应该检查 GPU 是否可用于您的设备:
import tensorflow as tf
gpus = tf.config.list_physical_devices('GPU')
print("GPU is", "available" if len(gpus)>0 else "NOT AVAILABLE")
然后允许 GPU 内存增长以避免内存错误并设置可见设备
for gpu in gpus:
print("Setting memory growth for", gpu)
tf.config.experimental.set_memory_growth(gpu, True)
# Make sure you add the following line to avoid crashing in training phase
tf.config.set_visible_devices(gpu, 'GPU')