简体   繁体   English

使用整数打印 PyTorch 张量时如何设置精度?

[英]How can I set precision when printing a PyTorch tensor with integers?

I have:我有:

mask = mask_model(input_spectrogram)
mask = torch.round(mask).float()
torch.set_printoptions(precision=4)
print(mask.size(), input_spectrogram.size(), masked_input.size())
print(mask[0][-40:], mask[0].size())

This prints:这打印:

tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0.], grad_fn=<SliceBackward>) torch.Size([400])

But I want it to print 1.0000 instead of 1. .但我希望它打印1.0000而不是1. . Why won't my set_precision do it?为什么我的set_precision不这样做? Even when I converted to float() ?即使我转换为float()

Unfortunately, this is simply how PyTorch displays tensor values.不幸的是,这只是 PyTorch 显示张量值的方式。 Your code works fine, if you do print(mask * 1.1) , you can see that PyTorch does indeed print out 4 decimal values when the tensor values can no longer be represented as integers.您的代码运行良好,如果您执行print(mask * 1.1) ,您可以看到当张量值不能再表示为整数时,PyTorch 确实会打印出 4 个十进制值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM