hat*_*hat 6 python tensorflow apple-m1
我已经安装了 tensorflow-macos 并且在训练时这是我的 CPU 使用率
和 GPU 使用
.
我可以让 Tensorflow 在 GPU 上运行吗?
G.S*_*G.S 13
我今天一直在设置我的新 M1 机器,并正在寻找Aman Anand已经提供的测试。使用使用 Homebrew 安装的 miniforge 包管理器以及从 #153 指南中的 YAML 文件克隆的环境,按照#153中提供的标准说明在 GPU 上成功运行。
我还运行了更小的更简单的代码片段,如下所示,它仅在 CPU 上运行,'% GPU' == 0%:
import numpy as np
import tensorflow as tf
### Aman's code to enable the GPU
#from tensorflow.python.compiler.mlcompute import mlcompute
#tf.compat.v1.disable_eager_execution()
#mlcompute.set_mlc_device(device_name='gpu')
#print("is_apple_mlc_enabled %s" % mlcompute.is_apple_mlc_enabled())
#print("is_tf_compiled_with_apple_mlc %s" % #mlcompute.is_tf_compiled_with_apple_mlc())
#print(f"eagerly? {tf.executing_eagerly()}")
#print(tf.config.list_logical_devices())
x = np.random.random((10000, 5))
y = np.random.random((10000, 2))
x2 = np.random.random((2000, 5))
y2 = np.random.random((2000, 2))
inp = tf.keras.layers.Input(shape = (5,))
l1 = tf.keras.layers.Dense(256, activation = 'sigmoid')(inp)
l1 = tf.keras.layers.Dense(256, activation = 'sigmoid')(l1)
l1 = tf.keras.layers.Dense(256, activation = 'sigmoid')(l1)
l1 = tf.keras.layers.Dense(256, activation = 'sigmoid')(l1)
l1 = tf.keras.layers.Dense(256, activation = 'sigmoid')(l1)
o = tf.keras.layers.Dense(2, activation = 'sigmoid')(l1)
model = tf.keras.models.Model(inputs = [inp], outputs = [o])
model.compile(optimizer = "Adam", loss = "mse")
model.fit(x, y, validation_data = (x2, y2), batch_size = 500, epochs = 500)
Run Code Online (Sandbox Code Playgroud)
取消注释从 Aman 代码中添加的行并重新运行使 GPU 再次工作:
如果这些脚本仍然不使用每个活动监视器的 GPU(在 view/update_Frequency 中将更新速率设置为 1 秒),请返回到 #153 页面从头开始并仔细按照说明进行操作,并确保忽略适用于 Intel/X86 的指令。
我的步骤:
2022 年 1 月 26 日更新:
在过去 6 个月中,在 Apple Silicon 上安装 Tensorflow 的工作流程变得更加容易,它仍然依赖于 miniforge,但包是通过 conda 和 pip 从标准 conda env 分发的,而不必从 yaml 文件创建包。这些说明非常容易遵循,您应该可以在 2 分钟内完成操作。唯一的例外是我必须运行一个额外的命令来通过 conda安装 openblas才能使其工作。
我上面的测试在 Tensorflow 2.7 中中断,因为他们更改了与m1 的 mlcompute 位置有关的内容,但继续说 mlcompute 不再需要通过 Metal 插件指示 GPU 的使用,并且只需删除第 5-10 行引用了 mlcompute,并在 GPU 上运行,如活动监视器中所示。
小智 5
TensorFlow-macos 2.5 的发布已修复此问题。在 Mac M1 上使用 GPU for Tensorflow 的最简单方法是创建一个新的 conda miniforge3 ARM64 环境并运行以下 3 个命令来安装 TensorFlow 及其依赖项:
conda install -c apple tensorflow-deps
python -m pip install tensorflow-macos
python -m pip install tensorflow-metal
Run Code Online (Sandbox Code Playgroud)
更多说明请参见此页面:https://developer.apple.com/metal/tensorflow-plugin/
“直接在 Mac 上使用 TensorFlow 加速机器学习模型的训练。安装 TensorFlow v2.5 和 tensorflow-metal PluggableDevice,以在 Mac GPU 上使用 Metal 加速训练。”
小智 5
您可以通过以下方式检查可用的 GPU 设备
import tensorflow as tf
tf.config.list_physical_devices()
Run Code Online (Sandbox Code Playgroud)
然后运行你的模型
with tf.device('/device:GPU:0'):
model.fit(x_train, y_train)
Run Code Online (Sandbox Code Playgroud)
另请参阅https://www.tensorflow.org/api_docs/python/tf/device
| 归档时间: |
|
| 查看次数: |
1521 次 |
| 最近记录: |