torch.set_printoptions()

这篇文章详细解释了如何使用torch.set_printoptions函数在PyTorch中调整Tensor的打印精度,包括小数位数、元素数量阈值、折叠策略以及科学计数法的启用。通过示例展示了如何设置和恢复默认的打印选项。
摘要由CSDN通过智能技术生成

方法:

torch.set_printoptions(precision=Nonethreshold=Noneedgeitems=Nonelinewidth=Noneprofile=Nonesci_mode=None)

作用: 更改print在打印tensor时的小数精度

Parameters  参数

  • precision – Number of digits of precision for floating point output (default = 4).

  • 精度设置,默认精确到小数点后4位

  • threshold – Total number of array elements which trigger summarization rather than full repr (default = 1000).

  • 由于我们的进行训练网络时,tensor都会很大,里面的数据很多,不方便全部显示出来,显示超过一定的个数(注意是个数,是行数× 列数,而不是行数或者列数)后就会进行折叠。此参数是指定tensor的数目超过多少时开始显示进行折叠。默认为1000

  • edgeitems – Number of array items in summary at beginning and end of each dimension (default = 3).

  • 此参数也与折叠有关,折叠后只显示前面的数据和后面的数据,此参数设置显示的前面与后面的数据的行数(注意这里是行数而不是个数),默认为3

  • linewidth – The number of characters per line for the purpose of inserting line breaks (default = 80). Thresholded matrices will ignore this parameter.

  • 此参数是指如果一行数据太多会插入换行符,此参数是指定每行的字符数(注意是字符数,不是数据的个数,这个一定要注意)到达多少时插入换行符,此参数对于显示时超过threshold而折叠的tensor并不适用。默认为80

  • profile – Sane defaults for pretty printing. Can override with any of the above options. (any one of default, short, full)

  • 其实此方法就是一个比较简便的显示方法,它有三个选项,分别为default、short、full用来满足我们的显示, 这里控制的是小数点后面小数的显示长短, 是在 precision的基础上再次设置的,如果precision设置的是4位, 那么full最多4位, 感觉不如直接设置precision来的好

  • sci_mode – Enable (True) or disable (False) scientific notation. If None (default) is specified, the value is defined by torch._tensor_str._Formatter. This value is automatically chosen by the framework.

  • 这方法是来指定显示的数字是否使用科学计数法,可以选择指定True或者False,如果选择None,那么是True还是False会由torch._tensor_str._Formatter来定义。值会自动的由框架来选择。一般默认为False

官方示例:torch.set_printoptions — PyTorch 2.2 documentation

 

>>> # Limit the precision of elements
>>> torch.set_printoptions(precision=2)
>>> torch.tensor([1.12345])
tensor([1.12])
>>> # Limit the number of elements shown
>>> torch.set_printoptions(threshold=5)
>>> torch.arange(10)
tensor([0, 1, 2, ..., 7, 8, 9])
>>> # Restore defaults
>>> torch.set_printoptions(profile='default')
>>> torch.tensor([1.12345])
tensor([1.1235])
>>> torch.arange(10)
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  • 20
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值