-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetwork.py
65 lines (49 loc) · 2.07 KB
/
NeuralNetwork.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
import numpy as np
import scipy.special
class NeuralNetwork:
def __init__(self, input_node, hidden_node, output_node, initial_wih, initial_who, learn_rate):
self.i_nodes = input_node
self.h_nodes = hidden_node
self.o_nodes = output_node
self.wih = initial_wih
self.who = initial_who
self.lr = learn_rate
self.sigmoid = lambda x: scipy.special.expit(x)
self.inputs = []
self.hidden_outputs = []
self.final_outputs = []
def train(self, has_won):
who_update = np.zeros_like(self.who)
wih_update = np.zeros_like(self.wih)
for k in range(len(self.inputs)):
move = np.argmax(self.final_outputs[k])
target_array = 0.5 * np.ones((self.o_nodes, 1))
target_array[move] = 0.99 if has_won else 0.01
output_errors = target_array - np.array(self.final_outputs[k])
hidden_errors = np.dot(self.who.T, output_errors)
who_update += (output_errors * self.final_outputs[k] *
(1.0 - self.final_outputs[k])) * np.transpose(self.hidden_outputs[k])
wih_update += np.dot((hidden_errors * self.hidden_outputs[k] * (1.0 - self.hidden_outputs[k])),
np.transpose(self.inputs[k]))
self.who += self.lr * who_update
self.wih += self.lr * wih_update
pass
# get AI decision
def query(self, input_list):
input_list = np.array(input_list, ndmin=2).T
self.inputs.append(input_list)
hidden_inputs = np.dot(self.wih, input_list)
hidden_outputs = self.sigmoid(hidden_inputs)
self.hidden_outputs.append(hidden_outputs)
final_inputs = np.dot(self.who, hidden_outputs)
final_outputs = self.sigmoid(final_inputs)
self.final_outputs.append(final_outputs)
return final_outputs
def return_wih(self):
return self.wih
def return_who(self):
return self.who
def reset(self):
self.inputs = []
self.hidden_outputs = []
self.final_outputs = []