-
Notifications
You must be signed in to change notification settings - Fork 1
/
01 Softmax Regression.py
129 lines (103 loc) · 3.71 KB
/
01 Softmax Regression.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
import numpy as np
import matplotlib.pyplot as plt
import mnist
from activation import relu, sigmoid, sigmoid_prime, softmax
from helper import one_hot_encoder
from initializer import initialize_weight
def train(train_x, train_y, learning_rate=0.2):
# Flatten input (batch_size, 28, 28) -> (batch_size, 784)
x = train_x.reshape(train_x.shape[0], -1)
# Turn labels into their one-hot representations
y = one_hot_encoder(train_y)
# Initialize weights
w1, b1 = initialize_weight((784, 256), bias=True)
w2, b2 = initialize_weight((256, 10), bias=True)
num_epochs = 50
loss_history = []
for epoch in range(1, num_epochs+1):
print("Epoch {}/{}\n===============".format(epoch, num_epochs))
# Forward Prop
h1 = np.dot(x, w1) + b1
a1 = sigmoid(h1)
h2 = np.dot(a1, w2) + b2
a2 = softmax(h2)
out = a2
# Cross Entropy Loss
loss = cross_entropy_loss(out, train_y)
loss_history.append(loss)
print("Loss: {:.6f}".format(loss))
# Compute and print accuracy
pred = np.argmax(out, axis=1)
pred = pred.reshape(pred.shape[0], 1)
acc = np.mean(pred == train_y)
print("Accuracy: {:.2f}%\n".format(acc*100))
# Backward Prop
m = out.shape[0]
dh2 = a2 - y
dw2 = (1/m) * np.dot(a1.T, dh2)
db2 = (1/m) * np.sum(dh2, axis=0, keepdims=True)
dh1 = np.dot(dh2, w2.T) * sigmoid_prime(a1)
dw1 = (1/m) * np.dot(x.T, dh1)
db1 = (1/m) * np.sum(dh1, axis=0, keepdims=True)
# Weight (and bias) update
w1 -= learning_rate * dw1
b1 -= learning_rate * db1
w2 -= learning_rate * dw2
b2 -= learning_rate * db2
return w1, b1, w2, b2, loss_history
def cross_entropy_loss(out, y):
batch_size = y.shape[0]
y = y.reshape(batch_size)
log_likelihood = -np.log(out[np.arange(batch_size), y])
return np.sum(log_likelihood) / batch_size
def test(test_x, test_y, w1, b1, w2, b2):
# Flatten input (batch_size, 28, 28) -> (batch_size, 784)
x = test_x.reshape(test_x.shape[0], -1)
# Turn labels into their one-hot representations
y = one_hot_encoder(test_y)
# Forward Pass
h1 = np.dot(x, w1) + b1
a1 = sigmoid(h1)
h2 = np.dot(a1, w2) + b2
a2 = softmax(h2)
out = a2
# Cross Entropy Loss
loss = cross_entropy_loss(out, test_y)
print("Loss: {:.6f}".format(loss))
# Compute and print accuracy
pred = np.argmax(out, axis=1)
pred = pred.reshape(pred.shape[0], 1)
acc = np.mean(pred == test_y)
print("Accuracy: {:.2f}%\n".format(acc*100))
def visualization(test_x, test_y, w1, b1, w2, b2):
x = test_x[:20]
x = x.reshape(x.shape[0], -1)
y = test_y[:20]
# Forward Pass
h1 = np.dot(x, w1) + b1
a1 = sigmoid(h1)
h2 = np.dot(a1, w2) + b2
a2 = softmax(h2)
out = a2
pred = np.argmax(out, axis=1)
fig = plt.figure(figsize=(25, 4))
for index in np.arange(20):
ax = fig.add_subplot(2, 20/2, index+1, xticks=[], yticks=[])
ax.imshow(test_x[index], cmap='gray')
ax.set_title("{} ({})".format(str(pred[index]), str(y[index][0])),
color=("green" if pred[index] == y[index] else "red"))
def main():
# Load dataset
train_x, train_y = mnist.load_dataset(download=True, train=True)
test_x, test_y = mnist.load_dataset(download=True, train=False)
# Train
w1, b1, w2, b2, loss_history = train(train_x, train_y)
# Display loss curve
plt.plot(loss_history, label="Training Loss")
plt.show()
# Test
test(test_x, test_y, w1, b1, w2, b2)
# A little visualization
visualization(test_x, test_y, w1, b1, w2, b2)
plt.show()
main()