昨天介紹完CNN卷積神經網路反向傳播程式,今天要來研究CNN卷積神經網路實際運作程式:
首先是全部正向傳播:
def forward_all(image, label):
#把圖片像素0~255轉乘-0.5~0.5
#另外把資料依序帶入捲積層,池化層,和全連階層
out = conv.forward((image / 255) - 0.5)
out = pool.forward(out)
out = softmax.forward(out)
#把機率值取log
loss = -np.log(out[label])
#機率最大值如果和實際label一樣ACC取1
acc = 1 if np.argmax(out) == label else 0
#返回輸出結果,loss,和acc
return out, loss, acc
接下來是訓練過程(正向+反向傳播)並修改權重:(程式如下)
#實際訓練
def train_all(im, label, lr=.005):
#正向傳播
out, loss, acc = forward_all(im, label)
#取出權重
gradient = np.zeros(10)
gradient[label] = -1 / out[label]
#反向傳播(學習率0.005)
gradient = softmax.backprop(gradient, lr)
gradient = pool.backprop(gradient)
gradient = conv.backprop(gradient, lr)
#返回 loss和acc
return loss, acc
實際帶入資料:
#做3次CNN訓練
for epoch in range(3):
print('--- 第 %d 次訓練---' % (epoch + 1))
#從0~len(train_images)序列將其打亂
permutation = np.random.permutation(len(train_images))
#把資料洗牌
train_images = train_images[permutation]
train_labels = train_labels[permutation]
# Train
#起始loss
loss = 0
#起始正確數
num_correct = 0
for i, (im, label) in enumerate(zip(train_images, train_labels)):
#每100次回報狀況
if i > 0 and i % 100 == 99:
print(
'[Step %d] Past 100 steps: Average Loss %.3f | Accuracy: %d%%' %
(i + 1, loss / 100, num_correct)
)
loss = 0
num_correct = 0
#
l, acc = train_all(im, label)
#loss加總
loss += l
#正確數幾總
num_correct += acc
訓練過程結果如下:
--- 第 1 次訓練---
[Step 100] Past 100 steps: Average Loss 2.235 | Accuracy: 18%
[Step 200] Past 100 steps: Average Loss 2.107 | Accuracy: 31%
[Step 300] Past 100 steps: Average Loss 1.982 | Accuracy: 45%
[Step 400] Past 100 steps: Average Loss 1.690 | Accuracy: 55%
[Step 500] Past 100 steps: Average Loss 1.403 | Accuracy: 62%
[Step 600] Past 100 steps: Average Loss 1.217 | Accuracy: 71%
[Step 700] Past 100 steps: Average Loss 1.064 | Accuracy: 69%
[Step 800] Past 100 steps: Average Loss 0.852 | Accuracy: 75%
[Step 900] Past 100 steps: Average Loss 0.738 | Accuracy: 81%
[Step 1000] Past 100 steps: Average Loss 0.771 | Accuracy: 73%
--- 第 2 次訓練---
[Step 100] Past 100 steps: Average Loss 0.770 | Accuracy: 81%
[Step 200] Past 100 steps: Average Loss 0.659 | Accuracy: 80%
[Step 300] Past 100 steps: Average Loss 0.511 | Accuracy: 86%
[Step 400] Past 100 steps: Average Loss 0.542 | Accuracy: 84%
[Step 500] Past 100 steps: Average Loss 0.525 | Accuracy: 84%
[Step 600] Past 100 steps: Average Loss 0.585 | Accuracy: 83%
[Step 700] Past 100 steps: Average Loss 0.644 | Accuracy: 79%
[Step 800] Past 100 steps: Average Loss 0.570 | Accuracy: 79%
[Step 900] Past 100 steps: Average Loss 0.524 | Accuracy: 84%
[Step 1000] Past 100 steps: Average Loss 0.523 | Accuracy: 84%
--- 第 3 次訓練---
[Step 100] Past 100 steps: Average Loss 0.451 | Accuracy: 86%
[Step 200] Past 100 steps: Average Loss 0.423 | Accuracy: 85%
[Step 300] Past 100 steps: Average Loss 0.437 | Accuracy: 91%
[Step 400] Past 100 steps: Average Loss 0.630 | Accuracy: 77%
[Step 500] Past 100 steps: Average Loss 0.355 | Accuracy: 91%
[Step 600] Past 100 steps: Average Loss 0.474 | Accuracy: 86%
[Step 700] Past 100 steps: Average Loss 0.453 | Accuracy: 88%
[Step 800] Past 100 steps: Average Loss 0.455 | Accuracy: 87%
[Step 900] Past 100 steps: Average Loss 0.335 | Accuracy: 92%
[Step 1000] Past 100 steps: Average Loss 0.449 | Accuracy: 89%
好,現在帶入test程式如下:
#起始loss
loss = 0
#起始正確數
num_correct = 0
for im, label in zip(test_images, test_labels):
#正向傳播
out, l, acc = forward_all(im, label)
#看loss
loss += l
#看正確數
num_correct += acc
#test總長
num_tests = len(test_images)
#算比例
print('Test Loss:', loss / num_tests)
print('Test Accuracy:', num_correct / num_tests)
結果如下:
Test Loss: 0.6609244800993207
Test Accuracy: 0.781
(資料來源:https://zhuanlan.zhihu.com/p/102119808並加上註解程式)
好,今天CNN程式算是研究完,機器學習算是告一段落
男孩看著狐狸,狐狸看著男孩,他們相交於森林入口,男孩全身被一團橘色氣包圍著,而望著沒有嘴巴狐狸,男孩只是訝異地看著牠,狐狸舉起牠紅色爪子,瞬間在周圍出現火焰包圍著了兩人,狐狸朝著男孩走去,男孩感覺到一陣恐懼,想要逃跑,但周圍都是火,男孩只能退到火焰周圍,狐狸慢慢地走到男孩面前,男孩閉上了眼睛,想像自己被爪子貫穿畫面,只是過了許久,都沒有感覺到痛覺,男孩好奇的張開眼睛,發現狐狸一動也不動,而他的爪子停留在戒指上,男孩把手指伸出了戒指,發現戒指就停留在空中並沒有掉落,而圍繞在兩人周圍火焰慢慢的熄滅,最後消失不見,男孩突然覺得口袋裡多出東西,他摸了摸口袋發現裡面有一朵木棉花
--|直到最後,那種窒息感還是存在 |-- MM.CS