700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 用Python搭建2层神经网络实现mnist手写数字分类

用Python搭建2层神经网络实现mnist手写数字分类

时间:2023-05-02 08:25:13

相关推荐

用Python搭建2层神经网络实现mnist手写数字分类

这是一个用python搭建2层NN(一个隐藏层)识别mnist手写数据集的示例

mnist.py文件提供了mnist数据集(6万张训练图,1万张测试图)的在线下载,每张图片是 28 ∗ 28 28*28 28∗28的尺寸,拉长为 1 ∗ 784 1*784 1∗784的向量作为NN的输入,隐藏层设置了50个神经元,输出层由于是十分类(数字0-9)所以设置了10个神经元,所以网络的架构是784-50-10;

设置的迭代学习次数是10000次,学习率0.1,mini-batch的大小为100个,程序中计算了训练精度,测试精度,训练的损失,并通过打印和画图呈现出来,还计算了程序运行时间,可以看出损失函数确实在不断减小,训练精度和测试精度都在上升且差距很小,说明NN正在正确地学习。

源码:

import timeimport numpy as npfrom dataset.mnist import load_mnistfrom SimpleNet import simpleNetimport matplotlib.pyplot as pltstart = time.clock() # 程序计时(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, one_hot_label=True)train_loss_list = []train_acc_list = []test_acc_list = []# 超参数iters_num = 10000train_size = x_train.shape[0]batch_size = 100learning_rate = 0.1iter_per_epoch = max(train_size / batch_size, 1)network = simpleNet(input_size=784, hidden_size=50, output_size=10)for i in range(iters_num):# 获取mini-batchbatch_mask = np.random.choice(train_size, batch_size)x_batch = x_train[batch_mask]t_batch = t_train[batch_mask]# 计算梯度# grad = network.numerical_gradient(x_batch, t_batch)grad = network.gradient(x_batch, t_batch)# 更新参数for key in ('W1', 'b1', 'W2', 'b2'):network.params[key] -= learning_rate * grad[key]# 记录学习过程的损失变化loss = network.loss(x_batch, t_batch)train_loss_list.append(loss)if i % iter_per_epoch == 0:train_acc = network.accuracy(x_train, t_train)test_acc = network.accuracy(x_test, t_test)train_acc_list.append(train_acc)test_acc_list.append(test_acc)print("train acc, test acc | " + str(train_acc) + ", " + str(test_acc))# 画损失函数的变化x1 = np.arange(len(train_loss_list))ax1 = plt.subplot(121)plt.plot(x1, train_loss_list)plt.xlabel("iteration")plt.ylabel("loss")# 画训练精度,测试精度随着epoch的变化markers = {'train': 'o', 'test': 's'}x2 = np.arange(len(train_acc_list))ax2 = plt.subplot(122)plt.plot(x2, train_acc_list, label='train acc')plt.plot(x2, test_acc_list, label='test acc', linestyle='--')plt.xlabel("epochs")plt.ylabel("accuracy")plt.ylim(0, 1.0)plt.legend(loc='lower right')plt.show()end = time.clock()print('Running Time: %s Seconds' %(end-start))

打印学习过程中训练和测试精度随着epoch的迭代的变化和程序运行时间:

train acc, test acc | 0.09871666666666666, 0.098train acc, test acc | 0.7794833333333333, 0.7855train acc, test acc | 0.8781166666666667, 0.8828train acc, test acc | 0.8979166666666667, 0.8994train acc, test acc | 0.90735, 0.9111train acc, test acc | 0.9139166666666667, 0.9153train acc, test acc | 0.9185666666666666, 0.9197train acc, test acc | 0.9235333333333333, 0.9248train acc, test acc | 0.9269, 0.9283train acc, test acc | 0.9310666666666667, 0.9317train acc, test acc | 0.9333666666666667, 0.935train acc, test acc | 0.93655, 0.9363train acc, test acc | 0.9398666666666666, 0.9391train acc, test acc | 0.9423, 0.9406train acc, test acc | 0.9442833333333334, 0.9432train acc, test acc | 0.9460333333333333, 0.9444train acc, test acc | 0.9479333333333333, 0.9457Running Time: 82.45328003986769 Seconds

用到的simpleNet类(这个类定义了2层的NN,包括前向预测和反向计算梯度的学习的所有函数)定义在SimpleNet.py中,和上面的主程序放在同一个文件夹下,所以用from SimpleNet import simpleNet命令导入:

# 这是一个2层网络,即输入-隐藏层-输出import numpy as npdef sigmoid(x):return 1 / (1 + np.exp(-x))def softmax(x):if x.ndim == 2:x = x.Tx = x - np.max(x, axis=0)y = np.exp(x) / np.sum(np.exp(x), axis=0)return y.Tx = x - np.max(x) # 溢出对策return np.exp(x) / np.sum(np.exp(x))# 计算sigmoid层的反向传播导数(根据数学推导知道是y(1-y))def sigmoid_grad(x):return (1.0 - sigmoid(x)) * sigmoid(x)def numerical_gradient(f, x):h = 1e-4 # 0.0001grad = np.zeros_like(x)it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])while not it.finished:idx = it.multi_indextmp_val = x[idx]x[idx] = float(tmp_val) + hfxh1 = f(x) # f(x+h)x[idx] = tmp_val - hfxh2 = f(x) # f(x-h)grad[idx] = (fxh1 - fxh2) / (2 * h)x[idx] = tmp_val # 还原值it.iternext()return graddef cross_entropy_error(y, t):if y.ndim == 1:t = t.reshape(1, t.size)y = y.reshape(1, y.size)# 监督数据是one-hot-vector的情况下,转换为正确解标签的索引if t.size == y.size:t = t.argmax(axis=1)batch_size = y.shape[0]return -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_sizeclass simpleNet:def __init__(self, input_size, hidden_size, output_size, weight_init_std = 0.01):# 初始化网络self.params = {}# weight_init_std:权重初始化标准差self.params['W1'] = weight_init_std * \np.random.randn(input_size, hidden_size) # 用高斯分布随机初始化一个权重参数矩阵self.params['b1'] = np.zeros(hidden_size)self.params['W2'] = weight_init_std * \np.random.randn(hidden_size, output_size)self.params['b2'] = np.zeros(output_size)def predict(self, x):# 前向传播,用点乘实现W1, W2 = self.params['W1'], self.params['W2']b1, b2 = self.params['b1'], self.params['b2']a1 = np.dot(x, W1) + b1z1 = sigmoid(a1)a2 = np.dot(z1, W2) + b2y = softmax(a2)return ydef loss(self, x, t):y = self.predict(x)loss = cross_entropy_error(y, t)return lossdef accuracy(self, x, t):y = self.predict(x)y = np.argmax(y, axis=1)t = np.argmax(t, axis=1)accuracy = np.sum( y==t ) / float(x.shape[0])return accuracydef numerical_gradient(self, x, t):loss_W = lambda W: self.loss(x, t)grads = {}grads['W1'] = numerical_gradient(loss_W, self.params['W1'])grads['b1'] = numerical_gradient(loss_W, self.params['b1'])grads['W2'] = numerical_gradient(loss_W, self.params['W2'])grads['b2'] = numerical_gradient(loss_W, self.params['b2'])return grads# 高速版计算梯度,利用批版本的反向传播实现计算高速def gradient(self, x, t):W1, W2 = self.params['W1'], self.params['W2']b1, b2 = self.params['b1'], self.params['b2']grads = {}batch_num = x.shape[0] # 把输入的所有列一起计算,因此可以快速# forwarda1 = np.dot(x, W1) + b1z1 = sigmoid(a1)a2 = np.dot(z1, W2) + b2y = softmax(a2)# backwarddy = (y - t) / batch_num # 输出和标签的平均距离,作为损失值grads['W2'] = np.dot(z1.T, dy)# numpy数组.T就是转置grads['b2'] = np.sum(dy, axis=0)# 这里和批版本的Affine层的反向传播导数计算一样dz1 = np.dot(dy, W2.T)da1 = sigmoid_grad(a1) * dz1grads['W1'] = np.dot(x.T, da1)grads['b1'] = np.sum(da1, axis=0)return grads

定义load_mnist()方法的mnist.py文件,放在dataset文件夹中,dataset文件夹放在和主程序相同的路径,所以用from dataset.mnist import load_mnist命令导入:

# coding: utf-8try:import urllib.request # 用于打开URL(主要是针对HTTP)except ImportError:raise ImportError('You should use Python 3.x')import os.path # os模块提供操作系统相关的操作import gzip # 这个模块提供接口解压和压缩文件import pickle # 把Python对象的结构序列化import osimport numpy as npurl_base = '/exdb/mnist/' # url字符串key_file = {'train_img':'train-images-idx3-ubyte.gz','train_label':'train-labels-idx1-ubyte.gz','test_img':'t10k-images-idx3-ubyte.gz','test_label':'t10k-labels-idx1-ubyte.gz'} # 字典变量dataset_dir = os.path.dirname(os.path.abspath(__file__))save_file = dataset_dir + "/mnist.pkl"train_num = 60000test_num = 10000img_dim = (1, 28, 28)img_size = 784# 下载一个文件def _download(file_name):file_path = dataset_dir + "/" + file_name# 已经有了就不再下载了if os.path.exists(file_path):returnprint("Downloading " + file_name + " ... ")urllib.request.urlretrieve(url_base + file_name, file_path)print("Done")# 下载 mnist 数据集,四个文件def download_mnist():for v in key_file.values():_download(v)def _load_label(file_name):file_path = dataset_dir + "/" + file_nameprint("Converting " + file_name + " to NumPy Array ...")with gzip.open(file_path, 'rb') as f:# gzip.open()打开gzip文件,转换为二进制文件,返回文件对象labels = np.frombuffer(f.read(), np.uint8, offset=8)print("Done")return labelsdef _load_img(file_name):file_path = dataset_dir + "/" + file_nameprint("Converting " + file_name + " to NumPy Array ...") with gzip.open(file_path, 'rb') as f:data = np.frombuffer(f.read(), np.uint8, offset=16)data = data.reshape(-1, img_size)print("Done")return datadef _convert_numpy():dataset = {}dataset['train_img'] = _load_img(key_file['train_img'])dataset['train_label'] = _load_label(key_file['train_label']) dataset['test_img'] = _load_img(key_file['test_img'])dataset['test_label'] = _load_label(key_file['test_label'])return datasetdef init_mnist():download_mnist()dataset = _convert_numpy()print("Creating pickle file ...")with open(save_file, 'wb') as f:pickle.dump(dataset, f, -1)print("Done!")def _change_one_hot_label(X):T = np.zeros((X.size, 10))for idx, row in enumerate(T):row[X[idx]] = 1return Tdef load_mnist(normalize=True, flatten=True, one_hot_label=False):"""读入MNIST数据集Parameters----------normalize : 将图像的像素值正规化为0.0~1.0one_hot_label : one_hot_label为True的情况下,标签作为one-hot数组返回one-hot数组是指[0,0,1,0,0,0,0,0,0,0]这样的数组flatten : 是否将图像展开为一维数组Returns-------(训练图像, 训练标签), (测试图像, 测试标签)"""if not os.path.exists(save_file):init_mnist()with open(save_file, 'rb') as f:dataset = pickle.load(f)if normalize:for key in ('train_img', 'test_img'):dataset[key] = dataset[key].astype(np.float32)dataset[key] /= 255.0if one_hot_label:dataset['train_label'] = _change_one_hot_label(dataset['train_label'])dataset['test_label'] = _change_one_hot_label(dataset['test_label'])if not flatten:for key in ('train_img', 'test_img'):dataset[key] = dataset[key].reshape(-1, 1, 28, 28)return (dataset['train_img'], dataset['train_label']), (dataset['test_img'], dataset['test_label']) if __name__ == '__main__':init_mnist()

文件放置位置,这个和主程序开头的import联系密切:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。