DAY 28
0
AI & Data

## DAY28 CNN(卷積神經網路 續二)

1.c≠k

2.c=k

1. 可以更新權重
2. 可以更新bias
3. 可以更新反向傳播至池化層數據

``````#全連接層反向傳播
def backprop(self, d_L_d_out, learn_rate):
#如果沒有梯度就下一個
continue
#exp(tc)-->分子
t_exp = np.exp(self.last_totals)

#S-->分母
S = np.sum(t_exp)

#dout/dt k != c 情況
d_out_d_t = -t_exp[i] * t_exp / (S ** 2)
#dout/dt k == c 情況
d_out_d_t[i] = t_exp[i] * (S - t_exp[i]) / (S ** 2)

#dt/dw
d_t_d_w = self.last_input
#dt/db
d_t_d_b = 1
#dt/input 1000 x 10
d_t_d_inputs = self.weights

#dL/dt = dout/dt*梯度

#dL/dw = dt/dw*dL/dt 	(1000, 1) @ (1, 10) = (1000, 10)
d_L_d_w = d_t_d_w[np.newaxis].T @ d_L_d_t[np.newaxis]
#dL/db = dL/dt*dt/db
d_L_d_b = d_L_d_t * d_t_d_b
#dL/dinputs = dt/dinputs*dL/dt 	(1000, 10) @ (10, 1)= (1000, 1)
d_L_d_inputs = d_t_d_inputs @ d_L_d_t

#更新權重還有bias

self.weights -= learn_rate * d_L_d_w
self.biases -= learn_rate * d_L_d_b

return d_L_d_inputs.reshape(self.last_input_shape)
``````

``````#反向運算
def backprop(self, d_L_d_out):
#dL/dinput -->一開始設置為0
d_L_d_input = np.zeros(self.last_input.shape)

for im_region, i, j in self.iterate_regions(self.last_input):
h, w, f = im_region.shape
#找尋那區域最大值
amax = np.amax(im_region, axis=(0, 1))

for i2 in range(h):
for j2 in range(w):
for f2 in range(f):
#如果是最大值就把剛剛dL/dinput傳入這層dL/dinput
if im_region[i2, j2, f2] == amax[f2]:
d_L_d_input[i + i2, j + j2, f2] = d_L_d_out[i, j, f2]

return d_L_d_input
``````

``````#反向運算
def backprop(self, d_L_d_out, learn_rate):
#dL/dfilter --> 一開始設置為0
d_L_d_filters = np.zeros(self.filters.shape)
for im_region, i, j in self.iterate_regions(self.last_input):
for f in range(self.num_filters):
#dL/dfilter = sum(dL/dout*照片區塊)
d_L_d_filters[f] += d_L_d_out[i, j, f] * im_region
#調整一開始filters   用學習率*dL/dfilter
self.filters -= learn_rate * d_L_d_filters
``````

``````在戒指發光同一時間,原本飄在空中羽毛掉落在地上,剛好掉到娃娃旁邊,原來娃娃在剛剛,終於精疲力盡倒下,而地面上還冒出鮮血和娃娃血液混合再一起,當羽毛碰到血的時候.羽毛突然產生變化,幾秒鐘之後,出現了一隻狐狸,只不過這隻狐狸沒有嘴巴,而且四肢通紅,狐狸環顧四周後,隨即抬起紅色爪子,而此時前方樹木開始燃燒,最終燒成灰燼,而前方赫然就是森林出口

--|你們到底是誰?為何要如此執著?|--  MM.CS
``````

### 1 則留言

0
juck30808
iT邦新手 3 級 ‧ 2021-10-12 18:36:54