生活随笔
收集整理的這篇文章主要介紹了
08-KNN手写数字识别
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
標(biāo)簽下載地址
文件內(nèi)容備注
| train-images-idx3-ubyte.gz | 訓(xùn)練集圖片:55000張訓(xùn)練圖片,5000張驗(yàn)證圖片 | |
| train-labels-idx1-ubyte.gz | 訓(xùn)練集圖片對(duì)應(yīng)的數(shù)字標(biāo)簽 | |
| t10k-images-idx3-ubyte.gz | 測(cè)試集圖片:10000張圖片 | t表示test,測(cè)試圖片,10k表示10*1000一共一萬(wàn)張圖片 |
| t10k-labels-idx1-ubyte.gz | 測(cè)試集圖片對(duì)應(yīng)的數(shù)字標(biāo)簽 | |
對(duì)于每一個(gè)樣本都有一個(gè)對(duì)應(yīng)的標(biāo)簽進(jìn)行唯一的標(biāo)識(shí),故為一個(gè)監(jiān)督學(xué)習(xí)
操作的每個(gè)圖片必須是灰度圖(單通道0是白色,1是黑色)
對(duì)于標(biāo)簽5401
標(biāo)簽中的4,并不是存儲(chǔ)4這個(gè)數(shù)字,而是存儲(chǔ)十位(0-9),第五行為黑色,則為1,即0000100000,因?yàn)?所處于第5個(gè),即描述為:4
KNN最近鄰域法
KNN的根本原理:一張待檢測(cè)的圖片,與相應(yīng)的樣本進(jìn)行比較,如果在樣本圖片中存在K個(gè)與待檢測(cè)圖片相類似的圖片,那么就會(huì)把當(dāng)前這K個(gè)圖片記錄下來(lái)。再在這K個(gè)圖片中找到相似性最大的(例如10個(gè)圖片中有8個(gè)描述的當(dāng)前數(shù)字都是1,那么這個(gè)圖片檢測(cè)出來(lái)的就是1)
裝載圖片:
input_data.read_data_sets('MNIST_data',one_hot=True)
參數(shù)一:當(dāng)前文件夾的名稱
參數(shù)二:one_hot是個(gè)布爾類型,one_hot中有一個(gè)為1,其余都為0
隨機(jī)獲取訓(xùn)練數(shù)組的下標(biāo):
np.random.choice(trainNum,trainSize,replace=False)
參數(shù)一:隨機(jī)值的范圍
參數(shù)二:生成trainSize這么多個(gè)隨機(jī)數(shù)
參數(shù)三:是否可以重復(fù)
在0-trainNum之間隨機(jī)選取trainSize這么多個(gè)隨機(jī)數(shù),且不可重復(fù)
import tensorflow
as tf
import numpy
as np
import random
from tensorflow
.examples
.tutorials
.mnist
import input_data
mnist
= input_data
.read_data_sets
('E:\\Jupyter_workspace\\study\\DL\\MNIST_data',one_hot
=True)
trainNum
= 55000
testNum
= 10000
trainSize
= 500
testSize
= 5
k
= 4
trainIndex
= np
.random
.choice
(trainNum
,trainSize
,replace
=False)
testIndex
= np
.random
.choice
(testNum
,testSize
,replace
=False)
trainData
= mnist
.train
.images
[trainIndex
]
trainLabel
= mnist
.train
.labels
[trainIndex
]
testData
= mnist
.test
.images
[testIndex
]
testLabel
= mnist
.test
.labels
[testIndex
]
print('trainData.shape=',trainData
.shape
)
print('trainLabel.shape=',trainLabel
.shape
)
print('testData.shape=',testData
.shape
)
print('testLabel.shape=',testLabel
.shape
)
print('testLabel=',testLabel
)
'''
testLabel= [[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] 3--->testData [0][0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] 1--->testData [1][0. 0. 0. 0. 0. 0. 0. 0. 0. 1.] 9--->testData [2][0. 0. 0. 0. 0. 0. 1. 0. 0. 0.] 6--->testData [3][0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]] 4--->testData [4]
'''
trainDataInput
= tf
.placeholder
(shape
=[None,784],dtype
=tf
.float32
)
trainLabelInput
= tf
.placeholder
(shape
=[None,10],dtype
=tf
.float32
)
testDataInput
= tf
.placeholder
(shape
=[None,784],dtype
=tf
.float32
)
testLabelInput
= tf
.placeholder
(shape
=[None,10],dtype
=tf
.float32
)
f1
= tf
.expand_dims
(testDataInput
,1)
f2
= tf
.subtract
(trainDataInput
,f1
)
f3
= tf
.reduce_sum
(tf
.abs(f2
),reduction_indices
=2)
f4
= tf
.negative
(f3
)
f5
,f6
= tf
.nn
.top_k
(f4
,k
=4)
f7
= tf
.gather
(trainLabelInput
,f6
)
f8
= tf
.reduce_sum
(f7
,reduction_indices
=1)
f9
= tf
.argmax
(f8
,dimension
=1)
with tf
.Session
() as sess
:p1
= sess
.run
(f1
,feed_dict
={testDataInput
:testData
[0:testSize
]})print('p1=',p1
.shape
)p2
= sess
.run
(f2
,feed_dict
={trainDataInput
:trainData
,testDataInput
:testData
[0:testSize
]})print('p2=',p2
.shape
)p3
= sess
.run
(f3
,feed_dict
={trainDataInput
:trainData
,testDataInput
:testData
[0:testSize
]})print('p3=',p3
.shape
)print('p3[0,0]=',p3
[0,0]) p4
= sess
.run
(f4
,feed_dict
={trainDataInput
:trainData
,testDataInput
:testData
[0:testSize
]})print('p4=',p4
.shape
)print('p4[0,0]',p4
[0,0])p5
,p6
= sess
.run
((f5
,f6
),feed_dict
={trainDataInput
:trainData
,testDataInput
:testData
[0:testSize
]})print('p5=',p5
.shape
)print('p6=',p6
.shape
)print('p5[0,0]',p5
[0])print('p6[0,0]',p6
[0])p7
= sess
.run
(f7
,feed_dict
={trainDataInput
:trainData
,testDataInput
:testData
[0:testSize
],trainLabelInput
:trainLabel
})print('p7=',p7
.shape
)print('p7[]',p7
)p8
= sess
.run
(f8
,feed_dict
={trainDataInput
:trainData
,testDataInput
:testData
[0:testSize
],trainLabelInput
:trainLabel
})print('p8=',p8
.shape
)print('p8[]=',p8
)p9
= sess
.run
(f9
,feed_dict
={trainDataInput
:trainData
,testDataInput
:testData
[0:testSize
],trainLabelInput
:trainLabel
})print('p9=',p9
.shape
)print('p9[]=',p9
)p10
= np
.argmax
(testLabel
[0:testSize
],axis
=1)print('p10[]=',p10
)j
= 0
for i
in range(0,5):if p10
[i
] == p9
[i
]:j
= j
+1
print('ac=',j
*100/testSize
)
總結(jié)
以上是生活随笔為你收集整理的08-KNN手写数字识别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。