-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNetwork.py
More file actions
57 lines (51 loc) · 1.82 KB
/
Copy pathNeuralNetwork.py
File metadata and controls
57 lines (51 loc) · 1.82 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
from AutoGrad import Value
import random, math
class Network:
def random_weight(self, input_size=1):
return Value(random.uniform(-math.sqrt(6/input_size), math.sqrt(6/input_size)))
def __init__(self, layer_sizes):
self.layers = []
for i in range(len(layer_sizes) - 1):
layer = []
for _ in range(layer_sizes[i+1]):
rows = []
for _ in range(layer_sizes[i]):
rows.append(self.random_weight(layer_sizes[i]))
layer.append(rows)
self.layers.append(layer)
self.print()
def forward(self, x):
for layer_index in range(len(self.layers)):
y = []
for i in range(len(self.layers[layer_index])):
sum = Value(0)
for j in range(len(self.layers[layer_index][i])):
result = self.layers[layer_index][i][j] * x[j]
sum = sum + result
y.append(sum)
if layer_index < len(self.layers) - 1:
x = []
for i in range(len(y)):
x.append(y[i].relu())
else:
x = y
x = self.softmax(x)
return x
def print(self,max_depth=3):
for layer in self.layers:
print(f"Layer:")
for row in layer:
print(f" Neuron:")
for weight in range(len(row)):
if weight >= max_depth:
print(f" ...")
break
print(f" Weight: {row[weight]}")
def softmax(self, x):
sum = Value(0)
for i in range(len(x)):
sum += math.exp(1)**x[i]
for i in range(len(x)):
x[i] = (math.exp(1)**x[i])
x[i] = x[i] / sum
return x