在 GPU 上强制执行 python 脚本 [英] Force python script on GPU

查看:52
本文介绍了在 GPU 上强制执行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 GPU 上强制执行 Python 脚本?在我的代码中,我使用了 tensorflow 和 keras,并且我已经有了 tensorflow-gpu 版本,但是我的代码无论如何都在 CPU 上运行.我想知道是否有办法强制在 Tensorflow、Numpy 或其他人的 GPU 上独立运行.

Is there a way to force a Python script on GPU? In my code I use tensorflow and keras, and I have already the tensorflow-gpu version, but my code runs on CPU anyway. I'd like to know if there is a way to force the running on GPU independently on Tensorflow, Numpy or others.

推荐答案

对于 TensorFlow(但不是一般的 Python),这里有一个很好的描述:https://www.tensorflow.org/guide/gpu

For TensorFlow (but not python in general) there is a good description of how to do this here: https://www.tensorflow.org/guide/gpu

要强制在特定处理器(CPU 或 GPU)上执行功能,请使用对 tf.device() 的 TensorFlow 调用,如下所示:

To force a function to be performed on a specific processor (CPU or GPU) use the TensorFlow call to tf.device() as follows:

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

在这种情况下,a 和 b 的数据将存储在 GPU0 上,matmul"操作也将在 GPU0 上执行.

in this case the data for a and b will be stored on GPU0, and the operation "matmul" will be performed on the GPU0 as well.

这篇关于在 GPU 上强制执行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆