xiaxin1998/DHCN

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

sorrowyn opened this issue · 2 comments

(pytorch) XXX@xxxxx:/DATA/XXX/DHCN$ python main.py --dataset Tmall --beta 0.02
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    from util import Data, split_validation
ImportError: cannot import name 'Data' from 'util' (/DATA/XXX/DHCN/util.py)
(pytorch) XXX@cvpruser:/DATA/XXX/DHCN$ python main.py --dataset Tmall --beta 0.02
Namespace(batchSize=100, beta=0.02, dataset='Tmall', embSize=100, epoch=30, filter=False, l2=1e-05, layer=3, lr=0.001)
/home/XXX/anaconda3/envs/pytorch/lib/python3.8/site-packages/numpy/core/_asarray.py:102: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  return array(a, dtype, copy=False, order=order)
-------------------------------------------------------
epoch:  0
start training:  2021-09-17 10:49:17.124433
Traceback (most recent call last):
  File "main.py", line 71, in <module>
    main()
  File "main.py", line 53, in main
    metrics, total_loss = train_test(model, train_data, test_data)
  File "/DATA/XXX/DHCN/model.py", line 180, in train_test
    targets, scores, con_loss = forward(model, i, train_data)
  File "/DATA/XXX/DHCN/model.py", line 168, in forward
    item_emb_hg, sess_emb_hgnn, con_loss = model(session_item, session_len, D_hat, A_hat, reversed_sess_item, mask)
  File "/home/XXX/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "/DATA/XXX/DHCN/model.py", line 153, in forward
    session_emb_lg = self.LineGraph(self.embedding.weight, D, A, session_item, session_len)
  File "/home/XXX/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "/DATA/XXX/DHCN/model.py", line 75, in forward
    session_emb_lgcn = np.sum(session, 0)
  File "<__array_function__ internals>", line 5, in sum
  File "/home/XXX/anaconda3/envs/pytorch/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 2247, in sum
    return _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,
  File "/home/XXX/anaconda3/envs/pytorch/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 87, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
  File "/home/XXX/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/_tensor.py", line 643, in __array__
    return self.numpy()
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

That's caused by different version. In our environment, it runs with no problem. But some people always encounter this problem. I have updated our codes in line 75. Please try again.

That's caused by different version. In our environment, it runs with no problem. But some people always encounter this problem. I have updated our codes in line 75. Please try again.
Thanks to you, the problem has been solved.
图片