import scipy.special import numpy import matplotlib.pyplot as plt import pickle
class neuralNetwork:
def __init__(self, inputnodes: int, hiddennodes: int, outputnodes: int, learningrate: int): self.inodes = inputnodes self.hnodes = hiddennodes self.onodes = outputnodes self.lr = learningrate
self.w_ih = numpy.random.normal(scale=pow(self.hnodes, -0.5), size=(self.hnodes, self.inodes)) self.w_ho = numpy.random.normal(scale=pow(self.onodes, -0.5), size=(self.onodes, self.hnodes)) self.active_fuc = lambda x: scipy.special.expit(x) self.re_active_fuc = lambda x: scipy.special.logit(x)
def train(self, input_list, target_list): inputs = numpy.array(input_list, ndmin=2).T targets = numpy.array(target_list, ndmin=2).T
hidden_inputs = numpy.dot(self.w_ih, inputs) hidden_outputs = self.active_fuc(hidden_inputs) final_inputs = numpy.dot(self.w_ho, hidden_outputs) final_outputs = self.active_fuc(final_inputs)
output_errors = targets-final_outputs hidden_errors = numpy.dot(self.w_ho.T, output_errors)
self.w_ho += self.lr * \ numpy.dot((output_errors*final_outputs*(1.0-final_outputs)), numpy.transpose(hidden_outputs))
self.w_ih += self.lr * \ numpy.dot((hidden_errors*hidden_outputs*(1.0-hidden_outputs)), numpy.transpose(inputs)) pass
def query(self, input_list: numpy.ndarray)->numpy.ndarray: inputs = numpy.array(input_list, ndmin=2).T hidden_input = numpy.dot(self.w_ih, inputs) hidden_output = self.active_fuc(hidden_input)
final_input = numpy.dot(self.w_ho, hidden_output) final_output = self.active_fuc(final_input)
return final_output
def backquery(self, targets_list: list)->numpy.ndarray: final_outputs = numpy.array(targets_list, ndmin=2).T
final_inputs = self.re_active_fuc(final_outputs)
hidden_outputs = numpy.dot(self.w_ho.T, final_inputs) hidden_outputs -= numpy.min(hidden_outputs) hidden_outputs /= numpy.max(hidden_outputs) hidden_outputs *= 0.98 hidden_outputs += 0.01
hidden_inputs = self.re_active_fuc(hidden_outputs)
inputs = numpy.dot(self.w_ih.T, hidden_inputs) inputs -= numpy.min(inputs) inputs /= numpy.max(inputs) inputs *= 0.98 inputs += 0.01
return inputs
if __name__ == "__main__": input_nodes = 784 hidden_nodes = 100 output_nodes = 10
learn_rate = 0.3
n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learn_rate)
savefile = open('/home/zqh/Program/Python_study/NN/nnmodel', 'rb') n.w_ih = pickle.load(savefile) n.w_ho = pickle.load(savefile) savefile.close() test_file = open("./mnist_test.csv") test_data = test_file.readlines() test_file.close() scorecard = [] for record in test_data: test_value = record.strip().split(',') inputs = (numpy.asfarray(test_value[1:]) / 255.0 * 0.99) + 0.01 outputs = n.query(inputs) label = numpy.argmax(outputs) if int(test_value[0]) == label: scorecard.append(1) else: scorecard.append(0)
print("准确率 = {:.2f}%".format( sum(scorecard) / len(scorecard)*100.0)) print('反向查询:') image_array = n.backquery( [0.01, 0.01, 0.99, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01]) plt.imshow(image_array.reshape(28, 28), cmap='Greys') plt.show()
|