質問編集履歴
2
2クラスでの識別器のコードを載せました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,4 +11,118 @@
|
|
11
11
|
](54b70acd4b4d7c08ffdf9d8a3401f736.png)
|
12
12
|
|
13
13
|
(追記)
|
14
|
-
画像中のw、bは識別に使う重み(パラメータ)です。
|
14
|
+
画像中のw、bは識別に使う重み(パラメータ)です。
|
15
|
+
|
16
|
+
### 2クラスでの識別器(自分で実装したもの)
|
17
|
+
```python
|
18
|
+
import numpy as np
|
19
|
+
import makeGaussianData
|
20
|
+
import matplotlib.pyplot as plt
|
21
|
+
|
22
|
+
K = 2
|
23
|
+
w = 0.02*np.random.rand(K)-0.01 #1.パラメータの初期化
|
24
|
+
b=0.02*np.random.rand()-0.01
|
25
|
+
|
26
|
+
X, lab, t = makeGaussianData.getData(K)
|
27
|
+
z = np.empty(X.shape[0],)
|
28
|
+
h = np.empty(X.shape[0],)
|
29
|
+
eta=0.01
|
30
|
+
cnt = 0
|
31
|
+
|
32
|
+
def s(x):
|
33
|
+
return 1/(1+np.exp(-x))
|
34
|
+
|
35
|
+
for i in range(10000): #2.適当な回数の繰り返し(学習データは400個)
|
36
|
+
n = np.random.randint(0,400) #i.400個のデータからランダムで1つ選択
|
37
|
+
z[n]= s(w @ X[n] + b)
|
38
|
+
h[n]= (-t[n]*np.log(z[n]))-((1-t[n])*np.log(1-z[n])) #ii.モデルの出力を求める
|
39
|
+
w = w-(eta*(X[n]*(z[n]-t[n])))
|
40
|
+
b = b-(eta*(z[n]-t[n])) #iii.パラメータの更新
|
41
|
+
if i % 1000 == 0: #2.iv. 1000の倍数になったときの処理
|
42
|
+
for j in range(X.shape[0]):
|
43
|
+
z[j]=s(w @ X[j] + b)
|
44
|
+
h[j]= (-1*t[j])*np.log(z[j])-(1-t[j])*np.log(1-z[j])
|
45
|
+
if z[j] > 0.5:
|
46
|
+
T = 1
|
47
|
+
else:
|
48
|
+
T = 0
|
49
|
+
if T == t[j]:
|
50
|
+
cnt = cnt+1
|
51
|
+
|
52
|
+
H = np.mean(h)
|
53
|
+
print("#{0}, H:{1} , {2}/{3}={4}".format(i,H,cnt,X.shape[0],cnt/X.shape[0]))
|
54
|
+
cnt=0
|
55
|
+
|
56
|
+
fig = plt.figure()
|
57
|
+
plt.xlim(-0.2, 1.2)
|
58
|
+
plt.ylim(-0.2, 1.2)
|
59
|
+
ax = fig.add_subplot(1, 1, 1)
|
60
|
+
ax.set_aspect(1)
|
61
|
+
ax.scatter(X[lab == 0, 0], X[lab == 0, 1], color = 'red')
|
62
|
+
ax.scatter(X[lab == 1, 0], X[lab == 1, 1], color = 'green')
|
63
|
+
if K == 3:
|
64
|
+
ax.scatter(X[lab == 2, 0], X[lab == 2, 1], color = 'blue')
|
65
|
+
|
66
|
+
fig.show()
|
67
|
+
plt.show()
|
68
|
+
```
|
69
|
+
|
70
|
+
makeGaussianData.py
|
71
|
+
```python
|
72
|
+
import numpy as np
|
73
|
+
|
74
|
+
|
75
|
+
def getData(nclass, seed = None):
|
76
|
+
|
77
|
+
assert nclass == 2 or nclass == 3
|
78
|
+
|
79
|
+
if seed != None:
|
80
|
+
np.random.seed(seed)
|
81
|
+
|
82
|
+
# 2次元の spherical な正規分布3つからデータを生成
|
83
|
+
X0 = 0.10 * np.random.randn(200, 2) + [ 0.3, 0.3 ]
|
84
|
+
X1 = 0.10 * np.random.randn(200, 2) + [ 0.7, 0.6 ]
|
85
|
+
X2 = 0.05 * np.random.randn(200, 2) + [ 0.3, 0.7 ]
|
86
|
+
|
87
|
+
# それらのラベル用のarray
|
88
|
+
lab0 = np.zeros(X0.shape[0], dtype = int)
|
89
|
+
lab1 = np.zeros(X1.shape[0], dtype = int) + 1
|
90
|
+
lab2 = np.zeros(X2.shape[0], dtype = int) + 2
|
91
|
+
|
92
|
+
# X (入力データ), label (クラスラベル), t(教師信号) をつくる
|
93
|
+
if nclass == 2:
|
94
|
+
X = np.vstack((X0, X1))
|
95
|
+
label = np.hstack((lab0, lab1))
|
96
|
+
t = np.zeros(X.shape[0])
|
97
|
+
t[label == 1] = 1.0
|
98
|
+
else:
|
99
|
+
X = np.vstack((X0, X1, X2))
|
100
|
+
label = np.hstack((lab0, lab1, lab2))
|
101
|
+
t = np.zeros((X.shape[0], nclass))
|
102
|
+
for ik in range(nclass):
|
103
|
+
t[label == ik, ik] = 1.0
|
104
|
+
|
105
|
+
return X, label, t
|
106
|
+
|
107
|
+
|
108
|
+
if __name__ == '__main__':
|
109
|
+
|
110
|
+
import matplotlib
|
111
|
+
import matplotlib.pyplot as plt
|
112
|
+
|
113
|
+
K = 3
|
114
|
+
|
115
|
+
X, lab, t = getData(K)
|
116
|
+
|
117
|
+
fig = plt.figure()
|
118
|
+
plt.xlim(-0.2, 1.2)
|
119
|
+
plt.ylim(-0.2, 1.2)
|
120
|
+
ax = fig.add_subplot(1, 1, 1)
|
121
|
+
ax.set_aspect(1)
|
122
|
+
ax.scatter(X[lab == 0, 0], X[lab == 0, 1], color = 'red')
|
123
|
+
ax.scatter(X[lab == 1, 0], X[lab == 1, 1], color = 'green')
|
124
|
+
if K == 3:
|
125
|
+
ax.scatter(X[lab == 2, 0], X[lab == 2, 1], color = 'blue')
|
126
|
+
plt.show()
|
127
|
+
```
|
128
|
+
以上2つのコードで2クラス識別器を作成しました。
|
1
画像の数式の補足をしました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,4 +8,7 @@
|
|
8
8
|
入力をx=(x_1,x_2,...,x_D)^Tとします。(D×1ベクトル)とし、出力がK個(クラスの数だけ)になるようなソフトマックス関数の実装方法がわからず困っています。(数式は理解しています。pythonでの実装方法がいまいちぴんと来ていません)
|
9
9
|
|
10
10
|
### 今回実装したいソフトマックスの式の形
|
11
|
-
](54b70acd4b4d7c08ffdf9d8a3401f736.png)
|
11
|
+
](54b70acd4b4d7c08ffdf9d8a3401f736.png)
|
12
|
+
|
13
|
+
(追記)
|
14
|
+
画像中のw、bは識別に使う重み(パラメータ)です。
|