深度学习中,可采用向量化替代for循环,优化耗时问题
对比例程如下,参考Andrew NG的课程笔记
import time import numpy as np a = np.random.rand(1000000) b = np.random.rand(1000000) tic = time.time() c = np.dot(a,b) toc = time.time() print(c) print("Vectorized version: " , str(1000*(toc-tic)) + "ms") c = 0 tic1 = time.time() for i in range(1000000): c += a[i]*b[i] toc1 = time.time() print(c) print("For loop version: " , str(1000*(toc1-tic1)) + "ms")
处理百万数据,耗时相差400多倍。
效果图:
例子
import numpy as np import time a = np.random.rand(1000000) b = np.random.rand(1000000) tic = time.time() c = np.dot(a,b) toc = time.time() print© print(“vectorized version:” + str((toc-tic))+“s”) c1 = 0 tic = time.time() for i in range(1000000): c1 += a[i]*b[i] toc = time.time() print(c1) print(“Nonvectorized version:” + str(toc-tic)+“s”)
结果
250487.97870397285
vectorized version:0.002000093460083008s
250487.9787039739
Nonvectorized version:0.957054615020752s
可以看出向量化后执行时间比使用for循环快478倍
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理