深度学习(一):Python神经网络——手写数字识别

声明:本文章为阅读书籍《Python神经网络编程》而来,代码与书中略有差异,书籍封面:

深度学习(一):Python神经网络——手写数字识别

源码

若要本地运行,请更改源码中图片与数据集的位置,环境为 Python3.6x.

import numpy as np
import scipy.special as ss
import matplotlib.pyplot as plt
import imageio as im
import glob as gl


class NeuralNetwork:
    # initialise the network
    def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
        # set number of each layer
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes
        self.wih = np.random.normal(0.0, pow(self.inodes, -0.5), (self.hnodes, self.inodes))
        self.who = np.random.normal(0.0, pow(self.hnodes, -0.5), (self.onodes, self.hnodes))
        # learning rate
        self.lr = learningrate
        # activation function is sigmoid
        self.activation_function = lambda x: ss.expit(x)
        pass

    # train the neural network
    def train(self, inputs_list, targets_list):
        inputs = np.array(inputs_list, ndmin=2).T
        targets = np.array(targets_list, ndmin=2).T
        hidden_inputs = np.dot(self.wih, inputs)
        hidden_outputs = self.activation_function(hidden_inputs)
        final_inputs = np.dot(self.who, hidden_outputs)
        final_outputs = self.activation_function(final_inputs)
        # errors
        output_errors = targets - final_outputs
        # b-p algorithm
        hidden_errors = np.dot(self.who.T, output_errors)
        # update weight
        self.who += self.lr * np.dot((output_errors * final_outputs * (1.0 - final_outputs)),
                                     np.transpose(hidden_outputs))
        self.wih += self.lr * np.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), np.transpose(inputs))
        pass

    # query the neural network
    def query(self, inputs_list):
        inputs = np.array(inputs_list, ndmin=2).T
        hidden_inputs = np.dot(self.wih, inputs)
        hidden_outputs = self.activation_function(hidden_inputs)
        final_inputs = np.dot(self.who, hidden_outputs)
        final_outputs = self.activation_function(final_inputs)
        return final_outputs

    # numbers


input_nodes = 784
hidden_nodes = 100
output_nodes = 10

# learning rate
learning_rate = 0.2

# creat instance of neural network
global n
n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)

# file read only ,root of the file
training_data_file = open(r"C:\Users\ELIO\Desktop\mnist_train.txt", ‘r‘)
training_data_list = training_data_file.readlines()
training_data_file.close()

# train the neural network
epochs = 5
for e in range(epochs):
    for record in training_data_list:
        all_values = record.split(‘,‘)
        # scale and shift the inputs
        inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
        targets = np.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 file into a list
test_data_file = open(r"C:\Users\ELIO\Desktop\mnist_train_100.csv.txt", ‘r‘)
test_data_list = test_data_file.readlines()
test_data_file.close()

# test the neural network
# score for how well the network performs
score = []

# go through all the records
for record in test_data_list:
    all_values = record.split(‘,‘)
    # correct answer is the first value
    correct_label = int(all_values[0])
    # scale and shift the inputs
    inputs = (np.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 = np.argmax(outputs)
    # append correct or incorrect to list
    if (label == correct_label):
        score.append(1)
    else:
        score.append(0)
        pass
pass
# module1 CORRECT-RATE
# calculate the score, the fraction of correct answers
score_array = np.asarray(score)
print("performance = ", score_array.sum() / score_array.size)

# module2 TEST MNIST
all_values = test_data_list[0].split(‘,‘)
print(all_values[0])
image_array = np.asfarray(all_values[1:]).reshape((28, 28))
plt.imshow(image_array, cmap=‘Greys‘, interpolation=‘None‘)
plt.show()

# module3 USE YOUR WRITING
# own image test data set
own_dataset = []
for image_file_name in gl.gl(r‘C:\Users\ELIO\Desktop\5.png‘):
    print("loading ... ", image_file_name)
    # use the filename to set the label
    label = int(image_file_name[-5:-4])
    # load image data from png files into an array
    img_array = im.imread(image_file_name, as_gray=True)
    # reshape from 28x28 to list of 784 values, invert values
    img_data = 255.0 - img_array.reshape(784)
    # then scale data to range from 0.01 to 1.0
    img_data = (img_data / 255.0 * 0.99) + 0.01
    print(np.min(img_data))
    print(np.max(img_data))
    # append label and image data  to test data set
    record = np.append(label, img_data)
    print(record)
    own_dataset.append(record)
    pass
all_values = own_dataset[0]
print(all_values[0])

 数据集,实验图片

链接:百度网盘 
提取码:1vbq

相关推荐