# How to cast a tensor to another type?

**URL:** <https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713>\
**Category:** Uncategorized\
**Created:** [May 5, 2017, 7:14pm UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713 "2017-05-05T19:14:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![alan\_ayu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alan_ayu/32/374_2.png) [@alan\_ayu](https://discuss.pytorch.org/u/alan_ayu)\
**Post date:** [May 5, 2017, 7:14pm UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/1 "2017-05-05T19:14:45Z")

</div>

if I got a float tensor,but the model needs a double tensor.what should I do to cast the float tensor to double tensor?

---

<div class="post-metadata">

**Author:** ![ypxie](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/y/43a26b/32.png) [@ypxie](https://discuss.pytorch.org/u/ypxie)\
**Post date:** [May 5, 2017, 7:16pm UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/2 "2017-05-05T19:16:54Z")

</div>

`tensor.double()`

…

---

<div class="post-metadata">

**Author:** ![ncullen93](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ncullen93/32/68760_2.png) [@ncullen93](https://discuss.pytorch.org/u/ncullen93)\
**Post date:** [May 5, 2017, 8:27pm UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/3 "2017-05-05T20:27:03Z")

</div>

Can also do `tensor.type(torch.DoubleTensor)` or `tensor.type('torch.DoubleTensor')` if you want to use a string

---

<div class="post-metadata">

**Author:** ![alan\_ayu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alan_ayu/32/374_2.png) [@alan\_ayu](https://discuss.pytorch.org/u/alan_ayu)\
**Post date:** [May 6, 2017, 2:22am UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/4 "2017-05-06T02:22:36Z")

</div>

thanks for your reply.but even I followed your suggestion to cast the type,it seems the error is still there.  
My usage is :

```auto
x,y=torch.from_numpy(x),torch.from_numpy(y)
x,y=x.type(torch.DoubleTensor),y.type(torch.DoubleTensor)
x=torch.unsqueeze(x,1)
x, y = Variable(x.cuda()), Variable(y.cuda())
preds=model(x)

```

Although I use print(x.type) before preds=model(x) ,and showed the type of x is torch.cuda.DoubleTensor,but the error RuntimeError: expected Double tensor (got Float tensor) appered every time.  
Can you give me some suggestions? Thank you!

---

<div class="post-metadata">

**Author:** ![mratsim](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mratsim/32/548_2.png) [@mratsim](https://discuss.pytorch.org/u/mratsim)\
**Post date:** [May 6, 2017, 4:09am UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/5 "2017-05-06T04:09:43Z")

</div>

@alan_ayu: Is the error really on those lines?

Can you paste a small test case + the actual error?

My experience is that expected Double got Float appears when you try to use `NLLLoss` or `CrossEntropyLoss` as to cover all `int32`, `float64` is needed.

Furthermore unless you have a Tesla card, you shouldn’t use DoubleTensor for your X, it’s 32 times slower than float32 on GeForce, Quadro and Titan cards on any recent cards ( Maxwell and Pascal so since 2014).

---

<div class="post-metadata">

**Author:** ![alan\_ayu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alan_ayu/32/374_2.png) [@alan\_ayu](https://discuss.pytorch.org/u/alan_ayu)\
**Post date:** [May 6, 2017, 8:27am UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/6 "2017-05-06T08:27:19Z")

</div>

Yes you are right,there are something wrong in my dataset  
Thank you for reminding me

---

<div class="post-metadata">

**Author:** ![BSalita](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/f19dbf/32.png) [@BSalita](https://discuss.pytorch.org/u/BSalita)\
**Post date:** [December 30, 2017, 10:46am UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/7 "2017-12-30T10:46:39Z")

</div>

But if your tensor is `tensor.cuda.FloatTensor`, converting using `tensor.DoubleTensor` removes cuda. Better to use `tensor.double()` because it works for both cpu and gpu tensors.

---

<div class="post-metadata">

**Author:** ![Royi](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/royi/32/2051_2.png) [@Royi](https://discuss.pytorch.org/u/Royi)\
**Post date:** [April 26, 2018, 8:18pm UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/8 "2018-04-26T20:18:56Z")

</div>

Bob,  
Could you explain how to convert from `LongTensor` to `FloatTensor` while keeping the `cuda()` property intact?  
What if we’re talking about `Variables` type?

---

<div class="post-metadata">

**Author:** ![mnazaal](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mnazaal/32/12026_2.png) [@mnazaal](https://discuss.pytorch.org/u/mnazaal)\
**Post date:** [June 29, 2018, 3:05pm UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/9 "2018-06-29T15:05:43Z")

</div>

Hi Royi,  
Heres a snippet of my code which does what I believe to be what you want

x = x.type(torch.cuda.FloatTensor)  
x\_cuda = Variable(x, requires\_grad=True).cuda()

Cheers

---

<div class="post-metadata">

**Author:** ![Jaja](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jaja/32/7796_2.png) [@Jaja](https://discuss.pytorch.org/u/Jaja)\
**Post date:** [February 4, 2019, 12:15pm UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/10 "2019-02-04T12:15:24Z")

</div>

> [@ncullen93](#):
>
> .type(‘torch.DoubleTensor’)

Cast seems not to work in pytorch 1.0. Please see the output below. How can I fix that, please?

> > > d.type(torch.DoubleTensor)  
> > > tensor([[[1.5446, 0.3419, 0.1070, -0.6632, 0.5054, 0.7074],  
> > > [-0.5460, -0.0041, -0.6613, -1.5072, 0.4836, 3.1626],  
> > > [-0.9564, 1.8512, -0.6912, -1.0977, 0.4808, -0.5918],  
> > > [-1.3628, 2.2673, -0.9875, 1.0004, 0.1614, -0.4596],  
> > > [-2.0670, 1.4336, -1.1763, 0.1440, -0.5740, 0.2190]],

```
    [[1.5446, 0.3419, 0.1070, -0.6632, 0.5054, 0.7074],
     [-0.5460, -0.0041, -0.6613, -1.5072, 0.4836, 3.1626],
     [-0.9564, 1.8512, -0.6912, -1.0977, 0.4808, -0.5918],
     [-1.3628, 2.2673, -0.9875, 1.0004, 0.1614, -0.4596],
     [-2.0670, 1.4336, -1.1763, 0.1440, -0.5740, 0.2190]]],
   dtype=torch.float64)

```

---

<div class="post-metadata">

**Author:** ![ezyang](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ezyang/32/486_2.png) [@ezyang](https://discuss.pytorch.org/u/ezyang)\
**Post date:** [February 4, 2019, 4:14pm UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/11 "2019-02-04T16:14:18Z")

</div>

In modern PyTorch, you just say `float_tensor.double()` to cast a float tensor to double tensor. There are methods for each type you want to cast to. If, instead, you have a dtype and want to cast to that, say `float_tensor.to(dtype=your_dtype)` (e.g., `your_dtype = torch.float64`)

---

<div class="post-metadata">

**Author:** ![gt\_tugsuu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gt_tugsuu/32/11678_2.png) [@gt\_tugsuu](https://discuss.pytorch.org/u/gt_tugsuu)\
**Post date:** [May 21, 2019, 6:05am UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/12 "2019-05-21T06:05:20Z")

</div>

@alan_ayu @ezyang  
Isn’t there a method to change dtype of a model?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [May 28, 2019, 1:19pm UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/15 "2019-05-28T13:19:27Z")

</div>

The `.to()` method will also work on models and `dtypes`, e.g. `model.to(torch.double)` will convert all parameters to `float64`.

---

<div class="post-metadata">

**Author:** ![GokulDAS027](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gokuldas027/32/8472_2.png) [@GokulDAS027](https://discuss.pytorch.org/u/GokulDAS027)\
**Post date:** [June 19, 2019, 2:15pm UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/16 "2019-06-19T14:15:33Z")

</div>

**Just an on the go solution**

tensor\_one.float() : converts the tensor\_one type to torch.float32  
tensor\_one.double() : converts the tensor\_one type to torch.float64  
tensor\_one.int() : converts the tensor\_one type to torch.int32

---

<div class="post-metadata">

**Author:** ![mathematics](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mathematics/32/40082_2.png) [@mathematics](https://discuss.pytorch.org/u/mathematics)\
**Post date:** [April 5, 2020, 5:39pm UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/17 "2020-04-05T17:39:23Z")

</div>

[cast your tensors using .long()](https://discuss.pytorch.org/t/typeerror-expected-torch-longtensor-got-torch-floattensor/27475/2)

This worked for me.

---

<div class="post-metadata">

**Author:** ![Edwardmark](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/edwardmark/32/22460_2.png) [@Edwardmark](https://discuss.pytorch.org/u/Edwardmark)\
**Post date:** [April 13, 2020, 6:27am UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/18 "2020-04-13T06:27:29Z")

</div>

> [@ezyang](#):
>
> float\_tensor.to(dtype=your\_dtype)

how to do the above conversion in libtorch?

---

<div class="post-metadata">

**Author:** ![saba](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/saba/32/19439_2.png) [@saba](https://discuss.pytorch.org/u/saba)\
**Post date:** [July 8, 2020, 7:09am UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/19 "2020-07-08T07:09:16Z")

</div>

Hi Ptrblck,

I am computing this commnad

```auto
 PP3=(P1*P2).view(-1).sum().item()

```

error is (I run this code on cpu does not work. I change it to GPU and )

It give me this error. I try all option to change the (P1\*P2) to the double but it gave me float again., Would you please help me with that?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [July 8, 2020, 9:16am UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/20 "2020-07-08T09:16:18Z")

</div>

Could you post the error message you are seeing as well as the workaround you are trying to use, please?

---

<div class="post-metadata">

**Author:** ![PatrickNa](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/patrickna/32/29335_2.png) [@PatrickNa](https://discuss.pytorch.org/u/PatrickNa)\
**Post date:** [October 2, 2020, 8:12am UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/21 "2020-10-02T08:12:32Z")

</div>

@ptrblck thanks for pointing to the dtype conversation for the whole model.  
After applying it to my model I first received an error that was due to the fact that I did not change the dtype of the input to what the model is now expecting:

```python
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.cuda.HalfTensor) should be the same

```

This made sense to me and I then switched the dtype of the input accordingly: `input.to(dtype=torch.float16)` but then I receive the following error which causes me trouble:

```python
RuntimeError: expected scalar type Float but found Half

```

Any help would be much appreciated. P.S. I searched similar issues but they did not help in my case.

---

<div class="post-metadata">

**Author:** ![PatrickNa](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/patrickna/32/29335_2.png) [@PatrickNa](https://discuss.pytorch.org/u/PatrickNa)\
**Post date:** [October 2, 2020, 9:41am UTC](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713/22 "2020-10-02T09:41:09Z")

</div>

Also: Iterating over the model states and printing their dtypes confirms that the conversion from float32 to float16 was successful.

[Next page](https://discuss.pytorch.org/t/how-to-cast-a-tensor-to-another-type/2713.md?page=2)
