2. 基本要求 掌握深度学习的网络设计、模型训练和测试方法。
3. 支撑的课程目标 本实验项目可以支撑 “ 课程目标 2. 熟练掌握和使用结构主义的智能算法,解决复杂系统 工程的智能处理和应用问题 ” 本实验通过实验理解并体会 ANN 的设计及网络结构的学习方法,通过样本数据训练网 络,并能使用训练成熟的网络进行预测、优化等,培养学生熟悉 ANN 的典型模型、基于神 经网络的知识表示与推理 , 以及深度学习、类脑计算等方法解决实际问题的能力,达到课程 目标的要求。
4. 实验原理 MNIST 数据集下载地址: http://yann.lecun.com/exdb/mnist/ 5. 实验步骤 1) 深度学习入门(六):手写数字识别: https://www.infoq.cn/article/fephligxt4egiokbjyet 2) 使用神经网络识别手写数字: https://hit-scir.gitbooks.io/neural-networks-and-deep-learning-zh_cn/content/chap1/c1s0 .html 3) 【深度学习系列】手写数字识别 -- 卷 积 神 经 网 络 CNN 原理详解 ( 一 ) : https://zhuanlan.zhihu.com/p/30665319 4) matlab — — 简 单 CNN 网 络 的 数 字 识 别 : https://blog.csdn.net/xjc0518/article/details/100653760 5) python 深 度 学 习 --- 手 写 数 字 识 别 : https://blog.csdn.net/shuiyihang0981/article/details/87554765 6) Python 实现深度学习 MNIST 手写数字识别(单文件,非框架,无需 GPU ,适合初 学者) :https://blog.csdn.net/discoverer100/article/details/88047658 7) C++ 实现 CNN 识别手写数字 : https://blog.csdn.net/taoyafan/article/details/80459824 8) [ 深 度 学 习 系 列 之 一 ] C++ 实 现 神 经 网 络 : https://www.geek-share.com/detail/2716287788.html 9) C++ 实 现 一 个 识 别 MNIST 数 字 的 卷 积 神 经 网 络 : https://satanwoo.github.io/2018/05/09/MNIST/ 10) 深 度 学习 Java 类库 deeplearning4j 学习 笔记 -MNIST 手写 数字 分 类问题 : https://www.jianshu.com/p/52ffcbb2d64d 11) 【 dl4j 】 Java 深 度 学 习 入 门 之 手 写 数 字 识 别 并 生 成 exe 可 执 行 文 件 : https://www.daimajiaoliu.com/daima/4875bafcb900418
使用python3.x版本的代码
import pickle
import gzip
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import random
def load_data():
f = gzip.open('mnist.pkl.gz', 'rb')
training_data, validation_data, test_data =pickle.load(f,encoding="bytes")
f.close()
return (training_data, validation_data, test_data)
def showimage():
training_set, validation_set, test_set = load_data()
flattened_images = validation_set[0]
images = [np.reshape(f, (-1, 28)) for f in flattened_images]
for i in range(16):
ax = plt.subplot(4, 4, i+1)
ax.matshow(images[i], cmap = matplotlib.cm.binary)
plt.xticks(np.array([]))
plt.yticks(np.array([]))
plt.show()
class Network(object):
def __init__(self, sizes):
self.num_layers = len(sizes)
self.sizes = sizes
self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
self.weights = [np.random.randn(y, x) for x, y in zip(sizes[:-1], sizes[1:])]
# print self.weights
# print self.biases
def feedforward(self, a):
for b, w in zip(self.biases, self.weights):
a = sigmoid(np.dot(w, a) + b)
return a
def SGD(self, training_data, epochs, mini_batch_size, eta, test_data=None):
if test_data:
test_data = list(test_data)
n_test = len(test_data)
training_data=list(training_data)
n = len(training_data)
for j in range(epochs):
random.shuffle(training_data)
mini_batches = [training_data[k:k + mini_batch_size]
for k in range(0, n, mini_batch_size)]
for mini_batch in mini_batches:
self.update_mini_batch(mini_batch, eta)
if test_data:
print("Epoch {0}: {1} / {2}".format(j, self.evaluate(test_data), n_test))
else:
print("Epoch {0} complete".format(j))
def update_mini_batch(self, mini_batch, eta):
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
for x, y in mini_batch:
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
nabla_b = [nb + dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw + dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
self.weights = [w - (eta / len(mini_batch)) * nw
for w, nw in zip(self.weights, nabla_w)]
self.biases = [b - (eta / len(mini_batch)) * nb
for b, nb in zip(self.biases, nabla_b)]
def backprop(self, x, y):
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
activation = x
activations = [x]
zs = []
for b, w in zip(self.biases, self.weights):
z = np.dot(w, activation) + b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
delta = self.cost_derivative(activations[-1], y) * sigmoid_prime(zs[-1])
nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta, activations[-2].transpose())
for l in range(2, self.num_layers):
z = zs[-l]
sp = sigmoid_prime(z)
delta = np.dot(self.weights[-l + 1].transpose(), delta) * sp
nabla_b[-l] = delta
nabla_w[-l] = np.dot(delta, activations[-l - 1].transpose())
return (nabla_b, nabla_w)
def evaluate(self, test_data):
test_results = [(np.argmax(self.feedforward(x)), y) for (x, y) in test_data]
return sum(int(x == y) for (x, y) in test_results)
def cost_derivative(self, output_activations, y):
return (output_activations - y)
def sigmoid(z):
return 1.0 / (1.0 + np.exp(-z))
def sigmoid_prime(z):
return sigmoid(z) * (1 - sigmoid(z))
def vectorized_result(j):
e = np.zeros((10, 1))
e[j] = 1.0
return e
def load_data_wrapper():
tr_d, va_d, te_d = load_data()
training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]
training_results = [vectorized_result(y) for y in tr_d[1]]
training_data = zip(training_inputs, training_results)
validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]
validation_data = zip(validation_inputs, va_d[1])
test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]
test_data = zip(test_inputs, te_d[1])
return (training_data, validation_data, test_data)
if __name__ == '__main__':
training_data, valivation_data, test_data = load_data_wrapper()
net = Network([784, 30, 10])
net.SGD(training_data, 10, 10, 3.0, test_data=test_data)
showimage()
实验结果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)