Shamoon
Shamoon

Reputation: 43559

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.. Why won't my set_precision do it? Even when I converted to float()?

Upvotes: 1

Views: 1251

Answers (1)

ccl
ccl

Reputation: 2378

Unfortunately, this is simply how PyTorch displays tensor values. 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.

Upvotes: 1

Related Questions