What is the difference between Tensor and tensor? Is Tensor going to be deprecated in the future?

username: yxchng
cooked: <p>I am using pytorch 0.4.</p>
<p>I get the following error when I use capitalized Tensor</p>
<pre><code class="lang-auto">&gt;&gt;&gt; torch.Tensor(1)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/usr/lib/python3.6/site-packages/torch/tensor.py", line 57, in __repr__
    return torch._tensor_str._str(self)
  File "/usr/lib/python3.6/site-packages/torch/_tensor_str.py", line 218, in _str
    fmt, scale, sz = _number_format(self)
  File "/usr/lib/python3.6/site-packages/torch/_tensor_str.py", line 96, in _number_format
    if value != math.ceil(value.item()):
RuntimeError: Overflow when unpacking long
</code></pre>
<p>but no problem if I use torch.tensor(1).</p>
<p>However, one good thing about capitalized Tensor is that I can specify shape like torch.Tensor(2,3) which gives me</p>
<pre><code class="lang-auto">tensor([[-1.0365e+18,  3.0754e-41,  5.5266e+30],
        [ 3.0754e-41,  4.4842e-44,  0.0000e+00]])
</code></pre>
<p>I can’t do that with torch.tensor(2,3) which gives me</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: tensor() takes 1 positional argument but 2 were given
</code></pre>
<p>Are  all these behaviors expected? When should I use which?</p>

username: richard
cooked: <p>There’s a bug in printing tensors where printing a tensor with a very large value causes a Runtime error.</p>
<p>You should use <code>torch.empty</code> in place of <code>torch.Tensor</code>.</p>

username: Naruto-Sasuke
cooked: <p><code>torch.tensor</code> is similar with numpy.array and also used to create scalar.</p>
<pre><code class="lang-python">a = torch.tensor([1,2]) # accept list
a = torch.tensor(1) # scalar
a = torch.Tensor(2,3) # Tensor
</code></pre>

username: yxchng
cooked: <p>Isn’t torch.tensor also Tensor?</p>

username: ProGamerGov
cooked: <p><a class="mention" href="/u/richard">@richard</a> So is it faster and more memory efficient to use <code>torch.empty()</code> instead of <code>torch.Tensor()</code>, when defining empty tensors in a class initialization function?</p>

username: richard
cooked: <p>Our <code>torch.Tensor</code> constructor is overloaded to do the same thing as both <code>torch.tensor</code> and <code>torch.empty</code>. We thought this overload would make code confusing, so we split <code>torch.Tensor</code> into <code>torch.tensor</code> and `torch.empty.</p>
<p>So <a class="mention" href="/u/yxchng">@yxchng</a> yes, to some extent, <code>torch.tensor</code> works similarly to <code>torch.Tensor</code> (when you pass in data). <a class="mention" href="/u/progamergov">@ProGamerGov</a> no, neither should be more efficient than the other. It’s just that the <code>torch.empty</code> and <code>torch.tensor</code> have a nicer API than our legacy <code>torch.Tensor</code> constructor.</p>

username: SoulAsylum
cooked: <p><code>torch.Tensor</code> is a kind of mixture between <code>torch.empty</code> and <code>torch.tensor</code> , but when pass in data, <code>torch.Tensor</code> uses a global default dtype, <code>torch.tensor</code> infers data type from data</p>

username: taiky
cooked: <p>It looks like the <code>torch.tensor</code> is better than <code>torch.Tensor</code> when realize the same function?</p>
<p>I found a case to use Tensor rather than tensor:</p>
<p><strong>torch.Tensor with int / float</strong></p>
<pre><code class="lang-auto">In[] : 1/torch.Tensor([1,2,3])
Out[]: tensor([ 1.0000,  0.5000,  0.3333])
</code></pre>
<p><strong>torch.tensor with int</strong></p>
<pre><code class="lang-auto">In[] : 1/torch.tensor([1,2,3])
------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
&lt;ipython-input-231-55a86778a1f4&gt; in &lt;module&gt;()
----&gt; 1 1/torch.tensor([1,2,3]).
.../anaconda3/lib/python3.6/site-packages/torch/tensor.py in __rdiv__(self, other)
    318 
    319     def __rdiv__(self, other):
--&gt; 320         return self.reciprocal() * other
    321     __rtruediv__ = __rdiv__
    322     __itruediv__ = _C._TensorBase.__idiv__

RuntimeError: reciprocal is not implemented for type torch.LongTensor
</code></pre>
<p><strong>torch.tensor with float</strong></p>
<pre><code class="lang-auto">In []: 1/torch.tensor([1.,2.,3.])
Out[]: tensor([ 1.0000,  0.5000,  0.3333])
</code></pre>

username: jon
cooked: <p>One important thing is that <code>torch.tensor(1)</code> gives you a fixed value of 1, while <code>torch.Tensor(1)</code> gives you a tensor of size 1 which is randomly initialized</p>

username: shaun
cooked: <p>So when do we use which?</p>

username: jon
cooked: <p>Well it depends on your use case, as my reply mentioned. If you want to <em>explicitly</em> specify what the tensor is, then use lowercase <code>tensor</code></p>

username: Pranavan_Theivendira
cooked: <aside class="quote no-group quote-modified" data-username="taiky" data-post="8" data-topic="17134">
<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/taiky/48/9446_2.png" class="avatar"> taiky:</div>
<blockquote>
<p>In<span class="chcklst-box fa fa-square-o fa-fw"></span> : 1/torch.tensor([1,2,3])</p>
</blockquote>
</aside>
<p>Update - Now it considers as integer.</p>
<pre><code class="lang-auto">import torch

a = torch.tensor([1, 2, 3])

print(1/a)
</code></pre>
<p>Here the output is [1, 0, 0]. Technically <code>//</code> operation is done.</p>
<p>Thanks</p>

username: Brando_Miranda
cooked: <p>answer to the title of the question:</p>
<aside class="onebox stackexchange">
  <header class="source">
      <a href="https://stackoverflow.com/questions/51911749/what-is-the-difference-between-torch-tensor-and-torch-tensor" target="_blank" rel="nofollow noopener">stackoverflow.com</a>
  </header>
  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/157726/juan-leni" target="_blank" rel="nofollow noopener">
    <img alt="Juan Leni" src="https://i.stack.imgur.com/WmhIh.jpg?s=128&amp;g=1" class="thumbnail onebox-avatar" width="128" height="128">
  </a>
<h4>
  <a href="https://stackoverflow.com/questions/51911749/what-is-the-difference-between-torch-tensor-and-torch-tensor" target="_blank" rel="nofollow noopener">What is the difference between torch.tensor and torch.Tensor?</a>
</h4>

<div class="tags">
  <strong>python, pytorch</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/157726/juan-leni" target="_blank" rel="nofollow noopener">
    Juan Leni
  </a>
  on <a href="https://stackoverflow.com/questions/51911749/what-is-the-difference-between-torch-tensor-and-torch-tensor" target="_blank" rel="nofollow noopener">07:06PM - 18 Aug 18 UTC</a>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


username: ado_sar
cooked: <aside class="quote no-group" data-username="jon" data-post="9" data-topic="17134" data-full="true">
<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/jon/48/24563_2.png" class="avatar"> jon:</div>
<blockquote>
<p>One important thing is that <code>torch.tensor(1)</code> gives you a fixed value of 1, while <code>torch.Tensor(1)</code> gives you a tensor of size 1 which is randomly initialized</p>
</blockquote>
</aside>
<p>Is this anywhere documented?</p>

username: ptrblck
cooked: <p>Yes:<br>
<a href="https://pytorch.org/docs/stable/generated/torch.tensor.html#torch.tensor"><code>torch.tensor</code></a>:</p>
<blockquote>
<p>Constructs a tensor with no autograd history (also known as a “leaf tensor”, see <a href="https://pytorch.org/docs/stable/notes/autograd.html">Autograd mechanics</a>) by copying <code>data</code>.</p>
</blockquote>
<p><a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor"><code>torch.Tensor</code></a> should not be used directly as the doc explains how to create tensors:</p>
<blockquote>
<p>There are a few main ways to create a tensor, depending on your use case.</p>
<ul>
<li>To create a tensor with pre-existing data, use <a href="https://pytorch.org/docs/stable/generated/torch.tensor.html#torch.tensor"><code>torch.tensor()</code></a>.</li>
<li>To create a tensor with specific size, use <code>torch.*</code> tensor creation ops (see <a href="https://pytorch.org/docs/stable/torch.html#tensor-creation-ops">Creation Ops</a>).</li>
<li>To create a tensor with the same size (and similar types) as another tensor, use <code>torch.*_like</code> tensor creation ops (see <a href="https://pytorch.org/docs/stable/torch.html#tensor-creation-ops">Creation Ops</a>).</li>
<li>To create a tensor with similar type but different size as another tensor, use <code>tensor.new_*</code> creation ops.</li>
</ul>
</blockquote>

username: ado_sar
cooked: <p>Thanks! I have checked the <a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor" rel="noopener nofollow ugc">torch.tensor</a> part but couldn’t find why calling <code>torch.Tensor(2, 4)</code> for example, will return a randomly initialized tensor of shape <code>(2, 4)</code>.</p>

username: ptrblck
cooked: <p>The docs do not show any kind of <code>torch.Tensor(shape)</code> usage, but point to the proper approach of initializing a tensor.<br>
Using the <code>type</code> to create an array would be the same in numpy:</p>
<pre data-code-wrap="python"><code class="lang-python">np.ndarray([2, 4], dtype=np.float32)
array([[-1.3117950e+05,  4.5620673e-41,  0.0000000e+00,  0.0000000e+00],
       [ 2.7815069e+27,  3.0730475e-41,  2.7817666e+27,  3.0730475e-41]],
      dtype=float32)
</code></pre>
<p>so I would recommend sticking to the documented behavior.</p>