質問編集履歴

3

質問の再掲

2018/11/29 09:45

投稿

kanpan
kanpan

スコア20

test CHANGED
File without changes
test CHANGED
File without changes

2

質問の変更

2018/11/29 09:45

投稿

kanpan
kanpan

スコア20

test CHANGED
@@ -1 +1 @@
1
- Chainerで簡単なCNN実装したい
1
+ ChainerのValueError解消したい
test CHANGED
@@ -1,511 +1,463 @@
1
- chainerを使って、簡単なCNNを実装したいのですが、変数の型おかしい(?)という旨のエラーがでてうまく実行できなくなってしまいました。
1
+ chainerを使って、簡単なCNNを実装したいのですが、ValueErrorがでてしまい、調べてみましたが、Errorの解消方法が分かりません
2
+
3
+ どうすればよいのでしょうか?
4
+
5
+
6
+
7
+ #ソースコード
8
+
9
+ ```python
10
+
11
+ import numpy as np
12
+
13
+ #from keras.utils import np_utils
14
+
15
+ import sys, os
16
+
17
+ import six
18
+
19
+ import argparse
20
+
21
+ import chainer
22
+
23
+ import chainer.links as L
24
+
25
+ from chainer import optimizers, cuda, serializers
26
+
27
+ import chainer.functions as F
28
+
29
+
30
+
31
+ from CNNSC import CNNSC
32
+
33
+
34
+
35
+ def get_parser():
36
+
37
+
38
+
39
+ DEF_GPU = -1
40
+
41
+ DEF_DATA = "..{sep}Data{sep}input.dat".format(sep=os.sep)
42
+
43
+ DEF_EPOCH = 100
44
+
45
+ DEF_BATCHSIZE = 50
46
+
47
+
48
+
49
+ #引数の設定
50
+
51
+ parser = argparse.ArgumentParser()
52
+
53
+ parser.add_argument('--gpu',
54
+
55
+ dest='gpu',
56
+
57
+ type=int,
58
+
59
+ default=DEF_GPU,
60
+
61
+ metavar='CORE_NUMBER',
62
+
63
+ help='use CORE_NUMBER gpu (default: use cpu)')
64
+
65
+ parser.add_argument('--epoch',
66
+
67
+ dest='epoch',
68
+
69
+ type=int,
70
+
71
+ default=DEF_EPOCH,
72
+
73
+ help='number of epochs to learn')
74
+
75
+ parser.add_argument('--batchsize',
76
+
77
+ dest='batchsize',
78
+
79
+ type=int,
80
+
81
+ default=DEF_BATCHSIZE,
82
+
83
+ help='learning minibatch size')
84
+
85
+ parser.add_argument('--save-model',
86
+
87
+ dest='save_model',
88
+
89
+ action='store',
90
+
91
+ type=str,
92
+
93
+ default=None,
94
+
95
+ metavar='PATH',
96
+
97
+ help='save model to PATH')
98
+
99
+ parser.add_argument('--save-optimizer',
100
+
101
+ dest='save_optimizer',
102
+
103
+ action='store',
104
+
105
+ type=str,
106
+
107
+ default=None,
108
+
109
+ metavar='PATH',
110
+
111
+ help='save optimizer to PATH')
112
+
113
+ parser.add_argument('--baseline',
114
+
115
+ dest='baseline',
116
+
117
+ action='store_true',
118
+
119
+ help='if true, run baseline model')
120
+
121
+
122
+
123
+ return parser
124
+
125
+
126
+
127
+ def save_model(model, file_path='sc_cnn.model'):
128
+
129
+ # modelを保存
130
+
131
+ print ('save the model')
132
+
133
+ model.to_cpu()
134
+
135
+ serializers.save_npz(file_path, model)
136
+
137
+
138
+
139
+ def save_optimizer(optimizer, file_path='sc_cnn.state'):
140
+
141
+ # optimizerを保存
142
+
143
+ print ('save the optimizer')
144
+
145
+ serializers.save_npz(file_path, optimizer)
146
+
147
+
148
+
149
+ def train(args):
150
+
151
+
152
+
153
+ batchsize = args.batchsize # minibatch size
154
+
155
+ n_epoch = args.epoch # エポック数
156
+
157
+
158
+
159
+ height = 100
160
+
161
+ width = 200
162
+
163
+
164
+
165
+ #訓練データの読み込み
166
+
167
+ x_train = np.load('posneg_train_data.npy')
168
+
169
+ y_train = np.load('posneg_train_label.npy')
170
+
171
+
172
+
173
+ #one_hot_y_train = np_utils.to_categorical(y_train)
174
+
175
+
176
+
177
+ #テストデータの読み込み
178
+
179
+ x_test = np.load('posneg_test_data.npy')
180
+
181
+ y_test = np.load('posneg_test_label.npy')
182
+
183
+
184
+
185
+ #one_hot_y_test = np_utils.to_categorical(y_test)
186
+
187
+
188
+
189
+ N_test = y_test.size # test data size
190
+
191
+ N = len(x_train) # train data size
192
+
193
+ in_units = x_train.shape[1] # 入力層のユニット数 (語彙数)
194
+
195
+
196
+
197
+ # (nsample, channel, height, width) の4次元テンソルに変換
198
+
199
+ input_channel = 1
200
+
201
+ x_train = x_train.reshape(len(x_train), input_channel, height, width)
202
+
203
+ x_test = x_test.reshape(len(x_test), input_channel, height, width)
204
+
205
+
206
+
207
+ n_label = 2 # ラベル数
208
+
209
+ filter_height = [3,4,5] # フィルタの高さ
210
+
211
+ baseline_filter_height = [3]
212
+
213
+ filter_width = width # フィルタの幅 (embeddingの次元数)
214
+
215
+ output_channel = 100
216
+
217
+ decay = 0.0001 # 重み減衰
218
+
219
+ grad_clip = 3 # gradient norm threshold to clip
220
+
221
+ max_sentence_len = height # max length of sentences
222
+
223
+
224
+
225
+ # モデルの定義
226
+
227
+ if args.baseline == False:
228
+
229
+ # 提案モデル
230
+
231
+ model = CNNSC(input_channel,
232
+
233
+ output_channel,
234
+
235
+ filter_height,
236
+
237
+ filter_width,
238
+
239
+ n_label,
240
+
241
+ max_sentence_len)
242
+
243
+ else:
244
+
245
+ # ベースラインモデル (フィルタの種類が1つ)
246
+
247
+ model = CNNSC(input_channel,
248
+
249
+ output_channel,
250
+
251
+ baseline_filter_height,
252
+
253
+ filter_width,
254
+
255
+ n_label,
256
+
257
+ max_sentence_len)
258
+
259
+
260
+
261
+ # Setup optimizer
262
+
263
+ optimizer = optimizers.AdaDelta()
264
+
265
+ optimizer.setup(model)
266
+
267
+ optimizer.add_hook(chainer.optimizer.GradientClipping(grad_clip))
268
+
269
+ optimizer.add_hook(chainer.optimizer.WeightDecay(decay))
270
+
271
+
272
+
273
+ #GPUを使うかどうか
274
+
275
+ if args.gpu >= 0:
276
+
277
+ cuda.check_cuda_available()
278
+
279
+ cuda.get_device(args.gpu).use()
280
+
281
+ model.to_gpu()
282
+
283
+ xp = np if args.gpu < 0 else cuda.cupy #args.gpu <= 0: use cpu, otherwise: use gpu
284
+
285
+
286
+
287
+ # Learning loop
288
+
289
+ for epoch in six.moves.range(1, n_epoch + 1):
290
+
291
+
292
+
293
+ print ('epoch', epoch, '/', n_epoch)
294
+
295
+
296
+
297
+ # training
298
+
299
+ perm = np.random.permutation(N) #ランダムな整数列リストを取得
300
+
301
+ sum_train_loss = 0.0
302
+
303
+ sum_train_accuracy = 0.0
304
+
305
+ for i in six.moves.range(0, N, batchsize):
306
+
307
+
308
+
309
+ #perm を使い x_train, y_trainからデータセットを選択 (毎回対象となるデータは異なる)
310
+
311
+ x = chainer.Variable(xp.asarray(x_train[perm[i:i + batchsize]]).astype(np.float32)) #source
312
+
313
+ t = chainer.Variable(xp.asarray(y_train[perm[i:i + batchsize]]).astype(np.float32)) #target
314
+
315
+
316
+
317
+ model.zerograds()
318
+
319
+
320
+
321
+ y = model(x)
322
+
323
+ loss = F.softmax_cross_entropy(y, t) # 損失の計算
324
+
325
+ accuracy = F.accuracy(y, t) # 正解率の計算
326
+
327
+
328
+
329
+ sum_train_loss += loss.data * len(t)
330
+
331
+ sum_train_accuracy += accuracy.data * len(t)
332
+
333
+
334
+
335
+ # 最適化を実行
336
+
337
+ loss.backward()
338
+
339
+ optimizer.update()
340
+
341
+
342
+
343
+ print('train mean loss={}, accuracy={}'.format(sum_train_loss / N, sum_train_accuracy / N)) #平均誤差
344
+
345
+
346
+
347
+ # evaluation
348
+
349
+ sum_test_loss = 0.0
350
+
351
+ sum_test_accuracy = 0.0
352
+
353
+ for i in six.moves.range(0, N_test, batchsize):
354
+
355
+
356
+
357
+ # all test data
358
+
359
+ x = chainer.Variable(xp.asarray(x_test[i:i + batchsize]).astype(np.float32))
360
+
361
+ t = chainer.Variable(xp.asarray(y_test[i:i + batchsize]).astype(np.float32))
362
+
363
+
364
+
365
+ y = model(x, False)
366
+
367
+ loss = F.softmax_cross_entropy(y, t) # 損失の計算
368
+
369
+ accuracy = F.accuracy(y, t) # 正解率の計算
370
+
371
+
372
+
373
+ sum_test_loss += loss.data * len(t)
374
+
375
+ sum_test_accuracy += accuracy.data * len(t)
376
+
377
+
378
+
379
+ print(' test mean loss={}, accuracy={}'.format(sum_test_loss / N_test, sum_test_accuracy / N_test)) #平均誤差
380
+
381
+
382
+
383
+ sys.stdout.flush()
384
+
385
+
386
+
387
+ return model, optimizer
388
+
389
+
390
+
391
+ def main():
392
+
393
+ parser = get_parser()
394
+
395
+ args = parser.parse_args()
396
+
397
+ model, optimizer = train(args)
398
+
399
+
400
+
401
+ if args.save_model != None:
402
+
403
+ save_model(model)
404
+
405
+ if args.save_optimizer != None:
406
+
407
+ save_optimizer(optimizer)
408
+
409
+
410
+
411
+ if __name__ == "__main__":
412
+
413
+ main()
2
414
 
3
415
  ```
4
416
 
5
- x_train.astype(np.float32)
417
+
6
-
7
- y_train.astype(np.int32)
418
+
8
-
9
- x_test.astype(np.float32)
10
-
11
- y_test.astype(np.int32)
419
+ #エラーメッセージ
12
420
 
13
421
  ```
14
422
 
15
- というコードをつけてみても同じエラーが出ました。
16
-
17
- どうすれば解決できるのでしょうか?
18
-
19
- x_train,y_train,x_test,y_testはそれぞれ、(26456, 100, 200),(26456,),(6613,100,200),(6613,)のnumpy配列になっています。
20
-
21
-
22
-
23
- #ソースコード
24
-
25
- ```python
26
-
27
- import numpy as np
28
-
29
- import sys, os
30
-
31
- import six
32
-
33
- import argparse
34
-
35
- import chainer
36
-
37
- import chainer.links as L
38
-
39
- from chainer import optimizers, cuda, serializers
40
-
41
- import chainer.functions as F
42
-
43
-
44
-
45
- from CNNSC import CNNSC
46
-
47
-
48
-
49
- def get_parser():
50
-
51
-
52
-
53
- DEF_GPU = -1
54
-
55
- DEF_DATA = "..{sep}Data{sep}input.dat".format(sep=os.sep)
56
-
57
- DEF_EPOCH = 100
58
-
59
- DEF_BATCHSIZE = 50
60
-
61
-
62
-
63
- #引数の設定
64
-
65
- parser = argparse.ArgumentParser()
66
-
67
- parser.add_argument('--gpu',
68
-
69
- dest='gpu',
70
-
71
- type=int,
72
-
73
- default=DEF_GPU,
74
-
75
- metavar='CORE_NUMBER',
76
-
77
- help='use CORE_NUMBER gpu (default: use cpu)')
78
-
79
- parser.add_argument('--epoch',
80
-
81
- dest='epoch',
82
-
83
- type=int,
84
-
85
- default=DEF_EPOCH,
86
-
87
- help='number of epochs to learn')
88
-
89
- parser.add_argument('--batchsize',
90
-
91
- dest='batchsize',
92
-
93
- type=int,
94
-
95
- default=DEF_BATCHSIZE,
96
-
97
- help='learning minibatch size')
98
-
99
- parser.add_argument('--save-model',
100
-
101
- dest='save_model',
102
-
103
- action='store',
104
-
105
- type=str,
106
-
107
- default=None,
108
-
109
- metavar='PATH',
110
-
111
- help='save model to PATH')
112
-
113
- parser.add_argument('--save-optimizer',
114
-
115
- dest='save_optimizer',
116
-
117
- action='store',
118
-
119
- type=str,
120
-
121
- default=None,
122
-
123
- metavar='PATH',
124
-
125
- help='save optimizer to PATH')
126
-
127
- parser.add_argument('--baseline',
128
-
129
- dest='baseline',
130
-
131
- action='store_true',
132
-
133
- help='if true, run baseline model')
134
-
135
-
136
-
137
- return parser
138
-
139
-
140
-
141
- def save_model(model, file_path='sc_cnn.model'):
142
-
143
- # modelを保存
144
-
145
- print ('save the model')
146
-
147
- model.to_cpu()
148
-
149
- serializers.save_npz(file_path, model)
150
-
151
-
152
-
153
- def save_optimizer(optimizer, file_path='sc_cnn.state'):
154
-
155
- # optimizerを保存
156
-
157
- print ('save the optimizer')
158
-
159
- serializers.save_npz(file_path, optimizer)
160
-
161
-
162
-
163
- def train(args):
164
-
165
-
166
-
167
- batchsize = args.batchsize # minibatch size
168
-
169
- n_epoch = args.epoch # エポック数
170
-
171
-
172
-
173
- height = 100
174
-
175
- width = 200
176
-
177
-
178
-
179
- #訓練データの読み込み
180
-
181
- x_train = np.load('posneg_train_data.npy')
182
-
183
- y_train = np.load('posneg_train_label.npy')
184
-
185
-
186
-
187
- #テストデータの読み込み
188
-
189
- x_test = np.load('posneg_test_data.npy')
190
-
191
- y_test = np.load('posneg_test_label.npy')
192
-
193
-
194
-
195
- N_test = y_test.size # test data size
196
-
197
- N = len(x_train) # train data size
198
-
199
- in_units = x_train.shape[1] # 入力層のユニット数 (語彙数)
200
-
201
-
202
-
203
- # (nsample, channel, height, width) の4次元テンソルに変換
204
-
205
- input_channel = 1
206
-
207
- x_train = x_train.reshape(len(x_train), input_channel, height, width)
208
-
209
- x_test = x_test.reshape(len(x_test), input_channel, height, width)
210
-
211
-
212
-
213
- n_label = 2 # ラベル数
214
-
215
- filter_height = [3,4,5] # フィルタの高さ
216
-
217
- baseline_filter_height = [3]
218
-
219
- filter_width = width # フィルタの幅 (embeddingの次元数)
220
-
221
- output_channel = 100
222
-
223
- decay = 0.0001 # 重み減衰
224
-
225
- grad_clip = 3 # gradient norm threshold to clip
226
-
227
- max_sentence_len = height # max length of sentences
228
-
229
-
230
-
231
- # モデルの定義
232
-
233
- if args.baseline == False:
234
-
235
- # 提案モデル
236
-
237
- model = CNNSC(input_channel,
238
-
239
- output_channel,
240
-
241
- filter_height,
242
-
243
- filter_width,
244
-
245
- n_label,
246
-
247
- max_sentence_len)
248
-
249
- else:
250
-
251
- # ベースラインモデル (フィルタの種類が1つ)
252
-
253
- model = CNNSC(input_channel,
254
-
255
- output_channel,
256
-
257
- baseline_filter_height,
258
-
259
- filter_width,
260
-
261
- n_label,
262
-
263
- max_sentence_len)
264
-
265
-
266
-
267
- # Setup optimizer
268
-
269
- optimizer = optimizers.AdaDelta()
270
-
271
- optimizer.setup(model)
272
-
273
- optimizer.add_hook(chainer.optimizer.GradientClipping(grad_clip))
274
-
275
- optimizer.add_hook(chainer.optimizer.WeightDecay(decay))
276
-
277
-
278
-
279
- #GPUを使うかどうか
280
-
281
- if args.gpu >= 0:
282
-
283
- cuda.check_cuda_available()
284
-
285
- cuda.get_device(args.gpu).use()
286
-
287
- model.to_gpu()
288
-
289
- xp = np if args.gpu < 0 else cuda.cupy #args.gpu <= 0: use cpu, otherwise: use gpu
290
-
291
-
292
-
293
- # Learning loop
294
-
295
- for epoch in six.moves.range(1, n_epoch + 1):
296
-
297
-
298
-
299
- print ('epoch', epoch, '/', n_epoch)
300
-
301
-
302
-
303
- # training
304
-
305
- perm = np.random.permutation(N) #ランダムな整数列リストを取得
306
-
307
- sum_train_loss = 0.0
308
-
309
- sum_train_accuracy = 0.0
310
-
311
- for i in six.moves.range(0, N, batchsize):
312
-
313
-
314
-
315
- #perm を使い x_train, y_trainからデータセットを選択 (毎回対象となるデータは異なる)
316
-
317
- x = chainer.Variable(xp.asarray(x_train[perm[i:i + batchsize]])) #source
318
-
319
- t = chainer.Variable(xp.asarray(y_train[perm[i:i + batchsize]])) #target
320
-
321
-
322
-
323
- model.zerograds()
324
-
325
-
326
-
327
- y = model(x)
328
-
329
- loss = F.softmax_cross_entropy(y, t) # 損失の計算
330
-
331
- accuracy = F.accuracy(y, t) # 正解率の計算
332
-
333
-
334
-
335
- sum_train_loss += loss.data * len(t)
336
-
337
- sum_train_accuracy += accuracy.data * len(t)
338
-
339
-
340
-
341
- # 最適化を実行
342
-
343
- loss.backward()
344
-
345
- optimizer.update()
346
-
347
-
348
-
349
- print('train mean loss={}, accuracy={}'.format(sum_train_loss / N, sum_train_accuracy / N)) #平均誤差
350
-
351
-
352
-
353
- # evaluation
354
-
355
- sum_test_loss = 0.0
356
-
357
- sum_test_accuracy = 0.0
358
-
359
- for i in six.moves.range(0, N_test, batchsize):
360
-
361
-
362
-
363
- # all test data
364
-
365
- x = chainer.Variable(xp.asarray(x_test[i:i + batchsize]))
366
-
367
- t = chainer.Variable(xp.asarray(y_test[i:i + batchsize]))
368
-
369
-
370
-
371
- y = model(x, False)
372
-
373
- loss = F.softmax_cross_entropy(y, t) # 損失の計算
374
-
375
- accuracy = F.accuracy(y, t) # 正解率の計算
376
-
377
-
378
-
379
- sum_test_loss += loss.data * len(t)
380
-
381
- sum_test_accuracy += accuracy.data * len(t)
382
-
383
-
384
-
385
- print(' test mean loss={}, accuracy={}'.format(sum_test_loss / N_test, sum_test_accuracy / N_test)) #平均誤差
386
-
387
-
388
-
389
- sys.stdout.flush()
390
-
391
-
392
-
393
- return model, optimizer
394
-
395
-
396
-
397
- def main():
398
-
399
- parser = get_parser()
400
-
401
- args = parser.parse_args()
423
+ (base) C:\jikken>python confirm.py --save-model C:/jikken --save-optimizer C:/jikken
424
+
425
+ epoch 1 / 100
426
+
427
+ Traceback (most recent call last):
428
+
429
+ File "confirm.py", line 202, in <module>
430
+
431
+ main()
432
+
433
+ File "confirm.py", line 194, in main
402
434
 
403
435
  model, optimizer = train(args)
404
436
 
405
-
406
-
407
- if args.save_model != None:
437
+ File "confirm.py", line 156, in train
408
-
438
+
409
- save_model(model)
439
+ y = model(x)
410
-
440
+
411
- if args.save_optimizer != None:
441
+ File "C:\jikken\CNNSC.py", line 56, in __call__
442
+
412
-
443
+ h_l1 = F.dropout(F.tanh(self[self.cnv_num+0](concat)), ratio=0.5, train=train)
444
+
445
+ File "C:\Anaconda\lib\site-packages\chainer\functions\noise\dropout.py", line 163, in dropout
446
+
447
+ kwargs, train='train argument is not supported anymore. '
448
+
449
+ File "C:\Anaconda\lib\site-packages\chainer\utils\argument.py", line 7, in check_unexpected_kwargs
450
+
413
- save_optimizer(optimizer)
451
+ raise ValueError(message)
414
-
415
-
416
-
452
+
417
- if __name__ == "__main__":
453
+ ValueError: train argument is not supported anymore. Use chainer.using_config
418
-
419
- main()
420
-
421
-
422
454
 
423
455
  ```
424
456
 
425
457
 
426
458
 
427
- #エラーメッセージ
428
-
429
- ```
430
-
431
- (base) C:\jikken>python confirm.py --save-model C:/jikken --save-optimizer C:/jikken
432
-
433
- epoch 1 / 100
434
-
435
- Traceback (most recent call last):
436
-
437
- File "confirm.py", line 202, in <module>
438
-
439
- main()
440
-
441
- File "confirm.py", line 194, in main
442
-
443
- model, optimizer = train(args)
444
-
445
- File "confirm.py", line 156, in train
446
-
447
- y = model(x)
448
-
449
- File "C:\jikken\CNNSC.py", line 50, in __call__
450
-
451
- h_conv[i] = F.relu(self[i](x))
452
-
453
- File "C:\Anaconda\lib\site-packages\chainer\links\connection\convolution_2d.py", line 175, in __call__
454
-
455
- groups=self.groups)
456
-
457
- File "C:\Anaconda\lib\site-packages\chainer\functions\connection\convolution_2d.py", line 582, in convolution_2d
458
-
459
- y, = fnode.apply(args)
460
-
461
- File "C:\Anaconda\lib\site-packages\chainer\function_node.py", line 243, in apply
462
-
463
- self._check_data_type_forward(in_data)
464
-
465
- File "C:\Anaconda\lib\site-packages\chainer\function_node.py", line 328, in _check_data_type_forward
466
-
467
- self.check_type_forward(in_type)
468
-
469
- File "C:\Anaconda\lib\site-packages\chainer\functions\connection\convolution_2d.py", line 68, in check_type_forward
470
-
471
- b_type.shape[0] == w_type.shape[0],
472
-
473
- File "C:\Anaconda\lib\site-packages\chainer\utils\type_check.py", line 524, in expect
474
-
475
- expr.expect()
476
-
477
- File "C:\Anaconda\lib\site-packages\chainer\utils\type_check.py", line 482, in expect
478
-
479
- '{0} {1} {2}'.format(left, self.inv, right))
480
-
481
- chainer.utils.type_check.InvalidType:
482
-
483
- Invalid operation is performed in: Convolution2DFunction (Forward)
484
-
485
-
486
-
487
- Expect: in_types[2].dtype == in_types[0].dtype
488
-
489
- Actual: float32 != float64
490
-
491
- ```
492
-
493
-
494
-
495
459
  #備考
496
460
 
497
461
  参考にしたページはこちらです。
498
462
 
499
463
  https://qiita.com/ichiroex/items/7ff1cff3840520cf2410
500
-
501
-
502
-
503
- printしてみて、
504
-
505
- ```
506
-
507
- y = model(x)
508
-
509
- ```
510
-
511
- がErrorの原因だと分かりましたが、解消方法が分かりません。

1

追記

2018/11/28 10:22

投稿

kanpan
kanpan

スコア20

test CHANGED
File without changes
test CHANGED
@@ -497,3 +497,15 @@
497
497
  参考にしたページはこちらです。
498
498
 
499
499
  https://qiita.com/ichiroex/items/7ff1cff3840520cf2410
500
+
501
+
502
+
503
+ printしてみて、
504
+
505
+ ```
506
+
507
+ y = model(x)
508
+
509
+ ```
510
+
511
+ がErrorの原因だと分かりましたが、解消方法が分かりません。