Construct new tensor on correct device based on input

ypxie: <p>I know <code>input.new()</code> will create a new tensor of the same dtype to <code>input</code>.</p>
<p>But what I want to do is to create a new tensor on the same device to input, but has different data types.<br>
for example, input is <code>torch.cuda.FloatTensor</code><br>
but I wanna something like  <code>torch.cuda.LongTensor</code></p>
<p>Is there any way to do it without <code>if_else</code>   wrt     <code>use_cuda</code> ?</p>

apaszke: <p><code>input.new().long()</code> should do it. Type casts retain the device, and if you give no arguments to <code>new</code> the tensor isn’t going to have any memory allocated, so the cast is nearly free.</p>

wouter: <p>Hi! I’m sorry to reply to an old post but I thought replying here would be better than starting a new topic.</p>
<p>What if you do give arguments to <code>new</code>, for instance to create 0’s or from a numpy list of data? I found some ways but I’m not sure what’s the best way to do it.</p>
<p>So let’s say we have given <code>X</code>:</p>
<pre><code>device = 1
X = torch.rand(10).cuda(device)  # or X = torch.rand(10)
</code></pre>
<p>How to make code below agnostic to whether X is on CPU/GPU and which device?</p>
<pre><code># Fill with numpy data
data = np.array([1, 2, 3])  # For example indices calculated by custom Python procedure
X.new().long().new(data)  # Works on CPU, error on GPU?
X.new().long().new(*data.shape).copy_(torch.from_numpy(data))  # Works but verbose
X.new(data.astype(float)).long()  # Inefficient, lossy?
# X.new(data)  # Error

# Or fill with zeros
X.new(10).long().zero_()  # Works, but inefficient?
X.new().long().new(10).zero_()  # Works, but verbose?</code></pre>

smth: <p><a class="mention" href="/u/wouter">@wouter</a> your post is unrelated to the parent topic (parent topic is about GPU tensors)</p>

wouter: <p><a class="mention" href="/u/smth">@smth</a> I’m sorry if it was not clear. Actually I meant that <code>X</code> can be on any device, then how to construct a Tensor with numpy data or 0’s on that same device. I edited my post, please let me know if I should open a new topic for this.</p>

bgobbi: <p>The new() method is deprecated.<br>
<aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/issues/3920" target="_blank" rel="nofollow noopener">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <a href="https://github.com/shekhovt" rel="nofollow noopener">
<img src="https://avatars1.githubusercontent.com/u/2486893?v=2&amp;s=96" class="thumbnail onebox-avatar" width="60" height="60">
</a>

<h4><a href="https://github.com/pytorch/pytorch/issues/3920" target="_blank" rel="nofollow noopener">Issue: [docs] Tensor.new is not documented</a></h4>

<div class="date" style="margin-top:10px;">
	<div class="user" style="margin-top:10px;">
	opened by <a href="https://github.com/shekhovt" target="_blank" rel="nofollow noopener">shekhovt</a>
	on <a href="https://github.com/pytorch/pytorch/issues/3920" target="_blank" rel="nofollow noopener">2017-11-28</a>
	</div>
	<div class="user">
	closed by <a href="https://github.com/soumith" target="_blank" rel="nofollow noopener">soumith</a>
	on <a href="https://github.com/pytorch/pytorch/issues/3920" target="_blank" rel="nofollow noopener"></a>
	</div>
</div>

<pre class="content" style="white-space: pre-wrap;">Docs master has in http://pytorch.org/docs/master/tensors.html:
new(*args, **kwargs)
Constructs a new tensor of the same data type.
Missing documentation of arguments. From some other pages...</pre>

<div class="labels">
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>What is the new alternative for this use case?</p>

shlomia: <p>new_tensor = new_tensor.to(input.device)<br>
will change new tensor to be cuda if needed.</p>

Noam_Salomonski: <blockquote>
<p>new_tensor = new_tensor.to(input.device)<br>
will change new tensor to be cuda if needed.</p>
</blockquote>
<p>This creates very ugly (and slow) code such as</p>
<pre><code>    if std.is_cuda:
        eps = torch.FloatTensor(std.size()).cuda().normal_()
    else:
        eps = torch.FloatTensor(std.size()).normal_()
</code></pre>
<p>instead of the much better<br>
<code>eps = std.new().normal_()</code></p>
<p>Isn’t there a better way?</p>

Noam_Salomonski: <p><a href="https://stackoverflow.com/a/65794995/913098" rel="noopener nofollow ugc">answer to my question</a></p>