python_deeplearning03_手写数字的数据集MNIST上

20180422 qzd

ch03 - 手写数字的数据集MNIST上


  1. 打开文件并获取其中的内容
    (得到的是文本形式)
data_file = open("mnist_dataset/mnist_train_100.csv",'r')
data_list = data_file.readlines()  #readline()会将整个文件读取到内存中
data_file.close()
  1. 列表元素
len(data_list)

输出结果:
100

data_list[4]

输出结果:
'5,0,0,……,0,0\n'
解释说明:
‘标签+像素(784--28*28)’

  1. 列表 --> 数组
    (将使用逗号分隔的数字列表转换成合适的数组)
  • 在都逗号处进行切分
  • 忽略第一个标签值
  • 绘制数组
#导入python扩展库
import numpy as np
import matplotlib.pyplot
%matplotlib inline

all_values= data_list[0].split(',')    #-->['5','0',……,'0\n']
#文件是以文本的形式读取的,每一行或每一条记录依然是文本。由逗号分割每一行得到的仍然是文本片段。
#np.asfarray()将文本字符串转换成实数,并创建这些数字的数组
image_array = np.asfarray(all_values[1:]).reshape((28,28))  #除第一个元素以外的所有值(忽略第一个标签值)--> [[ 0. 0. ……][ 0. 0. ……]……[ …… 0. 0.]]
matplotlib.pyplot.imshow(image_array,cmap='Greys',interpolation='None')

输出结果:


1524382215(1).png
  1. 准备MNIST训练数据
  • 用于训练和查询的输入数据(缩放)
    (0 ~ 255 --> 0.01 ~ 1.0, 刻意选择0.01作为范围最低点,是为了避免先前观察到的0值输入最终会人为地造成权重更新失败。没有选择0.99作为输入的上限值,是因为不需要避免输入1.0会造成这个问题。只需要避免输出值为1.0)
#scale input to range 0.01 to 1.00
scaled_input = (np.asfarray(all_values[1:]) / 255.0 *0.99) + 0.01
print(scaled_input)
  • 用于训练的输出数据
    (试图让神经网络生成0和1的输出,对于激活函数而言是不可能的,这会导致大的权重和饱和网络,因此需要重新调整这些数字。将使用值0.01和0.99来代替0和1)
#output nodes is 10 (example)
onodes = 10
targets = np.zeros(onodes) + 0.01
targets[int(all_values[0])] = 0.99

print(targets)

#输出结果:[ 0.01  0.01  0.01  0.01  0.01  0.99  0.01  0.01  0.01  0.01]
  1. 训练网络(完整代码)
#python notebook for Make Your Own Neural Network
#code for a 3-layer neural network, and code for learning the MNIST dataset

import numpy as np
#scipy.special for the sigmoid function expit()
import scipy.special
#library for plotting arrays
import matplotlib.pyplot
#ensure the plots are inside this notebook, not an external window
%matplotlib inline

#neural network class definition
class neuralNetwork:
    
    #initialise the neural network
    def __init__(self,inputnodes, hiddennodes, outputnodes, learningrate):
        #set number of nodes in each input, hidden, output layer
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes
        
        #link weight matrices, wih and who
        #weights inside the arrays are w_i_j, where link is from node i to node j in the next layer
        # w11 w21
        # w12 w22 ect
        self.wih = np.random.normal(0.0, pow(self.hnodes, -0.5),(self.hnodes, self.inodes))
        self.who = np.random.normal(0.0, pow(self.onodes, -0.5),(self.onodes, self.hnodes))
        #learning rate
        self.lr = learningrate
        
        #activation function is the sigmoid function
        self.activation_function = lambda x: scipy.special.expit(x)
        
        pass
    
    #train the neural network
    def train(self, inputs_list, targets_list):
        #convert inputs list to 2d array
        inputs = np.array(inputs_list, ndmin=2).T
        targets = np.array(targets_list, ndmin=2).T
        
        #calculate signals into hidden layer
        hidden_inputs = np.dot(self.wih,inputs)
        #caculate the signals emerging from hidden layer
        hidden_outputs = self.activation_function(hidden_inputs)
        
        #calculate signals into final output layer
        final_inputs = np.dot(self.who, hidden_outputs)
        #calculate the signals emerging from final output layer
        final_outputs = self.activation_function(final_inputs)
        
        #output layer error is the (target - actual)
        output_errors = targets - final_outputs
        #hidden layer error is the output_errors, split by weights, recombined at hidden nodes
        hidden_errors = np.dot(self.who.T, output_errors)
        
        #updata the weights for the links between the hidden and output layers
        self.who += self.lr * np.dot((output_errors * final_outputs * (1.0 - final_outputs)), np.transpose(hidden_outputs))
        #update the weights for the links between the input and hidden layers
        self.wih += self.lr *np.dot((hidden_errors * hidden_outputs*(1.0 - hidden_outputs)),np.transpose(inputs))
        
        pass
    #query the neural network
    def query(self, inputs_list):
        #convert inputs list to 2d array
        inputs = np.array(inputs_list,ndmin=2).T
        
        #calculate signals into hidden layer
        hidden_inputs = np.dot(self.wih,inputs)
        #calculate the signals emerging from hidden layer
        hidden_outputs = self.activation_function(hidden_inputs)
        
        #calculate signals into final output layer
        final_inputs = np.dot(self.who,hidden_outputs)
        #calculate the signals emerging from final output layer
        final_outputs = self.activation_function(final_inputs)
        
        return final_outputs
    
# number of input , hidden and output nodes
input_nodes = 784
hidden_nodes = 100
output_nodes = 10

#learning rate is 0.3
learning_rate = 0.3

#create instance of neural network
n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)

#load the mnist training data CSV file into a list
training_data_file = open("mnist_dataset/mnist_train_100.csv",'r')
training_data_list = training_data_file.readlines()
training_data_file.close()

#train the neural network

#go through all records in the training data set
for recode in training_data_list:
    #split the recode by the ',' commas
    all_values = recode.split(',')
    #scale and shift the inputs
    inputs = (np.asfarray(all_values[1:])/255.0*0.99)+0.01
    #creat the target output values (all 0.01, except the desired label which is 0.99)
    targets = np.zeros(output_nodes)+0.01
    #all_values[0] is the target label for this record
    targets[int(all_values[0])] = 0.99
    
    n.train(inputs,targets)
    pass

  1. 测试网络1(一个测试)
#load the mnist test data CSV file into a list
test_data_file = open("mnist_dataset/mnist_test_10.csv",'r')
test_data_list = test_data_file.readlines()
test_data_file.close()

#get the first test record
all_values = test_data_list[0].split(',')
#print the label
print(all_values[0])
#输出结果:7

image_array = np.asfarray(all_values[1:]).reshape((28,28))
matplotlib.pyplot.imshow(image_array,cmap='Greys',interpolation='None')

输出结果:


1524389620(1).png
n.query((numpy.asfarray(all_values[1:])/255.0*0.99)+0.01)

输出结果:array([[ 0.04266137],
[ 0.01119368],
[ 0.01747274],
[ 0.09164112],
[ 0.06795952],
[ 0.04385833],
[ 0.00848533],
[ 0.87066384],
[ 0.03463805],
[ 0.05105387]])

  1. 测试网络2(多个测试)
#test the neural network

#scorecard for how well the network performs, initially empty
scorecard = []

#go through all the records in the test data set
for record in test_data_list:
    #split the record by the ',' commas
    all_values = record.split(',')
    #correct answer is first value
    correct_label = int(all_values[0])
    print(correct_label, 'correct_label')
    #scale and shift the inputs
    inputs = (np.asfarray(all_values[1:])/255.0*0.99)+0.01
    #query the network
    outputs = n.query(inputs)
    #the index of the highest value corresponds to the label
    label = np.argmax(outputs)
    print(label,"network's answer")
    #append correct or incorrect to list
    if(label == correct_label):
        #network's answer matches correct answer, add 1 to scorecard
        scorecard.append(1)
    else:
        #network's answer doesn't match correct answer, add 0 to scorecard
        scorecard.append(0)
        pass
    pass
print(scorecard)

#calculate the performance score, the fraction of correct answers
scorecard_array = np.asarray(scorecard)  #将列表转化成数组
print(scorecard_array)
print("performance = ",scorecard_array.sum()/scorecard_array.size)

输出结果:

    7 correct_label
    7 network's answer
    2 correct_label
    0 network's answer
    1 correct_label
    1 network's answer
    0 correct_label
    0 network's answer
    4 correct_label
    4 network's answer
    1 correct_label
    1 network's answer
    4 correct_label
    4 network's answer
    9 correct_label
    4 network's answer
    5 correct_label
    4 network's answer
    9 correct_label
    7 network's answer

    [1, 0, 1, 1, 1, 1, 1, 0, 0, 0]

    [1 0 1 1 1 1 1 0 0 0]
    performance =  0.6
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,264评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,549评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,389评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,616评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,461评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,351评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,776评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,414评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,722评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,760评论 2 314
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,537评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,381评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,787评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,030评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,304评论 1 252
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,734评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,943评论 2 336

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,517评论 18 139
  • 改进神经网络的学习方法(上) 当一个高尔夫球员刚开始学习打高尔夫时,他们通常会在挥杆的练习上花费大多数时间。慢慢地...
    nightwish夜愿阅读 2,892评论 2 8
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 10,810评论 6 13
  • 无法忘记被挽回的城,随着鹿群的迁徙去向南方 虽然它们的影子只是冬季风中的一页信纸 虽然它们的蹄子深深浅浅看似毫无意...
    唐啵儿阅读 119评论 1 2
  • 导 小时候,总听大人说“这孩子太内向了,以后肯定不会有出息”。 长大以后,在喜欢的异性面前内向者的劣势也逐渐被放大...
    故明c阅读 1,166评论 3 1