-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Improve number formatting in tensor print #7632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Cool! Can you post some example outputs? |
torch/_tensor_str.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_tensor_str.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_tensor_str.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
I think the prints now are a bit verbose. Also, quite a few people wanted to have a shape information in the print. While this would break the constructor-valid contract, it could be worth considering. |
|
That's what it was like before, but there was some discussion on #6912 on whether it makes sense to do that, seeing as those defaults can be changed. |
|
Given that we are adding ellipses (which breaks Python parseability), I don't think slavishly following the "it must be a valid Python expression" makes sense. For example, we can add the shape as a comment as a newline at the very end of the expression. This is not strictly following the Python guidelines, but it follows the spirit and solves the problem. |
|
I personally would only print requires_grad if it's True. Same thing for device, only print it if it's not on the CPU. Also, I think that at some point this becomes a matter of taste, and maybe adding options to the printoptions to always enable / disable certain prints might be worth considering. |
|
I don't think there should be a space before the first element of a tensor, since numpy and python lists don't do that |
|
@alok Numpy does when it's leaving space for a negative. |
|
@pytorchbot retest this please |
1 similar comment
|
@pytorchbot retest this please |
|
So, what does the new formatting look like? An expect test for a simple tensor would go a long way towards making it more obvious to reviewers. |
4ed7b4f to
5145f95
Compare
test/test_torch.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
test/test_torch.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_tensor_str.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_tensor_str.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_tensor_str.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_tensor_str.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
@pytorchbot retest this please |
|
FYI, you can use |
|
@pytorchbot retest this please |
|
FWIW I'm not a fan of |
gchanan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some small comments / suggestions, but I think this is good to go.
torch/_tensor_str.py
Outdated
| self.max_width = max(self.max_width, len(value_str)) | ||
|
|
||
| else: | ||
| copy = torch.DoubleTensor(tensor.size()).copy_(tensor).view(tensor.nelement()) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_tensor_str.py
Outdated
| if math.isnan(value) or math.isinf(value): | ||
| self.max_width = max(self.max_width, len(value_str)) | ||
| else: | ||
| # for finites, appending a decimal will increase the len by 1 |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| else: | ||
| if exp_max == 0: | ||
| sz = 7 | ||
| if exp_max - exp_min > PRINT_OPTS.precision or exp_max > 8 or exp_min < -4: |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/_tensor_str.py
Outdated
| ret = ('{:' + str(self.max_width) + | ||
| '.' + str(PRINT_OPTS.precision) + 'e}').format(value) | ||
| else: | ||
| ret = ('{:.' + str(PRINT_OPTS.precision) + 'f}').format(value) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
@pytorchbot retest this please |
Fixes #7213, fixes #6918, fixes #6865, fixes #6811, fixes #6768.
Examples