代码之家  ›  专栏  ›  技术社区  ›  Vektor88

Theano GPU计算速度低于numpy

  •  3
  • Vektor88  · 技术社区  · 9 年前

    我正在学习使用泰诺。我想通过计算其中每个元素的二进制TF-IDF来填充术语文档矩阵(numpy稀疏矩阵):

    import theano
    import theano.tensor as T
    import numpy as np
    from time import perf_counter
    
    def tfidf_gpu(appearance_in_documents,num_documents,document_words):
        start = perf_counter()
        APP = T.scalar('APP',dtype='int32')
        N = T.scalar('N',dtype='int32')
        SF = T.scalar('S',dtype='int32')
        F = (T.log(N)-T.log(APP)) / SF
        TFIDF = theano.function([N,APP,SF],F)
        ret = TFIDF(num_documents,appearance_in_documents,document_words)
        end = perf_counter()
        print("\nTFIDF_GPU ",end-start," secs.")
        return ret
    
    def tfidf_cpu(appearance_in_documents,num_documents,document_words):
        start = perf_counter()
        tfidf = (np.log(num_documents)-np.log(appearance_in_documents))/document_words
        end = perf_counter()
        print("TFIDF_CPU ",end-start," secs.\n")
        return tfidf
    

    但numpy版本比theano实现快得多:

    Progress 1/43
    TFIDF_GPU  0.05702276699594222  secs.
    TFIDF_CPU  1.454801531508565e-05  secs.
    
    Progress 2/43
    TFIDF_GPU  0.023830442980397493  secs.
    TFIDF_CPU  1.1073017958551645e-05  secs.
    
    Progress 3/43
    TFIDF_GPU  0.021920352999586612  secs.
    TFIDF_CPU  1.0738993296399713e-05  secs.
    
    Progress 4/43
    TFIDF_GPU  0.02303648801171221  secs.
    TFIDF_CPU  1.1675001587718725e-05  secs.
    
    Progress 5/43
    TFIDF_GPU  0.02359767400776036  secs.
    TFIDF_CPU  1.4385004760697484e-05  secs.
    
    ....
    

    我已经读到,这可能是由于开销,对于小型操作可能会降低性能。

    是我的代码坏了,还是我应该因为开销而避免使用GPU?

    1 回复  |  直到 9 年前
        1
  •  7
  •   amir    9 年前

    问题是,您每次都在编译Theano函数。编译需要时间。尝试如下传递编译后的函数:

    def tfidf_gpu(appearance_in_documents,num_documents,document_words,TFIDF):
        start = perf_counter()
        ret = TFIDF(num_documents,appearance_in_documents,document_words)
        end = perf_counter()
        print("\nTFIDF_GPU ",end-start," secs.")
        return ret
    
    APP = T.scalar('APP',dtype='int32')
    N = T.scalar('N',dtype='int32')
    SF = T.scalar('S',dtype='int32')
    F = (T.log(N)-T.log(APP)) / SF
    TFIDF = theano.function([N,APP,SF],F)
    
    tfidf_gpu(appearance_in_documents,num_documents,document_words,TFIDF)
    

    此外,TFIDF任务是带宽密集型任务。Theano和GPU通常最适合计算密集型任务。

    当前的任务将相当大的开销将数据传送到GPU并返回,因为最终您需要读取每个元素O(1)次。但如果你想做更多的计算,使用GPU是有意义的。