验证 GPU 是否确实在 Keras/Tensorflow 中使用,而不仅仅是验证是否存在

2023-12-15

我刚刚构建了一个深度学习装备(AMD 12 核 Threadripper;GeForce RTX 2080 ti;64Gb RAM)。我本来想在 Ubuntu 19.0 上安装 CUDnn 和 CUDA,但是安装太痛苦了,看了一些资料后,我决定切换到 Windows 10...

在 condas 内外安装了几次 tensorflow-gpu 后,我遇到了进一步的问题,我认为这些问题归结为 CUDnn-CUDA-tensorflow 兼容性,因此卸载了各种版本的 CUDA 和 tf.我的输出来自nvcc --version:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:04_Central_Daylight_Time_2018
Cuda compilation tools, release 10.0, V10.0.130

还附上 nvidia-smi(显示 CUDA==11.0?!)

enter image description here

我也有:

 if tf.test.gpu_device_name():
        print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
    else:
        print("Please install GPU version of TF")
    print("keras version: {0} | Backend used: {1}".format(keras.__version__, backend.backend()))
    print("tensorflow version: {0} | Backend used: {1}".format(tf.__version__, backend.backend()))
    print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
    print("CUDA: {0} | CUDnn: {1}".format(tf_build_info.cuda_version_number,  tf_build_info.cudnn_version_number))

与输出:

My device: [name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 12853915229880452239
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 9104897474
lo

    cality {
      bus_id: 1
      links {
      }
    }
    incarnation: 7328135816345461398
    physical_device_desc: "device: 0, name: GeForce RTX 2080 Ti, pci bus id: 0000:42:00.0, compute capability: 7.5"
    ]
    Default GPU Device: /device:GPU:0
    keras version: 2.3.1 | Backend used: tensorflow
    tensorflow version: 2.1.0 | Backend used: tensorflow
    Num GPUs Available:  1
    CUDA: 10.1 | CUDnn: 7

所以(我希望)我的安装至少部分有效,我只是仍然不知道 GPU 是否用于我的训练,或者它只是公认的与现有的一样,但是CPUis still正在使用。我该如何区分这一点?

我也用pycharm。建议安装 Visio Studio 和附加步骤here:

5. Include cudnn.lib in your Visual Studio project.
Open the Visual Studio project and right-click on the project name.
Click Linker > Input > Additional Dependencies.
Add cudnn.lib and click OK.

我没有做这一步。我还读到我需要在环境变量中设置以下内容,但我的目录是空的:

SET PATH=C:\tools\cuda\bin;%PATH%

有人能验证一下吗?

另外,我的 Kera 模型需要搜索超参数:

grid = GridSearchCV(estimator=model,
                        param_grid=param_grids,
                        n_jobs=-1, # -1 for all cores
                        cv=KFold(),
                        verbose=10)

grid_result = grid.fit(X_standardized, Y)

这在我的 MBP 上运行良好(当然假设 n_jobs=-1 占用所有 CPU 核心)。在我的 DL 设备上,我收到警告:

ERROR: The process with PID 5156 (child process of PID 1184) could not be terminated.
Reason: Access is denied.
ERROR: The process with PID 1184 (child process of PID 6920) could not be terminated.
Reason: There is no running instance of the task.
2020-03-28 20:29:48.598918: E tensorflow/stream_executor/cuda/cuda_blas.cc:238] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2020-03-28 20:29:48.599348: E tensorflow/stream_executor/cuda/cuda_blas.cc:238] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2020-03-28 20:29:48.599655: E tensorflow/stream_executor/cuda/cuda_blas.cc:238] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2020-03-28 20:29:48.603023: E tensorflow/stream_executor/cuda/cuda_blas.cc:238] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2020-03-28 20:29:48.603649: E tensorflow/stream_executor/cuda/cuda_blas.cc:238] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2020-03-28 20:29:48.604236: E tensorflow/stream_executor/cuda/cuda_blas.cc:238] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2020-03-28 20:29:48.604773: E tensorflow/stream_executor/cuda/cuda_blas.cc:238] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2020-03-28 20:29:48.605524: E tensorflow/stream_executor/cuda/cuda_blas.cc:238] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2020-03-28 20:29:48.608151: E tensorflow/stream_executor/cuda/cuda_blas.cc:238] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2020-03-28 20:29:48.608369: W tensorflow/stream_executor/stream.cc:2041] attempting to perform BLAS operation using StreamExecutor without BLAS support
2020-03-28 20:29:48.608559: W tensorflow/core/common_runtime/base_collective_executor.cc:217] BaseCollectiveExecutor::StartAbort Internal: Blas GEMM launch failed : a.shape=(10, 8), b.shape=(8, 4), m=10, n=4, k=8
     [[{{node dense_1/MatMul}}]]
C:\Users\me\PycharmProjects\untitled\venv\lib\site-packages\sklearn\model_selection\_validation.py:536: FitFailedWarning: Estimator fit failed. The score on this train-test partition for these parameters will be set to nan. Details: 
tensorflow.python.framework.errors_impl.InternalError:  Blas GEMM launch failed : a.shape=(10, 8), b.shape=(8, 4), m=10, n=4, k=8
     [[node dense_1/MatMul (defined at C:\Users\me\PycharmProjects\untitled\venv\lib\site-packages\keras\backend\tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_982]

我可以假设使用时GridSearchCV,这仅利用CPU,而不利用GPU?尽管如此,当在我的代码中运行和计时另一个方法时,我将 MBP 的时间(使用 2.8 GHz Intel Core i7 时约为 40 秒)与台式机的时间(使用 12 核 threadripper 时约为 43 秒)进行了比较。即使在比较 CPU 时,我预计时间也比 MBP 快得多。那么我的假设是错误的吗?


您可以看到以下详细信息here.
根据文档:

If a TensorFlow operation has both CPU and GPU implementations, 
by default, the GPU devices will be given priority when the operation is assigned to a device.
For example, tf.matmul has both CPU and GPU kernels. 
On a system with devices CPU:0 and GPU:0, the GPU:0 device will be selected to run tf.matmul unless you explicitly request running it on another device.

记录设备放置

tf.debugging.set_log_device_placement(True)

# Create some tensors
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
c = tf.matmul(a, b)

print(c)
Example Result
Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0
tf.Tensor(
[[22. 28.]
 [49. 64.]], shape=(2, 2), dtype=float32)

对于手动设备放置

tf.debugging.set_log_device_placement(True)

# Place tensors on the CPU
with tf.device('/GPU:0'):
  a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
  b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])

c = tf.matmul(a, b)
print(c)
Example Result: 
Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0
tf.Tensor(
[[22. 28.]
 [49. 64.]], shape=(2, 2), dtype=float32)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

验证 GPU 是否确实在 Keras/Tensorflow 中使用,而不仅仅是验证是否存在 的相关文章

  • TensorFlow:在训练时更改变量

    如果我将输入管道从 feed dict 更改为 tf data dataset 如何在每次迭代后的训练期间更改网络内参数的值 澄清一下 旧代码看起来像这样 Define Training Step model is some class t
  • 父子进程之间的通信

    我正在尝试创建一个具有一个或多个子进程的 Python 3 程序 父进程生成子进程 然后继续处理自己的业务 有时我想向特定的子进程发送一条消息 由其捕获该消息并采取行动 此外 子进程在等待消息时需要处于非锁定状态 它将运行自己的循环来维护服
  • pip:需要将包名称tensorflow-gpu更改为tensorflow

    我正在尝试将具有 GPU 支持的张量流安装到 conda 环境中 我使用命令 pip install ignore installed upgrade https storage googleapis com tensorflow linu
  • 无法获取未知等级的 Shape 长度

    我有一个神经网络 来自tf data数据生成器和tf keras模型 如下 简化版本 因为太长 dataset A tf data Dataset反对与next x方法调用get next为了x train迭代器和next y方法调用get
  • 如何将 tf.contrib.seq2seq.Helper 用于非嵌入数据?

    我正在尝试使用 tf contrib seq2seq 模块对某些数据 仅 float32 向量 进行预测 但我使用 TensorFlow 中的 seq2seq 模块找到的所有示例都用于翻译 因此用于嵌入 我正在努力准确理解 tf contr
  • QDataWidgetMapper;将 TableWidget 映射到模型

    我没有找到任何文档显示 QDataWidgetMapper 实际上适用于哪些小部件 也没有找到任何使用 QTableWidget 进行映射的实现 它绝对适用于 QLineEdit 和 QComboBoxes 它们是输入小部件 但是是否可以映
  • 如何手动计算分类交叉熵?

    当我手动计算二元交叉熵时 我应用 sigmoid 来获取概率 然后使用交叉熵公式并平均结果 logits tf constant 1 1 0 1 2 labels tf constant 0 0 1 1 1 probs tf nn sigm
  • 如何结合pytube和tkinter标签来显示进度?

    我正在编写从 youtube 下载歌曲的小程序 使用 pytube 我想添加 python tkinter GUI 以在下载文件时显示百分比值 现在 当我执行代码时 程序首先下载文件 大约需要 60 秒 然后才显示 100 的标签 如果我希
  • Keras:多类 NLP 任务中 model.evaluate 与 model.predict 的准确性差异

    我正在使用以下代码在 keras 中为 NLP 任务训练一个简单模型 训练集 测试集和验证集的变量名称是不言自明的 该数据集有 19 个类 因此网络的最后一层有 19 个输出 标签也是 one hot 编码的 nb classes 19 m
  • Python排序算法[重复]

    这个问题在这里已经有答案了 我在Python中实现了不同的排序算法 以更好地理解它们 我想知道Python的内置排序方法实现什么类型的排序 这是一个叫做Timsort http en wikipedia org wiki Timsort由
  • Python:返回 // 正则表达式之间的字符串[重复]

    这个问题在这里已经有答案了 我仍然不明白正则表达式 我阅读了文档 但是在我想出了正则表达式字符之后 那么如何使用它们呢 例如 我只想返回前两个斜杠之间的任何内容 en lemon peel n ca llimona n is 的输出应该是
  • 在Python中,如何将矩阵逆时针旋转90度?

    gt gt gt def rotate matrix k List List int For example if I have m 1 2 3 2 3 3 5 4 3 rotate matrix m should give me 3 3
  • 如何使用一个模型中间层的输出作为另一个模型的输入?

    我训练一个模型A并尝试使用中间层的输出name layer x 作为模型的附加输入B 我尝试像 Keras 文档一样使用中间层的输出https keras io getting started faq how can i obtain th
  • 使用大数据集在 Google Colab TPU 上训练 seq2seq 模型 - Keras

    我正在尝试使用 Google Colab TPU 上的 Keras 训练用于机器翻译的序列到序列模型 我有一个可以加载到内存中的数据集 但我必须对其进行预处理才能将其提供给模型 特别是 我需要将目标单词转换为一个热向量 并且在许多示例中 我
  • 如何将字符串转换为浮点数? [复制]

    这个问题在这里已经有答案了 我需要转换变量 pi string 3 1415926 变成一个浮子 这是我正在处理的事情 你的线路应该是pi float float pi string float pi string 是一个浮点值 你不能给它
  • 验证 Transformer 中多头注意力的实现

    我已经实施了MultiAttention head in Transformers 周围有太多的实现 所以很混乱 有人可以验证我的实施是否正确 DotProductAttention 引用自 https www tensorflow org
  • Python中Decimal类型的澄清

    每个人都知道 或者至少 每个程序员都应该知道 http docs oracle com cd E19957 01 806 3568 ncg goldberg html 即使用float类型可能会导致精度错误 然而 在某些情况下 精确的解决方
  • 在flatpak项目中使用scrapy脚本

    我正在构建一个 flatpak 构建的项目 我有一个按钮 当单击它时我希望它运行 scrapy 脚本来抓取数据 窗口用户界面
  • 首先对列表中最长的项目进行排序

    我正在使用 lambda 来修改排序的行为 sorted list key lambda item item lower len item 对包含元素的列表进行排序A1 A2 A3 A B1 B2 B3 B 结果是A A1 A2 A3 B
  • 使用队列从多个输入文件中统一采样

    我的数据集中的每个类都有一个序列化文件 我想使用队列来加载每个文件 然后将它们放入 RandomShuffleQueue 中 这样我就可以从每个类中获得随机的示例组合 我认为这段代码会起作用 在此示例中 每个文件有 10 个示例 filen

随机推荐