h1 = np.maximum(
0,np.dot(x, w1)
+ b1)
# 計算第乙個隱層的啟用資料(nxh)
scores=np.dot(h1, w2)
+ b2 # 神經元輸出(nxc)
scores = scores - np.reshape(np.
max(scores,axis=1)
,(n,-1
))#nxc
#scores中的每個元素減去這行的最大值
#axis=1按照列標籤向下執行
p = np.exp(scores)
/np.reshape(np.
sum(np.exp(scores)
,axis=1)
,(n,-1
))#nxc
#scoes中e每個元素除以e每行元素之和
loss =
-sum
(np.log(p[np.arange(n)
,y]))/n
#loss是乙個數,取對數之後求和
loss +=
0.5*reg*np.
sum(w1*w1)
+0.5
*reg*np.
sum(w2*w2)
#正則化
dscores = p
dscores[
range
(n),y]
-=1.0
#nxc
#這個有公式推導的,就是這個樣子,p中這個類別中的元素減去這個類別的
dscores/=n#loss中除以了n所以這裡也要除
dw2 = np.dot(h1.t,dscores)
#hxc
dh2 = np.
sum(dscores,axis=
0,keepdims=
false
)#c*1
#對h2[i]求導的時候,因為scores中的每一列都包含了h2[i],所以得把這一列累加起來
#然後弄成乙個列向量
da2 = np.dot(dscores,w2.t)
#nxh
#此時是經過relu之後的
da2[h1<=0]
=0#nxh
#relu'=max(0,1)
dw1 = np.dot(x.t,da2)
#dxh
dh1 = np.
sum(da2,axis=
0,keepdims=
false
)#hx1
#隱藏層的偏置項
dw2 += reg*w2#正則化
dw1 += reg*w1#正則化
self.params[
'w1'
]-= learning_rate*w1
self.params[
'b1'
]-= learning_rate*b1
self.params[
'w2'
]-= learning_rate*w2
self.params[
'b2'
]-= learning_rate*b2
n, c, h, w=x.shape
f, _, hh, ww=w.shape
stride=conv_param[
'stride'
] pad=conv_param[
'pad'
] x_pad=np.pad(x,((
0,0)
,(0,
0),(pad,pad)
,(pad,pad)
),mode=
'constant'
) h_out =1+
(h +
2* pad - hh)
// stride
w_out =1+
(w +
2* pad - ww)
// stride
out=np.zeros(
(f, n, h_out, w_out)
)for i in
range
(f):
for j in
range
(n):
for k in
range
(h_out)
:for l in
range
(w_out)
: out[i,j,k,l]
=np.
sum(x_pad[j,
:,k*stride:k*stride+hh,l*stride:l*stride+ww]
*w[i]
)+b[i]
out=out.transpose(1,
0,2,
3)#n,f,h_out,w_out
h_new =1+
(h +
2* pad - hh)
// stride
w_new =1+
(w +
2* pad - ww)
// stride
dx = np.zeros_like(x)
dw = np.zeros_like(w)
db = np.zeros_like(b)
s = stride
x_padded = np.pad(x,((
0,0)
,(0,
0),(pad, pad)
,(pad, pad)),
'constant'
) dx_padded = np.pad(dx,((
0,0)
,(0,
0),(pad, pad)
,(pad, pad)),
'constant'
)for i in
range
(n):
# ith image
for f in
range
(f):
# fth filter
for j in
range
(h_new)
:for k in
range
(w_new)
: window = x_padded[i,
:, j*s:hh+j*s, k*s:ww+k*s]
db[f]
+= dout[i, f, j, k]
dw[f]
+= window * dout[i, f, j, k]
dx_padded[i,
:, j*s:hh+j*s, k*s:ww+k*s]
+= w[f]
* dout[i, f, j, k]
# unpad
dx = dx_padded[:,
:, pad:pad+h, pad:pad+w]
m = config[
'm']
v = config[
'v']
beta1 = config[
'beta1'
] beta2 = config[
'beta2'
] learning_rate = config[
'learning_rate'
] epsilon = config[
'epsilon'
] t = config[
't']
t +=
1 m = beta1 * m +(1
- beta1)
* dx
v = beta2 * v +(1
- beta2)
*(dx **2)
m_bias = m /(1
- beta1 ** t)
v_bias = v /(1
- beta2 ** t)
x +=
- learning_rate * m_bias /
(np.sqrt(v_bias)
+ epsilon)
next_x = x
config[
'm']
= m config[
'v']
= v config[
't']
= t
BP神經網路實現(python)
最近學習了浙江大學的機器學習課程,又學習了一些內容,用python實現了bp神經網路 如下 定義sigmoid和它的導數 import numpy as np import random,math defmake matrix m,n return np.random.random size m,n...
C 實現神經BP神經網路
bp.h pragma once include include include include include using std vector using std exp using std cout using std endl class bp bp.cpp include bp.h bp ...
C 實現神經BP神經網路
bp.h pragma once include include include include include using std vector using std exp using std cout using std endl class bp bp.cpp include bp.h bp ...