Skip to content

Instantly share code, notes, and snippets.

@BriFuture
Last active February 28, 2019 04:17
Show Gist options
  • Save BriFuture/d17e0316c27086d9025cff58d0f6c0ea to your computer and use it in GitHub Desktop.
Save BriFuture/d17e0316c27086d9025cff58d0f6c0ea to your computer and use it in GitHub Desktop.
"""by BriFuture
根据 http://neuralnetworksanddeeplearning.com/chap1.html 中的内容学习编写 Network,为了理解 Network,
自己根据文中的内容编写 Network 的代码,而不是简单的抄写文中的示例代码
"""
import numpy as np
def sigmiod(z):
"""z: [0, 1]
"""
return 1 / (1 + np.exp(-z) )
class Network(object):
def __init__(self):
# 输入层,隐藏层,输出层的神经元个数
self.layers = [784, 30, 10]
# 每两层单元之间的权重构成一个子数组
# np.random.randn(x, y) 创建 x 行,y 列数组
# self.raw_weight_initialize()
self.weight_initialize()
def raw_weight_initialize(self):
"""拆分解析的权重和偏移量初始化过程
"""
layer_len = len(self.layers)
# self.weights 是一个三维数组, len(self.weights) 为隐藏层的层数 +1,
# 假设隐藏层第一层序号为 0,后面层数序号依次加 1,输出层序号为 n,则 len(self.weights[i]) 为序号为 i 层的神经元个数
self.weights = []
for i in range(layer_len-1):
# 该数组内,前一层某个神经元连接到后一层神经元的所有权重又为一个子数组
# weights 是一个二维数组,行数为后一层的神经元个数,列数为前一层的神经元个数
weights = []
for j in range(self.layers[i+1]):
weight = []
for x in range(self.layers[i]):
# 随机权重
weight.append(np.random.rand())
weights.append(weight)
self.weights.append(weights)
# 执行完毕,self.weights 成为一个三维数组
self.biases = []
# 从隐藏层的第一层单元开始,
for i in range(1, layer_len):
# biases 的维度列数为 1,行数与该层神经元个数一致
biases = []
for j in range(self.layers[i]):
# 保持和 self.weights 的维数相同
biases.append( [np.random.rand()] )
self.biases.append(biases)
def weight_initialize(self):
layer_len = len(self.layers)
# self.weights = []
# for i in range(1, layer_len):
# # 子数组的行数为后一层的神经元个数,列数为前一层的神经元个数
# self.weights.append(np.random.randn(self.layers[i], self.layers[i-1]))
# 更简化的写法, l for later, f for forward
self.weights = [np.random.randn(l, f) for l, f in zip(self.layers[1:], self.layers[:-1])]
self.biases = [np.random.randn(l, 1) for l in self.layers[1:]]
def main():
import mnist_loader
training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
n = Network()
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
# interpreter: python3.x
""" by BriFuture
Custom mnist data loader for better understanding how to load and use mnist data.
Actually you dont need to write a loader yourself, you could facilize keras which is much more
covenient to use to load mnist data.
see more:
https://blog.csdn.net/simple_the_best/article/details/75267863 详解 MNIST 数据集
http://yann.lecun.com/exdb/mnist/
or
https://github.com/MichalDanielDobrzanski/DeepLearningPython35/blob/master/mnist_loader.py
last one is a little different with following codes because it reads from a pickle file
"""
from os import path
import struct
current_dir = path.abspath( path.dirname(__file__) )
# change the folder path to yours
# this script only support mnist or fashion-mnist
mnist_folder = path.abspath(path.join(path.expanduser("~"), '.keras', 'datasets', 'mnist'))
# mnist_folder = path.abspath(path.join(path.expanduser("~"), '.keras', 'datasets', 'fashion-mnist'))
def vectorized_result(j):
e = np.zeros((10, 1))
e[j] = 1.0
return e
import numpy as np
def load_data(train = True):
if train:
path_images = path.join(mnist_folder, 'train-images-idx3-ubyte')
path_labels = path.join(mnist_folder, 'train-labels-idx1-ubyte')
else:
path_images = path.join(mnist_folder, 't10k-images-idx3-ubyte')
path_labels = path.join(mnist_folder, 't10k-labels-idx1-ubyte')
# f = gzip.open(path_train_images, 'rb')
with open(path_labels, 'rb') as pl:
magic, n = struct.unpack('>II', pl.read(8))
labels = np.fromfile(pl, dtype = np.uint8)
with open(path_images, 'rb') as pi:
magic, num, rows, cols = struct.unpack('>IIII', pi.read(16))
images = np.fromfile(pi, dtype = np.uint8)
images = images.reshape(len(labels), 784) / 255
return images, labels
def load_all_data():
_training_data = load_data()
training_data = (_training_data[0][:-10000], _training_data[1][:-10000])
validation_data = (_training_data[0][-10000:], _training_data[1][-10000:])
test_data = load_data(train=False)
return training_data, validation_data, test_data
def load_data_wrapper():
"""image greyscale is between [0.0, 1.0]
custom modified version of data loader, it will load data from `~/.keras/datasets/mnist`
"""
tr_d, va_d, te_d = load_all_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 = list(zip(training_inputs, training_results))
validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]
validation_data = list(zip(validation_inputs, va_d[1]))
test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]
test_data = list(zip(test_inputs, te_d[1]))
return (training_data, validation_data, test_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment