我已经按照这些说明在 M1 (ARM) Mac 上安装了 TensorFlow
,一切工作正常。
但是,模型训练是在 CPU
上进行的。如何将训练转换到 GPU
?
In: tensorflow.config.list_physical_devices()
Out: [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]
在Apple的TensorFlow发行版文档中,我找到了以下内容较为混淆的段落:
无需更改您现有 TensorFlow 脚本即可使用 ML Compute 作为 TensorFlow 和 TensorFlow Addons 的后端。ML Compute 设备选择提供一个可选的
mlcompute.set_mlc_device(device_name='any')
API。device_name 的默认值为 'any',这意味着 ML Compute 将选择系统上最佳的可用设备,包括多 GPU 配置中的多个 GPU。其他可用选项是CPU
和GPU
。请注意,在急切模式下,ML Compute 将使用 CPU。例如,要选择 CPU 设备,可以执行以下操作:
# Import mlcompute module to use the optional set_mlc_device API for device selection with ML Compute.
from tensorflow.python.compiler.mlcompute import mlcompute
# Select CPU device.
mlcompute.set_mlc_device(device_name='cpu') # Available options are 'cpu', 'gpu', and 'any'.
我尝试运行:
from tensorflow.python.compiler.mlcompute import mlcompute
mlcompute.set_mlc_device(device_name='gpu')
并获得:
WARNING:tensorflow: Eager mode uses the CPU. Switching to the CPU.
我现在遇到了困难,如何在我的 MacBook Air 上使用 GPU 训练 keras
模型?
TensorFlow 版本:2.4.0-rc0
tf
代码... - clstaudtRuntimeError()
,我认为可能使用tf.function
装饰器可以解决这个问题或者让代码更进一步。 - InnatRuntimeError
。 - Innattensorflow_macros
有关。其他人也遇到了这个问题,但开发者尚未解决它(https://github.com/apple/tensorflow_macos/issues/88)。 - Innat