简体   繁体   English

torch.float32 与 numpy 的 float32 不同吗?

[英]Is torch.float32 different from numpy's float32?

Setting precision as 30 in PyTorch shows:在 PyTorch 中设置精度为 30 显示:

>>> torch.set_printoptions(precision=30)
>>> y
tensor([[-0.388252139091491699218750000000, -0.610148549079895019531250000000,
         -1.333969473838806152343750000000, -1.027917861938476562500000000000,
         -0.498563587665557861328125000000, -0.096793495118618011474609375000,
         -0.895992159843444824218750000000, -0.752071321010589599609375000000,
         -0.879653215408325195312500000000],
        [ 1.960780262947082519531250000000,  0.290681242942810058593750000000,
          0.111534759402275085449218750000, -1.412155270576477050781250000000,
          1.015806078910827636718750000000,  0.201809123158454895019531250000,
          0.131465137004852294921875000000, -1.262379050254821777343750000000,
         -0.480409622192382812500000000000],
        [ 0.111068181693553924560546875000, -2.129202365875244140625000000000,
          0.538800299167633056640625000000,  1.165832757949829101562500000000,
          0.194993987679481506347656250000, -1.110693812370300292968750000000,
         -1.451576709747314453125000000000, -3.398952484130859375000000000000,
          2.022404193878173828125000000000],
        [-2.374018669128417968750000000000, -1.442466974258422851562500000000,
         -0.406166225671768188476562500000,  0.045908458530902862548828125000,
         -0.835370421409606933593750000000, -0.302138328552246093750000000000,
         -0.421340197324752807617187500000,  0.931307554244995117187500000000,
          1.061386585235595703125000000000],
        [ 1.166660070419311523437500000000,  0.710260510444641113281250000000,
          1.008558034896850585937500000000,  1.957847237586975097656250000000,
         -1.070753335952758789062500000000,  0.319442749023437500000000000000,
         -1.140496969223022460937500000000, -1.723430752754211425781250000000,
          0.109533369541168212890625000000],
        [ 0.501820147037506103515625000000,  0.349833250045776367187500000000,
         -0.073374643921852111816406250000, -2.459295272827148437500000000000,
         -1.853959321975708007812500000000,  0.153838425874710083007812500000,
         -1.860147237777709960937500000000, -0.880943417549133300781250000000,
         -1.352821707725524902343750000000]])

but in numpy setting precision as 30 shows:但在 numpy 中将精度设置为 30 显示:

>>> np.set_printoptions(precision=30)
>>> y.numpy()
array([[-0.38825214 , -0.61014855 , -1.3339695  , -1.0279179  ,
        -0.4985636  , -0.096793495, -0.89599216 , -0.7520713  ,
        -0.8796532  ],
       [ 1.9607803  ,  0.29068124 ,  0.11153476 , -1.4121553  ,
         1.0158061  ,  0.20180912 ,  0.13146514 , -1.262379   ,
        -0.48040962 ],
       [ 0.11106818 , -2.1292024  ,  0.5388003  ,  1.1658328  ,
         0.19499399 , -1.1106938  , -1.4515767  , -3.3989525  ,
         2.0224042  ],
       [-2.3740187  , -1.442467   , -0.40616623 ,  0.04590846 ,
        -0.8353704  , -0.30213833 , -0.4213402  ,  0.93130755 ,
         1.0613866  ],
       [ 1.1666601  ,  0.7102605  ,  1.008558   ,  1.9578472  ,
        -1.0707533  ,  0.31944275 , -1.140497   , -1.7234308  ,
         0.10953337 ],
       [ 0.50182015 ,  0.34983325 , -0.073374644, -2.4592953  ,
        -1.8539593  ,  0.15383843 , -1.8601472  , -0.8809434  ,
        -1.3528217  ]], dtype=float32)e

Why are the results different?为什么结果不一样?

By default, if it takes less digits than the configured value of precision to distinguish a floating-point value from other values of the same dtype, NumPy will only print as many digits as necessary for that.默认情况下,如果将浮点值与具有相同 dtype 的其他值区分开来的位数少于配置的precision值,则 NumPy 将仅打印所需的位数。 You have to set the floatmode option to 'fixed' to get the behavior you were expecting:您必须将floatmode选项设置为'fixed'以获得您期望的行为:

numpy.set_printoptions(precision=30, floatmode='fixed')

Note that even if you print 30 decimal digits, 32-bit floats don't have anywhere near that level of precision.请注意,即使您打印 30 个十进制数字,32 位浮点数也没有接近该精度水平的任何地方。

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

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