質問編集履歴

2

大幅に質問内容を変更しました

2017/12/22 09:13

投稿

kamome01
kamome01

スコア16

test CHANGED
File without changes
test CHANGED
@@ -5,653 +5,3 @@
5
5
  現在は,時系列データを1パターンだけ学習させ,テストデータを与えるところまでは出ています.
6
6
 
7
7
  具体的には,時系列データのdatファイルが30個あり,15個を学習データ,15個をテストデータとして用いたいのです.
8
-
9
-
10
-
11
-
12
-
13
- ###該当のソースコード
14
-
15
- ```python
16
-
17
- import numpy as np
18
-
19
- import matplotlib.pyplot as plt
20
-
21
- from keras.models import Sequential
22
-
23
- from keras.layers.core import Dense, Activation
24
-
25
- from keras.layers.recurrent import GRU
26
-
27
- from keras.optimizers import Adam
28
-
29
- from keras.callbacks import EarlyStopping
30
-
31
- from sklearn.model_selection import train_test_split
32
-
33
- import os
34
-
35
-
36
-
37
- np.random.seed(0)
38
-
39
-
40
-
41
- def zscore(x, axis = None):
42
-
43
- xmean = x.mean(axis=axis, keepdims=True)
44
-
45
- xstd = np.std(x, axis=axis, keepdims=True)
46
-
47
- zscore = (x-xmean)/xstd
48
-
49
- return zscore
50
-
51
-
52
-
53
- def read(N=10, T=200):
54
-
55
- a=np.loadtxt('pre10.dat',delimiter=' ',usecols=0)
56
-
57
- b=np.loadtxt('pre10.dat',delimiter=' ',usecols=3)
58
-
59
- c=np.loadtxt('pre10.dat',delimiter=' ',usecols=1)
60
-
61
- d=np.loadtxt('pre10.dat',delimiter=' ',usecols=2)
62
-
63
- a=zscore(a)
64
-
65
- c=zscore(c)
66
-
67
- d=zscore(d)
68
-
69
- signals = np.zeros((N,T))
70
-
71
- for i in range(N):
72
-
73
- signals[i] = a[i]
74
-
75
- sig2=np.zeros((N,T))
76
-
77
- for i in range(N):
78
-
79
- sig2[i] = c[i]
80
-
81
- sig3=np.zeros((N,T))
82
-
83
- for i in range(N):
84
-
85
- sig3[i] = d[i]
86
-
87
- masks = np.zeros((N, T))
88
-
89
- for i in range(N):
90
-
91
- masks[i] = b[i]
92
-
93
- data = np.zeros((N, T, 3))
94
-
95
- data[:, :, 0] = signals[:]
96
-
97
- data[:, :, 1] = sig2[:]
98
-
99
- data[:, :, 2] = sig3[:]
100
-
101
- target = np.zeros((N,T))
102
-
103
- for i in range(N):
104
-
105
- target[i]=b[i]
106
-
107
- return (data, target)
108
-
109
-
110
-
111
- def read_test(N=10, T=200):
112
-
113
- a=np.loadtxt('pre14.dat',delimiter=' ',usecols=0)
114
-
115
- c=np.loadtxt('pre14.dat',delimiter=' ',usecols=1)
116
-
117
- d=np.loadtxt('pre14.dat',delimiter=' ',usecols=2)
118
-
119
- a=zscore(a)
120
-
121
- c=zscore(c)
122
-
123
- d=zscore(d)
124
-
125
- signals = np.zeros((N,T))
126
-
127
- for i in range(N):
128
-
129
- signals[i] = a[i]
130
-
131
- sig2=np.zeros((N,T))
132
-
133
- for i in range(N):
134
-
135
- sig2[i] = c[i]
136
-
137
- sig3=np.zeros((N,T))
138
-
139
- for i in range(N):
140
-
141
- sig3[i] = d[i]
142
-
143
- data = np.zeros((N, T, 3))
144
-
145
- data[:, :, 0] = signals[:]
146
-
147
- data[:, :, 1] = sig2[:]
148
-
149
- data[:, :, 2] = sig3[:]
150
-
151
- return (data)
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
- '''
160
-
161
- モデルファイル用設定
162
-
163
- '''
164
-
165
- MODEL_DIR = os.path.join(os.path.dirname(__file__), 'model')
166
-
167
-
168
-
169
- if os.path.exists(MODEL_DIR) is False:
170
-
171
- os.mkdir(MODEL_DIR)
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
- '''
180
-
181
- データの読み取り
182
-
183
- '''
184
-
185
- N = 13999
186
-
187
- T = 200
188
-
189
- maxlen = T
190
-
191
-
192
-
193
- X, Y = read(N=N, T=T)
194
-
195
- X_test=read_test(N=N,T=T)
196
-
197
- N_train = int(N * 0.9)
198
-
199
- N_validation = N - N_train
200
-
201
-
202
-
203
- '''
204
-
205
- モデル設定
206
-
207
- '''
208
-
209
- n_in = len(X[0][0]) # 2
210
-
211
- n_hidden = 100
212
-
213
- n_out = len(Y[0]) # 1
214
-
215
-
216
-
217
-
218
-
219
- def weight_variable(shape, name=None):
220
-
221
- return np.random.normal(scale=.01, size=shape)
222
-
223
-
224
-
225
-
226
-
227
- early_stopping = EarlyStopping(monitor='loss', patience=100, verbose=1)
228
-
229
-
230
-
231
- kernel=weight_variable
232
-
233
- kernel2=weight_variable
234
-
235
-
236
-
237
- model = Sequential()
238
-
239
- model.add(GRU(n_hidden,
240
-
241
- #kernel_initializer=kernel,
242
-
243
- input_shape=(maxlen, n_in)))
244
-
245
- model.add(Dense(n_out))
246
-
247
- model.add(Activation('linear'))
248
-
249
-
250
-
251
- optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)
252
-
253
- model.compile(loss='mean_squared_error',
254
-
255
- optimizer=optimizer)
256
-
257
-
258
-
259
- '''
260
-
261
- モデル学習
262
-
263
- '''
264
-
265
-
266
-
267
- epochs = 1000
268
-
269
- batch_size = 100
270
-
271
-
272
-
273
- hist = model.fit(X, Y,
274
-
275
- batch_size=batch_size,
276
-
277
- epochs=epochs,
278
-
279
- callbacks=[early_stopping])
280
-
281
-
282
-
283
- model.save(MODEL_DIR+'/model_relu.hdf5')
284
-
285
- print('Model saved')
286
-
287
-
288
-
289
- '''
290
-
291
- 学習の進み具合を可視化
292
-
293
- '''
294
-
295
-
296
-
297
- loss = hist.history['loss']
298
-
299
-
300
-
301
- plt.figure(1)
302
-
303
- plt.rc('font', family='serif')
304
-
305
- plt.plot(range(len(loss)), loss, label='loss', color='black')
306
-
307
- plt.xlabel('epochs')
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
- predicted = model.predict(X_test)
316
-
317
- A = np.loadtxt('pre14.dat',delimiter=' ',usecols=0)
318
-
319
- A=zscore(A)
320
-
321
-
322
-
323
- plt.figure(2)
324
-
325
- plt.ylim([-3, 3])
326
-
327
- plt.plot(A, color='#aaaaaa')
328
-
329
- #plt.plot(B,color='black' )
330
-
331
- plt.plot(predicted, color='red')
332
-
333
- plt.show()
334
-
335
- #plt.savefig(__file__ + '.eps')
336
-
337
-
338
-
339
- ```
340
-
341
-
342
-
343
- ###試したこと
344
-
345
- https://teratail.com/questions/105989 で回答を頂いたように,
346
-
347
- ```
348
-
349
- dat_paths = glob.glob("pre*.dat")
350
-
351
- a = [np.loadtxt(dat_path,delimiter=' ',usecols=0)for dat_path in dat_paths]
352
-
353
- b = [np.loadtxt(dat_path,delimiter=' ',usecols=3)for dat_path in dat_paths]
354
-
355
- c = [np.loadtxt(dat_path,delimiter=' ',usecols=1)for dat_path in dat_paths]
356
-
357
- d = [np.loadtxt(dat_path,delimiter=' ',usecols=2)for dat_path in dat_paths]
358
-
359
- ```
360
-
361
- 上記のように読み込み関数を書き換えたのですが,これだけではそれ以降の処理でエラーが出るのと,なにより複数サンプル学習させる方法として正しいのかわかりません.
362
-
363
- ちなみに,他にも少し書き換えているのでコードの全容を載せておきます
364
-
365
- ```
366
-
367
- import numpy as np
368
-
369
- import matplotlib.pyplot as plt
370
-
371
- from keras.models import Sequential
372
-
373
- from keras.layers.core import Dense, Activation
374
-
375
- from keras.layers.recurrent import GRU
376
-
377
- from keras.optimizers import Adam
378
-
379
- from keras.callbacks import EarlyStopping
380
-
381
- from sklearn.model_selection import train_test_split
382
-
383
- from sklearn.utils import shuffle
384
-
385
- import os
386
-
387
- import glob
388
-
389
-
390
-
391
-
392
-
393
- np.random.seed(0)
394
-
395
-
396
-
397
- def zscore(x, axis = None):
398
-
399
- xmean = x.mean(axis=axis, keepdims=True)
400
-
401
- xstd = np.std(x, axis=axis, keepdims=True)
402
-
403
- zscore = (x-xmean)/xstd
404
-
405
- return zscore
406
-
407
-
408
-
409
- def read(N=10, T=200):
410
-
411
- dat_paths = glob.glob("pre*.dat")
412
-
413
- A = [np.loadtxt(dat_path,delimiter=' ',usecols=0)for dat_path in dat_paths]
414
-
415
- b = [np.loadtxt(dat_path,delimiter=' ',usecols=3)for dat_path in dat_paths]
416
-
417
- C = [np.loadtxt(dat_path,delimiter=' ',usecols=1)for dat_path in dat_paths]
418
-
419
- D = [np.loadtxt(dat_path,delimiter=' ',usecols=2)for dat_path in dat_paths]
420
-
421
- #a=np.loadtxt('*.dat',delimiter=' ',usecols=0)
422
-
423
- #b=np.loadtxt('*.dat',delimiter=' ',usecols=3)
424
-
425
- #c=np.loadtxt('*.dat',delimiter=' ',usecols=1)
426
-
427
- #d=np.loadtxt('*.dat',delimiter=' ',usecols=2)
428
-
429
-
430
-
431
- signals = np.zeros((N,T))
432
-
433
- for i in range(N):
434
-
435
- signals[i] = a[i]
436
-
437
- sig2=np.zeros((N,T))
438
-
439
- for i in range(N):
440
-
441
- sig2[i] = c[i]
442
-
443
- sig3=np.zeros((N,T))
444
-
445
- for i in range(N):
446
-
447
- sig3[i] = d[i]
448
-
449
- masks = np.zeros((N, T))
450
-
451
- for i in range(N):
452
-
453
- masks[i] = b[i]
454
-
455
- data = np.zeros((N, T, 3))
456
-
457
- data[:, :, 0] = signals[:]
458
-
459
- data[:, :, 1] = sig2[:]
460
-
461
- data[:, :, 2] = sig3[:]
462
-
463
- target = np.zeros((N,T))
464
-
465
- for i in range(N):
466
-
467
- target[i]=b[i]
468
-
469
- return (data, target)
470
-
471
-
472
-
473
-
474
-
475
- '''
476
-
477
- モデルファイル用設定
478
-
479
- '''
480
-
481
- MODEL_DIR = os.path.join(os.path.dirname(__file__), 'model')
482
-
483
-
484
-
485
- if os.path.exists(MODEL_DIR) is False:
486
-
487
- os.mkdir(MODEL_DIR)
488
-
489
-
490
-
491
-
492
-
493
-
494
-
495
- '''
496
-
497
- データの読み取り
498
-
499
- '''
500
-
501
- N = 500000
502
-
503
- T = 200
504
-
505
- maxlen = T
506
-
507
-
508
-
509
- X, Y = read(N=N, T=T)
510
-
511
- N_train = int(N * 0.9)
512
-
513
- N_validation = N - N_train
514
-
515
-
516
-
517
- X_train,X_test,Y_train,Y_test=\
518
-
519
- train_test_split(X,Y,train_size=train_size)
520
-
521
- X_train,X_validation,Y_train,Y_validation= \
522
-
523
- train_test_split(X_train,Y_train,train_size=N_validation)
524
-
525
-
526
-
527
- '''
528
-
529
- モデル設定
530
-
531
- '''
532
-
533
- n_in = len(X[0][0]) # 2
534
-
535
- n_hidden = 100
536
-
537
- n_out = len(Y[0]) # 1
538
-
539
-
540
-
541
-
542
-
543
- def weight_variable(shape, name=None):
544
-
545
- return np.random.normal(scale=.01, size=shape)
546
-
547
-
548
-
549
-
550
-
551
- early_stopping = EarlyStopping(monitor='loss', patience=100, verbose=1)
552
-
553
-
554
-
555
- kernel=weight_variable
556
-
557
- kernel2=weight_variable
558
-
559
-
560
-
561
- model = Sequential()
562
-
563
- model.add(GRU(n_hidden,
564
-
565
- #kernel_initializer=kernel,
566
-
567
- input_shape=(maxlen, n_in)))
568
-
569
- model.add(Dense(n_out))
570
-
571
- model.add(Activation('linear'))
572
-
573
-
574
-
575
- optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)
576
-
577
- model.compile(loss='mean_squared_error',
578
-
579
- optimizer=optimizer)
580
-
581
-
582
-
583
- '''
584
-
585
- モデル学習
586
-
587
- '''
588
-
589
-
590
-
591
- epochs = 1000
592
-
593
- batch_size = 100
594
-
595
-
596
-
597
- hist = model.fit(X_train, Y_train,
598
-
599
- batch_size=batch_size,
600
-
601
- epochs=epochs,
602
-
603
- validation_data=(X_validation,Y_validation),
604
-
605
- callbacks=[early_stopping])
606
-
607
-
608
-
609
- model.save(MODEL_DIR+'/model_GRU.hdf5')
610
-
611
- print('Model saved')
612
-
613
-
614
-
615
- '''
616
-
617
- 学習の進み具合を可視化
618
-
619
- '''
620
-
621
-
622
-
623
-
624
-
625
- val_loss=hist.history['val_loss']
626
-
627
- val_acc=hist.history['val_acc']
628
-
629
-
630
-
631
- loss_and_metrics=model.evaluate(X_test,Y_test)
632
-
633
- print(loss_and_metrics)
634
-
635
-
636
-
637
- plt.figure(1)
638
-
639
- plt.plot(range(len(val_acc)),val_acc,label='acc',color='red')
640
-
641
- plt.xlabel('epochs')
642
-
643
-
644
-
645
- plt.figure(2)
646
-
647
- plt.plot(range(len(val_loss)),val_loss,label='loss',color='red')
648
-
649
- plt.xlabel('epochs')
650
-
651
-
652
-
653
- plt.show()
654
-
655
-
656
-
657
- ```

1

タグの編集

2017/12/22 09:13

投稿

kamome01
kamome01

スコア16

test CHANGED
File without changes
test CHANGED
File without changes