本节使用python环境,在不使用深度学习工具箱情况下搭建一个简单的神经网络结构(非CNN卷积网络)来训练mnist手写体数据库。
网络的结构可以很简单,比如就是([784,200,100,10]),输入维度为784是一个样本大小的28*28,网络包含dropout操作,更多的是理解这种最基础的反向传播机制的实现过程。
完整的项目点击github主页获取
下面看下可运行的包含训练测试的代码:
# -*- coding: utf-8 -*-
"""
@author: chen
"""
import numpy as np
import struct
from datetime import datetime
import matplotlib.pyplot as plt
#读取图像
def read_image(filename):
binfile = open(filename , 'rb')
buf = binfile.read()
index = 0
magic, numImages , numRows , numColumns = struct.unpack_from('>IIII' , buf , index)
index += struct.calcsize('>IIII')
data = np.zeros((numImages,numRows*numColumns))
for i in range(numImages):
im = struct.unpack_from('>784B' ,buf, index)
index += struct.calcsize('>784B')
im = np.array(im)
data[i,:] = im
return data
#读取图像label
def read_label(filename):
binfile = open(filename , 'rb')
buf = binfile.read()
index = 0
magic, numLabels = struct.unpack_from('>II' , buf , index)
index += struct.calcsize('>II')
data = np.zeros((numLabels,10))
for i in range(numLabels):
label = struct.unpack_from('>B' ,buf, index)[0]
label = np.array(label)
data[i,label] = 1
index += struct.calcsize('>B')
return data
# 建立与初始化网络参数
class nn_setup():
def __init__(self,net,learningRate = 2, epochs = 100, batch =