96 lines
3.2 KiB
Python
96 lines
3.2 KiB
Python
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
import numpy.random
|
|
import scipy.special as sc
|
|
|
|
|
|
class NeuralNetwork:
|
|
def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
|
|
self.inodes = input_nodes
|
|
self.hnodes = hidden_nodes
|
|
self.onodes = output_nodes
|
|
|
|
self.lr = learning_rate
|
|
|
|
self.wih = numpy.random.normal(0.0, pow(self.inodes, -0.5), (self.hnodes, self.inodes))
|
|
self.who = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.onodes, self.hnodes))
|
|
|
|
self.activation_func = lambda x: sc.expit(x)
|
|
pass
|
|
|
|
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_func(hidden_inputs)
|
|
|
|
final_inputs = np.dot(self.who, hidden_outputs)
|
|
final_outputs = self.activation_func(final_inputs)
|
|
|
|
output_errors = targets - final_outputs
|
|
hidden_errors = np.dot(self.who.T, output_errors)
|
|
|
|
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
|
|
|
|
def query(self, inputs_list):
|
|
inputs = np.array(inputs_list, ndmin=2).T
|
|
hidden_inputs = np.dot(self.wih, inputs)
|
|
hidden_outputs = self.activation_func(hidden_inputs)
|
|
|
|
final_inputs = np.dot(self.who, hidden_outputs)
|
|
final_outputs = self.activation_func(final_inputs)
|
|
|
|
return final_outputs
|
|
|
|
|
|
if __name__ == "__main__":
|
|
n = NeuralNetwork(784, 100, 10, 0.3)
|
|
storecard = []
|
|
# print(n.query([1.0, 0.5, -1.5]))
|
|
|
|
with open("/home/gemini/PycharmProjects/neural/mnist_train.csv", 'r') as f:
|
|
content = f.readlines()
|
|
# print(content[0])
|
|
# print(len(content))
|
|
|
|
for record in content:
|
|
all_val = record.split(',')
|
|
inputs = (np.asfarray(all_val[1:])/255.0 * 0.99) + 0.01
|
|
targets = np.zeros(n.onodes) + 0.01
|
|
# print(all_val)
|
|
|
|
targets[int(all_val[0])] = 0.99
|
|
# print(targets)
|
|
n.train(inputs, targets)
|
|
|
|
with open("/home/gemini/PycharmProjects/neural/mnist_test.csv", 'r') as f:
|
|
testing = f.readlines()
|
|
|
|
for record in testing:
|
|
all_values = record.split(',')
|
|
correct_answer = int(all_values[0])
|
|
print(correct_answer, "Истинное значение")
|
|
|
|
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
|
|
outputs = n.query(inputs)
|
|
|
|
net_answer = np.argmax(outputs)
|
|
print(net_answer,"Ответ сети")
|
|
|
|
if net_answer == correct_answer:
|
|
storecard.append(1)
|
|
else:
|
|
storecard.append(0)
|
|
pass
|
|
# print(storecard)
|
|
storecard_array = np.asarray(storecard)
|
|
print("Эффективность = ", storecard_array.sum() / storecard_array.size)
|
|
# image_array = np.asfarray(test_val[1:]).reshape((28, 28))
|
|
# plt.imshow(image_array, cmap='Greys', interpolation='None')
|
|
# plt.show()
|
|
|
|
# print(n.query((np.asfarray(test_val[1:])/ 255.0 * 0.99) + 0.01))
|