-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforward_propagation.py
148 lines (117 loc) · 3.64 KB
/
forward_propagation.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
# tensor flow
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# create the dataset
#https://cs231n.github.io/neural-networks-case-study/
def spiral_data(points, classes):
X = np.zeros((points*classes, 2))
y = np.zeros(points*classes, dtype='uint8')
for class_number in range(classes):
ix = range(points*class_number, points*(class_number+1))
r = np.linspace(0.0, 1, points) # radius
t = np.linspace(class_number*4, (class_number+1)*4, points) + np.random.randn(points)*0.2
X[ix] = np.c_[r*np.sin(t*2.5), r*np.cos(t*2.5)]
y[ix] = class_number
return X, y
# visualization
X, y = spiral_data(100,2)
class_1 = np.where(y == 0)
class_2 = np.where(y == 1)
# class_3 = np.where(y == 2)
plt.scatter(X[class_1, 0], X[class_1, 1], c='r', marker='o', s=40, )
plt.scatter(X[class_2, 0], X[class_2, 1], c='b', marker='x', s=50, )
# plt.scatter(X[class_3, 0], X[class_3, 1], c='g', marker='^', s=60, )
# two test examples represented in green stars
plt.scatter(-0.125, 0, marker='*', c = 'g', s = 100)
plt.scatter(0.125, 0, marker='*', c = 'g', s = 100)
plt.show()
print(X.shape)
print(y.shape)
"""## Tensorflow"""
tf.random.set_seed(1234)
model = Sequential(
[
tf.keras.Input(shape=(2,)),
Dense(4, activation='relu', name='layer_1'),
Dense(1, activation='sigmoid', name='layer_2')
]
)
model.summary()
W1, b1 = model.get_layer("layer_1").get_weights()
W2, b2 = model.get_layer("layer_2").get_weights()
print(f"W1{W1.shape}:\n", W1, f"\nb1{b1.shape}:", b1)
print(f"W2{W2.shape}:\n", W2, f"\nb2{b2.shape}:", b2)
model.compile(
loss = tf.keras.losses.BinaryCrossentropy(),
optimizer = tf.keras.optimizers.Adam(learning_rate=0.01),
)
model.fit(
X,y,
epochs=10,
)
W1, b1 = model.get_layer("layer_1").get_weights()
W2, b2 = model.get_layer("layer_2").get_weights()
print("W1:\n", W1, "\nb1:", b1)
print("W2:\n", W2, "\nb2:", b2)
X_test = np.array([
[-0.125, 0], # positive example
[0.125,0]]) # negative example
predictions = model.predict(X_test)
print("predictions made by Tensorflow = \n", predictions)
yhat = np.zeros_like(predictions)
for i in range(len(predictions)):
if predictions[i] >= 0.5:
yhat[i] = 1
else:
yhat[i] = 0
print(f"decisions = \n{yhat}")
"""## Numpy"""
# activation function
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def ReLU(x):
return np.maximum(0,x)
g = sigmoid
def my_dense(a_in, W, b, g):
"""
Computes dense layer
Args:
a_in (ndarray (m, n)) : Data, m examples by n features
W (ndarray (n,j)) : Weight matrix, n features per unit, j units [n rows and j columns]
b (ndarray (j, )) : bias vector, j units
Returns
a_out (ndarray (j,)) : j units|
"""
"""
units = W.shape[1]
a_out = np.zeros(units)
for j in range(units):
w = W[:,j]
z = np.dot(w, a_in) + b[j]
a_out[j] = g(z)
"""
z = np.dot(a_in, W) + b
a_out = g(z)
return(a_out)
def my_sequential(x, W1, b1, W2, b2):
a1 = my_dense(x, W1, b1, ReLU)
a2 = my_dense(a1, W2, b2, sigmoid)
return(a2)
def my_predict(X, W1, b1, W2, b2):
"""
m = X.shape[0]
p = np.zeros((m,1))
for i in range(m):
p[i,0] = my_sequential(X[i], W1, b1, W2, b2)
"""
p = my_sequential(X, W1, b1, W2, b2)
return(p)
# compare the results with TensorFlow
X_test = np.array([[-0.125, 0], # positive example
[0.125,0]]) # negative example
predictions = my_predict(X_test, W1, b1, W2, b2)
print("predictions made by Numpy = \n", predictions)