Chainerをつかって、pythonで自己符号器をつくろうとしています。次のようなコードを書いて実行しました。

```

    class Autoencoder(Chain):
        def __init__(self):
           super().__init__()
           with self.init_scope():
               self.l1 = L.Linear(3,2)
               self.l2 = L.Linear(2,3)
        def __call__(self,x):
           h1 = self.l1(x)
           h2 = self.l2(h1) 
    
           return h2
    
    class Dataset(dataset.DatasetMixin):
        def __init__(self,number_of_data, show_initial = False):
    
           noise_level = 1
    
           self.data = np.zeros((number_of_data,3),dtype = np.float32)
    
           OA_vector = np.array([3,2,1])
           OB_vector = np.array([2,-1,1])
    
           t = np.random.uniform(-0.5,0.5,number_of_data)
           s = np.random.uniform(-0.5,0.5,number_of_data)
    
           for i in range(0,number_of_data):
               noise = np.random.uniform(-noise_level, noise_level,3)
               self.data[i] = t[i]*OA_vector + s[i]*OB_vector + noise
    
       def __len__(self):
            return self.data.shape[0]
    
       def get_example(self,idx):
           return self.data[idx]
    
    if __name__ == "__main__":
    
        n_epoch = 5
        batch_size = 100
    
        number_of_data = 1000 #データ数
        train_data = Dataset(number_of_data,False)
    
        model = Autoencoder()
    
        optimizer = optimizers.SGD(lr=0.05).setup(model)
        train_iter = iterators.SerialIterator(train_data,batch_size)
    
        updater = training.StandardUpdater(train_iter,optimizer,device=0)
        trainer = training.Trainer(updater,(n_epoch,"epoch"),out="result")
    
        trainer.run()
    
```

なお、Dataset(number_of_data,False)はnumber_of_dataの数の3次元ベクトルを収得する関数です。FalseをTrueにすると得たdataを可視化できます。

実行すると次のようなエラーが生じます。原因は何でしょうか。

```

    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 71, 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'>
    
```

ちなみに、trainer.run()をコメントアウトするとエラーは出ません（もちろん学習もはじまりませんが…)

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'
    
```

というエラーが出ます。
CC BY-SA 4.0
2018-11-20T23:50:25.027
50482
2018-11-21T06:01:03.423
2018-11-21T00:48:54.847
31087
31087
question
python 機械学習 深層学習 chainer
Chainerでオートエンコーダーを作る際にTypeError: Unsupported type <class 'NoneType'>というエラー
正解データとloss functionが与えられていないためにエラーが起きています.  
trainerを使わずに書くと

```

    for batch in train_iter:
        con = chainer.dataset.concat_examples(batch) # dataset[0], dataset[1], ...を一つのarrayに入れる
        con = chainer.Variable(con) # arrayをchainer.Variableに入れる
        pred = model(con)　　　　　　 #推測する
        loss = F.mean_absolute_error(pred, con)　# 推測と正解データ(AEの場合入力そのもの)と比較する
        model.cleargrads() # 過去に計算されたgradientを初期化する
        loss.backward()　　# lossに対してgradientを計算する
        optimizer.update() # gradientに基づいてmodelを最適化する
    
```

というループで学習が実現できます.  
trainerを使いたい場合にはchianer.links.Classifier(<https://docs.chainer.org/en/stable/reference/generated/chainer.links.Classifier.html>)
のコードなどを参考にして,modelとloss functionを結びつける新しいモデルを書くとよいと思います.  
例えば,以下のようにです.

```

    from chainer import reporter
    class AEWrapper(Chain):
    
        def __init__(self, predictor,
                     lossfun=F.mean_absolute_error,):
            super(AEWrapper, self).__init__()
            self.lossfun = lossfun
            with self.init_scope():
                self.predictor = predictor
    
        def forward(self, *args, **kwargs):
            self.y = None
            self.loss = None
    
            self.y = self.predictor(*args)
            self.loss = self.lossfun(self.y, *args)
            reporter.report({'loss': self.loss}, self)
            return self.loss
    
```

ところで,J.J.Sakurai(なつかしい)さんのコードでは終了すると学習結果も過程も消えてしまうので,適当にログを取ったり,プリントするようにするといいと思います.

```

    n_epoch = 5
    batch_size = 100
    number_of_data = 1000 #データ数
    train_data = Dataset(number_of_data,False)
    
    model = Autoencoder()
    model = AEWrapper(model)
    
    optimizer = chainer.optimizers.SGD(lr=0.05).setup(model)
    train_iter = chainer.iterators.SerialIterator(train_data,batch_size)
    
    updater = chainer.training.StandardUpdater(train_iter,optimizer,device=0)
    trainer = chainer.training.Trainer(updater,(n_epoch,"epoch"),out="result")
    trainer.extend(extensions.LogReport(keys=["main/loss"],
                             trigger=training.triggers.IntervalTrigger(1, 'epoch')))
    trainer.extend(extensions.PrintReport(['epoch', 'main/loss']),
                    trigger=training.triggers.IntervalTrigger(1, 'epoch')) 
    trainer.extend(extensions.snapshot_object(model,
                                'model_{.updater.iteration}.npz'), trigger=(1, 'epoch'))
    trainer.run()
    
```

>

```

    epoch       main/loss 
    1           1.31194     
    2           1.0665      
    3           0.883515    
    4           0.743101    
    5           0.635571    
    
```
CC BY-SA 4.0
2018-11-21T02:56:50.823
50489
2018-11-21T06:01:03.423
2018-11-21T06:01:03.423
31088
31088
50482
answer