Coursera吴恩达《神经网络与深度学习》课程笔记（3）-- 神经网络基础之Python与向量化
2017-09-13 10:39:00      个评论    来源：红色石头的专栏

### Vectorization

```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(c)
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(c)
print("for loop:" + str(1000*(toc-tic)) + "ms")```

```250286.989866
Vectorized version:1.5027523040771484ms
250286.989866
For loop:474.29513931274414ms```

### Vectorizing Logistic Regression

Z=wTX+b

```Z = np.dot(w.T,X) + b
A = sigmoid(Z)```

### Vectorizing Logistic Regression’s Gradient Output

dZ=A?Y

db可表示为：

db=1m∑i=1mdz(i)

`db = 1/m*np.sum(dZ)`

dw可表示为：

dw=1mX?dZT

`dw = 1/m*np.dot(X,dZ.T)`

```Z = np.dot(w.T,X) + b
A = sigmoid(Z)
dZ = A-Y
dw = 1/m*np.dot(X,dZ.T)
db = 1/m*np.sum(dZ)

w = w - alpha*dw
b = b - alpha*db```

### A note on python/numpy vectors

python中，如果我们用下列语句来定义一个向量：

`a = np.random.randn(5)`

```a = np.random.randn(5,1)
b = np.random.randn(1,5)```

`assert(a.shape == (5,1))`

assert会对内嵌语句进行判断，即判断a的维度是不是（5，1）的。如果不是，则程序在此处停止。使用assert语句也是一种很好的习惯，能够帮助我们及时检查、发现语句是否正确。

`a.reshape((5,1))`

### Quick tour of Jupyter/iPython Notebooks

Jupyter notebook（又称IPython notebook）是一个交互式的笔记本，支持运行超过40种编程语言。本课程所有的编程练习题都将在Jupyter notebook上进行，使用的语言是python。

### Explanation of logistic regression cost function(optional)

y^=σ(wTx+b)

y^=P(y=1|x)

p(y|x)=y^

p(y|x)=1?y^

P(y|x)=y^y(1?y^)(1?y)

logP(y|x)=logy^y(1?y^)(1?y)=ylogy^+(1?y^)log(1?y)

L=?(ylogy^+(1?y^)log(1?y))

max∏i=1mP(y(i)|x(i))

J(w,b)=?1m∑i=1mL(y^(i),y(i))=?∑i=1my(i)logy^(i)+(1?y^(i))log(1?y(i))