# 梯度才是神经网络最美的样子

``` # Function for batch gradient decent
def Batch_GD (Learning_Rate,num_iterations,X,y):
#Step 1: Initial Parameter
N=len(X)
w=np.zeros((X.shape[1],1))
b=0
costs=[]
# Starting Loop
for i in range(num_iterations):
#Step 2: Apply Sigmoid Function and get y prediction
Z=np.dot(wT,XT)+b
y_pred=1/(1+1/np.exp(Z))

#Step 3: Calculate Loss Function
cost=-(1/N)*np.sum(y*np.log(y_pred)+(1-y)*np.log(1-y_pred))

dw=1/N*np.dot(XT,(y_pred-y).T)
db=1/N*np.sum(y_pred-y)

#Step 5: Update w & b
w = w - Learning_Rate * dw
b = b - Learning_Rate * db

# Records cost
if i % 1000 == 0:
costs.append(cost)
#print(cost)
return(w,b,costs)
# Run a function
Result_BatchGD=Batch_GD(Learning_Rate=0.01,num_iterations=100000,X=X,y=y) ```

``` # Function for Stochastic Gradient Descent
def Stochastic_GD (Learning_Rate,num_iterations,X,y):
# Step 1: Initial Parameter
N=len(X)
w=np.zeros((X.shape[1],1))
b=0
costs=[]
# Starting two layer of loops
for i in range(num_iterations):
for j in range(N):
# Choose 1 record
XX=X[j,:]
yy=y[j]
# Step 2: Apply Sigmoid Function and get y prediction
Z=np.dot(wT,XX.T)+b
y_pred=1/(1+1/np.exp(Z))
#Step 3: Calculate Loss Function
cost=-(yy*np.log(y_pred)+(1-yy)*np.log(1-y_pred))
dw=np.multiply(XX,(y_pred-yy)).reshape((2,1))
db=y_pred-yy
#Step 5: Update w & b
w = w - Learning_Rate * dw
b = b - Learning_Rate * db

#Step 6: Calculate Loss Function
Z_full=np.dot(wT,XT)+b
y_pred_full=1/(1+1/np.exp(Z_full))
cost=-(1/N)*np.sum(y*np.log(y_pred_full)+(1-y)*np.log(1-y_pred_full))
#Records cost
if i % 100 == 0:
costs.append(cost)
#print(cost)

return(w,b,costs)
# Run a function
Result_Stoc_GD=Stochastic_GD(Learning_Rate=0.01,num_iterations=2000,X=X,y=y) ```

```# Function for mini batch Gradient Descent
def Minibatch_GD (Learning_Rate,num_iterations,X,y,Minibatch):
# Part 1: Mini Batch
np.random.seed(1000)
N=len(X)
mini_batches=[]

#Step 1: Shuffle (X,y)
permutation=list(np.random.permutation(N))
shuffled_X=X[permutation,:]
shuffled_y=y[permutation]

#Step 2: Partition
num_complete_minibatches=int(np.floor(N/Minibatch))

for i in range(num_complete_minibatches):
mini_batch_X=shuffled_X[i*Minibatch:(i+1)*Minibatch,:]
mini_batch_y=shuffled_y[i*Minibatch:(i+1)*Minibatch]

mini_batch = (mini_batch_X, mini_batch_y)
mini_batches.append(mini_batch)

if N % Minibatch !=0:
mini_batch_X=shuffled_X[N-Minibatch:N,:]
mini_batch_y=shuffled_y[N-Minibatch:N]

mini_batch = (mini_batch_X, mini_batch_y)
mini_batches.append(mini_batch)

w=np.zeros((X.shape[1],1))
b=0
costs=[]

for i in range(num_iterations):
for j in range(num_complete_minibatches+1):
#Select Minibatch
XX=mini_batches[j][0]
yy=mini_batches[j][1]
#Step 2: Apply Sigmoid Function and get y prediction
Z=np.dot(wT,XX.T)+b
y_pred=1/(1+1/np.exp(Z))

dw=1/Minibatch*np.dot(XX.T,(y_pred-yy).T)
db=1/Minibatch*np.sum(y_pred-yy)
#Step 4: Update w & b
w = w - Learning_Rate * dw
b = b - Learning_Rate * db

#Step 5: Calculate Loss Function
Z_full=np.dot(wT,XT)+b
y_pred_full=1/(1+1/np.exp(Z_full))
cost=-(1/N)*np.sum(y*np.log(y_pred_full)+(1-y)*np.log(1-y_pred_full))

if i % 1000 ==0:
costs.append(cost)
#print(cost)

return(w,b,costs)
# Run a function
Result_MiniGD=Minibatch_GD(Learning_Rate=0.01,num_iterations=100000,X=X,y=y,Minibatch=50) ```

81 篇文章22 人订阅

0 条评论

## 相关文章

### 学术大讲堂 | （三）智慧农业领域的关键AI技术

1) 从收入增长方面看：手机流量红利持续， 创新业务和宽带业务快速增长， 带动行业收入增长2.9%; 中电信、中联通收入增速超行业平均份额。

20630

8640

### DeepMind论文：深度压缩感知，新框架提升GAN性能（附链接）

CS具有灵活性和数据效率高的优点，但由于其稀疏性和昂贵的重建过程，CS的应用受到限制。

20740

17720

12020

### Scikit-learn 基础

Scikit-learn 是开源的 Python 库，通过统一的界面实现机器学习、预处理、交叉验证及可视化算法。

15330

### 二值网络--Bi-Real Net: Enhancing the Performance of 1-bit CNNs

Bi-Real Net: Enhancing the Performance of 1-bit CNNs With Improved Representatio...

14620

12230

23410

17710