質問編集履歴
2
2クラスでの識別器のコードを載せました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -25,3 +25,231 @@
|
|
25
25
|
(追記)
|
26
26
|
|
27
27
|
画像中のw、bは識別に使う重み(パラメータ)です。
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
### 2クラスでの識別器(自分で実装したもの)
|
32
|
+
|
33
|
+
```python
|
34
|
+
|
35
|
+
import numpy as np
|
36
|
+
|
37
|
+
import makeGaussianData
|
38
|
+
|
39
|
+
import matplotlib.pyplot as plt
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
K = 2
|
44
|
+
|
45
|
+
w = 0.02*np.random.rand(K)-0.01 #1.パラメータの初期化
|
46
|
+
|
47
|
+
b=0.02*np.random.rand()-0.01
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
X, lab, t = makeGaussianData.getData(K)
|
52
|
+
|
53
|
+
z = np.empty(X.shape[0],)
|
54
|
+
|
55
|
+
h = np.empty(X.shape[0],)
|
56
|
+
|
57
|
+
eta=0.01
|
58
|
+
|
59
|
+
cnt = 0
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
def s(x):
|
64
|
+
|
65
|
+
return 1/(1+np.exp(-x))
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
for i in range(10000): #2.適当な回数の繰り返し(学習データは400個)
|
70
|
+
|
71
|
+
n = np.random.randint(0,400) #i.400個のデータからランダムで1つ選択
|
72
|
+
|
73
|
+
z[n]= s(w @ X[n] + b)
|
74
|
+
|
75
|
+
h[n]= (-t[n]*np.log(z[n]))-((1-t[n])*np.log(1-z[n])) #ii.モデルの出力を求める
|
76
|
+
|
77
|
+
w = w-(eta*(X[n]*(z[n]-t[n])))
|
78
|
+
|
79
|
+
b = b-(eta*(z[n]-t[n])) #iii.パラメータの更新
|
80
|
+
|
81
|
+
if i % 1000 == 0: #2.iv. 1000の倍数になったときの処理
|
82
|
+
|
83
|
+
for j in range(X.shape[0]):
|
84
|
+
|
85
|
+
z[j]=s(w @ X[j] + b)
|
86
|
+
|
87
|
+
h[j]= (-1*t[j])*np.log(z[j])-(1-t[j])*np.log(1-z[j])
|
88
|
+
|
89
|
+
if z[j] > 0.5:
|
90
|
+
|
91
|
+
T = 1
|
92
|
+
|
93
|
+
else:
|
94
|
+
|
95
|
+
T = 0
|
96
|
+
|
97
|
+
if T == t[j]:
|
98
|
+
|
99
|
+
cnt = cnt+1
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
H = np.mean(h)
|
104
|
+
|
105
|
+
print("#{0}, H:{1} , {2}/{3}={4}".format(i,H,cnt,X.shape[0],cnt/X.shape[0]))
|
106
|
+
|
107
|
+
cnt=0
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
fig = plt.figure()
|
112
|
+
|
113
|
+
plt.xlim(-0.2, 1.2)
|
114
|
+
|
115
|
+
plt.ylim(-0.2, 1.2)
|
116
|
+
|
117
|
+
ax = fig.add_subplot(1, 1, 1)
|
118
|
+
|
119
|
+
ax.set_aspect(1)
|
120
|
+
|
121
|
+
ax.scatter(X[lab == 0, 0], X[lab == 0, 1], color = 'red')
|
122
|
+
|
123
|
+
ax.scatter(X[lab == 1, 0], X[lab == 1, 1], color = 'green')
|
124
|
+
|
125
|
+
if K == 3:
|
126
|
+
|
127
|
+
ax.scatter(X[lab == 2, 0], X[lab == 2, 1], color = 'blue')
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
fig.show()
|
132
|
+
|
133
|
+
plt.show()
|
134
|
+
|
135
|
+
```
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
makeGaussianData.py
|
140
|
+
|
141
|
+
```python
|
142
|
+
|
143
|
+
import numpy as np
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
def getData(nclass, seed = None):
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
assert nclass == 2 or nclass == 3
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
if seed != None:
|
158
|
+
|
159
|
+
np.random.seed(seed)
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
# 2次元の spherical な正規分布3つからデータを生成
|
164
|
+
|
165
|
+
X0 = 0.10 * np.random.randn(200, 2) + [ 0.3, 0.3 ]
|
166
|
+
|
167
|
+
X1 = 0.10 * np.random.randn(200, 2) + [ 0.7, 0.6 ]
|
168
|
+
|
169
|
+
X2 = 0.05 * np.random.randn(200, 2) + [ 0.3, 0.7 ]
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
# それらのラベル用のarray
|
174
|
+
|
175
|
+
lab0 = np.zeros(X0.shape[0], dtype = int)
|
176
|
+
|
177
|
+
lab1 = np.zeros(X1.shape[0], dtype = int) + 1
|
178
|
+
|
179
|
+
lab2 = np.zeros(X2.shape[0], dtype = int) + 2
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
# X (入力データ), label (クラスラベル), t(教師信号) をつくる
|
184
|
+
|
185
|
+
if nclass == 2:
|
186
|
+
|
187
|
+
X = np.vstack((X0, X1))
|
188
|
+
|
189
|
+
label = np.hstack((lab0, lab1))
|
190
|
+
|
191
|
+
t = np.zeros(X.shape[0])
|
192
|
+
|
193
|
+
t[label == 1] = 1.0
|
194
|
+
|
195
|
+
else:
|
196
|
+
|
197
|
+
X = np.vstack((X0, X1, X2))
|
198
|
+
|
199
|
+
label = np.hstack((lab0, lab1, lab2))
|
200
|
+
|
201
|
+
t = np.zeros((X.shape[0], nclass))
|
202
|
+
|
203
|
+
for ik in range(nclass):
|
204
|
+
|
205
|
+
t[label == ik, ik] = 1.0
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
return X, label, t
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
if __name__ == '__main__':
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
import matplotlib
|
220
|
+
|
221
|
+
import matplotlib.pyplot as plt
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
K = 3
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
X, lab, t = getData(K)
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
fig = plt.figure()
|
234
|
+
|
235
|
+
plt.xlim(-0.2, 1.2)
|
236
|
+
|
237
|
+
plt.ylim(-0.2, 1.2)
|
238
|
+
|
239
|
+
ax = fig.add_subplot(1, 1, 1)
|
240
|
+
|
241
|
+
ax.set_aspect(1)
|
242
|
+
|
243
|
+
ax.scatter(X[lab == 0, 0], X[lab == 0, 1], color = 'red')
|
244
|
+
|
245
|
+
ax.scatter(X[lab == 1, 0], X[lab == 1, 1], color = 'green')
|
246
|
+
|
247
|
+
if K == 3:
|
248
|
+
|
249
|
+
ax.scatter(X[lab == 2, 0], X[lab == 2, 1], color = 'blue')
|
250
|
+
|
251
|
+
plt.show()
|
252
|
+
|
253
|
+
```
|
254
|
+
|
255
|
+
以上2つのコードで2クラス識別器を作成しました。
|
1
画像の数式の補足をしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -19,3 +19,9 @@
|
|
19
19
|
### 今回実装したいソフトマックスの式の形
|
20
20
|
|
21
21
|
![![イメージ説明](30a8d95636da158b1ac095e92d58beec.png)](54b70acd4b4d7c08ffdf9d8a3401f736.png)
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
(追記)
|
26
|
+
|
27
|
+
画像中のw、bは識別に使う重み(パラメータ)です。
|