(HTM)mnist

(HTM)层次时序记忆-mnist识别

如果你对HTM感兴趣,我建立了一个群,我们共同学习交流。515743445。

本文尝试使用空间沉积池进行手写数字的识别

 

这里参考了nupic.vision里的mnist项目的sp参数。

对于获得的训练集的输出sdr,使用KNN分类器进行分类。

# -- coding: utf-8 --

import numpy as np

from nupic.algorithms.spatial_pooler import SpatialPooler as SP

import pickle

from tqdm import tqdm

import numpy as np

from nupic.algorithms.knn_classifier import KNNClassifier


#mnist数据集下载:http://deeplearning.net/data/mnist/mnist.pkl.gz

file = open("C:/Users/mi/Documents/HTM/mnist.pkl", 'rb')

train_set, valid_set, test_set = pickle.load(file)

mnist = np.concatenate((train_set[0], valid_set[0]), axis=0)

mnist = np.round(mnist)  # 60000

mnist = mnist.reshape((60000, 28, 28))

train = np.zeros((60000, 32, 32))

train[:, 2:30, 2:30] = mnist

train.shape = (60000, 1024)

train_label = np.concatenate((train_set[1], valid_set[1]), axis=0)


sp = SP(inputDimensions=(1024, 1),

            columnDimensions=(1024, 1),

            potentialRadius=1024,

            potentialPct=0.9,

            globalInhibition=True,

            localAreaDensity=-1,

            numActiveColumnsPerInhArea=240,

            stimulusThreshold=0,

            synPermInactiveDec=0,

            synPermActiveInc=0,

            synPermConnected=0.2,

            minPctOverlapDutyCycle=0.001,

            dutyCyclePeriod=1000,

            boostStrength=0.0,

            seed=1946)



classifier = KNNClassifier(k=5,distanceNorm=2)


#获取训练集的输出sdr集合,训练KNN分类器

for d,l in tqdm(zip(train[:10000],train_label[:10000])):

    column = np.zeros((1024))

    sp.compute(d, False, column)

    classifier.learn(column,l)


#测试集上的正确率

test_data = np.round(test_set[0])  # 10000

label = test_set[1]

test = np.zeros((10000, 32, 32))

test_data.shape = (10000, 28, 28)

test[:, 2:30, 2:30] = test_data

test.shape = (10000, 1024)

ans = 0

for t, l in tqdm(zip(test, label)):

    column = np.zeros((1024))

    sp.compute(t, False, column)

    winner = classifier.infer(column)[0]

    if winner == l:

        ans+=1

print "正确率:",ans/100,"%"

 

在使用10000个训练数据训练时,正确率92%

使用60000个训练数据时,正确率95%

 

你可能感兴趣的