-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbai13_SoftmaxRegression_example.py
164 lines (133 loc) · 4.23 KB
/
bai13_SoftmaxRegression_example.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
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 24 01:36:06 2020
@author: phamk
"""
import numpy as np
means = [[2, 2], [8, 3], [3, 6]]
cov = [[1, 0], [0, 1]]
N = 500
X0 = np.random.multivariate_normal(means[0], cov, N)
X1 = np.random.multivariate_normal(means[1], cov, N)
X2 = np.random.multivariate_normal(means[2], cov, N)
X = np.concatenate((X0, X1, X2), axis = 0).T # each column is a datapoint
X = np.concatenate((np.ones((1, 3*N)), X), axis = 0)
C = 3
original_label = np.asarray([0]*N + [1]*N + [2]*N).T
import matplotlib.pyplot as plt
def display(X, label):
# K = np.amax(label) + 1
X0 = X[:, label == 0]
X1 = X[:, label == 1]
X2 = X[:, label == 2]
plt.plot(X0[0, :], X0[1, :], 'b^', markersize = 4, alpha = .8)
plt.plot(X1[0, :], X1[1, :], 'go', markersize = 4, alpha = .8)
plt.plot(X2[0, :], X2[1, :], 'rs', markersize = 4, alpha = .8)
# plt.axis('equal')
plt.axis('off')
plt.plot()
plt.show()
display(X[1:, :], original_label)
from scipy import sparse
def convert_labels(y, C = C):
"""
convert 1d label to a matrix label: each column of this
matrix coresponding to 1 element in y. In i-th column of Y,
only one non-zeros element located in the y[i]-th position,
and = 1 ex: y = [0, 2, 1, 0], and 3 classes then return
[[1, 0, 0, 1],
[0, 0, 1, 0],
[0, 1, 0, 0]]
"""
Y = sparse.coo_matrix((np.ones_like(y),
(y, np.arange(len(y)))), shape = (C, len(y))).toarray()
return Y
def softmax(Z):
"""
Compute softmax values for each sets of scores in V.
each column of V is a set of score.
"""
e_Z = np.exp(Z)
A = e_Z / e_Z.sum(axis = 0)
return A
def softmax_stable(Z):
"""
Compute softmax values for each sets of scores in V.
each column of V is a set of score.
"""
e_Z = np.exp(Z - np.max(Z, axis = 0, keepdims = True))
A = e_Z / e_Z.sum(axis = 0)
return A
def softmax_regression(X, y, W_init, eta, tol = 1e-4, max_count = 10000):
W = [W_init]
C = W_init.shape[1]
Y = convert_labels(y, C)
it = 0
N = X.shape[1]
d = X.shape[0]
count = 0
check_w_after = 20
while count < max_count:
# mix data
mix_id = np.random.permutation(N)
for i in mix_id:
xi = X[:, i].reshape(d, 1)
yi = Y[:, i].reshape(C, 1)
ai = softmax(np.dot(W[-1].T, xi))
W_new = W[-1] + eta*xi.dot((yi - ai).T)
count += 1
# stopping criteria
if count%check_w_after == 0:
if np.linalg.norm(W_new - W[-check_w_after]) < tol:
return W
W.append(W_new)
return W
eta = .05
d = X.shape[0]
W_init = np.random.randn(X.shape[0], C)
W = softmax_regression(X, original_label, W_init, eta)
print(W[-1])
def pred(W, X):
"""
predict output of each columns of X
Class of each x_i is determined by location of max probability
Note that class are indexed by [0, 1, 2, ...., C-1]
"""
A = softmax_stable(W.T.dot(X))
return np.argmax(A, axis = 0)
#Visualize
# x_min, x_max = X[:, 1].min() - .5, X[:, 1].max() + .5
# y_min, y_max = X[:, 2].min() - .5, X[:, 2].max() + .5
# x_min, x_max = -4, 14
# y_min, y_max = -4, 14
xm = np.arange(-2, 11, 0.025)
xlen = len(xm)
ym = np.arange(-3, 10, 0.025)
ylen = len(ym)
xx, yy = np.meshgrid(xm, ym)
# xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# xx.ravel(), yy.ravel()
print(np.ones((1, xx.size)).shape)
xx1 = xx.ravel().reshape(1, xx.size)
yy1 = yy.ravel().reshape(1, yy.size)
# print(xx.shape, yy.shape)
XX = np.concatenate((np.ones((1, xx.size)), xx1, yy1), axis = 0)
print(XX.shape)
Z = pred(W[-1], XX)
# Put the result into a color plot
Z = Z.reshape(xx.shape)
# plt.figure(1
# plt.pcolormesh(xx, yy, Z, cmap='jet', alpha = .35)
CS = plt.contourf(xx, yy, Z, 200, cmap='jet', alpha = .1)
# Plot also the training points
# plt.scatter(X[:, 1], X[:, 2], c=Y, edgecolors='k', cmap=plt.cm.Paired)
# plt.xlabel('Sepal length')
# plt.ylabel('Sepal width')
plt.xlim(-2, 11)
plt.ylim(-3, 10)
plt.xticks(())
plt.yticks(())
# plt.axis('equal')
display(X[1:, :], original_label)
plt.savefig('ex1.png', bbox_inches='tight', dpi = 300)
plt.show()