python - TensorFlow 在训练期间没有使用我的 M1 MacBook GPU

标签 python tensorflow apple-m1

我已经安装了 tensorflow-macos,在训练时这是我的 CPU 使用率
和 GPU 使用率 .
我可以让 Tensorflow 在 GPU 上运行吗?

最佳答案

我今天一直在设置我的新 M1 机器,并正在寻找一个测试,例如由 Aman Anand 提供的测试。已经在这里。在遵循 #153 中提供的标准指令后,它成功地在 GPU 上运行。使用使用 Homebrew 安装的 miniforge 包管理器和从 #153 指南中的 YAML 文件克隆的环境。
Process running on 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)

Training not using GPU
取消注释从 Aman 的代码中添加的行并重新运行使 GPU 再次工作:
Training uses GPU again
如果这些脚本仍然不使用每个事件监视器的 GPU(在 view/update_frequency 中将更新率设置为 1s),请返回 #153 页面以从头开始并仔细按照说明进行操作,并确保忽略针对 Intel/X86 的说明。
我的步骤:
  • 安装 xcode(来自应用商店)
  • install Homebrew (不要忘记在安装完成后按照建议设置 PATH,然后终端需要重新启动或重新加载您的 shell 配置文件)
  • 安装 miniforge ("brew install miniforge")
  • 使用 #153 中给出的命令复制 environment.yaml 文件并克隆为新的 conda 环境。
  • 利润。
  • 关于python - TensorFlow 在训练期间没有使用我的 M1 MacBook GPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67352841/

    相关文章:

    python - 防止 wxPython 显示 'Unhandled exception' 对话框

    java - Python脚本参数化

    python - 需要帮助使用祖先从 Scrapy 的列表中抓取项目

    tensorflow - conda install -c conda-forge tensorflow 只是停留在 Solving 环境中

    node.js - puppeteer@5.5.0 安装 : `node install.js` on M1

    c++ - 如何计算 mac M1 和 x86-64 上的前导零?

    python - 在selenium中单击select2下拉列表时出现问题

    python - TensorFlow 似乎不使用 GPU

    python - 在 Tensorflow 中使用镜像策略时出错

    php - Laravel Sail 没有在我的 macbook m1 上运行