This repository was archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
117 lines (102 loc) · 5.3 KB
/
test.py
File metadata and controls
117 lines (102 loc) · 5.3 KB
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
from task import NeuralNetwork
from sys import exc_info
from traceback import extract_tb
calculate_msg = 'Что-то не так с вычислением вывода нейронной сети (функция calculate). Убедитесь, что: веса ' \
'последнего слоя выставляются правильно, вы используете функцию активации, вы правильно вычисляете ' \
'значения ВСЕХ нейронов, вы не забываете умножать на веса и не перепутали индексы нейронов. '
eps = 1e-3
# Test cases to test NN's methods
def test_set_weights1():
try:
network = NeuralNetwork([5, 3, 3])
output = ([[2, 1, 3], [2, 1, 2], [3, 4, 5]])
assert network.set_weights(2, [2, 1, 3, 2, 1, 2, 3, 4, 5]) == output
except Exception as e:
print('Тест #1: Что-то не так с установкой весов во третьем слое. Проверьте функцию set_weights.')
if not (e.__class__ is AssertionError):
print('Причина:', e.args[0], 'в строке номер', str(extract_tb(exc_info()[2])[1][1]) + '.\n')
else:
print('Тест #1 пройден!\n')
def test_set_weights2():
output = ([[0.546, -4, 355678875865], [4, 678, 5], [345.76867, 6, 7], [8, -325.6, 6456.7], [4, 7, 87]])
try:
network = NeuralNetwork([5, 3, 3])
assert network.set_weights(1,
[0.546, -4, 355678875865, 4, 678, 5, 345.76867, 6, 7, 8, -325.6, 6456.7, 4,
7, 87]) == output
except Exception as e:
print('Тест #2: Что-то не так с установкой весов во третьем слое. Проверьте функцию set_weights.')
if not (e.__class__ is AssertionError):
print('Причина:', e.args[0], 'в строке номер', str(extract_tb(exc_info()[2])[1][1]) + '.\n')
else:
print('Тест #2 пройден!\n')
def test_calculate0():
try:
network = NeuralNetwork([5, 3, 3])
output = network.calculate([6, -3, 4.6, -5.7, 0])
assert output == [0, 0, 0]
except Exception as e:
print('Тест #3: Что-то не так с вычислением вывода нейронной сети (функция calculate). Убедитесь, '
'что все веса изначально равны 0.')
if not (e.__class__ is AssertionError):
print('Причина:', e.args[0], 'в строке номер', str(extract_tb(exc_info()[2])[1][1]) + '.\n')
else:
print('Тест #3 пройден!\n')
def test_calculate1():
try:
network = NeuralNetwork([5, 3, 3])
network.set_weights(2, [2, 1, 3, 2, 1, 2, 3, 4, 5])
network.set_biases(1, [3, -2, 5])
output = network.calculate([6, 3, 4.6, 5.7, 0])
assert abs(output[0] - 0.727) <= eps
assert abs(output[1] - 0.773) <= eps
assert abs(output[2] - 0.835) <= eps
except Exception as e:
print('Тест #4:', calculate_msg)
if not (e.__class__ is AssertionError):
print('Причина:', e.args[0], 'в строке номер', str(extract_tb(exc_info()[2])[1][1]) + '.\n')
else:
print('Тест #4 пройден!\n')
def test_calculate2():
try:
network = NeuralNetwork([5, 3, 3])
network.set_weights(1,
[0.546, -4, 355678875865, 4, 678, 5, 345.76867, 6, 7, 8, -325.6, 6456.7, 4, 7, 87,
56, 34, 67., 34., 908])
network.set_weights(2, [2, 1, 3, 2, 1, 2, 3, 4, 5, 3, -2, 5])
output = network.calculate([6, 3, 4.6, 5.7, 0])
assert abs(output[0] - 0.874) <= eps
assert abs(output[1] - 0.857) <= eps
assert abs(output[2] - 0.908) <= eps
except Exception as e:
print('Тест #5:', calculate_msg)
if not (e.__class__ is AssertionError):
print('Причина:', e.args[0], 'в строке номер', str(extract_tb(exc_info()[2])[1][1]) + '.\n')
else:
print('Тест #5 пройден!\n')
def test_calculate3():
try:
network = NeuralNetwork([5, 3, 3])
network.set_weights(1,
[0.546, -4, 355678875865, 4, 678, 5, 345.76867, 6, 7, 8, -325.6, 6456.7, 4, 7, 87])
network.set_biases(0, [56, 34, 67., 34., 908])
network.set_weights(2, [8, 9.96, -3, 0.062, -0.7, 82, 63, -4, -58])
network.set_biases(1, [0.3, -2, 51])
output = network.calculate([6, 3, 4.6, 5.7, 0])
assert abs(output[0] - 0.986) <= eps
assert abs(output[1] - 0.840) <= eps
assert abs(output[2] - 0.953) <= eps
except Exception as e:
print('Тест #6:', calculate_msg)
if not (e.__class__ is AssertionError):
print('Причина:', e.args[0], 'в строке номер', str(extract_tb(exc_info()[2])[1][1]) + '.\n')
else:
print('Тест #6 пройден!\n')
# Executing the tests in the above test case class
if __name__ == "__main__":
test_set_weights1()
test_set_weights2()
test_calculate0()
test_calculate1()
test_calculate2()
test_calculate3()