首页 > 解决方案 > 运行pytorch时如何加载cuda?

问题描述

我有一个带有 pytorch 和 cudatoolkit 10.1 的旧 conda 虚拟环境。我能够运行代码,并且在开始时总是会收到以下消息:

I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1

但是,我用 cudatoolkit 11.0 创建了一个新的虚拟环境,但是现在这条消息没有在代码的开头运行(我也没有收到一条错误消息说它找不到文件),并且代码在尝试时最终崩溃使用 gpu。如何确保它正确加载 GPU 支持?

标签: pythontensorflowpytorchgpu

解决方案


在 Tensorflow 中,要确保代码在 GPU 上正常运行,请运行此示例代码,

import tensorflow as tf
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)

#Output : It loads the GPU support
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)

推荐阅读