MNIST_NUMPY_WEIGHTS / README.md
pual's picture
Update README.md
dd58fb1
|
raw
history blame
49 kB
metadata
license: mit
language:
  - zh
pipeline_tag: image-classification
import numpy as np
import scipy.special as ssp
import matplotlib.pyplot as plt
input_nodes=784 # 输入层节点数
hide_nodes=200 # 隐藏层节点数,理论上越高越好,但是高到一定程度就到顶了(默认:200)
out_nodes=10  # 输出层节点数
learningrate = 0.1 #学习率
wih = np.random.normal(0.0, pow(hide_nodes, -0.5), (hide_nodes, input_nodes)) #矩阵大小为隐藏层节点数×输入层节点数
#np.random.normal()的意思是一个正态分布,normal这里是正态的意思
plt.hist(wih)
(array([[ 1.,  2.,  3., ...,  8.,  0.,  0.],
        [ 0.,  0.,  2., ...,  4.,  0.,  0.],
        [ 0.,  1.,  5., ...,  9.,  0.,  0.],
        ...,
        [ 0.,  1.,  2., ..., 10.,  0.,  0.],
        [ 0.,  1., 13., ...,  7.,  0.,  0.],
        [ 0.,  2.,  8., ...,  3.,  1.,  0.]]),
 array([-0.32167192, -0.25702275, -0.19237358, -0.12772441, -0.06307524,
         0.00157393,  0.0662231 ,  0.13087226,  0.19552143,  0.2601706 ,
         0.32481977]),
 <a list of 784 BarContainer objects>)

png

# Visualize weight matrix wih
plt.imshow(wih, cmap='coolwarm', aspect='auto')
#plt.imshow(wih, cmap='hot', aspect='auto')
plt.xlabel('Output Node')
plt.ylabel('Hidden Node')
plt.title('Weight Matrix (Hidden to input)')
plt.colorbar()
plt.show()

png

who = np.random.normal(0.0, pow(hide_nodes, -0.5), (out_nodes, hide_nodes)) #矩阵大小为输出层节点数×隐藏层节点数
plt.hist(who)
#同上
(array([[0., 0., 1., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 1., 1., 0.],
        ...,
        [0., 0., 0., ..., 1., 1., 0.],
        [0., 0., 0., ..., 1., 0., 0.],
        [0., 1., 2., ..., 0., 0., 0.]]),
 array([-0.26261651, -0.21194208, -0.16126765, -0.11059322, -0.05991879,
        -0.00924436,  0.04143007,  0.0921045 ,  0.14277893,  0.19345336,
         0.24412779]),
 <a list of 200 BarContainer objects>)

png

# Visualize weight matrix who
plt.imshow(who, cmap='coolwarm', aspect='auto')
plt.xlabel('Output Node')
plt.ylabel('Hidden Node')
plt.title('Weight Matrix (Hidden to Output)')
plt.colorbar()
plt.show()

png

#linspace 参考:https://blog.csdn.net/neweastsun/article/details/99676029
x = np.linspace(start=-6, stop=6, num=121) #从-6到6范围内创建121个距离相近的数字,从而生成x数组用于代入后面的y
'''
e.g.
x = np.linspace(start = 0, stop = 100, num = 5) ##从0到100范围内创建5个距离相近的数字
print(x)
OUT:[  0.  25.  50.  75. 100.]

#lambda示例
#lambda arg1,arg2,arg3… :<表达式>
func=lambda x : x+1  #func=x+1    
print(func(2)) #func=2+1=3
func=lambda x,y : x+y #func=x+y
print(func(1,2)) #func=1+2=3
'''
activation_function = lambda x: ssp.expit(x)   #logistic sigmoid函数,定义为expit(x)= 1 /(1 + exp(-x))
y = activation_function(x)
plt.plot(x, y)
plt.xlabel('x')
plt.title('logistic sigmoid(x)')
plt.show()

png

#数据集分为训练集和测试集,训练集有60000条数据,测试集有10000条数据,
#每一条数据都是由785个数字组成,数值大小在0~255之间,第一个数字代表该条数据所表示的数字,
#后面的784个数字可以形成28×28的矩阵(28x28=784),每一个数值都对应该位置的像素点的像素值灰度大小,由此形成了一幅像素为28×28的图片。

#这里是训练集

test_data_file = open("mnist_train.csv", 'r')
test_data_list = test_data_file.readlines()
test_data_file.close()
print("总数据量:",len(test_data_list))
print("第1条数据:",test_data_list[0])
print("第1条数据表示的数字:",test_data_list[0][0])
print("第1条数据的28x28矩阵数据:",test_data_list[0][1:])
总数据量: 60000
第1条数据: 5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,18,18,18,126,136,175,26,166,255,247,127,0,0,0,0,0,0,0,0,0,0,0,0,30,36,94,154,170,253,253,253,253,253,225,172,253,242,195,64,0,0,0,0,0,0,0,0,0,0,0,49,238,253,253,253,253,253,253,253,253,251,93,82,82,56,39,0,0,0,0,0,0,0,0,0,0,0,0,18,219,253,253,253,253,253,198,182,247,241,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,80,156,107,253,253,205,11,0,43,154,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,14,1,154,253,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,139,253,190,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,190,253,70,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,35,241,225,160,108,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,81,240,253,253,119,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,45,186,253,253,150,27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,93,252,253,187,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,249,253,249,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,46,130,183,253,253,207,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,39,148,229,253,253,253,250,182,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,114,221,253,253,253,253,201,78,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,66,213,253,253,253,253,198,81,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,171,219,253,253,253,253,195,80,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,55,172,226,253,253,253,253,244,133,11,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,136,253,253,253,212,135,132,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

第1条数据表示的数字: 5
第1条数据的28x28矩阵数据: ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,18,18,18,126,136,175,26,166,255,247,127,0,0,0,0,0,0,0,0,0,0,0,0,30,36,94,154,170,253,253,253,253,253,225,172,253,242,195,64,0,0,0,0,0,0,0,0,0,0,0,49,238,253,253,253,253,253,253,253,253,251,93,82,82,56,39,0,0,0,0,0,0,0,0,0,0,0,0,18,219,253,253,253,253,253,198,182,247,241,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,80,156,107,253,253,205,11,0,43,154,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,14,1,154,253,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,139,253,190,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,190,253,70,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,35,241,225,160,108,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,81,240,253,253,119,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,45,186,253,253,150,27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,93,252,253,187,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,249,253,249,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,46,130,183,253,253,207,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,39,148,229,253,253,253,250,182,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,114,221,253,253,253,253,201,78,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,66,213,253,253,253,253,198,81,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,171,219,253,253,253,253,195,80,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,55,172,226,253,253,253,253,244,133,11,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,136,253,253,253,212,135,132,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
all_values = test_data_list[0].split(',') # split()函数将第1条数据进行拆分,以‘,’为分界点进行拆分
image_array = np.asfarray(all_values[1:]).reshape((28,28)) # asfarray()函数将all_values中的后784个数字进行重新排列
# reshape()函数可以对数组进行整型,使其成为28×28的二维数组,asfarry()函数可以使其成为矩阵。
plt.imshow(image_array, interpolation = 'nearest')  # imshow()函数可以将28×28的矩阵中的数值当做像素值,使其形成图片
<matplotlib.image.AxesImage at 0x7fa3da4adfd0>

png

#接下去是第1层和最后1层的逻辑
# 对输入的数据进行处理,取后784个数据除以255,再乘以0.99,最后加上0。01,是所有的数据都在0.01到1.00之间
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01 #输入层,784个输入
# 建立准确输出结果矩阵,对应的位置标签数值为0.99,其他位置为0.01
#最终实现将0~255转换为0~1的浮点数
#可视化中间输出
print(inputs)
middle_layer_fig = np.asfarray((inputs-0.01)/0.99*255.0 )  
middle_layer_fig = np.asfarray(middle_layer_fig).reshape((28,28))
plt.imshow(middle_layer_fig, interpolation = 'nearest')  
[0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.02164706 0.07988235 0.07988235 0.07988235
 0.49917647 0.538      0.68941176 0.11094118 0.65447059 1.
 0.96894118 0.50305882 0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.12647059 0.14976471 0.37494118 0.60788235
 0.67       0.99223529 0.99223529 0.99223529 0.99223529 0.99223529
 0.88352941 0.67776471 0.99223529 0.94952941 0.76705882 0.25847059
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.20023529
 0.934      0.99223529 0.99223529 0.99223529 0.99223529 0.99223529
 0.99223529 0.99223529 0.99223529 0.98447059 0.37105882 0.32835294
 0.32835294 0.22741176 0.16141176 0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.07988235 0.86023529 0.99223529
 0.99223529 0.99223529 0.99223529 0.99223529 0.77870588 0.71658824
 0.96894118 0.94564706 0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.32058824 0.61564706 0.42541176 0.99223529
 0.99223529 0.80588235 0.05270588 0.01       0.17694118 0.60788235
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.06435294 0.01388235 0.60788235 0.99223529 0.35941176
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.54964706 0.99223529 0.74764706 0.01776471 0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.05270588
 0.74764706 0.99223529 0.28176471 0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.14588235 0.94564706
 0.88352941 0.63117647 0.42929412 0.01388235 0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.32447059 0.94176471 0.99223529
 0.99223529 0.472      0.10705882 0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.18470588 0.73211765 0.99223529 0.99223529
 0.59235294 0.11482353 0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.07211765 0.37105882 0.98835294 0.99223529 0.736
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.97670588 0.99223529 0.97670588 0.25847059 0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.18858824 0.51470588 0.72047059 0.99223529
 0.99223529 0.81364706 0.01776471 0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.16141176 0.58458824
 0.89905882 0.99223529 0.99223529 0.99223529 0.98058824 0.71658824
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.10317647 0.45258824 0.868      0.99223529 0.99223529 0.99223529
 0.99223529 0.79035294 0.31282353 0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.09929412 0.26623529 0.83694118 0.99223529
 0.99223529 0.99223529 0.99223529 0.77870588 0.32447059 0.01776471
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.07988235 0.67388235
 0.86023529 0.99223529 0.99223529 0.99223529 0.99223529 0.76705882
 0.32058824 0.04494118 0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.22352941 0.67776471 0.88741176 0.99223529 0.99223529 0.99223529
 0.99223529 0.95729412 0.52635294 0.05270588 0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.538      0.99223529
 0.99223529 0.99223529 0.83305882 0.53411765 0.52247059 0.07211765
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01       0.01       0.01
 0.01       0.01       0.01       0.01      ]





<matplotlib.image.AxesImage at 0x7fa3da408d00>

png

targets = np.zeros(out_nodes) + 0.01 
#输出层,10个数字,10个输出,0~1的概率范围
#输出层是1个list,由10个数字组成,第一个数字代表0的概率,依次类推,第10个数字代表9的概率
#这里是输出的[理想结果]
# all_values[0] is the target label for this record
#可视化中间输出
print(len(targets))
print(targets)
middle_layer_fig = np.asfarray((targets-0.01)/0.99*255.0 )  
middle_layer_fig = np.asfarray(middle_layer_fig).reshape((1,10))
plt.imshow(middle_layer_fig, interpolation = 'nearest')  
10
[0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01]





<matplotlib.image.AxesImage at 0x7fa3da3ad490>

png

#print("第1行数据:",all_values)
#print("第1行数据所表示的数字:",all_values[0])
targets[int(all_values[0])] = 0.99 
#将数据集的数据表示的数字在其指定的输出层的概率位置上的概率置0.99
#这里是第1行数据,对应的是数组5,因此按照其在输出层的表示的概率位置,应当将第6个数字改为0.99
#可视化中间输出
print(targets)
middle_layer_fig = np.asfarray((targets-0.01)/0.99*255.0 )  
middle_layer_fig = np.asfarray(middle_layer_fig).reshape((1,10))
plt.imshow(middle_layer_fig, interpolation = 'nearest')  
[0.01 0.01 0.01 0.01 0.01 0.99 0.01 0.01 0.01 0.01]





<matplotlib.image.AxesImage at 0x7fa3da30b4f0>

png

#对比
targets = np.zeros(out_nodes) + 0.01 
print(targets)
targets[int(all_values[0])] = 0.99 
print(targets)
[0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01]
[0.01 0.01 0.01 0.01 0.01 0.99 0.01 0.01 0.01 0.01]
#接下去是训练逻辑,训练的目标就是让输入的数据的概率尽可能接近理想结果
# 将导入的输入列表数据和正确的输出结果转换成二维矩阵
INPUT = np.array(inputs, ndmin = 2).T # array函数是矩阵生成函数,将输入的inputs转换成二维矩阵,ndmin=2表示二维矩阵
TARGETS = np.array(targets, ndmin = 2).T # .T表示矩阵的转置,生成后的矩阵的转置矩阵送入变量targets
#print(INPUT)
#print(TARGETS)
# 进行前向传播
# 利用导入的数据计算进入隐藏层的数据
hidden_inputs = np.dot(wih, INPUT) # dot()函数是指两个矩阵做点乘
#可视化中间输出
print(hidden_inputs.T)
# Visualize hidden layer activations
#hidden_inputs = hidden_inputs.reshape((20, 10))
plt.imshow(hidden_inputs.T, cmap='hot', aspect='auto')
plt.xlabel('Hidden Node')
plt.ylabel('Sample')
plt.title('Hidden Layer Activations')
plt.colorbar()
plt.show()
[[-8.64964137e-01 -1.96617581e+00  7.43349647e-01  6.52699592e-01
   3.90933284e-01  1.23038702e+00 -1.26960367e-01 -8.89064451e-01
   1.25956352e-01 -2.66009122e-01 -3.87411628e-01 -8.55340714e-01
  -3.73072385e-01 -4.88004264e-01  8.93519640e-01 -5.94015812e-01
  -3.94940660e-01 -6.03127644e-01 -1.83468156e-01  1.21212338e+00
   1.11156836e+00 -3.30592481e-03 -1.45441494e-01 -1.16176875e-01
  -6.79873194e-01  1.35716864e-03 -9.88715475e-01  2.53326180e-01
   7.95912751e-02 -8.71915904e-01 -4.99039240e-01 -9.32069427e-02
  -1.29952079e+00 -1.18946859e-01 -2.22242548e-01  1.07578559e+00
   1.69691315e-01 -4.42288856e-01  1.18089766e+00  3.81134469e-02
   3.15796540e-01  1.07374634e+00 -7.71978830e-01 -1.77028239e-01
   7.83445294e-01  1.16099348e+00  5.28529106e-01 -1.94025187e-02
   2.00808369e-01  6.72844377e-01  1.21480995e+00 -2.05275063e-01
  -1.02432531e+00 -1.40022847e+00  7.16467553e-01 -6.38000445e-01
  -1.44617295e-01  4.72539610e-01 -6.51132050e-02 -1.02462391e+00
   1.38454078e+00  7.12628876e-01  7.39171671e-02 -3.34221329e-01
   5.03935486e-01  2.08522402e+00  2.29977865e-01 -8.58595299e-01
   9.14983758e-01  5.27664003e-02 -3.49103724e-01 -1.29338789e+00
   8.10453241e-01  2.08934398e+00  1.66835420e+00 -1.12660303e+00
  -1.12181011e-01  1.70474734e-01  5.20577595e-01  6.00166910e-01
  -3.81956593e-01  1.30122404e-01 -5.23356991e-01 -1.01661725e+00
  -3.38834016e-01  6.30692963e-01  1.17169833e-01  9.13183907e-01
  -1.10728477e+00  9.91458051e-01 -2.88315338e-01  7.70893096e-01
   5.82703388e-01 -9.29590575e-02 -1.26294025e+00  1.94053320e-01
  -5.96912464e-01  2.60424259e-01  4.29504575e-02 -7.60243022e-01
   2.03240513e-02  7.27749904e-02 -7.19974851e-01  5.25634269e-01
  -4.96678397e-01 -1.62713415e+00  2.89082887e-01 -5.26173924e-01
  -3.82685176e-01 -1.76410064e+00 -1.33431697e+00  4.32481392e-01
   2.33941967e+00  7.52802920e-01  2.17849572e-01 -8.38437665e-02
  -5.51882457e-01  1.84692442e+00 -4.10696115e-01  3.97851800e-01
  -1.49071923e-01 -2.81875633e-01  1.95378425e+00 -4.66989868e-01
  -4.73375650e-01  1.66522535e-01  5.01408007e-01 -1.30089311e-01
   1.44543864e+00  4.28063957e-01  3.86986466e-01  6.62182100e-01
  -1.39480966e-01 -1.82625599e-01 -3.67218386e-01 -1.48826110e+00
  -4.31214177e-01 -8.92040712e-01 -4.15032383e-01 -3.76042786e-01
  -3.83971840e-01  7.49005651e-01 -3.16839497e-01 -7.70655367e-01
   3.56918546e-01 -1.93469779e-01 -4.51644191e-01 -5.20009826e-01
   7.61656212e-01 -5.39819400e-01  1.24457323e-01  4.02348827e-01
   4.96390519e-02 -1.61507281e-01 -6.04062425e-01  4.77674466e-01
   5.65500425e-01 -1.74931564e-02  1.82237163e-01 -2.52744493e-01
  -9.74909666e-01  4.39247112e-01  2.50623145e-01 -5.47588554e-01
  -1.10213410e+00 -7.96484480e-03  8.18154047e-01 -5.31161336e-01
   9.45395512e-02 -4.80934079e-02 -4.15248499e-01  2.01334670e-02
  -7.73149020e-01  5.16150140e-01 -1.11187297e+00 -3.84973353e-01
   1.57056302e-01  9.52205562e-02 -4.17473666e-04 -2.64269971e-01
   3.51661057e-02 -8.62097845e-01 -6.41290441e-01 -6.10216699e-01
   1.48703377e+00 -9.36182669e-01  2.29758638e-01  2.69581850e-03
  -9.90544195e-03 -1.16945542e-01  2.16055208e-01 -5.16034753e-01
  -5.47460522e-01  1.21898405e+00 -1.40917054e-01 -1.10955125e+00
  -1.06838867e+00 -8.16027514e-01  3.18583449e-01  7.11316110e-01]]

png

# 利用激活函数sigmoid计算隐藏层输出的数据
hidden_outputs = activation_function(hidden_inputs)
#可视化中间输出
print(hidden_outputs.T)
# Visualize hidden layer activations
plt.imshow(hidden_outputs.T, cmap='hot', aspect='auto')
plt.xlabel('Hidden Node')
plt.ylabel('Sample')
plt.title('Hidden Layer Activations')
plt.colorbar()
plt.show()
[[0.29630324 0.12280024 0.6777279  0.65761855 0.59650735 0.7738863
  0.46830247 0.29130293 0.53144752 0.43388711 0.40434055 0.29831372
  0.40779883 0.38036382 0.70961597 0.35571397 0.40252851 0.35362846
  0.45426119 0.77067444 0.75242139 0.49917352 0.46370359 0.4709884
  0.33628961 0.50033929 0.27116587 0.56299502 0.51988732 0.2948558
  0.37776648 0.47671512 0.21424568 0.4702983  0.44466693 0.74569562
  0.54232132 0.39119572 0.76510917 0.50952721 0.5782995  0.74530871
  0.3160512  0.45585816 0.68642218 0.76151319 0.62913998 0.49514952
  0.55003407 0.66213977 0.77114891 0.44886068 0.26418574 0.19777986
  0.67182867 0.34569868 0.46390856 0.61598467 0.48372745 0.2641277
  0.79971928 0.67098178 0.51847088 0.41721386 0.62338374 0.88945871
  0.55724239 0.29763291 0.71401891 0.51318854 0.41359978 0.21527993
  0.69220608 0.88986315 0.84135627 0.24478854 0.47198412 0.54251577
  0.62728282 0.64569449 0.40565508 0.53248478 0.37206759 0.26568684
  0.41609274 0.65264657 0.52925899 0.71365125 0.24837744 0.72937582
  0.42841635 0.68371406 0.64168922 0.47677696 0.22046816 0.54836166
  0.35505039 0.56474058 0.51073596 0.31859351 0.50508084 0.51818572
  0.32739852 0.6284643  0.37832157 0.16422333 0.57177159 0.3714097
  0.40547943 0.14627751 0.20844618 0.60646605 0.91208956 0.67978913
  0.55424802 0.47905133 0.36542777 0.86376559 0.39874522 0.59817142
  0.46280088 0.429994   0.87585869 0.38532895 0.38381758 0.5415347
  0.62279016 0.46752346 0.80929544 0.60541126 0.59555704 0.6597504
  0.46518618 0.45447007 0.40921333 0.18418287 0.39383643 0.29068888
  0.39770607 0.40708168 0.4051693  0.678962   0.42144618 0.31633735
  0.5882943  0.45178286 0.38896992 0.37284994 0.68171321 0.3682296
  0.53107423 0.59925186 0.51240722 0.45971072 0.35341483 0.61719858
  0.63772427 0.49562682 0.54543362 0.4371481  0.27390298 0.60807962
  0.56232987 0.36642406 0.24934024 0.4980088  0.69384436 0.37024607
  0.5236173  0.48797896 0.3976543  0.5050332  0.3157983  0.6262471
  0.24752187 0.40492795 0.53918356 0.52378717 0.49989563 0.43431435
  0.50879062 0.29690123 0.34495489 0.35200977 0.81563264 0.28167207
  0.5571883  0.50067395 0.49752366 0.47079689 0.55380467 0.37377991
  0.36645379 0.77188471 0.46482892 0.24795456 0.25570964 0.30660756
  0.57897899 0.67069191]]

png

# 利用隐藏层输出的数据计算导入输出层的数据
final_inputs = np.dot(who, hidden_outputs) # dot()函数是指两个矩阵做点乘
#可视化中间输出
print(final_inputs.T)
middle_layer_fig = np.asfarray((final_inputs-0.01)/0.99*255.0 )  
middle_layer_fig = np.asfarray(middle_layer_fig).reshape((1,10))
plt.imshow(middle_layer_fig, interpolation = 'nearest')
[[ 0.56028136  0.82552015  0.34670209  0.17793798 -0.66372393 -0.37233255
  -0.39555073 -0.76359914 -0.48399976 -0.23884983]]





<matplotlib.image.AxesImage at 0x7fa3d89ffc10>

png

# Or visualize final outputs as a heatmap
plt.imshow(final_inputs, cmap='hot', aspect='auto')
plt.xlabel('Output Node')
plt.ylabel('Sample')
plt.title('Final Inputs')
plt.colorbar()
plt.show()

png

# Visualize final layer inputs
plt.bar(range(out_nodes), final_inputs.flatten())
plt.xlabel('Output Node')
plt.ylabel('Input Value')
plt.title('Final Layer Inputs')
plt.show()

png

# 利用激活函数sigmoid计算输出层的输出结果
final_outputs = activation_function(final_inputs)
# 前向传播结束

#可视化中间输出
print(final_outputs.T)
middle_layer_fig = np.asfarray((final_outputs-0.01)/0.99*255.0 )  
middle_layer_fig = np.asfarray(middle_layer_fig).reshape((1,10))
plt.imshow(middle_layer_fig, interpolation = 'nearest') 
[[0.63651764 0.69540686 0.58581762 0.54436749 0.33990358 0.40797752
  0.40238179 0.31786536 0.38130809 0.44056981]]





<matplotlib.image.AxesImage at 0x7fa3da5f10a0>

png

# Or visualize final outputs as a heatmap
plt.imshow(final_outputs, cmap='hot', aspect='auto')
plt.xlabel('Output Node')
plt.ylabel('Sample')
plt.title('Final Outputs')
plt.colorbar()
plt.show()

png

# Visualize final layer outputs (sigmoid)
plt.bar(range(out_nodes), final_outputs.flatten())
plt.xlabel('Output Node')
plt.ylabel('Input Value')
plt.title('Final Layer Inputs')
plt.show()

png

# 进行反向传播
# 计算前向传播得到的输出结果与正确值之间的误差
output_errors = TARGETS - final_outputs

#可视化中间输出
print(output_errors.T)
middle_layer_fig = np.asfarray((output_errors-0.01)/0.99*255.0 )  
middle_layer_fig = np.asfarray(middle_layer_fig).reshape((1,10))
plt.imshow(middle_layer_fig, interpolation = 'nearest')  
[[-0.62651764 -0.68540686 -0.57581762 -0.53436749 -0.32990358  0.58202248
  -0.39238179 -0.30786536 -0.37130809 -0.43056981]]





<matplotlib.image.AxesImage at 0x7fa3d87db8b0>

png

# Visualize output errors as a bar chart
plt.bar(range(out_nodes), output_errors.flatten())
plt.xlabel('Output Node')
plt.ylabel('Error Value')
plt.title('Output Errors')
plt.show()

png

# Or visualize output errors as a scatter plot
plt.scatter(range(out_nodes), output_errors.flatten())
plt.xlabel('Output Node')
plt.ylabel('Error Value')
plt.title('Output Errors')
plt.show()

png

# 隐藏层的误差是由输出层的误差通过两个层之间的权重矩阵进行分配的,在隐藏层重新结合
hidden_errors = np.dot(who.T, output_errors) # 隐藏层与输出层之间的权重矩阵的转置与前向传播的误差矩阵的点乘
#可视化中间输出
print(hidden_errors.T)
#middle_layer_fig = np.asfarray((hidden_errors-0.01)/0.99*255.0 )  
#middle_layer_fig = np.asfarray(middle_layer_fig).reshape((20,10))
#plt.imshow(middle_layer_fig, interpolation = 'nearest')  
[[ 0.23145703 -0.09199276 -0.12220719 -0.15896069  0.06424253  0.10197068
  -0.23125848 -0.00782811 -0.08381227 -0.11514534 -0.09644854 -0.12429981
   0.11276763 -0.26363747 -0.00989155 -0.14107911  0.27482566  0.10077863
   0.08727872 -0.12703169  0.04482464  0.07979755 -0.08780178 -0.10513761
  -0.00644824 -0.11657829 -0.04453468  0.05577635  0.01531368  0.13738715
   0.03474212  0.22550981 -0.08763767 -0.06505764 -0.11262462 -0.04158586
  -0.09128322 -0.01086248  0.05525096 -0.12434499  0.17656152  0.04339815
  -0.03433653 -0.11152836  0.03669448 -0.01467246  0.01413861  0.17155288
  -0.12223192 -0.10968683  0.10515451  0.14353315  0.08262463  0.16657906
  -0.10807233 -0.10796653 -0.01689826  0.05175527 -0.02711501 -0.06925127
   0.24918363 -0.0658346  -0.01650576 -0.14181141 -0.06328054  0.11752269
   0.07361948 -0.25658514 -0.03837734  0.05291595  0.18022871 -0.02485894
  -0.11155773 -0.17969543  0.05235072 -0.03868002  0.07991305 -0.00944794
   0.01358124 -0.04854606 -0.11433062 -0.11457118 -0.10174756  0.08157923
  -0.07922054  0.16252699 -0.0668835   0.02633577 -0.25292949 -0.00164063
   0.17719827 -0.27838094  0.06372956 -0.08327759 -0.1045452   0.0994223
  -0.18854096  0.01717639 -0.22337965 -0.05331426 -0.09068925  0.00909319
  -0.11275048  0.02400681  0.15580461  0.04395622  0.05191163  0.07671998
  -0.07357827  0.04857611  0.01200461 -0.01824155  0.20218933 -0.01648541
  -0.08841815 -0.22972757 -0.06564815  0.25879827  0.03363929 -0.08144042
  -0.00117747  0.04931258 -0.28733007  0.09207885 -0.11084745  0.03480787
  -0.30290225  0.02605289 -0.03273764  0.13374028  0.06733113 -0.08264645
  -0.10579    -0.16626817 -0.19349467  0.2339928   0.25338442 -0.04781617
   0.01431193 -0.06614716 -0.03706169 -0.18027598  0.03546684  0.07375848
  -0.13524866 -0.14490857 -0.21459248  0.1796899   0.02376605 -0.02517879
   0.00632407  0.03003414 -0.11537092  0.03510202  0.07357026  0.0971219
  -0.08266574  0.03720117  0.09910707 -0.04312925 -0.08307132  0.02983252
   0.01496464  0.07249455 -0.1618727   0.11377448 -0.03207163  0.19216192
   0.09118743  0.01690548 -0.06923089  0.02959015  0.20129512 -0.04899694
   0.1233579  -0.20508642  0.01812198 -0.00063595  0.17360329  0.11723159
   0.15777609  0.07835488 -0.05387801 -0.01755501  0.10815374  0.22098465
  -0.12040005  0.025853   -0.08475004  0.24887947  0.07332807  0.0784619
   0.01351764 -0.08704183  0.08712977  0.0756019  -0.04051772 -0.15931343
  -0.04228901  0.13588616]]
# Visualize hidden errors as a bar chart
plt.bar(range(hide_nodes), hidden_errors.flatten())
plt.xlabel('Hidden Node')
plt.ylabel('Error Value')
plt.title('Hidden Errors')
plt.show()

png

# Or visualize hidden errors as a scatter plot
plt.scatter(range(hide_nodes), hidden_errors.flatten())
plt.xlabel('Hidden Node')
plt.ylabel('Error Value')
plt.title('Hidden Errors')
plt.show()

png

# 对隐藏层与输出层之间的权重矩阵进行更新迭代
who += learningrate * np.dot((output_errors * final_outputs * (1.0 - final_outputs)),np.transpose(hidden_outputs))
# 对输入层与隐藏层之间的权重矩阵进行更新迭代
wih += learningrate * np.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), np.transpose(INPUT))
#第一次迭代训练结束
print(wih)
print(who)
[[ 0.02067233 -0.07978803  0.03108053 ... -0.03073812  0.0557655
  -0.05129495]
 [ 0.07106607  0.08339657 -0.09380426 ...  0.0441884  -0.03837313
  -0.08557481]
 [ 0.05502248 -0.09130093  0.0384007  ... -0.00538593  0.06249898
   0.08624116]
 ...
 [-0.00994263 -0.07816935 -0.01082394 ...  0.00301429 -0.00230436
   0.09999818]
 [ 0.03612263 -0.01946694  0.0954403  ...  0.01146139 -0.00025476
  -0.12006706]
 [-0.05983042 -0.01998364 -0.06092712 ... -0.02392167 -0.06806361
   0.01094472]]
[[-0.0204511  -0.07749507 -0.00194097 ...  0.09540347  0.008829
  -0.02140005]
 [-0.01264093  0.06889351  0.04956639 ... -0.0669025  -0.01843888
   0.00722866]
 [-0.05481193  0.04000967 -0.09688887 ...  0.07287872  0.11162873
  -0.12241058]
 ...
 [-0.0972931   0.05829893  0.13900051 ...  0.04472318  0.0444388
  -0.1383636 ]
 [ 0.0109094   0.01127165  0.00850074 ...  0.00947806 -0.08093348
  -0.17257885]
 [-0.08861324  0.04998882  0.03560659 ...  0.05427103 -0.06461784
   0.01395731]]
print(wih)
# Visualize weight matrix wih
plt.imshow(wih, cmap='coolwarm', aspect='auto')
plt.xlabel('Output Node')
plt.ylabel('Hidden Node')
plt.title('Weight Matrix (Hidden to input)')
plt.colorbar()
plt.show()
[[ 0.02067233 -0.07978803  0.03108053 ... -0.03073812  0.0557655
  -0.05129495]
 [ 0.07106607  0.08339657 -0.09380426 ...  0.0441884  -0.03837313
  -0.08557481]
 [ 0.05502248 -0.09130093  0.0384007  ... -0.00538593  0.06249898
   0.08624116]
 ...
 [-0.00994263 -0.07816935 -0.01082394 ...  0.00301429 -0.00230436
   0.09999818]
 [ 0.03612263 -0.01946694  0.0954403  ...  0.01146139 -0.00025476
  -0.12006706]
 [-0.05983042 -0.01998364 -0.06092712 ... -0.02392167 -0.06806361
   0.01094472]]

png

print(who)
# Visualize weight matrix who
plt.imshow(who, cmap='coolwarm', aspect='auto')
plt.xlabel('Output Node')
plt.ylabel('Hidden Node')
plt.title('Weight Matrix (Hidden to Output)')
plt.colorbar()
plt.show()
[[-0.0204511  -0.07749507 -0.00194097 ...  0.09540347  0.008829
  -0.02140005]
 [-0.01264093  0.06889351  0.04956639 ... -0.0669025  -0.01843888
   0.00722866]
 [-0.05481193  0.04000967 -0.09688887 ...  0.07287872  0.11162873
  -0.12241058]
 ...
 [-0.0972931   0.05829893  0.13900051 ...  0.04472318  0.0444388
  -0.1383636 ]
 [ 0.0109094   0.01127165  0.00850074 ...  0.00947806 -0.08093348
  -0.17257885]
 [-0.08861324  0.04998882  0.03560659 ...  0.05427103 -0.06461784
   0.01395731]]

png

#完整训练流程
input_nodes=784 # 输入层节点数
hide_nodes=200 # 隐藏层节点数
out_nodes=10  # 输出层节点数
learningrate = 0.1 #学习率
train_errors = []
epochs=5

wih = np.random.normal(0.0, pow(hide_nodes, -0.5), (hide_nodes, input_nodes)) #矩阵大小为隐藏层节点数×输入层节点数
#np.random.normal()的意思是一个正态分布,normal这里是正态的意思
who = np.random.normal(0.0, pow(hide_nodes, -0.5), (out_nodes, hide_nodes)) #矩阵大小为输出层节点数×隐藏层节点数
activation_function = lambda x: ssp.expit(x)   #结合上述所学,这里写一段原理是logistic sigmoid的激活函数

test_data_file = open("mnist_train.csv", 'r')
test_data_list = test_data_file.readlines()
test_data_file.close()

for e in range(epochs):
    # go through all records in the training data set
    # 遍历所有输入的数据
    print('epochs start:',e)
     # 计算训练集上的误差
    train_error = 0.0
    for record in test_data_list:
        all_values = record.split(',') # split()函数将第1条数据进行拆分,以‘,’为分界点进行拆分
        inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01 #输入层,784个输入
        targets = np.zeros(out_nodes) + 0.01 
        targets[int(all_values[0])] = 0.99 
        INPUT = np.array(inputs, ndmin = 2).T # array函数是矩阵生成函数,将输入的inputs转换成二维矩阵,ndmin=2表示二维矩阵
        TARGETS = np.array(targets, ndmin = 2).T # .T表示矩阵的转置,生成后的矩阵的转置矩阵送入变量targets
        # 进行前向传播
        # 利用导入的数据计算进入隐藏层的数据
        hidden_inputs = np.dot(wih, INPUT) # dot()函数是指两个矩阵做点乘
        # 利用激活函数sigmoid计算隐藏层输出的数据
        hidden_outputs = activation_function(hidden_inputs)
        # 利用隐藏层输出的数据计算导入输出层的数据
        final_inputs = np.dot(who, hidden_outputs) # dot()函数是指两个矩阵做点乘
        # 利用激活函数sigmoid计算输出层的输出结果
        final_outputs = activation_function(final_inputs)
        # 前向传播结束
        # 进行反向传播
        # 计算前向传播得到的输出结果与正确值之间的误差
        output_errors = TARGETS - final_outputs
        # 隐藏层的误差是由输出层的误差通过两个层之间的权重矩阵进行分配的,在隐藏层重新结合
        hidden_errors = np.dot(who.T, output_errors) # 隐藏层与输出层之间的权重矩阵的转置与前向传播的误差矩阵的点乘
        # 对隐藏层与输出层之间的权重矩阵进行更新迭代
        who += learningrate * np.dot((output_errors * final_outputs * (1.0 - final_outputs)),np.transpose(hidden_outputs))
        # 对输入层与隐藏层之间的权重矩阵进行更新迭代
        wih += learningrate * np.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), np.transpose(INPUT))
        train_error += np.sum((output_errors) ** 2)
    train_error /= len(test_data_list)
    train_errors.append(train_error)

# 画出误差曲线
plt.plot(train_errors, label='training error')
plt.legend()
plt.show()
epochs start: 0
epochs start: 1
epochs start: 2
epochs start: 3
epochs start: 4

png

#最终结果,这两个变量就是最终的权重(weights)
print(who)
print(wih)
final_who=who
final_wih=wih
[[-1.16611326 -0.4525141  -0.06610833 ... -0.45357449 -0.48939251
   0.64537313]
 [-0.23350166 -0.07640343 -0.33892076 ... -0.42012762 -0.09425477
  -0.35624211]
 [ 0.02538154 -0.36034837 -0.31796842 ... -0.03179198  0.24630403
   0.53641215]
 ...
 [-0.62273744  1.44743377  0.37902492 ... -1.22510993  0.85708252
  -0.0379783 ]
 [-0.30649461 -0.45335212 -0.75158325 ...  0.27636151 -0.47017666
  -0.43715161]
 [ 0.01993143 -1.11644346  1.10811109 ...  0.39435807 -0.77164373
  -0.37836149]]
[[ 0.01027389 -0.06948278 -0.13336783 ...  0.0431249   0.0116984
   0.01118535]
 [ 0.04093141  0.13349408  0.0447183  ... -0.02876729 -0.08677845
  -0.05826928]
 [-0.11370514 -0.04104104  0.05438874 ... -0.00457712 -0.01669163
  -0.02552346]
 ...
 [-0.00480138  0.04369124 -0.07553194 ...  0.09218518  0.02003152
   0.0808828 ]
 [-0.00826098  0.07729079 -0.12576362 ...  0.03445958  0.02413203
  -0.08935369]
 [-0.03758297 -0.06222281  0.02554687 ...  0.13169544  0.01547494
  -0.07650541]]
#保存权重
np.save("weights", final_who)
np.save("weights02",final_wih)
#测试
#加载权重文件(weights)
final_who=np.load("weights.npy")
final_wih=np.load("weights02.npy")
# Visualize weight matrix wih
plt.imshow(final_wih, cmap='coolwarm', aspect='auto')
plt.xlabel('Output Node')
plt.ylabel('Hidden Node')
plt.title('Weight Matrix (Hidden to input)')
plt.colorbar()
plt.show()

png

# Visualize weight matrix who
plt.imshow(final_who, cmap='coolwarm', aspect='auto')
plt.xlabel('Output Node')
plt.ylabel('Hidden Node')
plt.title('Weight Matrix (Hidden to output)')
plt.colorbar()
plt.show()

png

test_data_file = open("mnist_test.csv", 'r')
test_data_list = test_data_file.readlines()
test_data_file.close()
data_serial_num=455
all_values = test_data_list[data_serial_num].split(',') # split()函数将第1条数据进行拆分,以‘,’为分界点进行拆分
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
#print(inputs)
image_array = np.asfarray(all_values[1:]).reshape((28,28)) # asfarray()函数将all_values中的后784个数字进行重新排列
# reshape()函数可以对数组进行整型,使其成为28×28的二维数组,asfarry()函数可以使其成为矩阵。
plt.imshow(image_array, interpolation = 'nearest')  # imshow()函数可以将28×28的矩阵中的数值当做像素值,使其形成图片
<matplotlib.image.AxesImage at 0x7fa3d809b2e0>

png

test_inputs = np.array(inputs, ndmin = 2).T
# 以下程序为计算输出结果的程序,与上面前向传播算法一致
hidden_inputs = np.dot(final_wih, test_inputs)
hidden_outputs = activation_function(hidden_inputs)
final_inputs = np.dot(final_who, hidden_outputs)
final_outputs = activation_function(final_inputs)
print(final_outputs)
[[0.01072488]
 [0.99333831]
 [0.00781424]
 [0.00584866]
 [0.02362064]
 [0.01216366]
 [0.00683059]
 [0.00921785]
 [0.00169813]
 [0.00730339]]
# Visualize hidden layer activations
#hidden_inputs = hidden_inputs.reshape((20, 10))
plt.imshow(hidden_inputs.T, cmap='hot', aspect='auto')
plt.xlabel('Hidden Node')
plt.ylabel('Sample')
plt.title('Hidden Layer Activations')
plt.colorbar()
plt.show()

png

# Visualize hidden layer activations
plt.imshow(hidden_outputs.T, cmap='hot', aspect='auto')
plt.xlabel('Hidden Node')
plt.ylabel('Sample')
plt.title('Hidden Layer Activations')
plt.colorbar()
plt.show()

png

#可视化中间输出
print(final_inputs.T)
middle_layer_fig = np.asfarray((final_inputs-0.01)/0.99*255.0 )  
middle_layer_fig = np.asfarray(middle_layer_fig).reshape((1,10))
plt.imshow(middle_layer_fig, interpolation = 'nearest')
[[-4.52440665  5.00469803 -4.84396237 -5.13567656 -3.72173031 -4.39706459
  -4.97949043 -4.67735268 -6.37652596 -4.91208681]]





<matplotlib.image.AxesImage at 0x7fa3cbe083a0>

png

# Or visualize final outputs as a heatmap
plt.imshow(final_inputs, cmap='hot', aspect='auto')
plt.xlabel('Input Node')
plt.ylabel('Sample')
plt.title('Final Inputs')
plt.colorbar()
plt.show()

png

# Visualize final layer inputs
plt.bar(range(out_nodes), final_inputs.flatten())
plt.xlabel('Output Node')
plt.ylabel('Input Value')
plt.title('Final Layer Inputs')
plt.show()

png

#可视化中间输出
print(final_outputs.T)
middle_layer_fig = np.asfarray((final_outputs-0.01)/0.99*255.0 )  
middle_layer_fig = np.asfarray(middle_layer_fig).reshape((1,10))
plt.imshow(middle_layer_fig, interpolation = 'nearest') 
[[0.01072488 0.99333831 0.00781424 0.00584866 0.02362064 0.01216366
  0.00683059 0.00921785 0.00169813 0.00730339]]





<matplotlib.image.AxesImage at 0x7fa3cbcba550>

png

# Or visualize final outputs as a heatmap
plt.imshow(final_outputs, cmap='hot', aspect='auto')
plt.xlabel('Output Node')
plt.ylabel('Sample')
plt.title('Final Outputs')
plt.colorbar()
plt.show()

png

# Visualize final layer outputs (sigmoid)
plt.bar(range(out_nodes), final_outputs.flatten())
plt.xlabel('Output Node')
plt.ylabel('Input Value')
plt.title('Final Layer Outputs')
plt.show()

png

lebal = np.argmax(final_outputs)
print(lebal)
1
#模型效果和性能测试
# load the mnist test data CSV file into a list
# 导入测试集数据
test_data_file = open("mnist_test.csv", 'r')
test_data_list = test_data_file.readlines()
test_data_file.close()
# test the neural network
# 用query函数对测试集进行检测
# go through all the records in the test data set for record in the test_data_list:
scorecard = 0 # 得分卡,检测对一个加一分
# 计算测试集上的误差

for record in test_data_list:
    # split the record by the ',' comas
    # 将所有测试数据通过逗号分隔开
    all_values = record.split(',')
    # correct answer is first value
    # 正确值为每一条测试数据的第一个数值
    correct_lebal = int(all_values[0])
    #print("correct lebal", correct_lebal) # 将正确的数值在屏幕上打印出来
    # scale and shift the inputs
    # 对输入数据进行处理,取后784个数据除以255,再乘以0.99,最后加上0。01,是所有的数据都在0.01到1.00之间
    inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01 #输入层,784个输入
    
    # query the network
    # 用query函数对测试集进行检测
    
    test_inputs = np.array(inputs, ndmin = 2).T
    # 以下程序为计算输出结果的程序,与上面前向传播算法一致
    hidden_inputs = np.dot(final_wih, test_inputs)
    hidden_outputs = activation_function(hidden_inputs)
    final_inputs = np.dot(final_who, hidden_outputs)
    final_outputs = activation_function(final_inputs)
  
    # the index of the highest value corresponds to out label
    # 得到的数字就是输出结果的最大的数值所对应的标签
    lebal = np.argmax(final_outputs) # argmax()函数用于找出数值最大的值所对应的标签
    #print("Output is ", lebal) # 在屏幕上打出最终输出的结果
    # output image of every digit
    # 输出每一个数字的图片
    #image_correct = np.asfarray(all_values[1:]).reshape((28, 28))
    #plt.imshow(image_correct, cmap = 'Greys', interpolation = 'None')
    #plt.show()
    # append correct or incorrect to list
    if (lebal == correct_lebal):
        # network's answer matchs correct answer, add 1 to scorecard
        scorecard += 1
    else:
        # network's answer doesn't match correct answer, add 0 to scorecard
        scorecard += 0
        pass
    pass

# calculate the performance score, the fraction
# 计算准确率 得分卡最后的数值/10000(测试集总个数)
print("performance = ", scorecard / 10000)
performance =  0.9722