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

本文详细记录了一个在使用Chainer进行模型训练时遇到的CUDA内存问题,具体表现为在训练过程中抛出TypeError异常,指出实际传入的是cupy.cuda.memory.MemoryPointer类型而非期望的numpy.ndarray或cuda.ndarray。通过将cupy类型转换为numpy类型,成功解决了该问题。

遇到一个bug

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

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

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

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

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

改正的方案如下所示:

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

另外加入 import cupy

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JensLee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值