TypeError: numpy.ndarray or cuda.ndarray are expected.

╰半橙微兮° 2022-03-09 15:15 268阅读 0赞

遇到一个bug

  1. Will finalize trainer extensions and updater before reraising the exception.
  2. Traceback (most recent call last):
  3. File "/home/zlee/PycharmProjects/hyperface-youtube/hyperface-master/scripts/train.py", line 176, in <module>
  4. trainer.run()
  5. File "/home/zlee/PycharmProjects/hyperface-youtube/venv/local/lib/python2.7/site-packages/chainer/training/trainer.py", line 329, in run
  6. six.reraise(*sys.exc_info())
  7. File "/home/zlee/PycharmProjects/hyperface-youtube/venv/local/lib/python2.7/site-packages/chainer/training/trainer.py", line 315, in run
  8. update()
  9. File "/home/zlee/PycharmProjects/hyperface-youtube/venv/local/lib/python2.7/site-packages/chainer/training/updaters/standard_updater.py", line 165, in update
  10. self.update_core()
  11. File "/home/zlee/PycharmProjects/hyperface-youtube/venv/local/lib/python2.7/site-packages/chainer/training/updaters/standard_updater.py", line 179, in update_core
  12. optimizer.update(loss_func, **in_arrays)
  13. File "/home/zlee/PycharmProjects/hyperface-youtube/venv/local/lib/python2.7/site-packages/chainer/optimizer.py", line 680, in update
  14. loss = lossfun(*args, **kwds)
  15. File "/home/zlee/PycharmProjects/hyperface-youtube/hyperface-master/scripts/models.py", line 114, in __call__
  16. h_landmark *= _disconnect(m_landmark)
  17. File "/home/zlee/PycharmProjects/hyperface-youtube/hyperface-master/scripts/models.py", line 22, in _disconnect
  18. return chainer.Variable(x.data)
  19. File "/home/zlee/PycharmProjects/hyperface-youtube/venv/local/lib/python2.7/site-packages/chainer/variable.py", line 505, in __init__
  20. raise TypeError(msg)
  21. TypeError: numpy.ndarray or cuda.ndarray are expected.
  22. Actual: <type 'cupy.cuda.memory.MemoryPointer'>

在调用函数时出现的问题:

  1. def _disconnect(x):
  2. print("zlee", type(x), type(x.data))
  3. return chainer.Variable(x.data)

我打印了一下,是如下类型:

  1. ('zlee', <type 'cupy.core.core.ndarray'>, <type 'cupy.cuda.memory.MemoryPointer'>)

改正的方案如下所示:

  1. def _disconnect(x):
  2. x = cupy.asnumpy(x)
  3. print("zlee : ", type(x),type(x.data))
  4. return chainer.Variable(x.data)

另外加入 import cupy

发表评论

表情:
评论列表 (有 0 条评论,268人围观)

还没有评论,来说两句吧...

相关阅读