質問編集履歴
5
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,270 @@
|
|
1
|
+
コード
|
2
|
+
|
3
|
+
```
|
4
|
+
|
5
|
+
import numpy as np
|
6
|
+
|
7
|
+
import matplotlib.pyplot as plt
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
class RBFkernel:
|
12
|
+
|
13
|
+
def __init__(self,*param):
|
14
|
+
|
15
|
+
self.param = list(param)
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
def __call__(self,x1,x2):
|
20
|
+
|
21
|
+
a,s,w = self.param
|
22
|
+
|
23
|
+
return a**2*np.exp(-((x1-x2)/s)**2) + w*(x1==x2)
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
x0 = (0,5,10,20,25,33,50)
|
28
|
+
|
29
|
+
y0 = (156,152,148,135,156,145,158)
|
30
|
+
|
31
|
+
x1 = np.linspace(0,50,201)
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
kernel = RBFkernel(10,5,3) # 適当なパラメータを使うカーネル関数
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
k00 = kernel(*np.meshgrid(x0,x0))
|
40
|
+
|
41
|
+
k00_1 = np.linalg.inv(k00) # 逆行列
|
42
|
+
|
43
|
+
k01 = kernel(*np.meshgrid(x0,x1,indexing='ij'))
|
44
|
+
|
45
|
+
k10 = k01.T
|
46
|
+
|
47
|
+
k11 = kernel(*np.meshgrid(x1,x1))
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
mu = k10.dot(k00_1.dot(y0))
|
54
|
+
|
55
|
+
sigma = k11 - k10.dot(k00_1.dot(k01))
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
plt.scatter(x0,y0,c='#ff77aa')
|
60
|
+
|
61
|
+
plt.plot(x1,mu,'g') # 推測された平均
|
62
|
+
|
63
|
+
std = np.sqrt(sigma.diagonal()) # 各点の標準偏差は共分散行列の対角成分
|
64
|
+
|
65
|
+
plt.fill_between(x1,mu-std,mu+std,alpha=0.2,color='g') # 推測された標準偏差の中の領域
|
66
|
+
|
67
|
+
plt.show()
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
class Kernel:
|
72
|
+
|
73
|
+
def __init__(self,param,bound=None):
|
74
|
+
|
75
|
+
self.param = np.array(param)
|
76
|
+
|
77
|
+
if(bound==None):
|
78
|
+
|
79
|
+
bound = np.zeros([len(param),2])
|
80
|
+
|
81
|
+
bound[:,1] = np.inf
|
82
|
+
|
83
|
+
self.bound = np.array(bound)
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
def __call__(self,x1,x2):
|
88
|
+
|
89
|
+
a,s,w = self.param
|
90
|
+
|
91
|
+
return a**2*np.exp(-0.5*((x1-x2)/s)**2) + w**2*(x1==x2)
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
class Gausskatei:
|
96
|
+
|
97
|
+
def __init__(self,kernel):
|
98
|
+
|
99
|
+
self.kernel = kernel
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
def gakushuu(self,x0,y0): # パラメータを調整せず学習
|
104
|
+
|
105
|
+
self.x0 = x0
|
106
|
+
|
107
|
+
self.y0 = y0
|
108
|
+
|
109
|
+
self.k00 = self.kernel(*np.meshgrid(x0,x0))
|
110
|
+
|
111
|
+
self.k00_1 = np.linalg.inv(self.k00)
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
def yosoku(self,x): # xからyを予測
|
116
|
+
|
117
|
+
k00_1 = self.k00_1
|
118
|
+
|
119
|
+
k01 = self.kernel(*np.meshgrid(self.x0,x,indexing='ij'))
|
120
|
+
|
121
|
+
k10 = k01.T
|
122
|
+
|
123
|
+
k11 = self.kernel(*np.meshgrid(x,x))
|
124
|
+
|
125
|
+
mu = k10.dot(k00_1.dot(self.y0))
|
126
|
+
|
127
|
+
sigma = k11 - k10.dot(k00_1.dot(k01))
|
128
|
+
|
129
|
+
std = np.sqrt(sigma.diagonal())
|
130
|
+
|
131
|
+
return mu,std
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
def logyuudo(self,param=None): # 対数尤度
|
136
|
+
|
137
|
+
if(param is None):
|
138
|
+
|
139
|
+
k00 = self.k00
|
140
|
+
|
141
|
+
k00_1 = self.k00_1
|
142
|
+
|
143
|
+
else:
|
144
|
+
|
145
|
+
self.kernel.param = param
|
146
|
+
|
147
|
+
k00 = self.kernel(*np.meshgrid(self.x0,self.x0))
|
148
|
+
|
149
|
+
k00_1 = np.linalg.inv(k00)
|
150
|
+
|
151
|
+
return -(np.linalg.slogdet(k00)[1]+self.y0.dot(k00_1.dot(self.y0)))
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
def saitekika(self,x0,y0,kurikaeshi=1000): # パラメータを調整して学習
|
156
|
+
|
157
|
+
self.x0 = x0
|
158
|
+
|
159
|
+
self.y0 = y0
|
160
|
+
|
161
|
+
param = self.kernel.param
|
162
|
+
|
163
|
+
logbound = np.log(self.kernel.bound)
|
164
|
+
|
165
|
+
s = (logbound[:,1]-logbound[:,0])/10.
|
166
|
+
|
167
|
+
n_param = len(param)
|
168
|
+
|
169
|
+
theta0 = np.log(param)
|
170
|
+
|
171
|
+
p0 = self.logyuudo(param)
|
172
|
+
|
173
|
+
lis_theta = []
|
174
|
+
|
175
|
+
lis_p = []
|
176
|
+
|
177
|
+
for i in range(kurikaeshi):
|
178
|
+
|
179
|
+
idou = np.random.normal(0,s,n_param)
|
180
|
+
|
181
|
+
hazure = (theta0+idou<logbound[:,0])|(theta0+idou>logbound[:,1])
|
182
|
+
|
183
|
+
while(np.any(hazure)):
|
184
|
+
|
185
|
+
idou[hazure] = np.random.normal(0,s,n_param)[hazure]
|
186
|
+
|
187
|
+
hazure = (theta0+idou<logbound[:,0])|(theta0+idou>logbound[:,1])
|
188
|
+
|
189
|
+
theta1 = theta0 + idou
|
190
|
+
|
191
|
+
param = np.exp(theta1)
|
192
|
+
|
193
|
+
p1 = self.logyuudo(param)
|
194
|
+
|
195
|
+
r = np.exp(p1-p0)
|
196
|
+
|
197
|
+
if(r>=1 or r>np.random.random()):
|
198
|
+
|
199
|
+
theta0 = theta1
|
200
|
+
|
201
|
+
p0 = p1
|
202
|
+
|
203
|
+
lis_theta.append(theta0)
|
204
|
+
|
205
|
+
lis_p.append(p0)
|
206
|
+
|
207
|
+
self.ar_theta = np.array(lis_theta)
|
208
|
+
|
209
|
+
self.ar_p = np.array(lis_p)
|
210
|
+
|
211
|
+
self.kernel.param = np.exp(lis_theta[np.argmax(lis_p)])
|
212
|
+
|
213
|
+
self.k00 = self.kernel(*np.meshgrid(x0,x0))
|
214
|
+
|
215
|
+
self.k00_1 = np.linalg.inv(self.k00)
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
n = 7 # 既知の点の数
|
222
|
+
|
223
|
+
x0 = (0,5,10,20,25,33,50) # 既知の点
|
224
|
+
|
225
|
+
y0 = (156,152,148,135,156,145,158)
|
226
|
+
|
227
|
+
param0 = [10,5,1] # パラメータの初期値
|
228
|
+
|
229
|
+
bound = [[1e-2,1e2],[1e-2,1e2],[1e-2,1e2]] # 下限上限
|
230
|
+
|
231
|
+
kernel = Kernel(param0,bound)
|
232
|
+
|
233
|
+
x1 = np.linspace(0,50,200)
|
234
|
+
|
235
|
+
gp = Gausskatei(kernel)
|
236
|
+
|
237
|
+
gp.gakushuu(x0,y0) # パラメータを調整せずに学習
|
238
|
+
|
239
|
+
plt.figure(figsize=[5,8])
|
240
|
+
|
241
|
+
for i in [0,1]:
|
242
|
+
|
243
|
+
if(i):
|
244
|
+
|
245
|
+
gp.saitekika(x0,y0,1000) # パラメータを調整する
|
246
|
+
|
247
|
+
plt.subplot(211+i)
|
248
|
+
|
249
|
+
plt.plot(x0,y0,'. ')
|
250
|
+
|
251
|
+
mu,std = gp.yosoku(x1)
|
252
|
+
|
253
|
+
plt.plot(x1,mu,'g')
|
254
|
+
|
255
|
+
plt.fill_between(x1,mu-std,mu+std,alpha=0.2,color='g')
|
256
|
+
|
257
|
+
plt.title('a=%.3f, s=%.3f, w=%.3f'%tuple(gp.kernel.param))
|
258
|
+
|
259
|
+
plt.tight_layout()
|
260
|
+
|
261
|
+
plt.show()
|
262
|
+
|
263
|
+
```
|
264
|
+
|
265
|
+
コード
|
266
|
+
|
1
|
-
### 前提・実現したいこと
|
267
|
+
```### 前提・実現したいこと
|
2
268
|
|
3
269
|
https://qiita.com/phyblas/items/d756803ec932ab621c56
|
4
270
|
|
@@ -62,266 +328,52 @@
|
|
62
328
|
|
63
329
|
### 該当のソースコード
|
64
330
|
|
65
|
-
|
331
|
+
|
66
|
-
|
67
|
-
|
332
|
+
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
class RBFkernel:
|
72
|
-
|
73
|
-
def __init__(self,*param):
|
74
|
-
|
75
|
-
self.param = list(param)
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
def __call__(self,x1,x2):
|
80
|
-
|
81
|
-
|
333
|
+
n = 7 # 既知の点の数
|
82
|
-
|
83
|
-
|
334
|
+
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
x0 = (0,5,10,20,25,33,50)
|
335
|
+
x0 = (0,5,10,20,25,33,50) # 既知の点
|
88
336
|
|
89
337
|
y0 = (156,152,148,135,156,145,158)
|
90
338
|
|
339
|
+
param0 = [10,5,1] # パラメータの初期値
|
340
|
+
|
341
|
+
bound = [[1e-2,1e2],[1e-2,1e2],[1e-2,1e2]] # 下限上限
|
342
|
+
|
343
|
+
kernel = Kernel(param0,bound)
|
344
|
+
|
91
|
-
x1 = np.linspace(0,50,20
|
345
|
+
x1 = np.linspace(0,50,200)
|
346
|
+
|
92
|
-
|
347
|
+
gp = Gausskatei(kernel)
|
93
|
-
|
94
|
-
|
348
|
+
|
95
|
-
k
|
349
|
+
gp.gakushuu(x0,y0) # パラメータを調整せずに学習
|
96
|
-
|
97
|
-
|
98
|
-
|
350
|
+
|
99
|
-
|
351
|
+
plt.figure(figsize=[5,8])
|
100
|
-
|
101
|
-
|
352
|
+
|
102
|
-
|
103
|
-
k01 = kernel(*np.meshgrid(x0,x1,indexing='ij'))
|
104
|
-
|
105
|
-
|
353
|
+
for i in [0,1]:
|
106
|
-
|
107
|
-
|
354
|
+
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
355
|
+
if(i):
|
114
|
-
|
356
|
+
|
115
|
-
|
357
|
+
gp.saitekika(x0,y0,1000) # パラメータを調整する
|
358
|
+
|
116
|
-
|
359
|
+
plt.subplot(211+i)
|
117
|
-
|
118
|
-
|
360
|
+
|
119
|
-
plt.
|
361
|
+
plt.plot(x0,y0,'. ')
|
362
|
+
|
120
|
-
|
363
|
+
mu,std = gp.yosoku(x1)
|
364
|
+
|
121
|
-
plt.plot(x1,mu,'g')
|
365
|
+
plt.plot(x1,mu,'g')
|
122
|
-
|
123
|
-
|
366
|
+
|
124
|
-
|
125
|
-
plt.fill_between(x1,mu-std,mu+std,alpha=0.2,color='g')
|
367
|
+
plt.fill_between(x1,mu-std,mu+std,alpha=0.2,color='g')
|
368
|
+
|
369
|
+
plt.title('a=%.3f, s=%.3f, w=%.3f'%tuple(gp.kernel.param))
|
370
|
+
|
371
|
+
plt.tight_layout()
|
126
372
|
|
127
373
|
plt.show()
|
128
374
|
|
129
375
|
|
130
376
|
|
131
|
-
class Kernel:
|
132
|
-
|
133
|
-
def __init__(self,param,bound=None):
|
134
|
-
|
135
|
-
self.param = np.array(param)
|
136
|
-
|
137
|
-
if(bound==None):
|
138
|
-
|
139
|
-
bound = np.zeros([len(param),2])
|
140
|
-
|
141
|
-
bound[:,1] = np.inf
|
142
|
-
|
143
|
-
self.bound = np.array(bound)
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
def __call__(self,x1,x2):
|
148
|
-
|
149
|
-
a,s,w = self.param
|
150
|
-
|
151
|
-
return a**2*np.exp(-0.5*((x1-x2)/s)**2) + w**2*(x1==x2)
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
class Gausskatei:
|
156
|
-
|
157
|
-
def __init__(self,kernel):
|
158
|
-
|
159
|
-
self.kernel = kernel
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
def gakushuu(self,x0,y0): # パラメータを調整せず学習
|
164
|
-
|
165
|
-
self.x0 = x0
|
166
|
-
|
167
|
-
self.y0 = y0
|
168
|
-
|
169
|
-
self.k00 = self.kernel(*np.meshgrid(x0,x0))
|
170
|
-
|
171
|
-
self.k00_1 = np.linalg.inv(self.k00)
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
def yosoku(self,x): # xからyを予測
|
176
|
-
|
177
|
-
k00_1 = self.k00_1
|
178
|
-
|
179
|
-
k01 = self.kernel(*np.meshgrid(self.x0,x,indexing='ij'))
|
180
|
-
|
181
|
-
k10 = k01.T
|
182
|
-
|
183
|
-
k11 = self.kernel(*np.meshgrid(x,x))
|
184
|
-
|
185
|
-
mu = k10.dot(k00_1.dot(self.y0))
|
186
|
-
|
187
|
-
sigma = k11 - k10.dot(k00_1.dot(k01))
|
188
|
-
|
189
|
-
std = np.sqrt(sigma.diagonal())
|
190
|
-
|
191
|
-
return mu,std
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
def logyuudo(self,param=None): # 対数尤度
|
196
|
-
|
197
|
-
if(param is None):
|
198
|
-
|
199
|
-
k00 = self.k00
|
200
|
-
|
201
|
-
k00_1 = self.k00_1
|
202
|
-
|
203
|
-
else:
|
204
|
-
|
205
|
-
self.kernel.param = param
|
206
|
-
|
207
|
-
k00 = self.kernel(*np.meshgrid(self.x0,self.x0))
|
208
|
-
|
209
|
-
k00_1 = np.linalg.inv(k00)
|
210
|
-
|
211
|
-
return -(np.linalg.slogdet(k00)[1]+self.y0.dot(k00_1.dot(self.y0)))
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
def saitekika(self,x0,y0,kurikaeshi=1000): # パラメータを調整して学習
|
216
|
-
|
217
|
-
self.x0 = x0
|
218
|
-
|
219
|
-
self.y0 = y0
|
220
|
-
|
221
|
-
param = self.kernel.param
|
222
|
-
|
223
|
-
logbound = np.log(self.kernel.bound)
|
224
|
-
|
225
|
-
s = (logbound[:,1]-logbound[:,0])/10.
|
226
|
-
|
227
|
-
n_param = len(param)
|
228
|
-
|
229
|
-
theta0 = np.log(param)
|
230
|
-
|
231
|
-
p0 = self.logyuudo(param)
|
232
|
-
|
233
|
-
lis_theta = []
|
234
|
-
|
235
|
-
lis_p = []
|
236
|
-
|
237
|
-
for i in range(kurikaeshi):
|
238
|
-
|
239
|
-
idou = np.random.normal(0,s,n_param)
|
240
|
-
|
241
|
-
hazure = (theta0+idou<logbound[:,0])|(theta0+idou>logbound[:,1])
|
242
|
-
|
243
|
-
while(np.any(hazure)):
|
244
|
-
|
245
|
-
idou[hazure] = np.random.normal(0,s,n_param)[hazure]
|
246
|
-
|
247
|
-
hazure = (theta0+idou<logbound[:,0])|(theta0+idou>logbound[:,1])
|
248
|
-
|
249
|
-
theta1 = theta0 + idou
|
250
|
-
|
251
|
-
param = np.exp(theta1)
|
252
|
-
|
253
|
-
p1 = self.logyuudo(param)
|
254
|
-
|
255
|
-
r = np.exp(p1-p0)
|
256
|
-
|
257
|
-
if(r>=1 or r>np.random.random()):
|
258
|
-
|
259
|
-
theta0 = theta1
|
260
|
-
|
261
|
-
p0 = p1
|
262
|
-
|
263
|
-
lis_theta.append(theta0)
|
264
|
-
|
265
|
-
lis_p.append(p0)
|
266
|
-
|
267
|
-
self.ar_theta = np.array(lis_theta)
|
268
|
-
|
269
|
-
self.ar_p = np.array(lis_p)
|
270
|
-
|
271
|
-
self.kernel.param = np.exp(lis_theta[np.argmax(lis_p)])
|
272
|
-
|
273
|
-
self.k00 = self.kernel(*np.meshgrid(x0,x0))
|
274
|
-
|
275
|
-
self.k00_1 = np.linalg.inv(self.k00)
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
n = 7 # 既知の点の数
|
282
|
-
|
283
|
-
x0 = (0,5,10,20,25,33,50) # 既知の点
|
284
|
-
|
285
|
-
y0 = (156,152,148,135,156,145,158)
|
286
|
-
|
287
|
-
param0 = [10,5,1] # パラメータの初期値
|
288
|
-
|
289
|
-
bound = [[1e-2,1e2],[1e-2,1e2],[1e-2,1e2]] # 下限上限
|
290
|
-
|
291
|
-
kernel = Kernel(param0,bound)
|
292
|
-
|
293
|
-
x1 = np.linspace(0,50,200)
|
294
|
-
|
295
|
-
gp = Gausskatei(kernel)
|
296
|
-
|
297
|
-
gp.gakushuu(x0,y0) # パラメータを調整せずに学習
|
298
|
-
|
299
|
-
plt.figure(figsize=[5,8])
|
300
|
-
|
301
|
-
for i in [0,1]:
|
302
|
-
|
303
|
-
if(i):
|
304
|
-
|
305
|
-
gp.saitekika(x0,y0,1000) # パラメータを調整する
|
306
|
-
|
307
|
-
plt.subplot(211+i)
|
308
|
-
|
309
|
-
plt.plot(x0,y0,'. ')
|
310
|
-
|
311
|
-
mu,std = gp.yosoku(x1)
|
312
|
-
|
313
|
-
plt.plot(x1,mu,'g')
|
314
|
-
|
315
|
-
plt.fill_between(x1,mu-std,mu+std,alpha=0.2,color='g')
|
316
|
-
|
317
|
-
plt.title('a=%.3f, s=%.3f, w=%.3f'%tuple(gp.kernel.param))
|
318
|
-
|
319
|
-
plt.tight_layout()
|
320
|
-
|
321
|
-
plt.show()
|
322
|
-
|
323
|
-
|
324
|
-
|
325
377
|
### 試したこと
|
326
378
|
|
327
379
|
|
4
タグ変更
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
3
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -108,7 +108,7 @@
|
|
108
108
|
|
109
109
|
|
110
110
|
|
111
|
-
|
111
|
+
|
112
112
|
|
113
113
|
mu = k10.dot(k00_1.dot(y0))
|
114
114
|
|
2
コード補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -62,6 +62,72 @@
|
|
62
62
|
|
63
63
|
### 該当のソースコード
|
64
64
|
|
65
|
+
import numpy as np
|
66
|
+
|
67
|
+
import matplotlib.pyplot as plt
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
class RBFkernel:
|
72
|
+
|
73
|
+
def __init__(self,*param):
|
74
|
+
|
75
|
+
self.param = list(param)
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
def __call__(self,x1,x2):
|
80
|
+
|
81
|
+
a,s,w = self.param
|
82
|
+
|
83
|
+
return a**2*np.exp(-((x1-x2)/s)**2) + w*(x1==x2)
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
x0 = (0,5,10,20,25,33,50)
|
88
|
+
|
89
|
+
y0 = (156,152,148,135,156,145,158)
|
90
|
+
|
91
|
+
x1 = np.linspace(0,50,201)
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
kernel = RBFkernel(10,5,3) # 適当なパラメータを使うカーネル関数
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
k00 = kernel(*np.meshgrid(x0,x0))
|
100
|
+
|
101
|
+
k00_1 = np.linalg.inv(k00) # 逆行列
|
102
|
+
|
103
|
+
k01 = kernel(*np.meshgrid(x0,x1,indexing='ij'))
|
104
|
+
|
105
|
+
k10 = k01.T
|
106
|
+
|
107
|
+
k11 = kernel(*np.meshgrid(x1,x1))
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
# ここでは上述の方程式の通りのμとΣ
|
112
|
+
|
113
|
+
mu = k10.dot(k00_1.dot(y0))
|
114
|
+
|
115
|
+
sigma = k11 - k10.dot(k00_1.dot(k01))
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
plt.scatter(x0,y0,c='#ff77aa')
|
120
|
+
|
121
|
+
plt.plot(x1,mu,'g') # 推測された平均
|
122
|
+
|
123
|
+
std = np.sqrt(sigma.diagonal()) # 各点の標準偏差は共分散行列の対角成分
|
124
|
+
|
125
|
+
plt.fill_between(x1,mu-std,mu+std,alpha=0.2,color='g') # 推測された標準偏差の中の領域
|
126
|
+
|
127
|
+
plt.show()
|
128
|
+
|
129
|
+
|
130
|
+
|
65
131
|
class Kernel:
|
66
132
|
|
67
133
|
def __init__(self,param,bound=None):
|
1
コードとエラーメッセージ補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,13 +8,207 @@
|
|
8
8
|
|
9
9
|
### 発生している問題・エラーメッセージ
|
10
10
|
|
11
|
+
---------------------------------------------------------------------------
|
12
|
+
|
13
|
+
AttributeError Traceback (most recent call last)
|
14
|
+
|
15
|
+
<ipython-input-63-3d592025a82f> in <module>()
|
16
|
+
|
17
|
+
11 for i in [0,1]:
|
18
|
+
|
19
|
+
12 if(i):
|
20
|
+
|
21
|
+
---> 13 gp.saitekika(x0,y0,1000) # パラメータを調整する
|
22
|
+
|
23
|
+
14 plt.subplot(211+i)
|
24
|
+
|
25
|
+
15 plt.plot(x0,y0,'. ')
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
1 frames
|
30
|
+
|
31
|
+
<ipython-input-62-54f0617fec3e> in saitekika(self, x0, y0, kurikaeshi)
|
32
|
+
|
33
|
+
49 n_param = len(param)
|
34
|
+
|
35
|
+
50 theta0 = np.log(param)
|
36
|
+
|
37
|
+
---> 51 p0 = self.logyuudo(param)
|
38
|
+
|
39
|
+
52 lis_theta = []
|
40
|
+
|
41
|
+
53 lis_p = []
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
<ipython-input-62-54f0617fec3e> in logyuudo(self, param)
|
46
|
+
|
47
|
+
39 k00 = self.kernel(*np.meshgrid(self.x0,self.x0))
|
48
|
+
|
49
|
+
40 k00_1 = np.linalg.inv(k00)
|
50
|
+
|
51
|
+
---> 41 return -(np.linalg.slogdet(k00)[1]+self.y0.dot(k00_1.dot(self.y0)))
|
52
|
+
|
53
|
+
42
|
54
|
+
|
55
|
+
43 def saitekika(self,x0,y0,kurikaeshi=1000): # パラメータを調整して学習
|
56
|
+
|
57
|
+
|
58
|
+
|
11
|
-
|
59
|
+
AttributeError: 'tuple' object has no attribute 'dot'
|
12
60
|
|
13
61
|
|
14
62
|
|
15
63
|
### 該当のソースコード
|
16
64
|
|
65
|
+
class Kernel:
|
66
|
+
|
67
|
+
def __init__(self,param,bound=None):
|
68
|
+
|
69
|
+
self.param = np.array(param)
|
70
|
+
|
71
|
+
if(bound==None):
|
72
|
+
|
73
|
+
bound = np.zeros([len(param),2])
|
74
|
+
|
75
|
+
bound[:,1] = np.inf
|
76
|
+
|
77
|
+
self.bound = np.array(bound)
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
def __call__(self,x1,x2):
|
82
|
+
|
83
|
+
a,s,w = self.param
|
84
|
+
|
85
|
+
return a**2*np.exp(-0.5*((x1-x2)/s)**2) + w**2*(x1==x2)
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
class Gausskatei:
|
90
|
+
|
91
|
+
def __init__(self,kernel):
|
92
|
+
|
93
|
+
self.kernel = kernel
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
def gakushuu(self,x0,y0): # パラメータを調整せず学習
|
98
|
+
|
99
|
+
self.x0 = x0
|
100
|
+
|
101
|
+
self.y0 = y0
|
102
|
+
|
103
|
+
self.k00 = self.kernel(*np.meshgrid(x0,x0))
|
104
|
+
|
105
|
+
self.k00_1 = np.linalg.inv(self.k00)
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
def yosoku(self,x): # xからyを予測
|
110
|
+
|
111
|
+
k00_1 = self.k00_1
|
112
|
+
|
113
|
+
k01 = self.kernel(*np.meshgrid(self.x0,x,indexing='ij'))
|
114
|
+
|
115
|
+
k10 = k01.T
|
116
|
+
|
117
|
+
k11 = self.kernel(*np.meshgrid(x,x))
|
118
|
+
|
119
|
+
mu = k10.dot(k00_1.dot(self.y0))
|
120
|
+
|
121
|
+
sigma = k11 - k10.dot(k00_1.dot(k01))
|
122
|
+
|
123
|
+
std = np.sqrt(sigma.diagonal())
|
124
|
+
|
125
|
+
return mu,std
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
def logyuudo(self,param=None): # 対数尤度
|
130
|
+
|
131
|
+
if(param is None):
|
132
|
+
|
133
|
+
k00 = self.k00
|
134
|
+
|
135
|
+
k00_1 = self.k00_1
|
136
|
+
|
137
|
+
else:
|
138
|
+
|
139
|
+
self.kernel.param = param
|
140
|
+
|
141
|
+
k00 = self.kernel(*np.meshgrid(self.x0,self.x0))
|
142
|
+
|
143
|
+
k00_1 = np.linalg.inv(k00)
|
144
|
+
|
145
|
+
return -(np.linalg.slogdet(k00)[1]+self.y0.dot(k00_1.dot(self.y0)))
|
146
|
+
|
147
|
+
|
148
|
+
|
17
|
-
|
149
|
+
def saitekika(self,x0,y0,kurikaeshi=1000): # パラメータを調整して学習
|
150
|
+
|
151
|
+
self.x0 = x0
|
152
|
+
|
153
|
+
self.y0 = y0
|
154
|
+
|
155
|
+
param = self.kernel.param
|
156
|
+
|
157
|
+
logbound = np.log(self.kernel.bound)
|
158
|
+
|
159
|
+
s = (logbound[:,1]-logbound[:,0])/10.
|
160
|
+
|
161
|
+
n_param = len(param)
|
162
|
+
|
163
|
+
theta0 = np.log(param)
|
164
|
+
|
165
|
+
p0 = self.logyuudo(param)
|
166
|
+
|
167
|
+
lis_theta = []
|
168
|
+
|
169
|
+
lis_p = []
|
170
|
+
|
171
|
+
for i in range(kurikaeshi):
|
172
|
+
|
173
|
+
idou = np.random.normal(0,s,n_param)
|
174
|
+
|
175
|
+
hazure = (theta0+idou<logbound[:,0])|(theta0+idou>logbound[:,1])
|
176
|
+
|
177
|
+
while(np.any(hazure)):
|
178
|
+
|
179
|
+
idou[hazure] = np.random.normal(0,s,n_param)[hazure]
|
180
|
+
|
181
|
+
hazure = (theta0+idou<logbound[:,0])|(theta0+idou>logbound[:,1])
|
182
|
+
|
183
|
+
theta1 = theta0 + idou
|
184
|
+
|
185
|
+
param = np.exp(theta1)
|
186
|
+
|
187
|
+
p1 = self.logyuudo(param)
|
188
|
+
|
189
|
+
r = np.exp(p1-p0)
|
190
|
+
|
191
|
+
if(r>=1 or r>np.random.random()):
|
192
|
+
|
193
|
+
theta0 = theta1
|
194
|
+
|
195
|
+
p0 = p1
|
196
|
+
|
197
|
+
lis_theta.append(theta0)
|
198
|
+
|
199
|
+
lis_p.append(p0)
|
200
|
+
|
201
|
+
self.ar_theta = np.array(lis_theta)
|
202
|
+
|
203
|
+
self.ar_p = np.array(lis_p)
|
204
|
+
|
205
|
+
self.kernel.param = np.exp(lis_theta[np.argmax(lis_p)])
|
206
|
+
|
207
|
+
self.k00 = self.kernel(*np.meshgrid(x0,x0))
|
208
|
+
|
209
|
+
self.k00_1 = np.linalg.inv(self.k00)
|
210
|
+
|
211
|
+
|
18
212
|
|
19
213
|
|
20
214
|
|