Printing all the contents of a tensor

Each Answer to this Q is separated by one/two green lines.

I came across this PyTorch tutorial (in where they construct a simple neural network and run an inference. I would like to print the contents of the entire input tensor for debugging purposes. What I get when I try to print the tensor is something like this and not the entire tensor:

enter image description here

I saw a similar link for numpy but was not sure about what would work for PyTorch. I can convert it to numpy and may be view it, but would like to avoid the extra overhead. Is there a way for me to print the entire tensor?

To avoid truncation and to control how much of the tensor data is printed use the same API as numpy’s numpy.set_printoptions(threshold=10_000).


x = torch.rand(1000, 2, 2)
print(x) # prints the truncated tensor
print(x) # prints the whole tensor

If your tensor is very large, adjust the threshold value to a higher number.

Another option is:

print(x) # prints the whole tensor
torch.set_printoptions(profile="default") # reset
print(x) # prints the truncated tensor

All the available set_printoptions arguments are documented here.

Though I don’t suggest to do that, if you want, then

In [18]: torch.set_printoptions(edgeitems=1)

In [19]: a
tensor([[-0.7698,  ..., -0.1949],
        [-0.7321,  ...,  0.8537]])

In [20]: torch.set_printoptions(edgeitems=3)

In [21]: a
tensor([[-0.7698,  1.3383,  0.5649,  ...,  1.3567,  0.6896, -0.1949],
        [-0.5761, -0.9789, -0.2058,  ..., -0.5843,  2.6311, -0.0008],
        [ 1.3152,  1.8851, -0.9761,  ...,  0.8639, -0.6237,  0.5646],
        [ 0.2851,  0.5504, -0.9471,  ...,  0.0688, -0.7777,  0.1661],
        [ 2.9616, -0.8685, -1.5467,  ..., -1.4646,  1.1098, -1.0873],
        [-0.7321,  0.7610,  0.3182,  ...,  2.5859, -0.9709,  0.8537]])

I came here actually looking for answers how to print the entire row of a tensor in one line of the console so I thought I’d add this.

tensor([[1.1573e+04, 6.0693e+02, 1.2436e+03, 2.7277e+04, 1.6673e+08, 2.0462e+00, 9.8891e-01],
    [2.0237e+04, 5.9074e+02, 1.7208e+03, 2.7449e+04, 2.1301e+08, 2.0678e+00, 1.0011e+00],
    [2.7456e+04, 6.1106e+02, 1.4897e+03, 2.7332e+04, 1.7310e+08, 2.0448e+00, 9.6041e-01],
    [1.7732e+04, 6.0232e+02, 1.2608e+03, 2.7371e+04, 1.8106e+08, 1.9594e+00, 1.0040e+00],
    [1.1167e+04, 5.9867e+02, 1.3440e+03, 2.7263e+04, 2.3160e+08, 2.0190e+00, 1.0075e+00],
    [1.6003e+04, 5.9590e+02, 1.2319e+03, 2.7368e+04, 1.7155e+08, 2.0171e+00, 1.0202e+00],
    [1.5499e+04, 6.1471e+02, 9.4877e+02, 2.7395e+04, 1.8146e+08, 1.9016e+00, 9.5884e-01],
    [3.3886e+04, 6.0689e+02, 1.0777e+03, 2.7259e+04, 2.1599e+08, 2.0179e+00, 1.0201e+00]], dtype=torch.float64)

I did this using


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .