-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
54 lines (39 loc) · 2.05 KB
/
predict.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
import numpy as np
import matplotlib.pyplot as plt
from my_sklearn.datasets import load_digits
import random
digits = load_digits() # загружаем данные
X = digits.data
y = digits.target
weight_0_1 = np.fromfile('weight_0_1.b', float).reshape(20, 64) # загружаем полученные веса после обучения
weight_1_2 = np.fromfile('weight_1_2.b', float).reshape(10, 20)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
sigmoid_mapper = np.vectorize(sigmoid)
def predict(inputs):
inputs_1 = np.dot(weight_0_1, inputs)
outputs_1 = sigmoid_mapper(inputs_1)
inputs_2 = np.dot(weight_1_2, outputs_1)
outputs_2 = sigmoid_mapper(inputs_2)
return outputs_2
def main():
TEST_QUANTITY = 2 # количество необходимых проверок
for _ in range(TEST_QUANTITY):
digit_index = random.randint(1500, 1797) # выбираем изображение для предсказания
digit = X[digit_index]
g = 0
for _ in range(8): # изображение в виде матрицы чисел
print(digit[g:g+8]) # каждому числу соответствует пиксель в оттенках серого цвета(0-белый цвет, 15-черный)
g += 8
print('\nCorrect digit: "{}"\n'.format(y[digit_index])) # правильное значение выбранной цифры
one_percent = sum(predict(digit).tolist()) / 100
sort_pred = predict(digit).tolist()
sort_pred.sort()
for j in range(3): # вывод предсказания нейронной сети. % - предпочтения нейронной сети
print('Predict digit: "{}" - {}% '.format(predict(digit).tolist().index(sort_pred[-1-j]), sort_pred[-1-j] / one_percent))
plt.figure(figsize=(5, 5)) # вывод изображения
plt.imshow(np.reshape(digit, (8, 8)), interpolation='nearest')
plt.set_cmap('binary')
plt.show()
if __name__ == '__main__':
main()