[已解决] RuntimeError: cuda runtime error (700) : an illegal memory access was encountered


这个error发生在我得到训练好的Decoder之后,对latent space采样并生成新数据的时候,如下

RuntimeError: cuda runtime error (700) : an illegal memory access was encountered at ..\aten\src\THC\THCCachingHostAllocator.cpp:278

  问题出在如何把数据sample存储在gpu上:torch.tensor(sample, device=device)。其中device变量为cuda:0。\

正文:
看了很多别人的问题,发现可能是我生成的sample没有存储在GPU上,所以对cuda来说储存在CPU上的数据就是illegal memory access,非法存储访问。代码和Traceback如下:

# we will sample N embeddings, then decode and visualize them
def vis_samples(model):
    ####################################### TODO #######################################
    ####################################################################################
    sampled_embeddings = torch.FloatTensor(batch_size,nz).normal_()
    decoded_samples = model.decoder(sampled_embeddings)       # decoder output images for sampled embeddings
    #################################### END TODO ######################################
    fig = plt.figure(figsize = (10, 10))   
    ax1 = plt.subplot(111)
    ax1.imshow(torchvision.utils.make_grid(decoded_samples[:16], nrow=4, pad_value=1.)\
                .data.cpu().numpy().transpose(1, 2, 0), cmap='gray')
    plt.show()
vis_samples(ae_model)
RuntimeError                              Traceback (most recent call last)
<ipython-input-13-09e84dc7a8e5> in <module>
     24     plt.show()
     25
---> 26 vis_samples(ae_model)

<ipython-input-13-09e84dc7a8e5> in vis_samples(model)
     20     fig = plt.figure(figsize = (10, 10))
     21     ax1 = plt.subplot(111)
---> 22     ax1.imshow(torchvision.utils.make_grid(decoded_samples[:16], nrow=4, pad_value=1.)\
     23                 .data.cpu().numpy().transpose(1, 2, 0), cmap='gray')
     24     plt.show()

~\AppData\Local\Continuum\anaconda3\envs\TF_GPU\lib\site-packages\torchvision\utils.py in make_grid(tensor, nrow, padding, normalize, range, scale_each, pad_value)
     53
     54     if tensor.dim() == 4 and tensor.size(1) == 1:  # single-channel images
---> 55         tensor = torch.cat((tensor, tensor, tensor), 1)
     56
     57     if normalize is True:

RuntimeError: cuda runtime error (700) : an illegal memory access was encountered at ..\aten\src\THC\THCCachingHostAllocator.cpp:278

于是我把生成sample的代码改为

    ####################################### TODO #######################################
    ####################################################################################
    sampled_embeddings = torch.FloatTensor(batch_size,nz).normal_()
    decoded_samples = model.decoder(torch.tensor(sampled_embeddings, device=device))
    #################################### END TODO ######################################

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值