-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_nn_matrix
51 lines (33 loc) · 1.08 KB
/
test_nn_matrix
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
import nn_matrix as mx
from utility import print_histograms
import math
import numpy as np
import random
xs = np.array(([3,5], [5,1], [10,2]), dtype=float)
ys = np.array(([75], [82], [93]), dtype=float)
xs = xs / np.amax(xs, axis=0)
ys = ys / 100 #Max test score is 100
nn_mx = mx.Neural_Network(2, 3, 1)
y_pred = nn_mx.forward(xs)
print_histograms(ys, y_pred)
delta = 0.0000001
learning_rate = 3
max_iterations = 1000000
prev_cost = nn_mx.cost(xs, ys)
cost = np.finfo(float).max
iteration = 0 # счетчик итераций
while iteration < max_iterations and abs(cost - prev_cost) > delta:
dJdW1, dJdW2, dJB1, dJB2 = nn_mx.backward(xs, ys)
nn_mx.W1 -= learning_rate * dJdW1
nn_mx.W2 -= learning_rate * dJdW2
nn_mx.B1 -= learning_rate * dJB1
nn_mx.B2 -= learning_rate * dJB2
prev_cost = cost
cost = nn_mx.cost(xs, ys)
iteration += 1
print("it : ", iteration, ' / ', max_iterations, ' -> ', cost)
x_new = [3 / 10, 5 / 5]
pred = nn_mx.forward(x_new)
print('pred : ', x_new, ' : ', pred)
y_pred = nn_mx.forward(xs)
print_histograms(ys, y_pred)