我已经安装了 tensorflow-macos,在训练时这是我的 CPU 使用率
和 GPU 使用率 .
我可以让 Tensorflow 在 GPU 上运行吗?
最佳答案
我今天一直在设置我的新 M1 机器,并正在寻找一个测试,例如由 Aman Anand 提供的测试。已经在这里。在遵循 #153 中提供的标准指令后,它成功地在 GPU 上运行。使用使用 Homebrew 安装的 miniforge 包管理器和从 #153 指南中的 YAML 文件克隆的环境。
我还运行了更小的更简单的片段,如下所示,它只在 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)
取消注释从 Aman 的代码中添加的行并重新运行使 GPU 再次工作:
如果这些脚本仍然不使用每个事件监视器的 GPU(在 view/update_frequency 中将更新率设置为 1s),请返回 #153 页面以从头开始并仔细按照说明进行操作,并确保忽略针对 Intel/X86 的说明。
我的步骤:
关于python - TensorFlow 在训练期间没有使用我的 M1 MacBook GPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67352841/