Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytorch how to remove cuda() from tensor

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)
like image 512
aerin Avatar asked Aug 03 '18 01:08

aerin


People also ask

What does tensor CUDA () do?

cuda. Returns a copy of this object in CUDA memory. If this object is already in CUDA memory and on the correct device, then no copy is performed and the original object is returned.

What does CUDA () to PyTorch?

torch. cuda is used to set up and run CUDA operations. It keeps track of the currently selected GPU, and all CUDA tensors you allocate will by default be created on that device. The selected device can be changed with a torch.

What does torch tensor () mean?

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.

Does PyTorch automatically use GPU?

GPU operations are asynchronous by default to enable a larger number of computations to be performed in parallel. Asynchronous operations are generally invisible to the user because PyTorch automatically synchronizes data copied between CPU and GPU or GPU and GPU.


1 Answers

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()

like image 148
Umang Gupta Avatar answered Nov 13 '22 12:11

Umang Gupta