import numpy as np
#神經網路的分類任務
#理論上兩層神經網路就足夠擬合任意函式
#為了使得神經網路不僅僅只能擬合線性函式,引入了啟用層
#常見的啟用函式有三種:階躍,sigmoid,relu函式
def affine_forward(x,w,b):
out=none
n=x.shape[0]
x_row=x.reshape(n,-1)
out=np.dot(x_row,w)+b #???為什麼b的維度不一樣
cache=(x,w,b)
return out,cache
def affine_backward(dout,cache):
x,w,b=cache
dx,dw,db=none,none,none
dx=np.dot(dout,w.t)
dx=np.reshape(dx,x.shape)
x_row=x.reshape(x.shape[0],-1)
dw=np.dot(x_row.t,dout)
db=np.sum(dout,axis=0,keepdims=true)
return dx,dw,db
x=np.array([[2,1],
[-1,1],
[-1,-1],
[1,-1]])
t=np.array([0,1,2,3])
np.random.seed(1)
input_dim=x.shape[1] #2
num_classes=t.shape[0] #4
hidden_dim=100
reg=0.001
epsilon=0.001 #梯度下降的學習率,為可調引數
w1=np.random.randn(input_dim,hidden_dim) #2x50
w2=np.random.randn(hidden_dim,num_classes) #50x4
b1=np.zeros((1,hidden_dim))
b2=np.zeros((1,num_classes))
for j in range(10000):
h,fc_cache=affine_forward(x,w1,b1)
h=np.maximum(0,h)
relu_cache=h
y,cachey=affine_forward(h,w2,b2)
probs=np.exp(y-np.max(y,axis=1,keepdims=true))
probs/=np.sum(probs,axis=1,keepdims=true)
n=y.shape[0]
#print(probs[np.arange(n),t])
loss=-np.sum(np.log(probs[np.arange(n),t]))/n
#print(loss)
dx=probs.copy()
dx[np.arange(n),t]-=1
dx/=n
dh1,dw2,db2=affine_backward(dx,cachey)
dh1[relu_cache<=0]=0
dx,dw1,db1=affine_backward(dh1,fc_cache)
#正則化懲罰項。為了避免最後求出的w過於集中所設定的項為了衡量分散度,故進行如下修正
dw2+=reg*w2
dw1+=reg*w1
w2+=-epsilon*dw2
b2+=-epsilon*db2
w1+=-epsilon*dw1
b1+=-epsilon*db1
test=np.array([[22,33],[-100,20],[-72,-5],[3,-20]])
h,cf_cache=affine_forward(test,w1,b1)
h=np.maximum(0,h)
relu_cache=h
y,cachey=affine_forward(h,w2,b2)
probs=np.exp(y-np.max(y,axis=1,keepdims=true))
probs/=np.sum(probs,axis=1,keepdims=true)
print(probs)
for k in range(4):
print(test[k,:],"所在的象限為",np.argmax(probs[k,:])+1)
用numpy構造的乙個簡單BP
coding utf 8 created on thu oct 4 08 28 15 2018 author 37989 import numpy as np import pandas as pd from matplotlib import pyplot 標準化 def standard x x...
乙個通用純C佇列的實現
佇列並不是很複雜的資料結構,但是非常實用,這裡實現乙個佇列是因為在我的另一篇部落格非常精簡的linux執行緒池實現中要用到。佇列api定義如下 ifndef queue h included define queue h included typedef struct queue queue t q...
乙個通用純C佇列的實現
佇列並不是很複雜的資料結構,但是非常實用,這裡實現乙個佇列是因為在我的另一篇部落格非常精簡的linux執行緒池實現中要用到。佇列api定義如下 ifndef queue h included define queue h included typedef struct queue queue t q...