自己动手制作人工神经网络0x6:真“反向传播”

这篇文章,我们来一探模型眼中,数字0~9长什么样。

这里我们把原来的输出层作为输入层,原来的输入层作为输出层,对于每一个数字,给数字对应节点给0.99这个值,其他节点给0.01这个值,得到了原输入层节点的值,然后把他可视化。可以勉强看出0、1、2、3、6、9,而这个5就有点鬼畜了,4的话好模糊。

# Reverse
 
fig = plt.figure()
 
# reverse count
for x in range(10):
    o = np.zeros(output_nodes) + 0.01
    o[x] = 0.99
    h = np.dot(nn.who.T, o)
    i = np.dot(nn.wih.T, h)
    img = i.reshape(28, 28)
 
    #show
    im = fig.add_subplot(2, 5, x+1)
    im.imshow(img, cmap='Greys', interpolation='None')
 
plt.show()

对了,numpy模块中有两个函数可以完成数据保存与读取。tofile("path")可以保存到文件中,fromfile("path")则可以从文件中读取。这样就不用每次都训练一遍了,可以在训练后把权重都保存下来,下次运行时在读取。numpy的数据保存方式占用空间很小,很方便。
下面的代码会把权重值保存到同目录的文件parameter0.bin、和parameter1.bin中。大家可以自行更改名称。

#save parameter
def save(self):
    self.wih.tofile("./parameter0.bin")
    self.who.tofile("./parameter1.bin")
 
 
#load parameter
def load(self):
    self.wih = np.fromfile("./parameter0.bin", dtype=np.float64)
    self.wih.shape = self.hN, self.iN
 
    self.who = np.fromfile("./parameter1.bin", dtype=np.float64)
    self.who.shape = self.oN, self.hN

这里非常抱歉,忘了截图,需要一个星期后才能补上。

你可能感兴趣的:(自己动手制作人工神经网络0x6:真“反向传播”)