python – Theano GPU计算比numpy慢
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Theano GPU计算比numpy慢,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2160字,纯文字阅读大概需要4分钟。
内容图文
![python – Theano GPU计算比numpy慢](/upload/InfoBanner/zyjiaocheng/736/16598de8b2e04d26a75159aa1c6baf04.jpg)
我正在学习使用theano.我想通过计算其中每个元素的二进制TF-IDF来填充term-document矩阵(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?
解决方法:
问题是你每次都在编译你的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是有意义的.
内容总结
以上是互联网集市为您收集整理的python – Theano GPU计算比numpy慢全部内容,希望文章能够帮你解决python – Theano GPU计算比numpy慢所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。