Tensor repr in debug should show shape first

Ilya_Kamenshchikov1: <p>I was debugging a lot of torch code recently, and can’t help complaining that string representation of tensor I see in debugger (PyCharm, but I assume it’s created by <code>__repr__</code> method) is not helpful. I see tensor([[ 2.6877e+01, -1.0073e+01,  8.0276e+02,  …, ]]), but what I want to know is what shape the tensor is. Ofc there is a partial hint - opening square brackets count - but I’d like to see the specific shape in the overview. Shape is basically a detail of the tensor type, which has more importance over what it is, where it comes from and what can be safely done with it than the specific numbers in the tensor IMO.</p>
<p>I know repr is usually such that copy-pasting it into python should yield code that recreates the original object, but this is anyhow not happening here because there are too many numbers to display, they are hidden with ellipsis … .</p>
<p>Does this make sense to anyone? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

Matias_Vasquez: <p>Hi,</p>
<p>you can print the shape of the tensor.</p>
<pre><code class="lang-python">tensor = torch.rand((5, 10, 15, 20))
print(tensor.shape)
</code></pre>
<pre><code class="lang-python">#Output
torch.Size([5, 10, 15, 20])
</code></pre>
<p>Or do you want it to be automatically in the _<em>repr</em>_ function of something specific?</p>

Ilya_Kamenshchikov1: <p>yes, I think debuggers use <code>__repr__</code> to show the value of variables in scope, so that’s what I’d suggest changing.</p>

Matias_Vasquez: <p>Oh sorry, I did not understand at first.</p>
<p>Yeah, I also think this would be a good idea to print the shape of the tensor.</p>
<p>Here is the <a href="https://github.com/pytorch/pytorch/blob/master/torch/_tensor.py" rel="noopener nofollow ugc">link</a> to the file, there you can make the suggestion directly to add the shape to the _<em>repr</em>_.</p>
<p>Or maybe someone will see this issue here.</p>
<p>If not, you can tell me and I can try to make the suggestion myself <img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=12" title=":blush:" class="emoji" alt=":blush:" loading="lazy" width="20" height="20"></p>

Ilya_Kamenshchikov1: <p>A colleague suggested following hack solution (put the code early in your script):</p>
<pre><code class="lang-python">normal_repr = torch.Tensor.__repr__
torch.Tensor.__repr__ = lambda self: f"{self.shape}_{normal_repr(self)}"
</code></pre>
<p><a class="mention" href="/u/matias_vasquez">@Matias_Vasquez</a> I would happily +1 your issue if you can create one. From what I know torch tries to be like numpy in many ways, so maybe this feature request should go to numpy as well.</p>

ivan_cobotic: <p>Hi,<br>
I just want to chime in that this would be one of my most wanted features with torch. I use vscode to write torch scripts and i constantly have to go to debug console and type tensor.shape to keep track of whats going on. I think the shape is the most important trait of a tensor for debugging / developing torch scripts so it would be great if the shape was included at the top of the tensor <strong>repr</strong> by default.<br>
Ive used some add-on libraries to torch that have this feature, but best would be if it was included in torch proper.</p>