神经网络----为什么使用向量化

我们在处理大数据的时候,尽量避免使用for循环,那样会将低速度

import numpy as np
import time 

a = np.random.rand(1000000)
b = np.random.rand(1000000)
tic = time.time()
z = np.dot(a,b)
toc = time.time()
print(z)
print('向量化:'+str((toc-tic)*1000)+'ms')

c = 0
for i in range(1000000):
    c += a[i]*b[i]
top = time.time()
print(c)
print('非向量化:'+str((top-tic)*1000)+'ms')



249951.26378956603
向量化:1.995086669921875ms
249951.2637895705
非向量化:307.2049617767334ms

我们看到dot函数能够很快的完成任务

posted @ 2022-09-24 20:44  故y  阅读(89)  评论(0)    收藏  举报