forked from amar-enkhbat/Softmax-Regression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
softmax_regression.py
326 lines (268 loc) · 12.5 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# モジュールをインポート
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import learning_curve
# =============================================================================
# Default settings
# =============================================================================
# Supress scientific notations
np.set_printoptions(suppress=True)
# Define a random seed
random_seed = 123
np.random.seed(random_seed)
# =============================================================================
# Import samples
# =============================================================================
wine = datasets.load_iris()
X = wine.data[:, [0, 3]]
y = wine.target
number_of_classes = len(np.unique(y))
# =============================================================================
# Separate samples
# =============================================================================
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = random_seed)
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size = 0.2, random_state = random_seed)
# =============================================================================
# Data standardization
# =============================================================================
stdsc = StandardScaler()
X_train_std = stdsc.fit_transform(X_train)
X_valid_std = stdsc.transform(X_valid)
X_test_std = stdsc.transform(X_valid)
# =============================================================================
# One hot encoder
# =============================================================================
class_labels = int(y.max() + 1)
def one_hot_encoder(y):
a = np.zeros((len(y), class_labels))
for idx, i in enumerate(y):
a[idx, int(i)] = 1
return a
y_train_coded = one_hot_encoder(y_train)
y_valid_coded = one_hot_encoder(y_valid)
y_test_coded= one_hot_encoder(y_test)
# =============================================================================
# Softmax function
# =============================================================================
def softmax(z):
log_c = np.max(z, axis = 1) * (-1)
log_c = log_c.reshape(-1, 1)
prob = np.exp(z + log_c)
prob = prob / np.exp(z + log_c).sum(axis = 1).reshape(-1, 1)
return np.clip(prob, 1e-15, 1-1e-15)
# return (np.exp(z.T) / np.sum(np.exp(z), axis=1)).T
# =============================================================================
# Net input function
# =============================================================================
def net_input(X, weight):
return X.dot(weight[1:]) + weight[0]
# =============================================================================
# Predict function
# =============================================================================
def predict(activation):
y_predicted = activation.argmax(axis = 1)
return y_predicted
# =============================================================================
# Full predict function
# =============================================================================
def full_predict(X, weight):
z = net_input(X, weight)
activation = softmax(z)
y_predicted = activation.argmax(axis = 1)
return y_predicted
# =============================================================================
# Compute cost function
# =============================================================================
def compute_cost(X, y, weight, cost_lambda):
y_pred_enc = one_hot_encoder(y)
z = net_input(X, weight)
activation = softmax(z)
cross_entropy = - np.sum(np.log(activation) * (y_pred_enc), axis = 1)
regularization = cost_lambda * np.sum(weight[1:]**2) / 2
cross_entropy = cross_entropy + regularization
return np.mean(cross_entropy)
# =============================================================================
# Compute error
# =============================================================================
def compute_error(X, y, weight, cost_lambda):
y = one_hot_encoder(y)
z = net_input(X, weight)
activation = softmax(z)
regularization = cost_lambda * np.mean(weight[2:] ** 2)/2
return np.mean((activation - y)**2)/2 + regularization
# =============================================================================
# Defining variables
# =============================================================================
# Number of epochs
epochs = 100
# Learning rate
learning_rate = 0.01
weight_scale = 1
# Regularization Lambda
cost_lambda = 10
# =============================================================================
# Training function
# =============================================================================
def train(X, y, epochs, learning_rate, cost_lambda):
weight = np.random.normal(loc = 0, scale = weight_scale, size = (X.shape[1] + 1, number_of_classes))
cost_array = []
y_encoded = one_hot_encoder(y)
for epoch in range(epochs):
z = net_input(X, weight)
activation = softmax(z)
diff = activation - y_encoded
grad = np.dot(X.T, diff)
weight[1:] -= learning_rate * (grad + cost_lambda * weight[1:])
weight[0] -= learning_rate * np.sum(diff, axis = 0)
cost = compute_cost(X, y, weight, cost_lambda)
cost_array.append(cost)
return weight, cost_array
# =============================================================================
# Train the classifier
# =============================================================================
learned_weights, cost_array = train(X_train_std, y_train, epochs, learning_rate, cost_lambda)
# =============================================================================
# Plot the cost-epoch graph
# =============================================================================
plt.plot(range(len(cost_array)), cost_array)
plt.show()
# =============================================================================
# Evaluation
# =============================================================================
#y_pred = full_predict(X_train_std, learned_weights)
#print("")
#print("テストデータでの性能:")
#conf_mat = confusion_matrix(y_train, y_pred)
#print("混同行列:")
#print(conf_mat)
#accuracy = accuracy_score(y_train, y_pred)
#print("精度:")
#print(accuracy)
# =============================================================================
# Evaluation of Softmax using LogisticRegression from sklearn
# =============================================================================
#classifier = LogisticRegression(C = 10)
#classifier.fit(X_train_std, y_train)
#y_pred = classifier.predict(X_train_std)
#print("")
#print("テストデータでの性能:")
#conf_mat = confusion_matrix(y_train, y_pred)
#print("混同行列:")
#print(conf_mat)
#accuracy = accuracy_score(y_train, y_pred)
#print("精度:")
#print(accuracy)
# Data plot
def plot_decision_regions(X, y, weight, resolution = 0.02):
markers = ('s', 'x', 'o', '^', 'v')
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
cmap = ListedColormap(colors[:len(np.unique(y))])
x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution))
Z = full_predict(np.array([xx1.ravel(), xx2.ravel()]).T, weight)
Z = Z.reshape(xx1.shape)
plt.contourf(xx1, xx2, Z, alpha = 0.3, cmap = cmap)
plt.xlim(xx1.min(), xx1.max())
plt.ylim(xx2.min(), xx2.max())
for idx, cl in enumerate(np.unique(y)):
plt.scatter(x = X[y == cl, 0], y = X[y == cl, 1], alpha = 0.8, c = colors[idx], marker = markers[idx], label = cl, edgecolor = 'black')
def plot_decision_regions_sklearn(X, y, classifier, resolution = 0.02):
markers = ('s', 'x', 'o', '^', 'v')
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
cmap = ListedColormap(colors[:len(np.unique(y))])
x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution))
Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
Z = Z.reshape(xx1.shape)
plt.contourf(xx1, xx2, Z, alpha = 0.3, cmap = cmap)
plt.xlim(xx1.min(), xx1.max())
plt.ylim(xx2.min(), xx2.max())
for idx, cl in enumerate(np.unique(y)):
plt.scatter(x = X[y == cl, 0], y = X[y == cl, 1], alpha = 0.8, c = colors[idx], marker = markers[idx], label = cl, edgecolor = 'black')
def plot_decision_regions_poly(X, y, weight, resolution = 0.02):
markers = ('s', 'x', 'o', '^', 'v')
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
cmap = ListedColormap(colors[:len(np.unique(y))])
x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution))
X_poly = poly.transform(np.array([xx1.ravel(), xx2.ravel()]).T)
Z = full_predict(X_poly, weight)
Z = Z.reshape(xx1.shape)
plt.contourf(xx1, xx2, Z, alpha = 0.3, cmap = cmap)
plt.xlim(xx1.min(), xx1.max())
plt.ylim(xx2.min(), xx2.max())
for idx, cl in enumerate(np.unique(y)):
plt.scatter(x = X[y == cl, 0], y = X[y == cl, 1], alpha = 0.8, c = colors[idx], marker = markers[idx], label = cl, edgecolor = 'black')
#
#plot_decision_regions(X_train_std, y_train, learned_weights)
#plt.show()
#
#plot_decision_regions_sklearn(X_train_std, y_train, classifier = classifier)
#plt.show()
# =============================================================================
# Polynomial models
# =============================================================================
for i in range(1, 10):
poly = PolynomialFeatures(i, include_bias = False)
X_train_std_poly = poly.fit_transform(X_train_std)
X_valid_std_poly = poly.transform(X_train_std)
# weight = np.random.normal(scale = 0.01, size = (X_train_std_poly.shape[1] + 1, len(np.unique(y))))
learned_weights_train, cost_array_train = train(X_train_std_poly, y_train, epochs, learning_rate, cost_lambda)
y_pred = full_predict(X_train_std_poly, learned_weights_train)
print("")
print("テストデータでの性能:")
conf_mat = confusion_matrix(y_train, y_pred)
print("混同行列:")
print(conf_mat)
accuracy = accuracy_score(y_train, y_pred)
print("精度:")
print(accuracy)
plot_decision_regions_poly(X_train_std_poly, y_train, learned_weights_train)
plt.show()
# =============================================================================
# Learning curve plot
# =============================================================================
# m = X_train_std.shape[0]
# error_train = np.zeros(m-1)
# error_val = np.zeros(m-1)
# for i in range(1, m):
# learned_weights, a = train(X_train_std[0:i, :], y_train[0:i], epochs, learning_rate, cost_lambda)
# error_train[i-1] = compute_error(X_train_std[0:i, :], y_train[0:i], learned_weights, 0)
# error_val[i-1]= compute_error(X_valid_std, y_valid, learned_weights, 0)
# plt.plot(range(m-1), error_train, label = "Training data")
# plt.plot(range(m-1), error_val, label = "Validation data")
# plt.title("Learning Curve")
# plt.xlabel("Number of data")
# plt.ylabel("Cost")
# plt.legend()
# plt.show()
# =============================================================================
# Validation plot
# =============================================================================
# lambda_vec = [0, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30, 100, 300, 3000]
# error_train = []
# error_val = []
# for i in lambda_vec:
# learned_weights, a = train(X_train_std, y_train, epochs, learning_rate, i)
# error_train.append(compute_error(X_train_std, y_train, learned_weights, 0))
# error_val.append(compute_error(X_valid_std, y_valid, learned_weights, 0))
# plt.plot(range(len(lambda_vec)), error_train, label = "Training data")
# plt.plot(range(len(lambda_vec)), error_val, label = "Validation data")
# plt.title("Learning Curve")
# plt.xlabel("Lambda")
# plt.ylabel("Cost")
# plt.legend()
# plt.show()
print(learned_weights_train.shape)
# for i in range(10):
# plt.imshow(learned_weights_train[i].reshape(28, 28))