向量化编程

在机器学习中,尽量使用内置函数,尽量避免显示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

参考 吴恩达老师的课程:神经网络与深度学习

你可能感兴趣的:(机器学习)