解决 TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host m

normal_traffic = np.concatenate((intrinsic_normal, content_normal, time_based_normal, host_based_normal, categorical_normal), axis=1)

报错:

Traceback (most recent call last):
  File "test_wgan.py", line 165, in <module>
    main()
  File "test_wgan.py", line 24, in main
    test_ids(options)
  File "test_wgan.py", line 37, in test_ids
    data = reassemble(options.attack, adversarial, adversarial_ff, nor_nff, nor_ff)
  File "test_wgan.py", line 51, in reassemble
    adversarial_traffic = np.concatenate((intrinsic, content, time_based, host_based, categorical), axis=1)
  File "/root/miniconda3/envs/ids_attack/lib/python3.7/site-packages/torch/tensor.py", line 433, in __array__
    return self.numpy()
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

参考TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu(),我尝试将 intrinsic_normal 改成 intrinsic_normal.cuda().data.cpu().numpy(),继续报新的错:

'numpy.ndarray' object has no attribute 'cuda'

参考 'numpy.ndarray' object has no attribute 'cuda' , 将 intrinsic_normal 转化成tensor类型

intrinsic_normal = torch.tensor(intrinsic_normal).cuda().data.cpu().numpy()

成功解决

上一篇:Odoo14 TypeError: Cannot read property 'classList' of undefined


下一篇:今天遇到个bug,分享一下js报错:Uncaught TypeError: i.push is not a function