-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeural_Network.py
82 lines (66 loc) · 2.72 KB
/
Neural_Network.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
import numpy as np
import mnist as data
# Applying Logistic Regression
def sigmoid(z):
return 1 / (1 + np.exp(-z))
# Applying Softmax Activation function
def softmax(logits):
exponentials = np.exp(logits)
return exponentials / np.sum(exponentials, axis=1).reshape(-1, 1)
# calculating gradient of logistic regression
def sigmoid_gradient(sigmoid):
return np.multiply(sigmoid, (1 - sigmoid))
# Computing Loss over using logistic regression
def loss(Y, y_hat):
return -np.sum(Y * np.log(y_hat)) / Y.shape[0]
# Adding bias
def prepend_bias(X):
return np.insert(X, 0, 1, axis=1)
# Basically doing prediction but named forward as its
# performing Forward-Propagation
def forward(X, w1, w2):
h = sigmoid(np.matmul(prepend_bias(X), w1))
y_hat = softmax(np.matmul(prepend_bias(h), w2))
return (y_hat, h)
# performing back-Propagation
def back(X, Y, y_hat, w2, h):
w2_gradient = np.matmul(prepend_bias(h).T, (y_hat - Y)) / X.shape[0]
w1_gradient = np.matmul(prepend_bias(X).T, np.matmul(y_hat - Y, w2[1:].T)
* sigmoid_gradient(h)) / X.shape[0]
return (w1_gradient, w2_gradient)
# Calling the predict() function
def classify(X, w1, w2):
y_hat, _ = forward(X, w1, w2)
labels = np.argmax(y_hat, axis=1)
return labels.reshape(-1, 1)
# initiazing weights for all nodes
def initialize_weights(n_input_variables, n_hidden_nodes, n_classes):
w1_rows = n_input_variables + 1
w1 = np.random.randn(w1_rows, n_hidden_nodes) * np.sqrt(1 / w1_rows)
w2_rows = n_hidden_nodes + 1
w2 = np.random.randn(w2_rows, n_classes) * np.sqrt(1 / w2_rows)
return (w1, w2)
# Printing results to the terminal screen
def report(iteration, X_train, Y_train, X_test, Y_test, w1, w2):
y_hat, _ = forward(X_train, w1, w2)
training_loss = loss(Y_train, y_hat)
classifications = classify(X_test, w1, w2)
accuracy = np.average(classifications == Y_test) * 100.0
print("Iteration: %5d, Loss: %.8f, Accuracy: %.2f%%" %
(iteration, training_loss, accuracy))
# training phase
def train(X_train, Y_train, X_test, Y_test, n_hidden_nodes, iterations, lr):
n_input_variables = X_train.shape[1]
n_classes = Y_train.shape[1]
w1, w2 = initialize_weights(n_input_variables, n_hidden_nodes, n_classes)
for iteration in range(iterations):
y_hat, h = forward(X_train, w1, w2)
w1_gradient, w2_gradient = back(X_train, Y_train, y_hat, w2, h)
w1 = w1 - (w1_gradient * lr)
w2 = w2 - (w2_gradient * lr)
report(iteration, X_train, Y_train, X_test, Y_test, w1, w2)
return (w1, w2)
import mnist
w1, w2 = train(mnist.X_train, mnist.Y_train,
mnist.X_test, mnist.Y_test,
n_hidden_nodes=200, iterations=1000, lr=0.01)