首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Chainer问题中的自动编码器

Chainer问题中的自动编码器
EN

Stack Overflow用户
提问于 2018-11-20 10:52:40
回答 1查看 79关注 0票数 1

我试着用python训练Chainer的自动编码器,并编写了下面的代码。但不起作用。为什么??

代码语言:javascript
复制
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()

我在用Chainer。数据集生成3个二维矢量。向量的数目是"number_of_data“。

我应该不用教练就这么做吗?我不明白问题出在哪里。

编辑

当我们使用device=0运行上面的代码时,会出现如下错误。

代码语言:javascript
复制
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'>

当我们使用设备=-1运行上面的代码时,会出现如下所示的错误。

代码语言:javascript
复制
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'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-21 04:07:17

我认为model需要在__call__方法中返回损失。

样本修改如下:

代码语言:javascript
复制
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 forward(self,x):
       h1 = self.l1(x)
       h2 = self.l2(h1) 
       return h2

    def __call__(self,x):
       h = self.forward(x)
       # Instead of h, __call__ should return loss.
       loss = F.mean_squared_error(h, x)
       return loss
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53391404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档