-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural_network_practice.py
87 lines (71 loc) · 2.02 KB
/
neural_network_practice.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Simplified version of multiplication gate to return the output or the gradient of the input variables
def multiply_gate(a, b, dx=1, backwards_pass=False):
if not backwards_pass:
return a * b
else:
da = b * dx
db = a * dx
return da, db
def add_gate(a, b, dx=1, backwards_pass=False):
if not backwards_pass:
return a + b
else:
da = 1 * dx
db = 1 * dx
return da, db
# We can combine different gates together
# For example, a + b + c
def add_gate_combined(a, b, c, dx=1, backwards_pass=False):
if not backwards_pass:
return a + b + c
else:
da = 1 * dx
db = 1 * dx
dc = 1 * dx
return da, db, dc
# Another example: combining addition and multiplication (a * b + c)
def add_multiplication_combined(a, b, c, dx=1, backwards_pass=False):
if not backwards_pass:
return a * b + c
else:
da = b * dx
db = a * dx
dc = 1 * dx
return da, db, dc
def sigmoid(x):
return 1 / (1 + (exp(-x)))
def sigmoid_derivative(x):
return sigmoid(x) * (1 - sigmoid(x))
# An even more complex neuron (sigmoid(a*x + b*y + c))
def complex_neuron(a, b, c, x, y, df=1, backwards_pass=False):
if not backwards_pass:
q = a*x + b*y + c
f = sigmoid(q)
return f
else:
dq = sigmoid_derivative(f) * df
da = x * dq
dx = a * dq
dy = b * dq
db = y * dq
dc = 1 * dq
return da, db, dc, dx, dy
# What if both inputs of a multiplication are equal (a * a)?
def square_neuron(a, dx=1, backwards_pass=False):
if not backwards_pass:
return a * a
else:
# From power rule:
da = 2 * a * dx
# Short form for:
# da = a * dx
# da += a * dx
return da
# For a*a + b*b + c*c:
def sum_squares_neuron(a, b, c, dx=1, backwards_pass=False):
if not backwards_pass:
return a*a + b*b + c*c
else:
da = 2 * a * dx
db = 2 * b * dx
dc = 2 * c * dx