《Python神经网络编程》——用Python制作神经网络

本文介绍了一个使用Python实现的三层数字神经网络,通过训练MNIST数据集进行手写数字识别。文章详细展示了神经网络的初始化、训练和查询过程,并使用了sigmoid激活函数。通过对权重的调整,网络在多次迭代后能有效提高识别准确率。

以下代码来自于《Python神经网络编程》

mnist_train.csv和mnist_test.csv来自于https://pjreddie.com/projects/mnist-in-csv/


# python notebook for Make Your Own Neural Network
# code for a 3-layer neural network, and code for learning the MNIST dataset
# (c) Tariq Rashid, 2016# license is GPLv2
import numpy
# scipy.special for the sigmoid function expit()
import scipy.special
# library for plotting arrays
import matplotlib.pyplot
# ensure the plots are inside this notebook, not an external window
%matplotlib inline
# neural network class definition
class neuralNetwork :
# initialise the neural network
	def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate) :
# set number of nodes in each input, hidden, output layer
		self.inodes = inputnodes
		self.hnodes = hiddennodes
		self.onodes = outputnodes
# link weight matrices, wih and who
# weights inside the arrays are w_i_j, where link is from node i to node j in the next layer
# w11 w21
# w12 w22 etc
		self.wih = numpy.random.normal(0.0, pow(self.hnodes, -0.5),(self.hnodes, self.inodes))
		self.who = numpy.random.normal(0.0, pow(self.onodes, -0.5),(self.onodes, self.hnodes))
# learning rate
		self.lr = learningrate# activation function is the sigmoid function
		self.activation_function = lambda x: scipy.special.expit(x)
		pass
# train the neural network
	def train(self, inputs_list, targets_list) :
# convert inputs list to 2d array
		inputs = numpy.array(inputs_list, ndmin=2).T
		targets = numpy.array(targets_list, ndmin=2).T
# calculate signals into hidden layer
		hidden_inputs = numpy.dot(self.wih, inputs)
# calculate the signals emerging from hidden layer
		hidden_outputs = self.activation_function(hidden_inputs)
# calculate signals into final output layer
		final_inputs = numpy.dot(self.who, hidden_outputs)
# calculate the signals emerging from final output layer
		final_outputs = self.activation_function(final_inputs)
# output layer error is the (target - actual)
		output_errors = targets - final_outputs
# hidden layer error is the output_errors, split by weights, recombined at hidden nodes
		hidden_errors = numpy.dot(self.who.T, output_errors)
# update the weights for the links between the hidden and output layers
		self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs))
# update the weights for the links between the input and hidden layers
		self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), numpy.transpose(inputs))
		pass
# query the neural network
	def query(self, inputs_list) :
# convert inputs list to 2d array
		inputs = numpy.array(inputs_list, ndmin=2).T
# calculate signals into hidden layer
		hidden_inputs = numpy.dot(self.wih, inputs)
# calculate the signals emerging from hidden layer
		hidden_outputs = self.activation_function(hidden_inputs)
# calculate signals into final output layer
		final_inputs = numpy.dot(self.who, hidden_outputs)
# calculate the signals emerging from final output layer
		final_outputs = self.activation_function(final_inputs)
		return final_outputs
# number of input, hidden and output nodes
input_nodes = 784
hidden_nodes = 200
output_nodes = 10
# learning rate
learning_rate = 0.1
# create instance of neural network
n = neuralNetwork(input_nodes,hidden_nodes,output_nodes, learning_rate)
# load the mnist training data CSV file into a list
training_data_file = open("F:/Book_Learning/Python神经网络编程/mnist_dataset/mnist_train.csv", 'r')
training_data_list = training_data_file.readlines()
training_data_file.close()
# train the neural network
# epochs is the number of times the training data set is used for training
epochs = 5
for e in range(epochs):# go through all records in the training data set
	for record in training_data_list:
# split the record by the ',' commas
		all_values = record.split(',')
# scale and shift the inputs
		inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
# create the target output values (all 0.01, except the desired label which is 0.99)
		targets = numpy.zeros(output_nodes) + 0.01
# all_values[0] is the target label for this record
		targets[int(all_values[0])] = 0.99
		n.train(inputs, targets)
		pass
	pass
# load the mnist test data CSV file into a list
test_data_file = open("F:/Book_Learning/Python神经网络编程/mnist_dataset/mnist_test.csv", 'r')
test_data_list = test_data_file.readlines()
test_data_file.close()
# test the neural network
# scorecard for how well the network performs, initially empty
scorecard = []
# go through all the records in the test data set
for record in test_data_list:
# split the record by the ',' commas
	all_values = record.split(',')
# correct answer is first value
	correct_label = int(all_values[0])
# scale and shift the inputs
	inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
# query the network
	outputs = n.query(inputs)
# the index of the highest value corresponds to the label
	label = numpy.argmax(outputs)
# append correct or incorrect to list
	if (label == correct_label):
# network's answer matches correct answer, add 1 to scorecard
		scorecard.append(1)
	else:
# network's answer doesn't match correct answer, add 0 toscorecard
		scorecard.append(0)
		pass
	pass
# calculate the performance score, the fraction of correct answers
scorecard_array = numpy.asarray(scorecard)
print ("performance = ", scorecard_array.sum() / scorecard_array.size)
LazyProgrammer, "Convolutional Neural Networks in Python: Master Data Science and Machine Learning with Modern Deep Learning in Python, Theano, and TensorFlow" 2016 | ASIN: B01FQDREOK | 52 pages | EPUB | 1 MB This is the 3rd part in my Data Science and Machine Learning series on Deep Learning in Python. At this point, you already know a lot about neural networks and deep learning, including not just the basics like backpropagation, but how to improve it using modern techniques like momentum and adaptive learning rates. You've already written deep neural networks in Theano and TensorFlow, and you know how to run code using the GPU. This book is all about how to use deep learning for computer vision using convolutional neural networks. These are the state of the art when it comes to image classification and they beat vanilla deep networks at tasks like MNIST. In this course we are going to up the ante and look at the StreetView House Number (SVHN) dataset - which uses larger color images at various angles - so things are going to get tougher both computationally and in terms of the difficulty of the classification task. But we will show that convolutional neural networks, or CNNs, are capable of handling the challenge! Because convolution is such a central part of this type of neural network, we are going to go in-depth on this topic. It has more applications than you might imagine, such as modeling artificial organs like the pancreas and the heart. I'm going to show you how to build convolutional filters that can be applied to audio, like the echo effect, and I'm going to show you how to build filters for image effects, like the Gaussian blur and edge detection. After describing the architecture of a convolutional neural network, we will jump straight into code, and I will show you how to extend the deep neural networks we built last time with just a few new functions to turn them into CNNs. We will then test their performance and show how convolutional neu
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值