質問編集履歴

5

コードの追加

2022/10/29 10:30

投稿

shinww
shinww

スコア4

test CHANGED
File without changes
test CHANGED
@@ -77,6 +77,59 @@
77
77
  self.accuracy = F.accuracy(h, t)
78
78
  return self.loss
79
79
 
80
+ train_imagenet.py
81
+
82
+ def train_loop():
83
+ # Trainer
84
+ graph_generated = False
85
+ while True:
86
+ while data_q.empty():
87
+ time.sleep(0.1)
88
+ inp = data_q.get()
89
+ if inp == 'end': # quit
90
+ res_q.put('end')
91
+ break
92
+ elif inp == 'train': # restart training
93
+ res_q.put('train')
94
+ model.train = True
95
+ continue
96
+ elif inp == 'val': # start validation
97
+ res_q.put('val')
98
+ #serializers.save_npz(args.out, model)
99
+ #model.to_cpu()
100
+ pickle.dump(model, open(args.out, 'wb'), -1)
101
+ serializers.save_npz(args.outstate, optimizer)
102
+ model.train = False
103
+ continue
104
+
105
+ volatile = 'off'if model.train else 'on'
106
+ #x = chainer.Variable(xp.asarray(inp[0]), volatile=volatile)
107
+ x = chainer.Variable(xp.asarray(inp[0]))
108
+ with chainer.no_backprop_mode():
109
+ #t = chainer.Variable(xp.asarray(inp[1]),volatile=volatile)
110
+ t = chainer.Variable(xp.asarray(inp[1]))
111
+ with chainer.no_backprop_mode():
112
+
113
+ #if model.train:
114
+ if model: #←
115
+ x = chainer.Variable(np.asarray(inp[0]))
116
+ t = chainer.Variable(np.asarray(inp[1]))
117
+ optimizer.update(model, x, t)
118
+ #print(f"acc: {model.accuracy.data}, loss: {model.loss.data}") #追加
119
+ if not graph_generated:
120
+ with open('graph.dot', 'w') as o:
121
+ o.write(computational_graph.build_computational_graph(
122
+ (model.loss,)).dump())
123
+ print('generated graph', file=sys.stderr)
124
+ graph_generated = True
125
+ else:
126
+ with chainer.no_backprop_mode():
127
+
128
+ model(x, t)
129
+
130
+ res_q.put((float(model.loss.data), float(model.accuracy.data)))
131
+ del x, t
132
+
80
133
  ### 試したこと
81
134
  以前の質問で解決したのですが、PCが故障してしまいPCを変えたところ動かなくなってしまいました。
82
135
  実行環境に関して覚えがなく、様々な環境で行ってみましたがうまいくいきませんでした。

4

説明追加

2022/10/29 08:10

投稿

shinww
shinww

スコア4

test CHANGED
File without changes
test CHANGED
@@ -78,6 +78,9 @@
78
78
  return self.loss
79
79
 
80
80
  ### 試したこと
81
+ 以前の質問で解決したのですが、PCが故障してしまいPCを変えたところ動かなくなってしまいました。
82
+ 実行環境に関して覚えがなく、様々な環境で行ってみましたがうまいくいきませんでした。
83
+ いろいろと調査したところchainerv4においてNumPy/CuPy 配列を引数に混在させた関数の呼び出しが機能しないというのは分かりましたが解決法が分からないという状態です。
84
+ ↑の場合にincompatible array types are mixed in the forward input のエラーが出ることが分かりました。
81
85
 
82
- ネット等での調査、同じエラーでの質問を見つけたが理解できなかった
83
86
 

3

変更

2022/10/27 08:59

投稿

shinww
shinww

スコア4

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提
2
2
 
3
- chainerで機械学習
3
+ chainerで画像分類のための機械学習をしていたところエラーが発生した
4
4
 
5
5
  ### 実現したいこと
6
6
  - [ ] エラーを修正

2

エラーコードの修正

2022/10/27 07:14

投稿

shinww
shinww

スコア4

test CHANGED
File without changes
test CHANGED
@@ -6,9 +6,43 @@
6
6
  - [ ] エラーを修正
7
7
 
8
8
  ### 発生している問題・エラーメッセージ
9
+ Traceback (most recent call last):
10
+
11
+ File "train_imagenet.py", line 352, in <module>
12
+ train_loop()
13
+
14
+ File "train_imagenet.py", line 327, in train_loop
15
+ optimizer.update(model, x, t)
16
+
17
+ File "C:\Users\user01\AppData\Local\Programs\Python\Python37\lib\site-packages\chainer\optimizer.py", line 864, in update
18
+ loss = lossfun(*args, **kwds)
19
+
20
+ File "C:\Users\user01\Desktop\実験データ\20201114\nin.py", line 35, in __call__
21
+ h = F.max_pooling_2d(F.relu(self.mlpconv1(x)), 3, stride=2)
22
+
23
+ File "C:\Users\user01\AppData\Local\Programs\Python\Python37\lib\site-packages\chainer\link.py", line 294, in __call__
24
+ out = forward(*args, **kwargs)
25
+
26
+ File "C:\Users\user01\AppData\Local\Programs\Python\Python37\lib\site-packages\chainer\links\connection\mlp_convolution_2d.py", line 107, in forward
27
+ x = f(l(x))
28
+
29
+ File "C:\Users\user01\AppData\Local\Programs\Python\Python37\lib\site-packages\chainer\link.py", line 294, in __call__
30
+ out = forward(*args, **kwargs)
31
+
32
+ File "C:\Users\user01\AppData\Local\Programs\Python\Python37\lib\site-packages\chainer\links\connection\convolution_2d.py", line 184, in forward
33
+ groups=self.groups)
34
+
35
+ File "C:\Users\user01\AppData\Local\Programs\Python\Python37\lib\site-packages\chainer\functions\connection\convolution_2d.py", line 589, in convolution_2d
36
+ y, = fnode.apply(args)
37
+
38
+ File "C:\Users\user01\AppData\Local\Programs\Python\Python37\lib\site-packages\chainer\function_node.py", line 289, in apply
39
+ utils._check_arrays_forward_compatible(in_data, self.label)
40
+
41
+ File "C:\Users\user01\AppData\Local\Programs\Python\Python37\lib\site-packages\chainer\utils\__init__.py", line 93, in _check_arrays_forward_compatible
42
+ ', '.join(str(type(a)) for a in arrays)))
43
+
9
44
  TypeError: incompatible array types are mixed in the forward input (Convolution2DFunction).
10
45
  Actual: <class 'numpy.ndarray'>, <class 'cupy.core.core.ndarray'>, <class 'cupy.core.core.ndarray'>
11
-
12
46
  ### 該当のソースコード
13
47
 
14
48
  ```python

1

コードの修正

2022/10/27 06:25

投稿

shinww
shinww

スコア4

test CHANGED
File without changes
test CHANGED
@@ -12,13 +12,31 @@
12
12
  ### 該当のソースコード
13
13
 
14
14
  ```python
15
+ def __init__(self):
16
+ w = math.sqrt(2) # MSRA scaling
17
+ super(NIN, self).__init__(
18
+ mlpconv1=L.MLPConvolution2D(
19
+ 3, (96, 96, 96), 11, stride=4),
20
+ mlpconv2=L.MLPConvolution2D(
21
+ 96, (256, 256, 256), 5, pad=2), #wscale=w削除
22
+ mlpconv3=L.MLPConvolution2D(
23
+ 256, (384, 384, 384), 3, pad=1),
24
+ mlpconv4=L.MLPConvolution2D(
25
+ 384, (1024, 1024, 1000), 3, pad=2),
26
+ )
27
+ self.train = True
28
+
29
+ def clear(self):
30
+ self.loss = None
31
+ self.accuracy = None
32
+
15
33
  def __call__(self, x, t):
16
34
  self.clear()
17
35
 
18
36
  h = F.max_pooling_2d(F.relu(self.mlpconv1(x)), 3, stride=2)
19
37
  h = F.max_pooling_2d(F.relu(self.mlpconv2(h)), 3, stride=2)
20
38
  h = F.max_pooling_2d(F.relu(self.mlpconv3(h)), 3, stride=2)
21
- h = self.mlpconv4(F.dropout(h)) #変更
39
+ h = self.mlpconv4(F.dropout(h))
22
40
  h = F.reshape(F.average_pooling_2d(h, 6), (x.data.shape[0], 1000))
23
41
 
24
42
  self.loss = F.softmax_cross_entropy(h, t)