How to cast a tensor to another type?

alan_ayu: <p>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?</p>

ypxie: <p><code>tensor.double()</code></p>
<p>…</p>

ncullen93: <p>Can also do <code>tensor.type(torch.DoubleTensor)</code> or <code>tensor.type('torch.DoubleTensor')</code> if you want to use a string</p>

alan_ayu: <p>thanks for your reply.but even I followed your suggestion to cast the type,it seems the error is still there.<br>
My usage is :</p>
<pre><code class="lang-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)
</code></pre>
<p>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.<br>
Can you give me some suggestions? Thank you!</p>

mratsim: <p><a class="mention" href="/u/alan_ayu">@alan_ayu</a>: Is the error really on those lines?</p>
<p>Can you paste a small test case + the actual error?</p>
<p>My experience is that expected Double got Float appears when you try to use <code>NLLLoss</code> or <code>CrossEntropyLoss</code> as to cover all <code>int32</code>, <code>float64</code> is needed.</p>
<p>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).</p>

alan_ayu: <p>Yes you are right,there are something wrong in my dataset<br>
Thank you for reminding me</p>

BSalita: <p>But if your tensor is <code>tensor.cuda.FloatTensor</code>, converting using <code>tensor.DoubleTensor</code> removes cuda. Better to use <code>tensor.double()</code> because it works for both cpu and gpu tensors.</p>

Royi: <p>Bob,<br>
Could you explain how to convert from <code>LongTensor</code> to <code>FloatTensor</code> while keeping the <code>cuda()</code> property intact?<br>
What if we’re talking about <code>Variables</code> type?</p>

mnazaal: <p>Hi Royi,<br>
Heres a snippet of my code which does what I believe to be what you want</p>
<p>x = x.type(torch.cuda.FloatTensor)<br>
x_cuda = Variable(x, requires_grad=True).cuda()</p>
<p>Cheers</p>

Jaja: <aside class="quote no-group quote-modified" data-username="ncullen93" data-post="3" data-topic="2713">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/8baadc/48.png" class="avatar"> ncullen93:</div>
<blockquote>
<p>.type(‘torch.DoubleTensor’)</p>
</blockquote>
</aside>
<p>Cast seems not to work in pytorch 1.0. Please see the output below. How can I fix that, please?</p>
<blockquote>
<blockquote>
<blockquote>
<p>d.type(torch.DoubleTensor)<br>
tensor([[[ 1.5446,  0.3419,  0.1070, -0.6632,  0.5054,  0.7074],<br>
[-0.5460, -0.0041, -0.6613, -1.5072,  0.4836,  3.1626],<br>
[-0.9564,  1.8512, -0.6912, -1.0977,  0.4808, -0.5918],<br>
[-1.3628,  2.2673, -0.9875,  1.0004,  0.1614, -0.4596],<br>
[-2.0670,  1.4336, -1.1763,  0.1440, -0.5740,  0.2190]],</p>
</blockquote>
</blockquote>
</blockquote>
<pre><code>    [[ 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)
</code></pre>

ezyang: <p>In modern PyTorch, you just say <code>float_tensor.double()</code> 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 <code>float_tensor.to(dtype=your_dtype)</code> (e.g., <code>your_dtype = torch.float64</code>)</p>

gt_tugsuu: <p><a class="mention" href="/u/alan_ayu">@alan_ayu</a> <a class="mention" href="/u/ezyang">@ezyang</a><br>
Isn’t there a method to change dtype of a model?</p>

ptrblck: <p>The <code>.to()</code> method will also work on models and <code>dtypes</code>, e.g. <code>model.to(torch.double)</code> will convert all parameters to <code>float64</code>.</p>

GokulDAS027: <p><strong>Just an on the go solution</strong></p>
<p>tensor_one.float() : converts the tensor_one type to torch.float32<br>
tensor_one.double() : converts the tensor_one type to torch.float64<br>
tensor_one.int() : converts the tensor_one type to torch.int32</p>

mathematics: <p><a href="https://discuss.pytorch.org/t/typeerror-expected-torch-longtensor-got-torch-floattensor/27475/2">  cast your tensors using .long()</a></p>
<p>This worked for me.</p>

Edwardmark: <aside class="quote no-group" data-username="ezyang" data-post="11" data-topic="2713">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ezyang/48/486_2.png" class="avatar"> ezyang:</div>
<blockquote>
<p>float_tensor.to(dtype=your_dtype)</p>
</blockquote>
</aside>
<p>how to do the above conversion in libtorch?</p>

saba: <p>Hi Ptrblck,</p>
<p>I am computing this commnad</p>
<pre><code class="lang-auto"> PP3=(P1*P2).view(-1).sum().item()
</code></pre>
<p>error is (I run this code on cpu does not work. I change it to GPU and )</p>
<p>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?</p>

ptrblck: <p>Could you post the error message you are seeing as well as the workaround you are trying to use, please?</p>

PatrickNa: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> thanks for pointing to the dtype conversation for the whole model.<br>
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:</p>
<pre><code class="lang-python">RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.cuda.HalfTensor) should be the same
</code></pre>
<p>This made sense to me and I then switched the dtype of the input accordingly: <code>input.to(dtype=torch.float16)</code> but then I receive the following error which causes me trouble:</p>
<pre><code class="lang-python">RuntimeError: expected scalar type Float but found Half
</code></pre>
<p>Any help would be much appreciated. P.S. I searched similar issues but they did not help in my case.</p>

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