pytorch how to remove cuda() from tensor

£可爱£侵袭症+ 提交于 2019-12-10 03:52:26

问题


I got TypeError: expected torch.LongTensor (got torch.cuda.FloatTensor).

How do I convert torch.cuda.FloatTensor to torch.LongTensor?

  Traceback (most recent call last):
  File "train_v2.py", line 110, in <module>
    main()
  File "train_v2.py", line 81, in main
    model.update(batch)
  File "/home/Desktop/squad_vteam/src/model.py", line 131, in update
    loss_adv = self.adversarial_loss(batch, loss, self.network.lexicon_encoder.embedding.weight, y)
  File "/home/Desktop/squad_vteam/src/model.py", line 94, in adversarial_loss
    adv_embedding = torch.LongTensor(adv_embedding)
TypeError: expected torch.LongTensor (got torch.cuda.FloatTensor)

回答1:


You have a float tensor f and want to convert it to long, you do long_tensor = f.long()

You have cuda tensor i.e data is on gpu and want to move it to cpu you can do cuda_tensor.cpu().

So to convert a torch.cuda.Float tensor A to torch.long do A.long().cpu()




回答2:


Best practice for Pytorch 0.4.0 is to write device agnostic code: That is, instead of using .cuda() or .cpu() you can simply use .to(torch.device("cpu"))

A = A.to(dtype=torch.long, device=torch.device("cpu"))

Note that .to() is not an "in-place" operation (see, e.g., this answer), thus you need to assign A.to(...) back into A.




回答3:


If you have a tensor t.

t = t.cpu() 

would be the old way.

t = t.to("cpu")

would be the new API.



来源:https://stackoverflow.com/questions/51664192/pytorch-how-to-remove-cuda-from-tensor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!