-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpuTest.py
106 lines (80 loc) · 2.52 KB
/
cpuTest.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
import os
import numpy as np
import random as rnd
from time import perf_counter as pc
def teach_analyzers(rates, sensors, numTrue, activate_level):
values = np.sum(rates * sensors, axis=1) >= activate_level
for i, v in enumerate(values):
if numTrue == i and not v: # не узнал своего
rates[i] += sensors
elif numTrue != i and v: # признал чужого
rates[i] -= sensors
return values * 1, rates
neurons_count = 10
centres_count = 10
sensors_count = 15
learn_count = 100000
activate_level = 33
sensors = np.zeros(sensors_count)
rates = []
if os.path.exists('rates.txt'):
f = open('rates.txt')
s = f.readline()
while s:
rates.append(list(map(float, s.split())))
s = f.readline()
f.close()
if not (len(rates) == neurons_count and len(rates[0]) == sensors_count):
rates = list()
for i in range(neurons_count):
ent = np.zeros(sensors_count)
rates.append(ent)
rates = np.array(rates)
center_decision = []
if os.path.exists('center_decision.txt'):
f = open('center_decision.txt')
s = f.readline()
while s:
center_decision.append(list(map(float, s.split())))
s = f.readline()
f.close()
if not (len(center_decision) == centres_count and len(center_decision[0]) == neurons_count):
center_decision = list()
for i in range(centres_count):
ent = np.zeros(neurons_count)
center_decision.append(ent)
center_decision = np.array(center_decision)
print(*rates, sep='\n')
print("--")
print(*center_decision, sep='\n')
# mx elem 109647424 * 2 + 2
f = open("input.txt") # Считываем тесты
s = f.readline()
tests = []
while s:
line = list(s.split())
isTrue = int(line[0])
test = list(map(int, line[1:]))
tests.append([isTrue, test])
s = f.readline()
f.close()
test_count = len(tests)
t = pc()
for learn_pos in range(learn_count): # Обучение
test_num = rnd.randint(0, test_count - 1)
sensors = np.array(tests[test_num][1])
numTrue = tests[test_num][0]
output, rates = teach_analyzers(rates, sensors, numTrue, activate_level)
result, center_decision = teach_analyzers(center_decision, output, numTrue, activate_level)
print(pc() - t)
f = open('rates.txt', 'w')
print(*rates, sep='\n')
for rate in rates:
f.write(' '.join(map(str, rate)) + '\n')
f.close()
print("--")
f = open('center_decision.txt', 'w')
print(*center_decision, sep='\n')
for rate in center_decision:
f.write(' '.join(map(str, rate)) + '\n')
f.close()