自己符号化器を作ろうとしているのですが、うまくいきません。
次のようなコードを実行しました。
python
1 2class Autoencoder(Chain): 3 def __init__(self): 4 super().__init__() 5 with self.init_scope(): 6 self.l1 = L.Linear(3,2) 7 self.l2 = L.Linear(2,3) 8 9 def __call__(self,x): 10 h1 = self.l1(x) 11 h2 = self.l2(h1) 12 13 return h2 14 15class Dataset(dataset.DatasetMixin): 16 def __init__(self,number_of_data, show_initial = False): 17 18 noise_level = 1 19 20 self.data = np.zeros((number_of_data,3),dtype = np.float32) 21 22 OA_vector = np.array([3,2,1]) 23 OB_vector = np.array([2,-1,1]) 24 25 t = np.random.uniform(-0.5,0.5,number_of_data) 26 s = np.random.uniform(-0.5,0.5,number_of_data) 27 28 for i in range(0,number_of_data): 29 noise = np.random.uniform(-noise_level, noise_level,3) 30 self.data[i] = t[i]*OA_vector + s[i]*OB_vector + noise 31 32 def __len__(self): 33 return self.data.shape[0] 34 35 def get_example(self,idx): 36 return self.data[idx] 37 38 39if __name__ == "__main__": 40 41 n_epoch = 5 42 batch_size = 100 43 44 number_of_data = 1000 #データ数 45 train_data = Dataset(number_of_data,False) 46 47 # NNのモデル宣言 48 model = Autoencoder() 49 50 optimizer = optimizers.SGD(lr=0.05).setup(model) 51 train_iter = iterators.SerialIterator(train_data,batch_size) 52 53 updater = training.StandardUpdater(train_iter,optimizer,device=0) 54 trainer = training.Trainer(updater,(n_epoch,"epoch"),out="result") 55 56 trainer.run()
data は 3次元のデータです。活性化関数を恒等関数にしてるのはわざとです。このコードを実行すると次のようなエラーがでるのですが、原因が全くわかりません。なぜでしょうか。
Exception in main training loop: Unsupported type <class 'NoneType'>
Traceback (most recent call last):
File "/home//.local/lib/python3.5/site-packages/chainer/training/trainer.py", line 308, in run
update()
File "/home//.local/lib/python3.5/site-packages/chainer/training/updaters/standard_updater.py", line 149, in update
self.update_core()
File "/home//.local/lib/python3.5/site-packages/chainer/training/updaters/standard_updater.py", line 164, in update_core
optimizer.update(loss_func, in_arrays)
File "/home//.local/lib/python3.5/site-packages/chainer/optimizer.py", line 655, in update
loss.backward(loss_scale=self._loss_scale)
File "/home//.local/lib/python3.5/site-packages/chainer/variable.py", line 966, in backward
self._backward_main(retain_grad, loss_scale)
File "/home//.local/lib/python3.5/site-packages/chainer/variable.py", line 1095, in _backward_main
target_input_indexes, out_grad, in_grad)
File "/home//.local/lib/python3.5/site-packages/chainer/function_node.py", line 548, in backward_accumulate
gxs = self.backward(target_input_indexes, grad_outputs)
File "/home//.local/lib/python3.5/site-packages/chainer/functions/activation/relu.py", line 73, in backward
return ReLUGrad2(y).apply((gy,))
File "/home//.local/lib/python3.5/site-packages/chainer/function_node.py", line 258, in apply
outputs = self.forward(in_data)
File "/home//.local/lib/python3.5/site-packages/chainer/function_node.py", line 368, in forward
return self.forward_cpu(inputs)
File "/home//.local/lib/python3.5/site-packages/chainer/functions/activation/relu.py", line 97, in forward_cpu
y = (self.b > 0) * inputs[0]
File "cupy/core/core.pyx", line 1310, in cupy.core.core.ndarray.mul
File "cupy/core/elementwise.pxi", line 753, in cupy.core.core.ufunc.call
File "cupy/core/elementwise.pxi", line 68, in cupy.core.core._preprocess_args
Will finalize trainer extensions and updater before reraising the exception.
Traceback (most recent call last):
File "AC.py", line 70, in <module>
trainer.run()
File "/home//.local/lib/python3.5/site-packages/chainer/training/trainer.py", line 322, in run
six.reraise(sys.exc_info())
File "/home//.local/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/home/**/.local/lib/python3.5/site-packages/chainer/training/trainer.py", line 308, in run
update()
File "/home//.local/lib/python3.5/site-packages/chainer/training/updaters/standard_updater.py", line 149, in update
self.update_core()
File "/home/**/.local/lib/python3.5/site-packages/chainer/training/updaters/standard_updater.py", line 164, in update_core
optimizer.update(loss_func, in_arrays)
File "/home//.local/lib/python3.5/site-packages/chainer/optimizer.py", line 655, in update
loss.backward(loss_scale=self._loss_scale)
File "/home/**/.local/lib/python3.5/site-packages/chainer/variable.py", line 966, in backward
self._backward_main(retain_grad, loss_scale)
File "/home//.local/lib/python3.5/site-packages/chainer/variable.py", line 1095, in _backward_main
target_input_indexes, out_grad, in_grad)
File "/home/**/.local/lib/python3.5/site-packages/chainer/function_node.py", line 548, in backward_accumulate
gxs = self.backward(target_input_indexes, grad_outputs)
File "/home//.local/lib/python3.5/site-packages/chainer/functions/activation/relu.py", line 73, in backward
return ReLUGrad2(y).apply((gy,))
File "/home/**/.local/lib/python3.5/site-packages/chainer/function_node.py", line 258, in apply
outputs = self.forward(in_data)
File "/home/*/.local/lib/python3.5/site-packages/chainer/function_node.py", line 368, in forward
return self.forward_cpu(inputs)
File "/home/**/.local/lib/python3.5/site-packages/chainer/functions/activation/relu.py", line 97, in forward_cpu
y = (self.b > 0) * inputs[0]
File "cupy/core/core.pyx", line 1310, in cupy.core.core.ndarray.mul
File "cupy/core/elementwise.pxi", line 753, in cupy.core.core.ufunc.call
File "cupy/core/elementwise.pxi", line 68, in cupy.core.core._preprocess_args
TypeError: Unsupported type <class 'NoneType'>
ちなみに cupyのエラーが出てるので、GPU関連かなと思い、
updater = training.StandardUpdater(train_iter,optimizer,device=-1)
としてみたのですが、今度は
Exception in main training loop: unsupported operand type(s) for : 'bool' and 'NoneType'
Traceback (most recent call last):
File "/home//.local/lib/python3.5/site-packages/chainer/training/trainer.py", line 308, in run
update()
File "/home/**/.local/lib/python3.5/site-packages/chainer/training/updaters/standard_updater.py", line 149, in update
self.update_core()
File "/home//.local/lib/python3.5/site-packages/chainer/training/updaters/standard_updater.py", line 164, in update_core
optimizer.update(loss_func, in_arrays)
File "/home/**/.local/lib/python3.5/site-packages/chainer/optimizer.py", line 655, in update
loss.backward(loss_scale=self._loss_scale)
File "/home//.local/lib/python3.5/site-packages/chainer/variable.py", line 966, in backward
self._backward_main(retain_grad, loss_scale)
File "/home/**/.local/lib/python3.5/site-packages/chainer/variable.py", line 1095, in _backward_main
target_input_indexes, out_grad, in_grad)
File "/home//.local/lib/python3.5/site-packages/chainer/function_node.py", line 548, in backward_accumulate
gxs = self.backward(target_input_indexes, grad_outputs)
File "/home/**/.local/lib/python3.5/site-packages/chainer/functions/activation/relu.py", line 73, in backward
return ReLUGrad2(y).apply((gy,))
File "/home//.local/lib/python3.5/site-packages/chainer/function_node.py", line 258, in apply
outputs = self.forward(in_data)
File "/home/**/.local/lib/python3.5/site-packages/chainer/function_node.py", line 368, in forward
return self.forward_cpu(inputs)
File "/home//.local/lib/python3.5/site-packages/chainer/functions/activation/relu.py", line 97, in forward_cpu
y = (self.b > 0) * inputs[0]
Will finalize trainer extensions and updater before reraising the exception.
Traceback (most recent call last):
File "AC.py", line 70, in <module>
trainer.run()
File "/home//.local/lib/python3.5/site-packages/chainer/training/trainer.py", line 322, in run
six.reraise(sys.exc_info())
File "/home//.local/lib/python3.5/site-packages/six.py", line 693, in reraise
raise value
File "/home//.local/lib/python3.5/site-packages/chainer/training/trainer.py", line 308, in run
update()
File "/home//.local/lib/python3.5/site-packages/chainer/training/updaters/standard_updater.py", line 149, in update
self.update_core()
File "/home//.local/lib/python3.5/site-packages/chainer/training/updaters/standard_updater.py", line 164, in update_core
optimizer.update(loss_func, in_arrays)
File "/home//.local/lib/python3.5/site-packages/chainer/optimizer.py", line 655, in update
loss.backward(loss_scale=self._loss_scale)
File "/home//.local/lib/python3.5/site-packages/chainer/variable.py", line 966, in backward
self._backward_main(retain_grad, loss_scale)
File "/home//.local/lib/python3.5/site-packages/chainer/variable.py", line 1095, in _backward_main
target_input_indexes, out_grad, in_grad)
File "/home//.local/lib/python3.5/site-packages/chainer/function_node.py", line 548, in backward_accumulate
gxs = self.backward(target_input_indexes, grad_outputs)
File "/home//.local/lib/python3.5/site-packages/chainer/functions/activation/relu.py", line 73, in backward
return ReLUGrad2(y).apply((gy,))
File "/home//.local/lib/python3.5/site-packages/chainer/function_node.py", line 258, in apply
outputs = self.forward(in_data)
File "/home//.local/lib/python3.5/site-packages/chainer/function_node.py", line 368, in forward
return self.forward_cpu(inputs)
File "/home/****/.local/lib/python3.5/site-packages/chainer/functions/activation/relu.py", line 97, in forward_cpu
y = (self.b > 0) * inputs[0]
TypeError: unsupported operand type(s) for *: 'bool' and 'NoneType'
というエラーがでます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/20 23:21
2018/11/20 23:22
2018/11/21 00:37
2018/11/21 00:39
2018/11/21 00:42 編集
2018/11/21 00:41