Pytorch tips and tricks

How to print tensor data on the same scale

When one prints out a tensor often the entry points have different e-0X multipliers in the scientific notation and thus are hard to compare visually, e.g. try to find the largest entry point in the following tensor:

print(preds[0])
tensor([-7.3169e-02,  1.3782e-01, 5.8808e-02, 2.4611e-01, -9.3025e-02, 
        -3.6066e-02, -3.1601e-02, 1.5187e-01, 6.2414e-02,  9.2027e-03], grad_fn=<SelectBackward>)

Here is how to print the data on the same scale of 1.

torch.set_printoptions(sci_mode=False)
print(preds[0])
tensor([-0.0732,  0.1378, 0.0588, 0.2461, -0.0930,
        -0.0361, -0.0316, 0.1519, 0.0624,  0.0092], grad_fn=<SelectBackward>)

Now one can quickly tell that entry 3 is the largest.

To restore the default behavior:

torch.set_printoptions(sci_mode=True)

Thanks to Thomas Vman for this recipe.

2 Likes