【问题标题】:Ensuring if Python code is running on GPU or CPU确保 Python 代码是在 GPU 还是 CPU 上运行
【发布时间】:2017-09-02 01:21:31
【问题描述】:

我编写了一些基本的Deep Learning 代码,其中包含 2 个LSTM 层。我使用KerasTheano 作为我的后端。与AWS 上的另一台机器相比,此代码在AWS 上的我的机器上花费的时间太长了。在运行速度更快的机器上,每个 epoch 需要 640 秒,而在运行速度较慢的机器上,每个 epoch 需要超过 10,000 秒。

我开始认为较慢机器上的代码没有在 GPU 上运行。两台机器上运行的代码完全相同。机器配置也一样。

看起来Theano 安装在速度较慢的机器上。我运行了以下代码并得到了结果:

有没有办法检查我的代码是在 GPU 还是 CPU 上运行?

在这方面的任何帮助将不胜感激。

TIA。

编辑

根据@Marcin 的建议,我添加了以下代码:

但是当我运行以下代码时,我仍然得到Used the cpu 结果:

【问题讨论】:

    标签: machine-learning neural-network deep-learning theano keras


    【解决方案1】:

    有几种方法可以检查:

    1. 签入Theano标志:

      import os
      print(os.environ["THEANO_FLAGS"])
      

      看看device设置了什么。

    2. 尝试运行此代码 sn-p 提供here:

    代码:

    from theano import function, config, shared, tensor
    import numpy
    import time
    
    vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
    iters = 1000
    
    rng = numpy.random.RandomState(22)
    x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
    f = function([], tensor.exp(x))
    print(f.maker.fgraph.toposort())
    t0 = time.time()
    for i in range(iters):
        r = f()
    t1 = time.time()
    print("Looping %d times took %f seconds" % (iters, t1 - t0))
    print("Result is %s" % (r,))
    if numpy.any([isinstance(x.op, tensor.Elemwise) and
                  ('Gpu' not in type(x.op).__name__)
                  for x in f.maker.fgraph.toposort()]):
        print('Used the cpu')
    else:
        print('Used the gpu')
    

    编辑:

    尝试将此 sn-p 添加为代码的前两行(重要)

    import os
    os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=gpu,floatX=float32"
    

    【讨论】:

    • 非常感谢您的回复。我运行了你的代码,确实得到了Used the cpu。您是否知道如何将其更改为使用GPU
    • 您能打印出theano 标志的结果吗?
    • 天哪——看来你根本没有安装Theano。这也可以解释巨大的训练时间。
    • 试试device=cuda 而不是device=gpu
    • 是的——就是这样。这意味着您没有CUDA。老实说-我会提出一个新问题-我们超出了问题的范围:)
    猜你喜欢
    • 1970-01-01
    • 2020-07-18
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2020-07-26
    • 1970-01-01
    • 2011-07-17
    相关资源
    最近更新 更多