初始化引數,layer_dims為各層維度
# layer_dims : (5,4,4,3...)
definitialize_parameters
(layer_dims)
: l =
len(layer_dims)
params =
for i in
range(1
, l)
: params[
'w'+
str(i)
]= np.random.randn(layer_dims[i]
, layer_dims[i-1]
) params[
'b'+
str(i)
]= np.zeros(
(layer_dims[i],1
))return params
啟用函式sigmoid和relu,其中relu為分段函式,rel
u(x)
=x, & x >= 0 \\ 0, & x < 0 \end
relu(x
)=l =
len(caches)
da =
-(y/a-(1
-y)/(1
-a))
current_cache = caches[l-1]
grads[
"da"
+str
(l)]
, grads[
"dw"
+str
(l)]
, grads[
"db"
+str
(l)]
= linear_activation_backward(da, current_cache,
"sigmoid"
)for i in
range
(l-2,-
1,-1
):current_cache = caches[i]
grads[
"da"
+str
(i+1)]
, grads[
"dw"
+str
(i+1)]
, grads[
"db"
+str
(i+1)]
= linear_activation_backward(grads[
"da"
+str
(i+2)]
, current_cache,
"relu"
)return gradssigmoid函式的導數為ϕ′(
z)=ϕ
(z)∗
(1−ϕ
(z))
\phi'(z) = \phi(z)*(1-\phi(z))
ϕ′(z)=
ϕ(z)
∗(1−
ϕ(z)
),relu函式的導數為rel
u′(x
)=
1, & x >= 0 \\ 0, & x < 0 \end
relu′(
x)={
1,0,
x>=0
x<0
def
linear_activation_backward
(da, cache, activation)
: w, a, z, a = cache
if activation ==
'sigmoid'
: dz = da*a*(1
-a)elif activation ==
'relu'
: dz = da.copy(
) dz[z<=0]
=0return linear_backward(dz, w, a)
對於第l層的線性函式z=w
a+
bz = wa+b
z=wa+b
,所以dw=
dz∗a
,db=
dz∗1
,da=
dz∗w
dw=dz*a,db = dz *1,da = dz*w
dw=dz∗
a,db
=dz∗
1,da
=dz∗
w
def
linear_backward
(dz, w, a)
: m = a.shape[0]
dw = np.dot(dz, a.t)
/m db = np.
sum(dz, axis=
1, keepdims=
true)/m
da = np.dot(w.t, dz)
return da, dw, db
梯度更新
def
update_parameters
(params, grads, learning_rate)
: l =
len(params)//2
for l in
range
(l):
params[
"w"+
str(l+1)
]= params[
"w"+
str(l+1)
]- learning_rate*grads[
"dw"
+str
(l+1)]
params[
"b"+
str(l+1)
]= params[
"b"+
str(l+1)
]- learning_rate*grads[
"db"
+str
(l+1)]
return params
l層的深度神經網路模型組裝
def
dnn(x, y, layers_dims, learning_rate =
0.001
, num_iterations =
1000):
costs =
params = initialize_parameters(layers_dims)
for i in
range
(num_iterations)
:
a, caches = forward_propagation(x, params)
cost = compute_cost(a, y)
grads = backward_propagation(a, y, caches)
params = update_parameters(params, grads, learning_rate)
if i %
100==0:
return params, costs
TensorFlow深度學習筆記 迴圈神經網路實踐
歡迎star,有問題可以到issue區討論 官方教程位址 text8中只包含27種字元 小寫的從a到z,以及空格符。如果把它打出來,讀起來就像是去掉了所有標點的wikipedia。用zipfile讀取zip內容為字串,並拆分成單詞list 用connections模組統計單詞數量並找出最常見的單詞 ...
TensorFlow深度學習筆記 迴圈神經網路實踐
歡迎star,有問題可以到issue區討論 官方教程位址 text8中只包含27種字元 小寫的從a到z,以及空格符。如果把它打出來,讀起來就像是去掉了所有標點的wikipedia。用zipfile讀取zip內容為字串,並拆分成單詞list 用connections模組統計單詞數量並找出最常見的單詞 ...
深度學習 神經網路筆記
rnn 遞迴神經網路 一 神經網路的種類 基礎神經網路 單層感知機,執行緒神經網路,bp神經網路,hopfield神經網路等 高階神經網路 玻爾茲曼機,受限玻爾茲曼機,遞迴神經網路等 深度神經網路 深度置信網路,卷積神經網路,迴圈神經網路,lstm聖經網路等 一 感知機 svm 有n個輸入資料,通過...