DeepLearning.ai--吴恩达--Chapter 2_Vecterization

2017-12-08 13:48:22

在深度学习 处理大数据时,采用常用的 for 循环无法及时有效地处理,因此采用 矢量化的方法来处理矩阵运算。

以 Python 的 numpy 库来对比 10^6 (百万)数量级

传统的 for circulate (for 循环 ) 与 矢量化 处理 的对比

 1 import numpy as np
 2 import time
 3 
 4 a = np.random.rand(1000000)
 5 b = np.random.rand(1000000)
 6 
 7 tic = time.time()
 8 c = np.dot(a,b)
 9 toc = time.time()
10 
11 print(c)
12 print("Vecterization: " + str(1000*(toc - tic)) + " ms")
13 
14 c = 0
15 tic = time.time()
16 for i in range (1000000):
17     c += a[i] * b[i]
18 toc = time.time()
19 print(c)
20 print("ForLoop Version: " + str(1000*(toc - tic)) + " ms")

执行结果:

附: Pycharm 安装 Numpy ,MatlopLib  Plugine

1-- File => Settings

2-- 进入菜单 添加 搜索栏

3-- 在搜索栏里 输入

原文地址:https://www.cnblogs.com/masterSoul/p/8005133.html