-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNet.py
53 lines (42 loc) · 1.93 KB
/
Net.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
import random
from typing import *
class Net:
def __init__(self, activation_function: Callable, neuron_count=100):
self.neuron_count = neuron_count
self.activation_f = activation_function
self.weights_hidden = []
self.weights_output = []
def initialize_weights(self, n_inputs, n_outputs, threshold=1):
self.weights_hidden = []
self.weights_output = []
for i in range(self.neuron_count):
n_biased_inputs = n_inputs + 1
neuron_weights = [random.uniform(-threshold, threshold) for _ in range(n_biased_inputs)]
self.weights_hidden.append(neuron_weights)
for i in range(n_outputs):
n_biased_hiddens = self.neuron_count + 1
neuron_weights = [random.uniform(-threshold, threshold) for _ in range(n_biased_hiddens)]
self.weights_output.append(neuron_weights)
def predict(self, input_sample):
hidden_net = self.net_hidden(input_sample)
hidden_layer = [self.activation_f(net_j) for net_j in hidden_net] + [1]
outputs_net = self.net_output(hidden_layer)
outputs = [self.activation_f(net_k) for net_k in outputs_net]
return outputs
def net_hidden(self, input_sample):
biased_input = input_sample + (1,)
hidden_layer = []
for j in range(self.neuron_count):
weights_j = self.weights_hidden[j]
net_j = sum(x_p * w_p for (x_p, w_p) in zip(biased_input, weights_j))
hidden_layer.append(net_j)
return hidden_layer
def net_output(self, hidden_layer):
# input should be already biased
biased_hidden_layer = hidden_layer # + [1]
output_net = []
for k in range(len(self.weights_output)):
weights_k = self.weights_output[k]
net_k = sum(i_k * w_k for (i_k, w_k) in zip(biased_hidden_layer, weights_k))
output_net.append(net_k)
return output_net