-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
148 lines (122 loc) · 4.55 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from typing import List, Tuple
import scipy.ndimage.interpolation
from neural_network import NeuralNetwork
import numpy as np
import matplotlib.pyplot as plot
import imageio
import os
import time
def load_trains_data(filename: str, max_line: int = 1000):
# 训练数据
_trains_data: List[List[float]] = []
# 期望数据
_targets_list: List[Tuple[int, List[float]]] = []
data_list = []
with open(filename) as f:
i = 0
while True and i < max_line:
line = f.readline()
if line == "":
break
data_list.append(line)
i += 1
for data in data_list:
if data.strip() == "":
continue
all_values = data.split(",")
# 读入期望数据
target = [0.01 for _ in range(0, 10)]
target[int(all_values[0])] = 0.99
_targets_list.append((int(all_values[0]), target))
# 读入训练数据
_trains_data.append([float(x) for x in all_values[1:]])
return _trains_data, _targets_list
def fix_data(data: List[float]):
return [x / 255.0 * 0.99 + 0.01 for x in data]
def show(_trains_data: List[float], _targets_list: List[float], result: np.ndarray):
i = 0
for i in range(0, 10):
if _targets_list[i] == 0.99:
break
print("ans: ", i)
i = 0
for j in range(0, 10):
if result[j] > result[i]:
i = j
print("predict: ", i)
plot.imshow(np.asfarray(_trains_data).reshape(28, 28), cmap="Greys")
plot.show()
def test(n: NeuralNetwork):
print("reading testing data...")
test_data, test_target = load_trains_data("./trains_data/mnist_test.csv", 10000)
print("start querying...")
score = 0
for i in range(0, len(test_data)):
result = n.query(test_data[i])[1]
recognize = np.argmax(result)
if int(recognize) == test_target[i][0]:
score += 1
print("total: ", len(test_data), ", correct: ", score, ", 正确率: ", score / len(test_data))
def test_img(n: NeuralNetwork, img_path: str) -> None:
img_arr = imageio.imread(img_path, as_gray=True)
img_data = 255.0 - img_arr.reshape(784)
result = n.query(fix_data(img_data))[1]
print("recognize: ", np.argmax(result))
plot.imshow(img_data.reshape(28, 28), cmap="Greys")
plot.show()
def train_nn(n: NeuralNetwork):
t = time.perf_counter()
print("start reading...")
trains_data, targets_list = load_trains_data("./trains_data/mnist_train.csv", 5000)
print("start learning...")
epochs = 4
for e in range(epochs):
print("start generation ", e)
for i in range(0, len(trains_data)):
n.train(fix_data(trains_data[i]), targets_list[i][1])
print(f'training coast: {time.perf_counter() - t:.8f}s')
def supper_train(n: NeuralNetwork):
t = time.perf_counter()
print("start reading...")
trains_data, targets_list = load_trains_data("./trains_data/mnist_train.csv", 5000)
print("start learning...")
epochs = 4
for e in range(epochs):
print("start generation ", e)
for i in range(0, len(trains_data)):
fixed = fix_data(trains_data[i])
plus10_img = scipy.ndimage.interpolation.rotate(np.array(fixed).reshape(28, 28), 10,
cval=0.01, reshape=False)
minus10_img = scipy.ndimage.interpolation.rotate(np.array(fixed).reshape(28, 28), -10,
cval=0.01, reshape=False)
n.train(fixed, targets_list[i][1])
n.train(plus10_img.reshape(784), targets_list[i][1])
n.train(minus10_img.reshape(784), targets_list[i][1])
print(f'training coast: {time.perf_counter() - t:.8f}s')
if __name__ == '__main__':
# 输入、隐藏和输出结点的数量
input_nodes = 28 * 28
hidden_nodes = 100
output_nodes = 10
# 学习率
learning_rate = 0.2
# 神经学习引擎
n = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)
# 训练模型
path = "./trains_data/module"
if os.path.exists(path + ".npz"):
print("load from file?")
if input().strip().lower() == 'y':
n.load_from_file(path + ".npz")
else:
os.remove(path + ".npz")
train_nn(n)
n.save_to_file(path)
else:
train_nn(n)
n.save_to_file(path)
print("start testing...")
test(n)
scipy.ndimage.interpolation.rotate()
# test_img(n, "./trains_data/8.png")
# See PyCharm help at https://www.jetbrains.com/help/pycharm/