ベクトルプログラミング

4366 ワード

機械学習では,内蔵関数をできるだけ使用し,forサイクルの表示をできるだけ避け,量子化プログラミングに向けてプログラムの実行速度を著しく速めることができる.
import time
a = np.random.rand(1000000)
b = np.random.rand(1000000)
tic = time.time()
c = np.dot(a,b)
toc = time.time()
print "Vectorized version:" + str(1000*(toc-tic)) +"ms"

c = 0
tic = time.time()
for i in range(1000000):
    c == a[i]*b[i]
toc = time.time()
print "For loop:"+ str(1000*(toc-tic)) +"ms"

Vectorized version:3.00002098083ms For loop:807.000160217ms
import time

A = np.random.rand(10000,100)
b = np.random.rand(100,1)

c = np.zeros((10000,1))
tic = time.time()
for i in range(10000):
    for j in range(100):
        c[i] += A[i][j]*b[j]
toc = time.time()
print "For loop:"+ str(1000*(toc-tic)) +"ms"

tic = time.time()
c = np.dot(A,b)
toc = time.time()
print "Vectorized version:" + str(1000*(toc-tic)) +"ms"

For loop:414.999961853ms Vectorized version:0.999927520752ms
import time

v = np.random.rand(1000000,1)
c = np.zeros((1000000,1))
tic = time.time()
for i in range(1000000):
    c[i] = math.exp(v[i])
toc = time.time()
print "For loop:"+ str(1000*(toc-tic)) +"ms"

tic = time.time()
c = np.exp(v)
toc = time.time()
print "Vectorized version:" + str(1000*(toc-tic)) +"ms"

For loop:1101.00007057ms Vectorized version:29.9999713898ms
参考呉恩達先生の課程:ニューラルネットワークと深い学習