-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsoftmax_regression.py
83 lines (71 loc) · 2.45 KB
/
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
import numpy as np
import matplotlib.pyplot as plt
import metrics
import regularizer
import scipy
class SoftmaxRegression:
def __init__(self, debug=True):
self.__debug = debug
def fit(self, X, y, epochs, optimizer, regularizer=regularizer.Regularizer(0)):
'''
Parameters
----------
X : shape (n_samples, n_features)
Training data
y : One-hot encoder, shape (n_samples, n_classes)
Target values
epochs : The number of epochs
optimizer : Optimize algorithm, see also optimizer.py
regularizer : Regularize algorithm, see also regularizer.py
'''
n_samples, n_features = X.shape
n_classes = y.shape[1]
self.__W = np.zeros((n_features, n_classes))
self.__b = np.zeros((1, n_classes))
if self.__debug:
accuracy = []
loss = []
for _ in range(epochs):
h = self.score(X)
g_W = X.T.dot(h - y) / n_samples + regularizer.regularize(self.__W)
g_b = np.mean(h - y, axis=0)
g_W, g_b = optimizer.optimize([g_W, g_b])
self.__W -= g_W
self.__b -= g_b
if self.__debug:
h = self.score(X)
loss.append(np.mean(-np.sum(y * np.log(h), axis=1)))
accuracy.append(metrics.accuracy(np.argmax(y, axis=1), np.argmax(h, axis=1)))
if self.__debug:
_, ax_loss = plt.subplots()
ax_loss.plot(loss, 'b')
ax_accuracy = ax_loss.twinx()
ax_accuracy.plot(accuracy, 'r')
plt.show()
def predict(self, X, classes):
'''
Parameters
----------
X : shape (n_samples, n_features)
Predicting data
classes : shape (n_classes,)
The all labels
Returns
-------
y : shape (n_samples,)
Predicted class label per sample.
'''
return classes[np.argmax(self.score(X), axis=1)]
def score(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Predicting data
Returns
-------
y : shape (n_samples, n_classes)
Predicted score of class per sample.
'''
out = X.dot(self.__W) + self.__b
return scipy.special.softmax(out - np.max(out), axis=1)